Showing 1 changed files with 102 additions and 92 deletions
+102 -92
lib/DBIx/Custom.pm
... ...
@@ -8,8 +8,8 @@ use base 'Object::Simple';
8 8
 use Carp 'croak';
9 9
 use DBI;
10 10
 use DBIx::Custom::Result;
11
-use DBIx::Custom::QueryBuilder;
12 11
 use DBIx::Custom::Query;
12
+use DBIx::Custom::QueryBuilder;
13 13
 use Encode qw/encode_utf8 decode_utf8/;
14 14
 
15 15
 __PACKAGE__->attr('dbh');
... ...
@@ -457,10 +457,15 @@ our $VERSION = '0.1605';
457 457
 This module is not stable. Method name and implementations will be changed.
458 458
 
459 459
 =head1 SYNOPSYS
460
-    
460
+
461
+Connect to database.
462
+
461 463
     # Connect
462 464
     my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
463 465
                                     user => 'ken', password => '!LFKD%$&');
466
+
467
+Insert, update, delete statement.
468
+
464 469
     # Insert 
465 470
     $dbi->insert(table  => 'books',
466 471
                  param  => {title => 'perl', author => 'Ken'},
... ...
@@ -484,7 +489,9 @@ This module is not stable. Method name and implementations will be changed.
484 489
     
485 490
     # Delete all
486 491
     $dbi->delete_all(table => 'books');
487
-    
492
+
493
+Select statement.
494
+
488 495
     # Select
489 496
     my $result = $dbi->select(table => 'books');
490 497
     
... ...
@@ -503,8 +510,10 @@ This module is not stable. Method name and implementations will be changed.
503 510
         column => ['books.name as book_name']
504 511
         relation => {'books.id' => 'rental.book_id'}
505 512
     );
506
-    
507
-    # Execute SQL
513
+
514
+Execute SQL source.
515
+
516
+    # Execute from SQL source
508 517
     $dbi->execute("select title from books");
509 518
     
510 519
     # Execute SQL with parameters and filter
... ...
@@ -517,11 +526,18 @@ This module is not stable. Method name and implementations will be changed.
517 526
         "select id from books where {= author} && {like title}"
518 527
     );
519 528
     $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
520
-    
529
+
530
+More features.
531
+
521 532
     # Default filter
522 533
     $dbi->default_bind_filter('encode_utf8');
523 534
     $dbi->default_fetch_filter('decode_utf8');
524
-    
535
+
536
+    # Get DBI object
537
+    my $dbh = $dbi->dbh;
538
+
539
+Fetch row.
540
+
525 541
     # Fetch
526 542
     while (my $row = $result->fetch) {
527 543
         # ...
... ...
@@ -532,8 +548,6 @@ This module is not stable. Method name and implementations will be changed.
532 548
         
533 549
     }
534 550
     
535
-    # Get DBI object
536
-    my $dbh = $dbi->dbh;
537 551
 
538 552
 =head1 DESCRIPTION
539 553
 
... ...
@@ -574,32 +588,31 @@ Provide suger methods, such as insert(), update(), delete(), and select().
574 588
     my $user = $dbi->user;
575 589
     $dbi     = $dbi->user('Ken');
576 590
 
577
-Database user name.
578
-This is used for connect().
591
+User name.
592
+C<connect()> method use this value to connect the database.
579 593
     
580 594
 =head2 C<password>
581 595
 
582 596
     my $password = $dbi->password;
583 597
     $dbi         = $dbi->password('lkj&le`@s');
584 598
 
585
-Database password.
586
-This is used for connect().
599
+Password.
600
+C<connect()> method use this value to connect the database.
587 601
 
588 602
 =head2 C<data_source>
589 603
 
590 604
     my $data_source = $dbi->data_source;
591
-    $dbi            = $dbi->data_source("dbi:mysql:dbname=$database");
605
+    $dbi            = $dbi->data_source("DBI:mysql:database=dbname");
592 606
 
593
-Database data source.
594
-This is used for connect().
607
+Data source.
608
+C<connect()> method use this value to connect the database.
595 609
 
596 610
 =head2 C<dbh>
597 611
 
598 612
     my $dbh = $dbi->dbh;
599 613
     $dbi    = $dbi->dbh($dbh);
600 614
 
601
-Database handle. This is a L<DBI> object.
602
-You can call all methods of L<DBI>
615
+L<DBI> object. You can call all methods of L<DBI>.
603 616
 
604 617
     my $sth    = $dbi->dbh->prepare("...");
605 618
     my $errstr = $dbi->dbh->errstr;
