Showing 5 changed files with 59 additions and 243 deletions
+38 -19
lib/DBIx/Custom.pm
... ...
@@ -454,9 +454,11 @@ sub last_insert_id {
454 454
 
455 455
 # Insert
456 456
 sub insert {
457
-    my ($self, $table, $insert_params, $query_edit_cb) = @_;
458
-    $table         ||= '';
459
-    $insert_params ||= {};
457
+    my $self             = shift;
458
+    my $table            = shift || '';
459
+    my $insert_params    = shift || {};
460
+    my $append_statement = shift unless ref $_[0];
461
+    my $query_edit_cb    = shift;
460 462
     
461 463
     # Insert keys
462 464
     my @insert_keys = keys %$insert_params;
... ...
@@ -467,7 +469,7 @@ sub insert {
467 469
     
468 470
     # Templte for insert
469 471
     my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
470
-    
472
+    $template .= " $append_statement" if $append_statement;
471 473
     # Create query
472 474
     my $query = $self->create_query($template);
473 475
     
... ...
@@ -486,12 +488,13 @@ sub insert {
486 488
 
487 489
 # Update
488 490
 sub update {
489
-    my ($self, $table, $update_params,
490
-        $where_params, $query_edit_cb, $options) = @_;
491
-    
492
-    $table         ||= '';
493
-    $update_params ||= {};
494
-    $where_params  ||= {};
491
+    my $self             = shift;
492
+    my $table            = shift || '';
493
+    my $update_params    = shift || {};
494
+    my $where_params     = shift || {};
495
+    my $append_statement = shift unless ref $_[0];
496
+    my $query_edit_cb    = shift;
497
+    my $options          = shift;
495 498
     
496 499
     # Update keys
497 500
     my @update_keys = keys %$update_params;
... ...
@@ -522,6 +525,7 @@ sub update {
522 525
     
523 526
     # Template for update
524 527
     my $template = "update $table $update_clause $where_clause";
528
+    $template .= " $append_statement" if $append_statement;
525 529
     
526 530
     # Create query
527 531
     my $query = $self->create_query($template);
... ...
@@ -544,17 +548,25 @@ sub update {
544 548
 
545 549
 # Update all rows
546 550
 sub update_all {
547
-    my ($self, $table, $update_params, $query_edit_cb) = @_;
548
-    
549
-    return $self->update($table, $update_params, {}, $query_edit_cb,
550
-                         {allow_update_all => 1});
551
+    my $self             = shift;
552
+    my $table            = shift || '';
553
+    my $update_params    = shift || {};
554
+    my $append_statement = shift unless ref $_[0];
555
+    my $query_edit_cb    = shift;
556
+    my $options          = {allow_update_all => 1};
557
+    
558
+    return $self->update($table, $update_params, {}, $append_statement,
559
+                         $query_edit_cb, $options);
551 560
 }
552 561
 
553 562
 # Delete
554 563
 sub delete {
555
-    my ($self, $table, $where_params, $query_edit_cb, $options) = @_;
556
-    $table        ||= '';
557
-    $where_params ||= {};
564
+    my $self             = shift;
565
+    my $table            = shift || '';
566
+    my $where_params     = shift || {};
567
+    my $append_statement = shift unless ref $_[0];
568
+    my $query_edit_cb    = shift;
569
+    my $options          = shift;
558 570
     
559 571
     # Where keys
560 572
     my @where_keys = keys %$where_params;
... ...
@@ -575,6 +587,7 @@ sub delete {
575 587
     
576 588
     # Template for delete
577 589
     my $template = "delete from $table $where_clause";
590
+    $template .= " $append_statement" if $append_statement;
578 591
     
579 592
     # Create query
580 593
     my $query = $self->create_query($template);
... ...
@@ -594,8 +607,14 @@ sub delete {
594 607
 
595 608
 # Delete all rows
596 609
 sub delete_all {
597
-    my ($self, $table) = @_;
598
-    return $self->delete($table, {}, undef, {allow_delete_all => 1});
610
+    my $self             = shift;
611
+    my $table            = shift || '';
612
+    my $append_statement = shift unless ref $_[0];
613
+    my $query_edit_cb    = shift;
614
+    my $options          = {allow_delete_all => 1};
615
+    
616
+    return $self->delete($table, {}, $append_statement, $query_edit_cb,
617
+                         $options);
599 618
 }
600 619
 
601 620
 sub _select_usage { return << 'EOS' }
-86
lib/DBIx/Custom/DB2.pm
... ...
@@ -1,86 +0,0 @@
1
-package DBIx::Custom::DB2;
2
-use base 'DBIx::Custom::Basic';
3
-
4
-use warnings;
5
-use strict;
6
-
7
-my $class = __PACKAGE__;
8
-
9
-$class->add_format(
10
-    datetime => $class->formats->{SQL99_datetime},
11
-    date     => $class->formats->{SQL99_date},
12
-    time     => $class->formats->{SQL99_time},
13
-);
14
-
15
-
16
-sub connect {
17
-    my $self = shift;
18
-    
19
-    if (!$self->data_source && (my $database = $self->database)) {
20
-        $self->data_source("dbi:DB2:dbname=$database");
21
-    }
22
-    
23
-    return $self->SUPER::connect;
24
-}
25
-
26
-=head1 NAME
27
-
28
-DBIx::Custom::DB2 - DBIx::Custom DB2 implementation
29
-
30
-=head1 Version
31
-
32
-Version 0.0102
33
-
34
-=head1 Synopsys
35
-
36
-    # New
37
-    my $dbi = DBIx::Custom::DB2->new(user => 'taro', $password => 'kliej&@K',
38
-                                      database => 'sample_db');
39
-    # Insert 
40
-    $dbi->insert('books', {title => 'perl', author => 'taro'});
41
-    
42
-    # Update 
43
-    # same as 'update books set (title = 'aaa', author = 'ken') where id = 5;
44
-    $dbi->update('books', {title => 'aaa', author => 'ken'}, {id => 5});
45
-    
46
-    # Delete
47
-    $dbi->delete('books', {author => 'taro'});
48
-    
49
-    # select * from books;
50
-    $dbi->select('books');
51
-    
52
-    # select * from books where ahthor = 'taro'; 
53
-    $dbi->select('books', {author => 'taro'});
54
-
55
-=head1 See DBIx::Custom and DBI::Custom::Basic documentation
56
-
57
-This class is L<DBIx::Custom::Basic> subclass,
58
-and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass.
59
-
60
-You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
61
-Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation.
62
-
63
-=head1 Object methods
64
-
65
-=head2 connect
66
-
67
-    This method override DBIx::Custom::connect
68
-    
69
-    If database attribute is set, automatically data source is created and connect
70
-
71
-=head1 Author
72
-
73
-Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
74
-
75
-Github L<http://github.com/yuki-kimoto>
76
-
77
-I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom>
78
-
79
-=head1 Copyright & license
80
-
81
-Copyright 2009 Yuki Kimoto, all rights reserved.
82
-
83
-This program is free software; you can redistribute it and/or modify it
84
-under the same terms as Perl itself.
85
-
86
-
-60
lib/DBIx/Custom/ODBC.pm
... ...
@@ -1,60 +0,0 @@
1
-package DBIx::Custom::ODBC;
2
-use base 'DBIx::Custom::Basic';
3
-
4
-use warnings;
5
-use strict;
6
-
7
-my $class = __PACKAGE__;
8
-
9
-sub connect {
10
-    my $self = shift;
11
-    
12
-    if (!$self->data_source && (my $database = $self->database)) {
13
-        $self->data_source("dbi:ODBC:dbname=$database");
14
-    }
15
-    
16
-    return $self->SUPER::connect;
17
-}
18
-
19
-=head1 NAME
20
-
21
-DBIx::Custom::ODBC - DBIx::Custom ODBC implementation
22
-
23
-=head1 Version
24
-
25
-Version 0.0102
26
-
27
-=head1 Synopsys
28
-
29
-=head1 See DBIx::Custom and DBI::Custom::Basic documentation
30
-
31
-This class is L<DBIx::Custom::Basic> subclass,
32
-and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass.
33
-
34
-You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
35
-Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation.
36
-
37
-=head1 Object methods
38
-
39
-=head2 connect
40
-
41
-    This method override DBIx::Custom::connect
42
-    
43
-    If database attribute is set, automatically data source is created and connect
44
-
45
-=head1 Author
46
-
47
-Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
48
-
49
-Github L<http://github.com/yuki-kimoto>
50
-
51
-I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom>
52
-
53
-=head1 Copyright & license
54
-
55
-Copyright 2009 Yuki Kimoto, all rights reserved.
56
-
57
-This program is free software; you can redistribute it and/or modify it
58
-under the same terms as Perl itself.
59
-
60
-
-75
lib/DBIx/Custom/Oracle.pm
... ...
@@ -1,75 +0,0 @@
1
-package DBIx::Custom::Oracle;
2
-use base 'DBIx::Custom::Basic';
3
-
4
-use warnings;
5
-use strict;
6
-
7
-my $class = __PACKAGE__;
8
-
9
-sub connect {
10
-    my $self = shift;
11
-    
12
-    if (!$self->data_source && (my $database = $self->database)) {
13
-        $self->data_source("dbi:Oracle:dbname=$database");
14
-    }
15
-    
16
-    return $self->SUPER::connect;
17
-}
18
-
19
-=head1 NAME
20
-
21
-DBIx::Custom::Oracle - DBIx::Custom Oracle implementation
22
-
23
-=head1 Synopsys
24
-
25
-    # New
26
-    my $dbi = DBIx::Custom::Oracle->new(user => 'taro', $password => 'kliej&@K',
27
-                                        database => 'sample_db');
28
-    # Insert 
29
-    $dbi->insert('books', {title => 'perl', author => 'taro'});
30
-    
31
-    # Update 
32
-    # same as 'update books set (title = 'aaa', author = 'ken') where id = 5;
33
-    $dbi->update('books', {title => 'aaa', author => 'ken'}, {id => 5});
34
-    
35
-    # Delete
36
-    $dbi->delete('books', {author => 'taro'});
37
-    
38
-    # select * from books;
39
-    $dbi->select('books');
40
-    
41
-    # select * from books where ahthor = 'taro'; 
42
-    $dbi->select('books', {author => 'taro'});
43
-
44
-=head1 See DBIx::Custom and DBI::Custom::Basic documentation
45
-
46
-This class is L<DBIx::Custom::Basic> subclass,
47
-and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass.
48
-
49
-You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
50
-Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation.
51
-
52
-=head1 Object methods
53
-
54
-=head2 connect
55
-
56
-    This method override DBIx::Custom::connect
57
-    
58
-    If database attribute is set, automatically data source is created and connect
59
-
60
-=head1 Author
61
-
62
-Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
63
-
64
-Github L<http://github.com/yuki-kimoto>
65
-
66
-I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom>
67
-
68
-=head1 Copyright & license
69
-
70
-Copyright 2009 Yuki Kimoto, all rights reserved.
71
-
72
-This program is free software; you can redistribute it and/or modify it
73
-under the same terms as Perl itself.
74
-
75
-
+21 -3
t/dbi-custom-core-sqlite.t
... ...
@@ -495,12 +495,17 @@ $result = $dbi->execute($SELECT_TMPL->{0});
495 495
 $rows   = $result->fetch_hash_all;
496 496
 is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
497 497
 
498
+$dbi->insert('table1', {key1 => 1, key2 => 2}, '   ', sub {
499
+    my $query = shift;
500
+    like($query->sql, qr/insert into table1 \(.+\) values \(\?, \?\)    ;/, 
501
+        "$test: append statement");
502
+});
498 503
 
499 504
 test 'insert error';
500 505
 eval{$dbi->insert('table1')};
501 506
 like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
502 507
 
503
-eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')};
508
+eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, '', 'aaa')};
504 509
 like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
505 510
 
506 511
 
... ...
@@ -545,6 +550,12 @@ is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
545 550
                   {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
546 551
                   "$test : query edit callback");
547 552
 
553
+$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
554
+    my $query = shift;
555
+    is($query->sql, 'update table1 set key2 = ? where key1 = ?    ;',
556
+       "$test: append statement");
557
+});
558
+
548 559
 
549 560
 test 'update error';
550 561
 $dbi = DBIx::Custom->new($NEW_ARGS->{0});
... ...
@@ -557,7 +568,7 @@ eval{$dbi->update('table1', {key2 => 1})};
557 568
 like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/,
558 569
          "$test : where key-value pairs not specified");
559 570
 
560
-eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, 'aaa')};
571
+eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, '', 'aaa')};
561 572
 like($@, qr/Query edit callback must be code reference/, 
562 573
          "$test : query edit callback not code reference");
563 574
 
... ...
@@ -605,6 +616,13 @@ $result = $dbi->execute($SELECT_TMPL->{0});
605 616
 $rows   = $result->fetch_hash_all;
606 617
 is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
607 618
 
619
+$dbi->delete('table1', {key1 => 1}, '   ', sub {
620
+    my $query = shift;
621
+    is($query->sql, 'delete from table1 where key1 = ?    ;',
622
+       "$test: append statement");
623
+});
624
+
625
+
608 626
 $dbi->delete_all('table1');
609 627
 $dbi->insert('table1', {key1 => 1, key2 => 2});
610 628
 $dbi->insert('table1', {key1 => 3, key2 => 4});
... ...
@@ -620,7 +638,7 @@ eval{$dbi->delete('table1')};
620 638
 like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
621 639
          "$test : where key-value pairs not specified");
622 640
 
623
-eval{$dbi->delete('table1', {key1 => 1}, 'aaa')};
641
+eval{$dbi->delete('table1', {key1 => 1}, '', 'aaa')};
624 642
 like($@, qr/Query edit callback must be code reference/, 
625 643
          "$test : query edit callback not code ref");
626 644