Showing 3 changed files with 47 additions and 81 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1616
2
+  removed experimental register_method(), and methods attribute, because it is too magical
1 3
 0.1615
2 4
   fixed DBIx::Custom::QueryBuilder build_query() bug
3 5
   required Perl 5.008001 to use @CARP_NOT 
+24 -65
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1615';
3
+our $VERSION = '0.1616';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -58,6 +58,7 @@ sub connect {
58 58
     my $user        = $self->user;
59 59
     my $password    = $self->password;
60 60
     
61
+    
61 62
     # Connect
62 63
     my $dbh = eval {DBI->connect(
63 64
         $data_source,
... ...
@@ -116,7 +117,7 @@ sub insert {
116 117
     
117 118
     # Execute query
118 119
     my $ret_val = $self->execute($source, param  => $param, 
119
-                                            filter => $filter);
120
+                                          filter => $filter);
120 121
     
121 122
     return $ret_val;
122 123
 }
... ...
@@ -354,11 +355,7 @@ sub create_query {
354 355
     # Prepare statement handle
355 356
     my $sth;
356 357
     eval { $sth = $self->dbh->prepare($query->{sql})};
357
-    if ($@) {
358
-        my $error = $@;
359
-        $error =~ s/\s+at\s+.*?\s+line\s+\d+.*$//s;
360
-        croak qq{$error. SQL: "$query->{sql}"};
361
-    }
358
+    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
362 359
     
363 360
     # Set statement handle
364 361
     $query->sth($sth);
... ...
@@ -366,6 +363,25 @@ sub create_query {
366 363
     return $query;
367 364
 }
368 365
 
366
+sub _croak {
367
+    my ($self, $error, $append) = @_;
368
+    $append ||= "";
369
+    
370
+    # Verbose
371
+    if ($Carp::Verbose) { croak $error }
372
+    
373
+    # Not verbose
374
+    else {
375
+        
376
+        # Remove line and module infromation
377
+        my $at_pos = rindex($error, ' at ');
378
+        $error = substr($error, 0, $at_pos);
379
+        $error =~ s/\s+$//;
380
+        
381
+        croak "$error$append";
382
+    }
383
+}
384
+
369 385
 our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
370 386
 
371 387
 sub execute{
... ...
@@ -392,11 +408,7 @@ sub execute{
392 408
     my $sth      = $query->sth;
393 409
     my $affected;
394 410
     eval {$affected = $sth->execute(@$bind_values)};
395
-    if ($@) {
396
-        my $error = $@;
397
-        $error =~ s/\s+at\s+.*?\s+line\s+\d+.*$//s;
398
-        croak $error;
399
-    }
411
+    $self->_croak($@) if $@;
400 412
     
401 413
     # Return resultset if select statement is executed
402 414
     if ($sth->{NUM_OF_FIELDS}) {
... ...
@@ -475,43 +487,12 @@ sub _check_filter {
475 487
     }
476 488
 }
477 489
 
478
-__PACKAGE__->attr('methods' => sub { {} });
479
-
480
-sub register_method {
481
-    my $self = shift;
482
-    
483
-    # Register method
484
-    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
485
-    $self->methods({%{$self->methods}, %$methods});
486
-    
487
-    return $self;
488
-}
489
-
490
-our $AUTOLOAD;
491
-sub AUTOLOAD {
492
-    my $self = shift;
493
-    my $method = $AUTOLOAD;
494
-    $method =~ s/.*:://;
495
-   
496
-   return if $method eq 'DESTROY';
497
-   
498
-   croak qq{Method "$method" is not registered"}
499
-     unless $self->methods->{$method};
500
-   
501
-   return $self->methods->{$method}->($self, @_);
502
-}
503
-
504 490
 1;
505 491
 
506 492
 =head1 NAME
507 493
 
508 494
 DBIx::Custom - DBI interface, having hash parameter binding and filtering system
509 495
 
510
-=head1 STABILITY
511
-
512
-B<This module is not stable>.
513
-Method name and implementations will be changed for a while.
514
-
515 496
 =head1 SYNOPSYS
516 497
 
517 498
 Connect to the database.
... ...
@@ -1236,13 +1217,6 @@ Default to 1.
1236 1217
 This check maybe damege performance.
1237 1218
 If you require performance, set C<filter_check> attribute to 0.
1238 1219
 
1239
-=head2 C<(experimental) methods>
1240
-
1241
-    my $methods = $dbi->methods;
1242
-    $dbi        = $dbi->methods(\%methods);
1243
-
1244
-Additional methods.
1245
-
1246 1220
 =head1 METHODS
1247 1221
 
1248 1222
 L<DBIx::Custom> inherits all methods from L<Object::Simple>
... ...
@@ -1505,21 +1479,6 @@ B<Example:>
1505 1479
         }
1506 1480
     );
1507 1481
 
1508
-=head2 C<(experimental) register_method>
1509
-
1510
-    $dbi->register_method(
1511
-        begin_work => sub { shift->dbh->begin_work },
1512
-        commit     => sub { shift->dbh->commit },
1513
-        rollback   => sub { shift->dbh->rollback}
1514
-    );
1515
-
1516
-Register methods to the object. You can call these methods
1517
-from the object.
1518
-
1519
-    $dbi->begin_work;
1520
-    $dbi->commit;
1521
-    $dbi->rollback;
1522
-
1523 1482
 =head1 BUGS
1524 1483
 
1525 1484
 Please tell me bugs if found.
+21 -16
t/dbix-custom-core-sqlite.t
... ...
@@ -542,8 +542,17 @@ like($@, qr/\QColumn name "not_exists" in bind filter is not found in paramters/
542 542
 test 'execute';
543 543
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
544 544
 $dbi->execute($CREATE_TABLE->{0});
545
-eval{$dbi->execute('select * frm table1')};
546
-like($@, qr/\Qselect * frm table1;/, "$test : fail prepare");
545
+{
546
+    local $Carp::Verbose = 0;
547
+    eval{$dbi->execute('select * frm table1')};
548
+    like($@, qr/\Qselect * frm table1;/, "$test : fail prepare");
549
+    like($@, qr/\.t /, "$test: fail : not verbose");
550
+}
551
+{
552
+    local $Carp::Verbose = 1;
553
+    eval{$dbi->execute('select * frm table1')};
554
+    like($@, qr/Custom.*\.t /s, "$test : fail : verbose");
555
+}
547 556
 
548 557
 eval{$dbi->execute('select * from table1', no_exists => 1)};
549 558
 like($@, qr/\Q"no_exists" is invalid argument/, "$test : invald SQL");
... ...
@@ -553,17 +562,13 @@ $dbi->dbh->disconnect;
553 562
 eval{$dbi->execute($query, param => {key1 => {a => 1}})};
554 563
 ok($@, "$test: execute fail");
555 564
 
556
-eval{$dbi->create_query('select * from table1 where {0 key1}')};
557
-like($@, qr/\Q.t /, "$test : caller spec");
558
-
559
-
560
-test 'register_method';
561
-$dbi = DBIx::Custom::SQLite->new;
562
-$dbi->register_method(
563
-    one => sub { 1 },
564
-);
565
-$dbi->register_method({
566
-    two => sub { 2 }
567
-});
568
-is($dbi->one, 1, "$test : hash");
569
-is($dbi->two, 2, "$test : hash reference");
565
+{
566
+    local $Carp::Verbose = 0;
567
+    eval{$dbi->create_query('select * from table1 where {0 key1}')};
568
+    like($@, qr/\Q.t /, "$test : caller spec : not vebose");
569
+}
570
+{
571
+    local $Carp::Verbose = 1;
572
+    eval{$dbi->create_query('select * from table1 where {0 key1}')};
573
+    like($@, qr/QueryBuilder.*\.t /s, "$test : caller spec : not vebose");
574
+}