Commit b86b53ce authored by IKEDA Soji's avatar IKEDA Soji Committed by Markus Jylhänkangas
Browse files

SDM: Adding begin()/commit()/rollback() methods.

parent 88a70924
......@@ -406,6 +406,27 @@ sub prepare_query_log_values {
# DEPRECATED: Use tools::eval_in_time() and fetchall_arrayref().
#sub fetch();
sub begin {
my $self = shift;
my $dbh = $self->__dbh;
return $dbh->begin_work if $dbh;
return undef;
}
sub commit {
my $self = shift;
my $dbh = $self->__dbh;
return $dbh->commit if $dbh;
return undef;
}
sub rollback {
my $self = shift;
my $dbh = $self->__dbh;
return $dbh->rollback if $dbh;
return undef;
}
sub disconnect {
my $self = shift;
......@@ -534,6 +555,16 @@ TBD.
I<Constructor>.
Creates new database instance.
=item begin ( )
I<Instance method>, I<only for SQL>.
Begin transaction.
=item commit ( )
I<Instance method>, I<only for SQL>.
Commit transaction.
=item do_operation ( $operation, options... )
I<Instance method>, I<only for LDAP>.
......@@ -564,6 +595,11 @@ Returns:
Statement handle (L<DBI::st> object or such), or C<undef>.
=item rollback ( )
I<Instance method>, I<only for SQL>.
Rollback transaction.
=back
=head1 SEE ALSO
......
......@@ -539,6 +539,14 @@ provided by L<Sympa::Database> class:
=over
=item begin ( )
I<Overridable>, I<only for SQL driver>.
=item commit ( )
I<Overridable>, I<only for SQL driver>.
=item do_operation ( $operation, $parameters, ...)
I<Overridable>, I<only for LDAP driver>.
......@@ -551,6 +559,10 @@ I<Overridable>, I<only for SQL driver>.
I<Overridable>, I<only for SQL driver>.
=item rollback ( )
I<Overridable>, I<only for SQL driver>.
=item AS_DOUBLE ( $value )
I<Overridable>.
......
......@@ -520,10 +520,50 @@ sub translate_type {
return $type;
}
# As SQLite does not support nested transactions, these are not effective
# during when {_transaction_level} attribute is positive, i.e. only the
# outermost transaction will be available.
sub begin {
my $self = shift;
$self->{_transaction_level} //= 0;
if ($self->{_transaction_level}++) {
return 1;
}
return $self->SUPER::begin;
}
sub commit {
my $self = shift;
unless ($self->{_transaction_level}) {
die 'bug in logic. Ask developer';
}
if (--$self->{_transaction_level}) {
return 1;
}
return $self->SUPER::commit;
}
sub rollback {
my $self = shift;
unless ($self->{_transaction_level}) {
die 'bug in logic. Ask developer';
}
if (--$self->{_transaction_level}) {
return 1;
}
return $self->SUPER::rollback;
}
# Note:
# To prevent "database is locked" error, acquire "immediate" lock
# by each query. Most queries excluding "SELECT" need to lock in this
# manner.
# - To prevent "database is locked" error, acquire "immediate" lock
# by each query. Most queries excluding "SELECT" need to lock in this
# manner.
# - If a transaction has been begun, lock is not needed, because SQLite
# does not support nested transactions.
sub do_query {
my $self = shift;
my $sth;
......@@ -531,8 +571,8 @@ sub do_query {
my $need_lock =
($_[0] =~
/^\s*(ALTER|CREATE|DELETE|DROP|INSERT|REINDEX|REPLACE|UPDATE)\b/i
);
/^\s*(ALTER|CREATE|DELETE|DROP|INSERT|REINDEX|REPLACE|UPDATE)\b/i)
unless $self->{_transaction_level};
## acquire "immediate" lock
unless (!$need_lock or $self->__dbh->begin_work) {
......@@ -571,8 +611,8 @@ sub do_prepared_query {
my $need_lock =
($_[0] =~
/^\s*(ALTER|CREATE|DELETE|DROP|INSERT|REINDEX|REPLACE|UPDATE)\b/i
);
/^\s*(ALTER|CREATE|DELETE|DROP|INSERT|REINDEX|REPLACE|UPDATE)\b/i)
unless $self->{_transaction_level};
## acquire "immediate" lock
unless (!$need_lock or $self->__dbh->begin_work) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment