Showing 5 changed files with 61 additions and 93 deletions
+3
Changes
... ...
@@ -1,3 +1,6 @@
1
+0.1601
2
+  select, insert, update, update_all, delete, delete_all, execute only receive hash argument(not backword compatible)
3
+  
1 4
 0.1503
2 5
   removed reconnect method
3 6
   removed connected method
+57 -90
lib/DBIx/Custom.pm
... ...
@@ -38,7 +38,7 @@ sub connect {
38 38
     my $password    = $self->password;
39 39
     
40 40
     # Connect
41
-    my $dbh = eval{DBI->connect(
41
+    my $dbh = eval {DBI->connect(
42 42
         $data_source,
43 43
         $user,
44 44
         $password,
... ...
@@ -62,31 +62,29 @@ sub disconnect {
62 62
     my $self = shift;
63 63
     
64 64
     # Disconnect
65
-    $self->dbh->disconnect;
65
+    my $ret = eval { $self->dbh->disconnect };
66
+    croak $@ if $@;
66 67
     $self->dbh(undef);
67 68
     
68
-    return $self;
69
+    return $ret;
69 70
 }
70 71
 
71 72
 our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
72 73
 
73 74
 sub insert {
74
-    my $self = shift;
75
-    
76
-    # Arguments
77
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
75
+    my ($self, %args) = @_;
78 76
 
79 77
     # Check arguments
80
-    foreach my $name (keys %$args) {
78
+    foreach my $name (keys %args) {
81 79
         croak "\"$name\" is invalid name"
82 80
           unless $VALID_INSERT_ARGS{$name};
83 81
     }
84 82
     
85 83
     # Arguments
86
-    my $table  = $args->{table} || '';
87
-    my $param  = $args->{param} || {};
88
-    my $append = $args->{append} || '';
89
-    my $filter = $args->{filter};
84
+    my $table  = $args{table} || '';
85
+    my $param  = $args{param} || {};
86
+    my $append = $args{append} || '';
87
+    my $filter = $args{filter};
90 88
     
91 89
     # Insert keys
92 90
     my @insert_keys = keys %$param;
... ...
@@ -110,23 +108,21 @@ our %VALID_UPDATE_ARGS
110 108
   = map { $_ => 1 } qw/table param where append filter allow_update_all/;
111 109
 
112 110
 sub update {
113
-    my $self = shift;
114
-
115
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
111
+    my ($self, %args) = @_;
116 112
     
117 113
     # Check arguments
118
-    foreach my $name (keys %$args) {
114
+    foreach my $name (keys %args) {
119 115
         croak "\"$name\" is invalid name"
120 116
           unless $VALID_UPDATE_ARGS{$name};
121 117
     }
122 118
     
123 119
     # Arguments
124
-    my $table            = $args->{table} || '';
125
-    my $param            = $args->{param} || {};
126
-    my $where            = $args->{where} || {};
127
-    my $append_statement = $args->{append} || '';
128
-    my $filter           = $args->{filter};
129
-    my $allow_update_all = $args->{allow_update_all};
120
+    my $table            = $args{table} || '';
121
+    my $param            = $args{param} || {};
122
+    my $where            = $args{where} || {};
123
+    my $append_statement = $args{append} || '';
124
+    my $filter           = $args{filter};
125
+    my $allow_update_all = $args{allow_update_all};
130 126
     
131 127
     # Update keys
132 128
     my @update_keys = keys %$param;
... ...
@@ -183,40 +179,26 @@ sub update {
183 179
     return $ret_val;
184 180
 }
185 181
 
186
-sub update_all {
187
-    my $self = shift;;
188
-    
189
-    # Arguments
190
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
191
-        
192
-    # Allow all update
193
-    $args->{allow_update_all} = 1;
194
-    
195
-    # Update all rows
196
-    return $self->update($args);
197
-}
182
+sub update_all { shift->update(allow_update_all => 1, @_) };
198 183
 
199 184
 our %VALID_DELETE_ARGS
200 185
   = map { $_ => 1 } qw/table where append filter allow_delete_all/;
201 186
 
202 187
 sub delete {
203
-    my $self = shift;
204
-    
205
-    # Arguments
206
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
188
+    my ($self, %args) = @_;
207 189
     
208 190
     # Check arguments
209
-    foreach my $name (keys %$args) {
191
+    foreach my $name (keys %args) {
210 192
         croak "\"$name\" is invalid name"
211 193
           unless $VALID_DELETE_ARGS{$name};
212 194
     }
213 195
     
214 196
     # Arguments
215
-    my $table            = $args->{table} || '';
216
-    my $where            = $args->{where} || {};
217
-    my $append_statement = $args->{append};
218
-    my $filter           = $args->{filter};
219
-    my $allow_delete_all = $args->{allow_delete_all};
197
+    my $table            = $args{table} || '';
198
+    my $where            = $args{where} || {};
199
+    my $append_statement = $args{append};
200
+    my $filter           = $args{filter};
201
+    my $allow_delete_all = $args{allow_delete_all};
220 202
     
221 203
     # Where keys
222 204
     my @where_keys = keys %$where;
... ...
@@ -246,42 +228,29 @@ sub delete {
246 228
     return $ret_val;
247 229
 }
248 230
 
249
-sub delete_all {
250
-    my $self = shift;
251
-    
252
-    # Arguments
253
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
254
-    
255
-    # Allow all delete
256
-    $args->{allow_delete_all} = 1;
257
-    
258
-    # Delete all rows
259
-    return $self->delete($args);
260
-}
231
+sub delete_all { shift->delete(allow_delete_all => 1, @_) }
261 232
 
262 233
 our %VALID_SELECT_ARGS
263 234
   = map { $_ => 1 } qw/table column where append relation filter param/;
264 235
 
265 236
 sub select {
266
-    my $self = shift;;
267
-    
268
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
237
+    my ($self, %args) = @_;
269 238
     
270 239
     # Check arguments
271
-    foreach my $name (keys %$args) {
240
+    foreach my $name (keys %args) {
272 241
         croak "\"$name\" is invalid name"
273 242
           unless $VALID_SELECT_ARGS{$name};
274 243
     }
275 244
     
276 245
     # Arguments
277
-    my $tables = $args->{table} || [];
246
+    my $tables = $args{table} || [];
278 247
     $tables = [$tables] unless ref $tables eq 'ARRAY';
279
-    my $columns  = $args->{column} || [];
280
-    my $where    = $args->{where} || {};
281
-    my $relation = $args->{relation};
282
-    my $append   = $args->{append};
283
-    my $filter   = $args->{filter};
284
-    my $param    = $args->{param} || {};
248
+    my $columns  = $args{column} || [];
249
+    my $where    = $args{where} || {};
250
+    my $relation = $args{relation};
251
+    my $append   = $args{append};
252
+    my $filter   = $args{filter};
253
+    my $param    = $args{param} || {};
285 254
     
286 255
     # SQL template for select statement
287 256
     my $template = 'select ';
... ...
@@ -359,7 +328,8 @@ sub create_query {
359 328
     }
360 329
     
361 330
     # Prepare statement handle
362
-    my $sth = $self->dbh->prepare($query->{sql});
331
+    my $sth = eval {$self->dbh->prepare($query->{sql})};
332
+    croak $@ if $@;
363 333
     
364 334
     # Set statement handle
365 335
     $query->sth($sth);
... ...
@@ -370,19 +340,15 @@ sub create_query {
370 340
 our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
371 341
 
372 342
 sub execute{
373
-    my $self  = shift;
374
-    my $query = shift;
375
-    
376
-    # Arguments
377
-    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
343
+    my ($self, $query, %args)  = @_;
378 344
     
379 345
     # Check arguments
380
-    foreach my $name (keys %$args) {
346
+    foreach my $name (keys %args) {
381 347
         croak "\"$name\" is invalid name"
382 348
           unless $VALID_EXECUTE_ARGS{$name};
383 349
     }
384 350
     
385
-    my $params = $args->{param} || {};
351
+    my $params = $args{param} || {};
386 352
     
387 353
     # First argument is SQL template
388 354
     unless (ref $query eq 'DBIx::Custom::Query') {
... ...
@@ -396,25 +362,15 @@ sub execute{
396 362
         $query = $self->create_query($template);
397 363
     }
398 364
     
399
-    my $filter = $args->{filter} || $query->filter || {};
365
+    my $filter = $args{filter} || $query->filter || {};
400 366
     
401 367
     # Create bind value
402 368
     my $bind_values = $self->_build_bind_values($query, $params, $filter);
403 369
     
404 370
     # Execute
405 371
     my $sth      = $query->sth;
406
-    my $affected = eval{$sth->execute(@$bind_values)};
407
-    
408
-    # Execute error
409
-    if (my $execute_error = $@) {
410
-        require Data::Dumper;
411
-        my $sql              = $query->{sql} || '';
412
-        my $params_dump      = Data::Dumper->Dump([$params], ['*params']);
413
-        
414
-        croak("$execute_error" . 
415
-              "<Your SQL>\n$sql\n" . 
416
-              "<Your parameters>\n$params_dump");
417
-    }
372
+    my $affected = eval {$sth->execute(@$bind_values)};
373
+    croak $@ if $@;
418 374
     
419 375
     # Return resultset if select statement is executed
420 376
     if ($sth->{NUM_OF_FIELDS}) {
... ...
@@ -494,6 +450,9 @@ sub register_filter {
494 450
 sub auto_commit {
495 451
     my $self = shift;
496 452
     
453
+    # Not connected
454
+    croak "Not connected" unless $self->dbh;
455
+    
497 456
     if (@_) {
498 457
         
499 458
         # Set AutoCommit
... ...
@@ -504,8 +463,16 @@ sub auto_commit {
504 463
     return $self->dbh->{AutoCommit};
505 464
 }
506 465
 
507
-sub commit   { shift->dbh->commit }
508
-sub rollback { shift->dbh->rollback }
466
+sub commit   { 
467
+    my $ret = eval { shift->dbh->commit };
468
+    croak $@ if $@;
469
+    return $ret;
470
+}
471
+sub rollback {
472
+    my $ret = eval { shift->dbh->rollback };
473
+    croak $@ if $@;
474
+    return $ret;
475
+}
509 476
 
510 477
 sub DESTROY {
511 478
     my $self = shift;
-1
t/dbix-custom-core-sqlite.t
... ...
@@ -67,7 +67,6 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
67 67
 $dbi->disconnect;
68 68
 ok(!$dbi->dbh, $test);
69 69
 
70
-
71 70
 # Prepare table
72 71
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
73 72
 $dbi->execute($CREATE_TABLE->{0});
-1
t/dbix-custom-core.t
... ...
@@ -19,7 +19,6 @@ our $SQL_TMPL = {
19 19
 };
20 20
 my $dbi;
21 21
 
22
-
23 22
 test 'Constructor';
24 23
 $dbi = DBIx::Custom->new(
25 24
     user => 'a',
+1 -1
t/dbix-custom-sqlite.t
... ...
@@ -57,7 +57,7 @@ test 'last_insert_rowid';
57 57
 $dbi = DBIx::Custom::SQLite->new;
58 58
 $dbi->connect_memory;
59 59
 $ret_val = $dbi->execute($CREATE_TABLE->{0});
60
-$dbi->insert({table => 'table1', param => {key1 => 1, key2 => 2}});
60
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
61 61
 is($dbi->last_insert_rowid, 1, "$test: first");
62 62
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
63 63
 is($dbi->last_insert_rowid, 2, "$test: second");