Showing 2 changed files with 201 additions and 49 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1609
2
+  updated document.
1 3
 0.1608
2 4
   update document
3 5
   renamed DBIx::Custom::QueryBuilder::TagProcessors functions(not backword compatible)
+199 -49
lib/DBIx/Custom.pm
... ...
@@ -457,11 +457,11 @@ DBIx::Custom - DBI interface, having hash parameter binding and filtering system
457 457
 
458 458
 =cut
459 459
 
460
-our $VERSION = '0.1608';
460
+our $VERSION = '0.1609';
461 461
 
462 462
 =head1 STABILITY
463 463
 
464
-B<This module is not stable>. 
464
+B<This module is not stable>.
465 465
 Method name and implementations will be changed for a while.
466 466
 
467 467
 =head1 SYNOPSYS
... ...
@@ -508,7 +508,7 @@ Select
508 508
         table  => 'books',
509 509
         column => [qw/author title/],
510 510
         where  => {author => 'Ken'},
511
-        append => 'order by id limit 1',
511
+        append => 'order by id limit 5',
512 512
         filter => {title => 'encode_utf8'}
513 513
     );
514 514
     
... ...
@@ -518,6 +518,13 @@ Select
518 518
         column   => ['books.name as book_name']
519 519
         relation => {'books.id' => 'rental.book_id'}
520 520
     );
521
+    
522
+    # Select, more flexible where
523
+    my $result = $dbi->select(
524
+        table  => 'books',
525
+        where  => ['{= author} and {like title}', 
526
+                   {author => 'Ken', title => '%Perl%'}]
527
+    );
521 528
 
522 529
 Execute SQL
523 530
 
... ...
@@ -525,17 +532,17 @@ Execute SQL
525 532
     $dbi->execute("select title from books");
526 533
     
527 534
     # Execute SQL with hash binding and filtering
528
-    $dbi->execute("select id from books where {= author} && {like title}",
535
+    $dbi->execute("select id from books where {= author} and {like title}",
529 536
                   param  => {author => 'ken', title => '%Perl%'},
530 537
                   filter => {title => 'encode_utf8'});
531 538
 
532 539
     # Create query and execute it
533 540
     my $query = $dbi->create_query(
534
-        "select id from books where {= author} && {like title}"
541
+        "select id from books where {= author} and {like title}"
535 542
     );
536
-    $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
543
+    $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
537 544
 
538
-More features.
545
+Other features.
539 546
 
540 547
     # Default filter
541 548
     $dbi->default_bind_filter('encode_utf8');
... ...
@@ -556,7 +563,6 @@ Fetch row.
556 563
         
557 564
     }
558 565
     
559
-
560 566
 =head1 DESCRIPTION
561 567
 
562 568
 =head2 1. Features
... ...
@@ -566,19 +572,20 @@ such as L<DBIx::Class>, L<DBIx::Simple>.
566 572
 
567 573
 This module is not O/R mapper. O/R mapper is useful,
568 574
 but you must learn many syntax of the O/R mapper,
569
-which is almost another language
570
-Create SQL statement is offten not effcient and damage SQL performance.
575
+which is almost another language.
576
+Created SQL statement is offten not effcient and damage SQL performance.
571 577
 so you have to execute raw SQL in the end.
572 578
 
573 579
 L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
574
-L<DBIx::Custom> provide flexible hash parameter binding adn filtering system,
575
-and suger method, such as C<select()>, C<update()>, C<delete()>, C<select()>
576
-to execute a query easily.
580
+L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
581
+and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()>
582
+to execute SQL easily.
577 583
 
578
-L<DBIx::Custom> respects SQL. SQL is not beautiful, but de-facto standard,
579
-so all people learing database system know it.
580
-If you know SQL statement,
581
-you learn a little thing about L<DBIx::Custom> to do your works.
584
+L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful,
585
+but de-facto standard,
586
+so all people learing database know it.
587
+If you know SQL,
588
+you learn a little thing to do your works, using L<DBIx::Custom>
582 589
 
583 590
 =head2 1. Connect to the database
584 591
 
... ...
@@ -594,7 +601,7 @@ If database is SQLite, use L<DBIx::Custom::SQLite>. you connect database easy wa
594 601
     use DBIx::Custom::SQLite;
595 602
     my $dbi = DBIx::Custom->connect(database => 'books');
596 603
     
597
-If database is  MySQL, use L<DBIx::Costm::MySQL>.
604
+If database is  MySQL, use L<DBIx::Costom::MySQL>.
598 605
 
599 606
     use DBIx::Custom::MySQL;