... ...
@@ -615,22 +628,19 @@ You can call all methods of L<DBI>
615 628
 Filter functions.
616 629
 By default, "encode_utf8" and "decode_utf8" is registered.
617 630
 
618
-    $encode_utf8 = $dbi->filters->{encode_utf8};
619
-    $decode_utf8 = $dbi->filters->{decode_utf8};
620
-
621 631
 =head2 C<default_bind_filter>
622 632
 
623 633
     my $default_bind_filter = $dbi->default_bind_filter
624 634
     $dbi                    = $dbi->default_bind_filter('encode_utf8');
625 635
 
626
-Default filter for value binding
636
+Default filter when parameter binding is executed.
627 637
 
628 638
 =head2 C<default_fetch_filter>
629 639
 
630 640
     my $default_fetch_filter = $dbi->default_fetch_filter;
631 641
     $dbi                     = $dbi->default_fetch_filter('decode_utf8');
632 642
 
633
-Default filter for fetching.
643
+Default filter when row is fetched.
634 644
 
635 645
 =head2 C<result_class>
636 646
 
... ...
@@ -647,28 +657,60 @@ Default to L<DBIx::Custom::Result>.
647 657
 
648 658
 SQL builder. sql_builder must be 
649 659
 the instance of L<DBIx::Custom::QueryBuilder> subclass
650
-Default to DBIx::Custom::QueryBuilder.
660
+Default to L<DBIx::Custom::QueryBuilder>.
661
+
662
+=head2 C<cache>
663
+
664
+    my $cache = $dbi->cache;
665
+    $dbi      = $dbi->cache(1);
666
+
667
+Enable cache of the query after parsing SQL source.
668
+Default to 1.
669
+
670
+=head2 C<cache_method>
671
+
672
+    $dbi          = $dbi->cache_method(\&cache_method);
673
+    $cache_method = $dbi->cache_method
674
+
675
+Method for cache.
676
+
677
+B<Example:>
678
+
679
+    $dbi->cache_method(
680
+        sub {
681
+            my $self = shift;
682
+            
683
+            $self->{_cached} ||= {};
684
+            
685
+            if (@_ > 1) {
686
+                $self->{_cached}{$_[0]} = $_[1] 
687
+            }
688
+            else {
689
+                return $self->{_cached}{$_[0]}
690
+            }
691
+        }
692
+    );
651 693
 
652 694
 =head1 METHODS
653 695
 
654
-This class is L<Object::Simple> subclass.
655
-You can use all methods of L<Object::Simple>
696
+L<DBIx::Custom> inherits all methods from L<Object::Simple>
697
+and implements the following new ones.
656 698
 
657 699
 =head2 C<connect>
658 700
 
659
-    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
701
+    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
660 702
                                     user => 'ken', password => '!LFKD%$&');
661 703
 
662
-Connect to database.
663
-"AutoCommit" and "RaiseError" option is true, 
664
-and "PrintError" option is false by default.
704
+Create a new L<DBIx::Custom> object and connect to the database.
705
+By default, "AutoCommit" and "RaiseError" option is true, 
706
+and "PrintError" option is false.
665 707
 
666 708
 =head2 C<insert>
667 709
 
668
-    $affected = $dbi->insert(table  => $table, 
669
-                             param  => \%param,
670
-                             append => $append,
671
-                             filter => \%filter);
710
+    $dbi->insert(table  => $table, 
711
+                 param  => \%param,
712
+                 append => $append,
713
+                 filter => \%filter);
672 714
 
673 715
 Insert row.
674 716
 Retrun value is the count of affected rows.
... ...
@@ -682,11 +724,11 @@ B<Example:>
682 724
 
683 725
 =head2 C<update>
684 726
 
685
-    $affected = $dbi->update(table  => $table, 
686
-                             param  => \%params,
687
-                             where  => \%where,
688
-                             append => $append,
689
-                             filter => \%filter)
727
+    $dbi->update(table  => $table, 
728
+                 param  => \%params,
729
+                 where  => \%where,
730
+                 append => $append,
731
+                 filter => \%filter)
690 732
 
691 733
 Update rows.
692 734
 Retrun value is the count of affected rows.
... ...
@@ -701,10 +743,10 @@ B<Example:>
701 743
 
702 744
 =head2 C<update_all>
703 745
 
704
-    $affected = $dbi->update_all(table  => $table, 
705
-                                 param  => \%params,
706
-                                 filter => \%filter,
707
-                                 append => $append);
746
+    $dbi->update_all(table  => $table, 
747
+                     param  => \%params,
748
+                     filter => \%filter,
749
+                     append => $append);
708 750
 
