| ... | ... |
@@ -59,8 +59,9 @@ sub filters : Attr { type => 'hash', deref => 1, auto_build => sub { shift->filt
|
| 59 | 59 |
sub add_filter { shift->filters(@_) }
|
| 60 | 60 |
|
| 61 | 61 |
sub result_class : Attr { auto_build => sub { shift->result_class('DBI::Custom::Result') }}
|
| 62 |
-sub dbh : Attr { auto_build => sub { shift->connect } }
|
|
| 62 |
+sub dbh : Attr {}
|
|
| 63 | 63 |
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQLTemplate->new) } }
|
| 64 |
+sub auto_commit : Attr {}
|
|
| 64 | 65 |
|
| 65 | 66 |
|
| 66 | 67 |
our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/;
|
| ... | ... |
@@ -93,13 +94,13 @@ sub connect {
|
| 93 | 94 |
|
| 94 | 95 |
sub DESTROY {
|
| 95 | 96 |
my $self = shift; |
| 96 |
- $self->disconnect; |
|
| 97 |
+ $self->disconnect if $self->connected; |
|
| 97 | 98 |
} |
| 98 | 99 |
|
| 99 | 100 |
# Is connected? |
| 100 | 101 |
sub connected {
|
| 101 | 102 |
my $self = shift; |
| 102 |
- return exists $self->{dbh} && eval {$self->dbh->can('prepare')};
|
|
| 103 |
+ return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')};
|
|
| 103 | 104 |
} |
| 104 | 105 |
|
| 105 | 106 |
# Disconnect |
| ... | ... |
@@ -118,6 +119,18 @@ sub reconnect {
|
| 118 | 119 |
$self->connect; |
| 119 | 120 |
} |
| 120 | 121 |
|
| 122 |
+# Commit |
|
| 123 |
+sub commit {
|
|
| 124 |
+ my $self = shift; |
|
| 125 |
+ return $self->dbh->commit; |
|
| 126 |
+} |
|
| 127 |
+ |
|
| 128 |
+# Rollback |
|
| 129 |
+sub rollback {
|
|
| 130 |
+ my $self = shift; |
|
| 131 |
+ return $self->dbh->rollback; |
|
| 132 |
+} |
|
| 133 |
+ |
|
| 121 | 134 |
sub dbh_option {
|
| 122 | 135 |
my $self = shift; |
| 123 | 136 |
croak("Not connected") unless $self->connected;
|
| ... | ... |
@@ -153,6 +166,9 @@ sub query {
|
| 153 | 166 |
$filter ||= $self->bind_filter; |
| 154 | 167 |
|
| 155 | 168 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
| 169 |
+ |
|
| 170 |
+ $self->connect unless $self->connected; |
|
| 171 |
+ |
|
| 156 | 172 |
my $sth = $self->dbh->prepare($sql); |
| 157 | 173 |
|
| 158 | 174 |
if ($sth_options) {
|
| ... | ... |
@@ -161,7 +177,7 @@ sub query {
|
| 161 | 177 |
} |
| 162 | 178 |
} |
| 163 | 179 |
|
| 164 |
- $sth->execute(@bind); |
|
| 180 |
+ my $ret_val = $sth->execute(@bind); |
|
| 165 | 181 |
|
| 166 | 182 |
# Select |
| 167 | 183 |
if ($sth->{NUM_OF_FIELDS}) {
|
| ... | ... |
@@ -169,18 +185,19 @@ sub query {
|
| 169 | 185 |
my $result = $result_class->new({sth => $sth});
|
| 170 | 186 |
return $result; |
| 171 | 187 |
} |
| 172 |
- return; |
|
| 188 |
+ return $ret_val; |
|
| 173 | 189 |
} |
| 174 | 190 |
|
| 175 | 191 |
|
| 176 | 192 |
sub query_raw_sql {
|
| 177 | 193 |
my ($self, $sql, @bind) = @_; |
| 194 |
+ |
|
| 195 |
+ $sefl->connect unless $self->connected; |
|
| 178 | 196 |
my $sth = $self->dbh->prepare($sql); |
| 179 | 197 |
$sth->execute(@bind); |
| 180 | 198 |
return $sth; |
| 181 | 199 |
} |
| 182 | 200 |
|
| 183 |
-sub auto_commit : Attr {}
|
|
| 184 | 201 |
|
| 185 | 202 |
|
| 186 | 203 |
Object::Simple->build_class; |