600 607
     my $dbi = DBIx::Custom->connect(database => 'books',
... ...
@@ -603,24 +610,29 @@ If database is  MySQL, use L<DBIx::Costm::MySQL>.
603 610
 =head2 2. Suger methods
604 611
 
605 612
 L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>,
606
-C<delete()> and C<select()>. If you want to do simple works,
607
-You don't have to create SQL statement.
613
+C<delete()> and C<select()>. If you want to do small works,
614
+You don't have to create SQL statements.
608 615
 
609 616
 =head3 insert()
610 617
 
618
+Execute insert statement.
619
+
611 620
     $dbi->insert(table  => 'books',
612 621
                  param  => {title => 'perl', author => 'Ken'});
613 622
 
614 623
 The following SQL is executed.
615 624
 
616
-    insert into (title, author) values (?, ?)
625
+    insert into (title, author) values (?, ?);
617 626
 
618 627
 The values of C<title> and C<author> is embedded into placeholders.
619 628
 
620
-C<append> and C<filter> argument can be specified if you need.
629
+C<append> and C<filter> argument can be specified
630
+to C<insert()> method if you need.
621 631
 
622 632
 =head3 update()
623 633
 
634
+Execute update statement.
635
+
624 636
     $dbi->update(table  => 'books', 
625 637
                  param  => {title => 'aaa', author => 'Ken'}, 
626 638
                  where  => {id => 5});
... ...
@@ -631,12 +643,15 @@ The following SQL is executed.
631 643
 
632 644
 The values of C<title> and C<author> is embedded into placeholders.
633 645
 
634
-If you want to update all rows, use C<update_all()> method instead.
646
+C<append> and C<filter> argument can be specified
647
+to C<update()> method if you need.
635 648
 
636
-C<append> and C<filter> argument can be specified if you need.
649
+If you want to update all rows, use C<update_all()> method instead.
637 650
 
638 651
 =head3 delete()
639 652
 
653
+Execute delete statement.
654
+
640 655
     $dbi->delete(table  => 'books',
641 656
                  where  => {author => 'Ken'});
642 657
 
... ...
@@ -644,15 +659,16 @@ The following SQL is executed.
644 659
 
645 660
     delete from books where id = ?;
646 661
 
647
-The value of C<id> is embedded into a placehodler.
662
+The value of C<id> is embedded into the placehodler.
648 663
 
649
-C<append> and C<filter> argument can be specified if you need.
664
+C<append> and C<filter> argument can be specified
665
+to C<delete()> method if you need.
650 666
 
651 667
 If you want to delete all rows, use C<delete_all()> method instead.
652 668
 
653 669
 =head3 select()
654 670
 
655
-Specify only table:
671
+Execute select statement, only C<table> argument specified :
656 672
 
657 673
     my $result = $dbi->select(table => 'books');
658 674
 
... ...
@@ -661,7 +677,7 @@ The following SQL is executed.
661 677
     select * from books;
662 678
 
663 679
 the result of C<select()> method is L<DBIx::Custom::Result> object.
664
-use C<fetch()> method to fetch a row.
680
+You can fetch row.
665 681
 
666 682
     while (my $row = $result->fetch) {
667 683
         my $title  = $row->[0];
... ...
@@ -669,9 +685,9 @@ use C<fetch()> method to fetch a row.
669 685
     }
670 686
 
671 687
 L<DBIx::Custom::Result> has various methods to fetch row.
672
-See "3. Result of select statement".
688
+See "3. Fetch row".
673 689
 
674
-Specify C<column> and C<where> arguments:
690
+Specify C<column> and C<where> arguments.
675 691
 
676 692
     my $result = $dbi->select(
677 693
         table  => 'books',
... ...
@@ -684,7 +700,7 @@ The following SQL is executed.
684 700
 
685 701
 the value of C<author> is embdded into placeholder.
686 702
 
687
-If C<relation> argument is specifed, you can join tables.
703
+If you want to join tables, specify C<relation> argument. 
688 704
 
689 705
     my $result = $dbi->select(
690 706
         table    => ['books', 'rental'],
... ...
@@ -698,7 +714,7 @@ The following SQL is executed.
698 714
     where books.id = rental.book_id;
699 715
 
700 716
 C<append> argument add a string to the end of SQL statement.
701
-It is useful to add "order by" or "limit" cluase.
717
+You can add "order by" or "limit" cluase.
702 718
 
703 719
     # Select, more complex
704 720
     my $result = $dbi->select(
... ...
@@ -711,12 +727,13 @@ The following SQL is executed.
711 727
 
712 728
     select * books where author = ? order by price limit 5;
713 729
 
714
-C<filter> argument can be specified if you need.
730
+C<filter> argument can be specified to filter parameters
731
+if you need.
715 732
 
716
-=head2 3. Result of select statement
733
+=head2 3. Fetch row
717 734
 
718
-C<select> method reurn L<DBIx::Custom::Result> object.
719
-Using various methods, you can fetch row.
735
+C<select()> method return L<DBIx::Custom::Result> object.
736
+You can fetch row by various methods.
720 737
 
721 738
 Fetch row into array.
722 739
     
... ...
@@ -771,7 +788,7 @@ Fetch all rows into array of hash
771 788
 
772 789
     my $rows = $result->fetch_hash_all;
773 790
 
774
-If you want to access row statement handle of L<DBI>, use sth() attribute.
791
+If you want to access raw statement handle of L<DBI>, use C<sth()> attribute.
775 792
 
776 793
     my $sth = $result->sth;
777 794
 
... ...
@@ -788,11 +805,11 @@ At frist, I show normal way of parameter binding.
788 805
     );
789 806
     $sth->execute('Ken', '%Perl%');
790 807
 
791
-This is very good way because database system enable SQL caching,
792
-and parameter is quoted automatically.
808
+This is very good way because database system can enable SQL caching,
809
+and parameter is quoted automatically, it is secure.
793 810
 
794
-L<DBIx::Custom>hash parameter binding system improve normal parameter binding to
795
-specify hash parameter.
811
+L<DBIx::Custom> hash parameter binding system improve
812
+normal parameter binding way to specify hash parameter.
796 813
 
797 814
     my $result = $dbi->execute(
798 815
         "select * from books where {= author} and {like title};"
... ...
@@ -800,12 +817,40 @@ specify hash parameter.
800 817
     );
801 818
 
802 819
 This is same as the normal way, execpt that the parameter is hash.
803
-{= author} is called C<tag>. tag is expand to placeholder internally.
820
+{= author} is called C<tag>. tag is expand to placeholder string internally.
804 821
 
805 822
     select * from books where {= author} and {like title}
806 823
       -> select * from books where author = ? and title like ?;
807 824
 
808
-See L<DBIx::Custom::QueryBuilder> to know all tags.
825
+The following tags is available.
826
+
827
+=head1 Tags
828
+
829
+The following tags is available.
830
+
831
+    [TAG]                       [REPLACED]
832
+    {? NAME}               ->   ?
833
+    {= NAME}               ->   NAME = ?
834
+    {<> NAME}              ->   NAME <> ?
835
+    
836
+    {< NAME}               ->   NAME < ?
837
+    {> NAME}               ->   NAME > ?
838
+    {>= NAME}              ->   NAME >= ?
839
+    {<= NAME}              ->   NAME <= ?
840
+    
841
+    {like NAME}            ->   NAME like ?
842
+    {in NAME COUNT}        ->   NAME in [?, ?, ..]
843
+    
844
+    {insert NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
845
+    {update NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
846
+
847
+See also L<DBIx::Custom::QueryBuilder>.
848
+
849
+Default start tag is '{'. end tag is '}'.
850
+You can change this tag.
851
+
852
+    $dbi->query_builder->start_tag('|');
853
+    $dbi->query_builder->end_tag('|');
809 854
 
810 855
 =head2 5. Filtering
811 856
 
... ...
@@ -814,7 +859,7 @@ If you want to save the string to database, You must encode the string.
814 859
 Filtering system help you to convert a data to another data
815 860
 when you save to the data and get the data form database.
816 861
 
817
-If you want to register filter, use register_filter() method.
862
+If you want to register filter, use C<register_filter()> method.
818 863
 
819 864
     $dbi->register_filter(
820 865
         to_upper_case => sub {
... ...
@@ -829,7 +874,7 @@ You can specify these filters to C<filter> argument of C<execute()> method.
829 874
 
830 875
     my $result = $dbi->execute(
831 876
         "select * from books where {= author} and {like title};"
832
-        param => {author => 'Ken', title => '%Perl%'});
877
+        param  => {author => 'Ken', title => '%Perl%'},
833 878
         filter => {author => 'to_upper_case, title => 'encode_utf8'}
834 879
     );
835 880
 
... ...
@@ -848,8 +893,7 @@ delete(), delete_all(), select().
848 893
         filter => {title => 'encode_utf8'}
849 894
     );
850 895
 
851
-Filter work to each parmeter, but you prepare default filter for all parameters.
852
-you can use C<default_bind_filter()> attribute.
896
+Filter works each parmeter, but you prepare default filter for all parameters.
853 897
 
854 898
     $dbi->default_bind_filter('encode_utf8');
855 899
 
... ...
@@ -870,7 +914,7 @@ This is same as the following one.
870 914
         filter => {title => 'encode_uft8' author => 'to_upper_case'}
871 915
     );
872 916
 
873
-You can also specify filter when the row is fetching. This is reverse of bindig filter.
917
+You can also specify filter when the row is fetched. This is reverse of bind filter.
874 918
 
875 919
     my $result = $dbi->select(table => 'books');
876 920
     $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
... ...
@@ -879,7 +923,7 @@ you can specify C<default_fetch_filter()>.
879 923
 
880 924
     $dbi->default_fetch_filter('decode_utf8');
881 925
 
882
-C<DBIx::Custom::Result::filter> overwrites the filter specified
926
+C<DBIx::Custom::Result::filter()> overwrites the filter specified
883 927
 by C<default_fetch_filter()>
884 928
 
885 929
     $dbi->default_fetch_filter('decode_utf8');
... ...
@@ -897,6 +941,112 @@ This is same as the following one.
897 941
     );
898 942
     $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
899 943
 
944
+=head2 6. Performance
945
+
946
+If you execute insert statement by using select() method,
947
+you sometimes can't meet performance requirment.
948
+
949
+C<insert()> method is a little slow because SQL statement and statement handle
950
+is created every time.
951
+
952
+In that case, you can prepare a query by C<create_query()> method.
953
+    
954
+    my $query = $dbi->create_query(
955
+        "insert into books {insert title author};"
956
+    );
957
+    
958
+    # (In the case of update statement)
959
+    my $query = $dbi->create_query(
960
+        "update books {update author};";
961
+    );
962
+
963
+Execute query repeatedly
964
+    
965
+    my $inputs = [
966
+        {title => 'Perl',      author => 'Ken'},
967
+        {title => 'Good days', author => 'Mike'}
968
+    ];
969
+    
970
+    foreach my $input (@$inputs) {
971
+        $dbi->execute($query, $input);
972
+    }
973
+
974
+This is faster than C<insert()> and C<update()> method.
975
+
976
+C<execute()> method cache the parsing result of SQL soruce.
977
+Default to 1
978
+
979
+    $dbi->cache(1);
980
+
981
+Caching is on memory, but you can change this by C<cache_method()>.
982
+First argument is L<DBIx::Custom> object.
983
+Second argument is SQL source,
984
+such as "select * from books where {=title} and {=author};";
985
+Third argument is parsed result, such as
986
+{sql => "select * from books where title = ? and author = ?",
987
+ columns => ['title', 'author']}, this is hash reference.
988
+If argument is more than two, this is called ti set cache.
989
+otherwise, called to get cache.
990
+
991
+    $dbi->cache_mehod(sub {
992
+        sub {
993
+            my $self = shift;
994
+            
995
+            $self->{_cached} ||= {};
996
+            
997
+            # Set cache
998
+            if (@_ > 1) {
999
+                $self->{_cached}{$_[0]} = $_[1] 
1000
+            }
1001
+            
1002
+            # Get cache
1003
+            else {
1004
+                return $self->{_cached}{$_[0]}
1005
+            }
1006
+        }
1007
+    });
1008
+
1009
+=head2 7. More features
1010
+
1011
+=head3 Get DBI object
1012
+
1013
+You can get L<DBI> object and call any method of L<DBI>.
1014
+
1015
+    $dbi->dbh->begin_work;
1016
+    $dbi->dbh->commit;
1017
+    $dbi->dbh->rollback;
1018
+
1019
+=head3 Change Result class
1020
+
1021
+You can change Result class if you need.
1022
+
1023
+    package Your::Result;
1024
+    use base 'DBIx::Custom::Result';
1025
+    
1026
+    sub some_method { ... }
1027
+
1028
+    1;
1029
+    
1030
+    package main;
1031
+    
1032
+    use Your::Result;
1033
+    
1034
+    my $dbi = DBIx::Custom->connect(...);
1035
+    $dbi->result_class('Your::Result');
1036
+
1037
+=head3 Custamize SQL builder object
1038
+
1039
+You can custamize SQL builder object
1040
+
1041
+    my $dbi = DBIx::Custom->connect(...);
1042
+    $dbi->query_builder->start_tag('|');
1043
+    $dbi->query_builder->end_tag('|');
1044
+    $dbi->query_builder->register_tag_processor(
1045
+        name => sub {
1046
+           ...
1047
+        }
1048
+    );
1049
+
900 1050
 =head1 ATTRIBUTES
901 1051
 
902 1052
 =head2 C<user>