709 751
 Update all rows.
710 752
 Retrun value is the count of affected rows.
... ...
@@ -717,10 +759,10 @@ B<Example:>
717 759
 
718 760
 =head2 C<delete>
719 761
 
720
-    $affected = $dbi->delete(table  => $table,
721
-                             where  => \%where,
722
-                             append => $append,
723
-                             filter => \%filter);
762
+    $dbi->delete(table  => $table,
763
+                 where  => \%where,
764
+                 append => $append,
765
+                 filter => \%filter);
724 766
 
725 767
 Delete rows.
726 768
 Retrun value is the count of affected rows.
... ...
@@ -734,7 +776,7 @@ B<Example:>
734 776
 
735 777
 =head2 C<delete_all>
736 778
 
737
-    $affected = $dbi->delete_all(table => $table);
779
+    $dbi->delete_all(table => $table);
738 780
 
739 781
 Delete all rows.
740 782
 Retrun value is the count of affected rows.
... ...
@@ -745,12 +787,12 @@ B<Example:>
745 787
 
746 788
 =head2 C<select>
747 789
     
748
-    $result = $dbi->select(table    => $table,
749
-                           column   => [@column],
750
-                           where    => \%where,
751
-                           append   => $append,
752
-                           relation => \%relation,
753
-                           filter   => \%filter);
790
+    my $result = $dbi->select(table    => $table,
791
+                              column   => [@column],
792
+                              where    => \%where,
793
+                              append   => $append,
794
+                              relation => \%relation,
795
+                              filter   => \%filter);
754 796
 
755 797
 Select rows.
756 798
 Return value is the instance of L<DBIx::Custom::Result>.
... ...
@@ -758,13 +800,13 @@ Return value is the instance of L<DBIx::Custom::Result>.
758 800
 B<Example:>
759 801
 
760 802
     # select * from books;
761
-    $result = $dbi->select(table => 'books');
803
+    my $result = $dbi->select(table => 'books');
762 804
     
763 805
     # select * from books where title = 'Perl';
764
-    $result = $dbi->select(table => 'books', where => {title => 1});
806
+    my $result = $dbi->select(table => 'books', where => {title => 1});
765 807
     
766 808
     # select title, author from books where id = 1 for update;
767
-    $result = $dbi->select(
809
+    my $result = $dbi->select(
768 810
         table  => 'books',
769 811
         column => ['title', 'author'],
770 812
         where  => {id => 1},
... ...
@@ -790,8 +832,8 @@ using L<DBIx::Custom::QueryBuilder>.
790 832
 
791 833
 =head2 C<execute>
792 834
 
793
-    $result = $dbi->execute($query,  param => $params, filter => \%filter);
794
-    $result = $dbi->execute($source, param => $params, filter => \%filter);
835
+    my $result = $dbi->execute($query,  param => $params, filter => \%filter);
836
+    my $result = $dbi->execute($source, param => $params, filter => \%filter);
795 837
 
796 838
 Execute the instace of L<DBIx::Custom::Query> or
797 839
 the string written by SQL template.
... ...
@@ -799,7 +841,7 @@ Return value is the instance of L<DBIx::Custom::Result>.
799 841
 
800 842
 B<Example:>
801 843
 
802
-    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
844
+    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
803 845
                             param => {name => 'taro', age => 19});
804 846
     
805 847
     while (my $row = $result->fetch) {
... ...
@@ -832,38 +874,6 @@ B<Example:>
832 874
         }
833 875
     );
834 876
 
835
-=head2 C<cache>
836
-
837
-    $dbi   = $dbi->cache(1);
838
-    $cache = $dbi->cache;
839
-
840
-Cache the result of parsing SQL template.
841
-Default to 1.
842
-
843
-=head2 C<cache_method>
844
-
845
-    $dbi          = $dbi->cache_method(\&cache_method);
846
-    $cache_method = $dbi->cache_method
847
-
848
-Method for cache.
849
-
850
-B<Example:>
851
-
852
-    $dbi->cache_method(
853
-        sub {
854
-            my $self = shift;
855
-            
856
-            $self->{_cached} ||= {};
857
-            
858
-            if (@_ > 1) {
859
-                $self->{_cached}{$_[0]} = $_[1] 
860
-            }
861
-            else {
862
-                return $self->{_cached}{$_[0]}
863
-            }
864
-        }
865
-    );
866
-
867 877
 =head1 BUGS
868 878
 
869 879
 Please tell me bugs.