Showing 2 changed files with 55 additions and 46 deletions
+54 -45
lib/DBI/Custom.pm
... ...
@@ -61,7 +61,20 @@ sub add_filter { shift->filters(@_) }
61 61
 sub result_class : Attr { auto_build => sub { shift->result_class('DBI::Custom::Result') }}
62 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
+# Auto commit
66
+sub auto_commit {
67
+    my $self = shift;
68
+    
69
+    croak("Cannot change AutoCommit becouse of not connected")
70
+        unless $self->dbh;
71
+    
72
+    if (@_) {
73
+        $self->dbh->{AutoCommit} = $_[0];
74
+        return $self;
75
+    }
76
+    return $self->dbh->{AutoCommit};
77
+}
65 78
 
66 79
 
67 80
 our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/;
... ...
@@ -88,8 +101,8 @@ sub connect {
88 101
         }
89 102
     );
90 103
     
91
-    $self->auto_commit($dbh->{AutoCommit});
92 104
     $self->dbh($dbh);
105
+    return $self;
93 106
 }
94 107
 
95 108
 sub DESTROY {
... ...
@@ -122,12 +135,14 @@ sub reconnect {
122 135
 # Commit
123 136
 sub commit {
124 137
     my $self = shift;
138
+    croak("Connection is not established") unless $self->connected;
125 139
     return $self->dbh->commit;
126 140
 }
127 141
 
128 142
 # Rollback
129 143
 sub rollback {
130 144
     my $self = shift;
145
+    croak("Connection is not established") unless $self->connected;
131 146
     return $self->dbh->rollback;
132 147
 }
133 148
 
... ...
@@ -220,65 +235,59 @@ use Object::Simple;
220 235
 sub sth          : Attr {}
221 236
 sub fetch_filter : Attr {}
222 237
 
223
-sub fetchrow_arrayref {
224
-    my $self = shift;
225
-    my $sth = $self->{sth};
226
-    
227
-    my $array = $sth->fetchrow_arrayref;
238
+sub fetch {
239
+    my ($self, $type) = @_;
240
+    my $sth = $self->sth;
241
+    $type ||= 'array';
228 242
     
229
-    return $array unless $array;
230
-    
231
-    my $keys = $sth->{NAME_lc};
232 243
     my $fetch_filter = $self->fetch_filter;
233
-    if ($fetch_filter) {
234
-        for (my $i = 0; $i < @$keys; $i++) {
235
-            $array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]);
244
+    if ($type eq 'hash') {
245
+        my $hash = $sth->fetchrow_hashref;
246
+        return unless $hash;
247
+        if ($fetch_filter) {
248
+            foreach my $key (keys %$hash) {
249
+                $hash->{$key} = $fetch_filter->($key, $hash->{$key});
250
+            }
236 251
         }
252
+        return wantarray ? %$hash : $hash;
237 253
     }
238
-    return $array;
239
-}
240
-
241
-sub fetchrow_array {
242
-    my $self = shift;
243
-    my $sth = $self->{sth};
244
-    
245
-    my @array = $sth->fetchrow_array;
246
-    
247
-    return unless @array;
248
-    
249
-    my $keys = $sth->{NAME_lc};
250
-    
251
-    my $fetch_filter = $self->fetch_filter;
252
-    if ($fetch_filter) {
253
-        for (my $i = 0; $i < @$keys; $i++) {
254
-            $array[$i] = $fetch_filter->($keys->[$i], $array[$i]);
254
+    else {
255
+        my $array = $sth->fetchrow_arrayref;
256
+        return unless $array;
257
+        if ($fetch_filter) {
258
+            my $keys = $sth->{NAME_lc};
259
+            for (my $i = 0; $i < @$keys; $i++) {
260
+                $array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]);
261
+            }
255 262
         }
263
+        return wantarray ? @$array : $array;
256 264
     }
257
-    return @array;
258 265
 }
259 266
 
260
-sub fetchrow_hashref {
261
-    my $self = shift;
262
-    my $sth = $self->{sth};
267
+sub fetch_all {
268
+    my ($self, $type) = @_;
269
+    $type ||= 'array';
263 270
     
264
-    my $hash = $sth->fetchrow_hashref;
265
-    
266
-    return unless $hash;
267
-    my $fetch_filter = $self->fetch_filter;
268
-    
269
-    if ($fetch_filter) {
270
-        foreach my $key (keys %$hash) {
271
-            $hash->{$key} = $fetch_filter->($key, $hash->{$key});
271
+    if ($type eq 'hash') {
272
+        my $array_of_hash = [];
273
+        while(my %hash = $self->fetch('hash')) {
274
+            push @$array_of_hash, {%hash};
272 275
         }
276
+        return wantarray ? @$array_of_hash : $array_of_hash;
277
+    }
278
+    else {
279
+        my $array_of_array = [];
280
+        while(my %array = $self->fetch('hash')) {
281
+            push @$array_of_array, {%array};
282
+        }
283
+        return wantarray ? @$array_of_array : $array_of_array;
273 284
     }
274
-    return $hash;
275 285
 }
276 286
 
277 287
 sub err    { shift->sth->err }
278 288
 sub errstr { shift->sth->errstr }
279
-sub finish { shift->sth->finish }
280
-sub rows   { shift->sth->rows }
281 289
 sub state  { shift->sth->state }
290
+sub finish { shift->sth->finish }
282 291
 
283 292
 Object::Simple->build_class;
284 293
 
+1 -1
t/02-sqlite.t
... ...
@@ -31,7 +31,7 @@ $dbi->query_raw_sql("create table t1 (k1 char(10), k2 char(10))");
31 31
     
32 32
     my $result = $dbi->query("select k1, k2 from t1");
33 33
     
34
-    my $row = $result->fetchrow_arrayref;
34
+    my $row = $result->fetch;
35 35
     my @values = @$row;
36 36
     $result->finish;
37 37