Showing 13 changed files with 234 additions and 353 deletions
+19
Changes
... ...
@@ -1,3 +1,22 @@
1
+0.1502
2
+  update document
3
+  moved host attribute to DBIx::Custom::MySQL
4
+  moved port attribute to DBIx::Custom::MySQL
5
+  moved database attribute to DBIx::Custom::MySQL and DBIx::Custom::SQLite
6
+  
7
+0.1501
8
+  removed register_format()
9
+  removed formats()
10
+  removed run_transaction()
11
+  removed create_table()
12
+  removed drop_table()
13
+  changed select() arguments
14
+  changed insert() arguments
15
+  changed update() arguments
16
+  changed update_all() arguments
17
+  changed delete() arguments
18
+  changed delete_all() arguments
19
+  changed execute() arguments
1 20
 0.1402
2 21
   remove finish(), error()
3 22
   can receive code ref to filter()
+64 -173
lib/DBIx/Custom.pm 1000755 → 1000644
... ...
@@ -10,31 +10,23 @@ use DBI;
10 10
 use DBIx::Custom::Result;
11 11
 use DBIx::Custom::SQLTemplate;
12 12
 use DBIx::Custom::Query;
13
+use Encode qw/encode_utf8 decode_utf8/;
13 14
 
14 15
 __PACKAGE__->attr('dbh');
15 16
 
16
-__PACKAGE__->class_attr(_query_caches     => sub { {} });
17
-__PACKAGE__->class_attr(_query_cache_keys => sub { [] });
18
-
19
-__PACKAGE__->class_attr('query_cache_max', default => 50,
20
-                                           inherit => 'scalar_copy');
21
-
22 17
 __PACKAGE__->attr([qw/user password data_source/]);
23
-__PACKAGE__->attr([qw/database host port/]);
24
-__PACKAGE__->attr([qw/default_query_filter default_fetch_filter options/]);
18
+__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]);
25 19
 
26 20
 __PACKAGE__->dual_attr('filters', default => sub { {} },
27 21
                                   inherit => 'hash_copy');
28 22
 __PACKAGE__->register_filter(
29
-    encode_utf8 => sub { encode('UTF-8', $_[0]) },
30
-    decode_utf8 => sub { decode('UTF-8', $_[0]) }
23
+    encode_utf8 => sub { encode_utf8($_[0]) },
24
+    decode_utf8 => sub { decode_utf8($_[0]) }
31 25
 );
32 26
 
33 27
 __PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
34 28
 __PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new });
35 29
 
36
-
37
-
38 30
 sub register_filter {
39 31
     my $invocant = shift;
40 32
     
... ...
@@ -71,7 +63,6 @@ sub connect {
71 63
     my $data_source = $self->data_source;
72 64
     my $user        = $self->user;
73 65
     my $password    = $self->password;
74
-    my $options     = $self->options;
75 66
     
76 67
     # Connect
77 68
     my $dbh = eval{DBI->connect(
... ...
@@ -82,7 +73,6 @@ sub connect {
82 73
             RaiseError => 1,
83 74
             PrintError => 0,
84 75
             AutoCommit => 1,
85
-            %{$options || {} }
86 76
         }
87 77
     )};
88 78
     
... ...
@@ -130,36 +120,28 @@ sub reconnect {
130 120
 sub create_query {
131 121
     my ($self, $template) = @_;
132 122
     
133
-    my $class = ref $self;
134
-    
135
-    if (ref $template eq 'ARRAY') {
136
-        $template = $template->[1];
137
-    }
138
-    
139 123
     # Create query from SQL template
140 124
     my $sql_template = $self->sql_template;
141 125
     
142
-    # Try to get cached query
143
-    my $cached_query = $class->_query_caches->{"$template"};
126
+    # Get cached query
127
+    my $cache = $self->{_cache}->{"$template"};
144 128
     
145 129
     # Create query
146 130
     my $query;
147
-    if ($cached_query) {
131
+    if ($cache) {
148 132
         $query = DBIx::Custom::Query->new(
149
-            sql       => $cached_query->sql,
150
-            columns => $cached_query->columns
133
+            sql       => $cache->sql,
134
+            columns   => $cache->columns
151 135
         );
152 136
     }
153 137
     else {
154 138
         $query = eval{$sql_template->create_query($template)};
155 139
         croak($@) if $@;
156 140
         
157
-        $class->_add_query_cache("$template", $query);
141
+        $self->{_cache}->{$template} = $query
142
+          unless $self->{_cache};
158 143
     }
159 144
     
160
-    # Connect if not
161
-    $self->connect unless $self->connected;
162
-    
163 145
     # Prepare statement handle
164 146
     my $sth = $self->dbh->prepare($query->{sql});
165 147
     
... ...
@@ -549,41 +531,17 @@ sub select {
549 531
     return $result;
550 532
 }
551 533
 
552
-sub _add_query_cache {
553
-    my ($class, $template, $query) = @_;
554
-    
555
-    # Query information
556
-    my $query_cache_keys = $class->_query_cache_keys;
557
-    my $query_caches     = $class->_query_caches;
558
-    
559
-    # Already cached
560
-    return $class if $query_caches->{$template};
561
-    
562
-    # Cache
563
-    $query_caches->{$template} = $query;
564
-    push @$query_cache_keys, $template;
565
-    
566
-    # Check cache overflow
567
-    my $overflow = @$query_cache_keys - $class->query_cache_max;
568
-    for (my $i = 0; $i < $overflow; $i++) {
569
-        my $template = shift @$query_cache_keys;
570
-        delete $query_caches->{$template};
571
-    }
572
-    
573
-    return $class;
574
-}
575
-
576 534
 =head1 NAME
577 535
 
578
-DBIx::Custom - DBI with hash bind and filtering system 
536
+DBIx::Custom - DBI with hash parameter binding and filtering system
579 537
 
580 538
 =head1 VERSION
581 539
 
582
-Version 0.1501
540
+Version 0.1502
583 541
 
584 542
 =cut
585 543
 
586
-our $VERSION = '0.1501';
544
+our $VERSION = '0.1502';
587 545
 $VERSION = eval $VERSION;
588 546
 
589 547
 =head1 STATE
... ...
@@ -598,23 +556,23 @@ This module is not stable. Method name and functionality will be change.
598 556
     
599 557
     # Insert 
600 558
     $dbi->insert(table  => 'books',
601
-                 param  => {title => 'perl', author => 'Ken'}
559
+                 param  => {title => 'perl', author => 'Ken'},
602 560
                  filter => {title => 'encode_utf8'});
603 561
     
604 562
     # Update 
605 563
     $dbi->update(table  => 'books', 
606 564
                  param  => {title => 'aaa', author => 'Ken'}, 
607
-                 where  => {id => 5}
608
-                 filter => {title => 'encode_utf8');
565
+                 where  => {id => 5},
566
+                 filter => {title => 'encode_utf8'});
609 567
     
610 568
     # Update all
611 569
     $dbi->update_all(table  => 'books',
612
-                     param  => {title => 'aaa'}
570
+                     param  => {title => 'aaa'},
613 571
                      filter => {title => 'encode_utf8'});
614 572
     
615 573
     # Delete
616 574
     $dbi->delete(table  => 'books',
617
-                 where  => {author => 'Ken'}
575
+                 where  => {author => 'Ken'},
618 576
                  filter => {title => 'encode_utf8'});
619 577
     
620 578
     # Delete all
... ...
@@ -625,13 +583,11 @@ This module is not stable. Method name and functionality will be change.
625 583
     
626 584
     # Select(more complex)
627 585
     my $result = $dbi->select(
628
-        'books',
629
-        {
630
-            columns => [qw/author title/],
631
-            where   => {author => 'Ken'},
632
-            append  => 'order by id limit 1',
633
-            filter  => {tilte => 'encode_utf8'}
634
-        }
586
+        table  => 'books',
587
+        column => [qw/author title/],
588
+        where  => {author => 'Ken'},
589
+        append => 'order by id limit 1',
590
+        filter => {tilte => 'encode_utf8'}
635 591
     );
636 592
 
637 593
     # Execute SQL
... ...
@@ -656,157 +612,87 @@ This module is not stable. Method name and functionality will be change.
656 612
         
657 613
     }
658 614
     
615
+    # DBI instance
616
+    my $dbh = $dbi->dbh;
659 617
     
660 618
 =head1 ATTRIBUTES
661 619
 
662 620
 =head2 user
663 621
 
664
-Database user name
622
+Database user name.
665 623
     
666 624
     $dbi  = $dbi->user('Ken');
667 625
     $user = $dbi->user;
668 626
     
669 627
 =head2 password
670 628
 
671
-Database password
629
+Database password.
672 630
     
673 631
     $dbi      = $dbi->password('lkj&le`@s');
674 632
     $password = $dbi->password;
675 633
 
676 634
 =head2 data_source
677 635
 
678
-Database data source
636
+Database data source.
679 637
     
680 638
     $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
681 639
     $data_source = $dbi->data_source;
682 640
     
683 641
 If you know data source more, See also L<DBI>.
684 642
 
685
-=head2 database
686
-
687
-Database name
688
-
689
-    $dbi      = $dbi->database('books');
690
-    $database = $dbi->database;
691
-
692
-=head2 host
693
-
694
-Host name
695
-
696
-    $dbi  = $dbi->host('somehost.com');
697
-    $host = $dbi->host;
698
-
699
-You can also set IP address like '127.03.45.12'.
700
-
701
-=head2 port
702
-
703
-Port number
704
-
705
-    $dbi  = $dbi->port(1198);
706
-    $port = $dbi->port;
707
-
708
-=head2 options
709
-
710
-DBI options
711
-
712
-    $dbi     = $dbi->options({PrintError => 0, RaiseError => 1});
713
-    $options = $dbi->options;
714
-
715 643
 =head2 sql_template
716 644
 
717
-SQLTemplate object
645
+SQLTemplate instance. sql_template attribute must be 
646
+the instance of L<DBIx::Cutom::SQLTemplate> subclass.
718 647
 
719 648
     $dbi          = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new);
720 649
     $sql_template = $dbi->sql_template;
721 650
 
722
-See also L<DBIx::Custom::SQLTemplate>.
651
+the instance of DBIx::Cutom::SQLTemplate is set to 
652
+this attribute by default.
723 653
 
724 654
 =head2 filters
725 655
 
726 656
 Filters
727 657
 
728
-    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
658
+    $dbi     = $dbi->filters({%filters});
729 659
     $filters = $dbi->filters;
730
-    
731
-This method is generally used to get a filter.
732 660
 
733
-    $filter = $dbi->filters->{encode_utf8};
661
+encode_utf8 and decode_utf8 is set to this attribute by default.
734 662
 
735
-If you add filter, use register_filter method.
663
+    $encode_utf8 = $dbi->filters->{encode_utf8};
664
+    $decode_utf8 = $dbi->filters->{decode_utf8};
736 665
 
737 666
 =head2 default_query_filter
738 667
 
739
-Default query filter
668
+Default query filter.
740 669
 
741
-    $dbi                  = $dbi->default_query_filter($default_query_filter);
670
+    $dbi                  = $dbi->default_query_filter('encode_utf8');
742 671
     $default_query_filter = $dbi->default_query_filter
743 672
 
744
-Query filter example
745
-    
746
-    $dbi->register_filter(encode_utf8 => sub {
747
-        my $value = shift;
748
-        
749
-        require Encode 'encode_utf8';
750
-        
751
-        return encode_utf8($value);
752
-    });
753
-    
754
-    $dbi->default_query_filter('encode_utf8')
755
-
756
-Bind filter arguemts is
757
-
758
-    1. $value : Value
759
-    3. $dbi   : DBIx::Custom instance
760
-
761 673
 =head2 default_fetch_filter
762 674
 
763
-Fetching filter
675
+Fetching filter.
764 676
 
765
-    $dbi                  = $dbi->default_fetch_filter($default_fetch_filter);
677
+    $dbi                  = $dbi->default_fetch_filter('decode_utf8');
766 678
     $default_fetch_filter = $dbi->default_fetch_filter;
767 679
 
768
-Fetch filter example
769
-
770
-    $dbi->register_filter(decode_utf8 => sub {
771
-        my $value = shift;
772
-        
773
-        require Encode 'decode_utf8';
774
-        
775
-        return decode_utf8($value);
776
-    });
777
-
778
-    $dbi->default_fetch_filter('decode_utf8');
779
-
780
-Fetching filter arguemts is
781
-
782
-    1. Value
783
-    2. DBIx::Custom instance
784
-
785 680
 =head2 result_class
786 681
 
787
-Resultset class
682
+Result class.
788 683
 
789 684
     $dbi          = $dbi->result_class('DBIx::Custom::Result');
790 685
     $result_class = $dbi->result_class;
791 686
 
792
-Default is L<DBIx::Custom::Result>
687
+L<DBIx::Custom::Result> is set to this attribute by default.
793 688
 
794 689
 =head2 dbh
795 690
 
796
-Database handle
691
+Database handle.
797 692
     
798 693
     $dbi = $dbi->dbh($dbh);
799 694
     $dbh = $dbi->dbh;
800 695
     
801
-=head2 query_cache_max
802
-
803
-Query cache max
804
-
805
-    $class           = DBIx::Custom->query_cache_max(50);
806
-    $query_cache_max = DBIx::Custom->query_cache_max;
807
-
808
-Default value is 50
809
-
810 696
 =head1 METHODS
811 697
 
812 698
 This class is L<Object::Simple> subclass.
... ...
@@ -814,20 +700,29 @@ You can use all methods of L<Object::Simple>
814 700
 
815 701
 =head2 auto_commit
816 702
 
817
-Set and Get auto commit
703
+Auto commit.
818 704
 
819
-    $self        = $dbi->auto_commit($auto_commit);
705
+    $self        = $dbi->auto_commit(1);
820 706
     $auto_commit = $dbi->auto_commit;
821
-    
707
+
708
+This is equal to
709
+
710
+    $dbi->dbh->{AutoCommit} = 1;
711
+    $auto_commit = $dbi->dbh->{AutoCommit};
712
+
822 713
 =head2 connect
823 714
 
824
-Connect to database
715
+Connect to database.
716
+    
717
+    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
718
+                                    user => 'ken', password => '!LFKD%$&');
825 719
 
826
-    $dbi->connect;
720
+"AutoCommit" and "RaiseError" option is true, 
721
+and "PrintError" option is false by dfault.
827 722
 
828 723
 =head2 disconnect
829 724
 
830
-Disconnect database
725
+Disconnect database.
831 726
 
832 727
     $dbi->disconnect;
833 728
 
... ...
@@ -835,7 +730,7 @@ If database is already disconnected, this method do nothing.
835 730
 
836 731
 =head2 reconnect
837 732
 
838
-Reconnect to database
733
+Reconnect to database.
839 734
 
840 735
     $dbi->reconnect;
841 736
 
... ...
@@ -847,11 +742,11 @@ Check if database is connected.
847 742
     
848 743
 =head2 register_filter
849 744
 
850
-Resister filter
745
+Resister filter.
851 746
     
852
-    $dbi->register_filter($fname1 => $filter1, $fname => $filter2);
747
+    $dbi->register_filter(%filters);
853 748
     
854
-register_filter example
749
+Example.
855 750
 
856 751
     $dbi->register_filter(
857 752
         encode_utf8 => sub {
... ...
@@ -872,7 +767,7 @@ register_filter example
872 767
 
873 768
 =head2 create_query
874 769
     
875
-Create Query object parsing SQL template
770
+Create Query instance parsing SQL template
876 771
 
877 772
     my $query = $dbi->create_query("select * from authors where {= name} and {= age}");
878 773
 
... ...
@@ -1029,10 +924,6 @@ You can join multi tables
1029 924
         "where table1.id = table2.id",       # join clause (must start 'where')
1030 925
     );
1031 926
 
1032
-=head1 DBIx::Custom default configuration
1033
-
1034
-By default, "AutoCommit" and "RaiseError" is true.
1035
-
1036 927
 =head1 AUTHOR
1037 928
 
1038 929
 Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
+56 -21
lib/DBIx/Custom/MySQL.pm
... ...
@@ -3,11 +3,17 @@ package DBIx::Custom::MySQL;
3 3
 use warnings;
4 4
 use strict;
5 5
 
6
-use base 'DBIx::Custom::Basic';
6
+use base 'DBIx::Custom';
7
+
7 8
 use Carp 'croak';
8 9
 
10
+__PACKAGE__->attr([qw/database host port/]);
11
+
9 12
 sub connect {
10
-    my $self = shift;
13
+    my $proto = shift;
14
+    
15
+    # Create
16
+    my $self = ref $proto ? $proto : $proto->new(@_);
11 17
     
12 18
     # Create data source
13 19
     if (!$self->data_source) {
... ...
@@ -15,11 +21,9 @@ sub connect {
15 21
         my $host     = $self->host;
16 22
         my $port     = $self->port;
17 23
         my $data_source = "dbi:mysql:";
18
-        my $data_source_original = $data_source;
19 24
         $data_source .= "database=$database;" if $database;
20 25
         $data_source .= "host=$host;"         if $host;
21 26
         $data_source .= "port=$port;"         if $port;
22
-        $data_source =~ s/:$// if $data_source eq $data_source_original;
23 27
         $self->data_source($data_source);
24 28
     }
25 29
     
... ...
@@ -42,38 +46,69 @@ sub last_insert_id {
42 46
 
43 47
 =head1 NAME
44 48
 
45
-DBIx::Custom::MySQL - DBIx::Custom MySQL implementation
49
+DBIx::Custom::MySQL - a MySQL implementation of DBIx::Custom
46 50
 
47 51
 =head1 SYNOPSYS
48 52
 
49
-    # New
50
-    my $dbi = DBIx::Custom::MySQL->new(user => 'taro', $password => 'kliej&@K',
51
-                                       database => 'sample_db');
53
+    # Connect
54
+    my $dbi = DBIx::Custom::MySQL->connect(user      => 'taro', 
55
+                                           password => 'kliej&@K',
56
+                                           database  => 'your_database');
57
+    
58
+    # Last insert id
59
+    my $id = $dbi->last_insert_id;
52 60
 
53
-=head1 METHODS
61
+=head1 ATTRIBUTES
54 62
 
55
-This class is L<DBIx::Custom::Basic> subclass.
56
-You can use all methods of L<DBIx::Custom::Basic>
63
+This class is L<DBIx::Custom> subclass.
64
+You can use all attributes of L<DBIx::Custom>
57 65
 
58
-=head2 connect
66
+=head2 database
59 67
 
60
-Connect to database
68
+Database name
61 69
 
62
-    $self->connect;
70
+    $dbi      = $dbi->database('your_database');
71
+    $database = $dbi->database;
63 72
 
64
-If you set database, host, or port, data source is automatically created.
73
+=head2 host
65 74
 
66
-=head2 last_insert_id
75
+Database host name.
67 76
 
68
-    $last_insert_id = $dbi->last_insert_id;
77
+    $dbi  = $dbi->host('somehost.com');
78
+    $host = $dbi->host;
79
+
80
+IP address can be set to host attribute.
81
+
82
+    $dbi->host('127.03.45.12');
83
+
84
+=head2 port
85
+
86
+Database port.
87
+
88
+    $dbi  = $dbi->port(1198);
89
+    $port = $dbi->port;
90
+
91
+=head1 METHODS
92
+
93
+This class is L<DBIx::Custom> subclass.
94
+You can use all methods of L<DBIx::Custom>.
95
+
96
+=head2 connect - overridden
97
+
98
+Connect to database.
99
+
100
+    # Connect
101
+    my $dbi = DBIx::Custom::MySQL->connect(user      => 'taro', 
102
+                                           password => 'kliej&@K',
103
+                                           database  => 'your_database');
104
+
105
+=head2 last_insert_id
69 106
 
70
-The folloing is last_insert_id sample.
107
+Last insert ID.
71 108
 
72
-    $dbi->insert('books', {title => 'Perl', author => 'taro'});
73 109
     $last_insert_id = $dbi->last_insert_id;
74 110
 
75
-This is equal to MySQL function
111
+This is equal to MySQL last_insert_id() function.
76 112
 
77
-    last_insert_id()
78 113
     
79 114
 =cut
+10 -17
lib/DBIx/Custom/Query.pm
... ...
@@ -11,60 +11,53 @@ __PACKAGE__->attr([qw/sql columns default_filter filter sth/]);
11 11
 
12 12
 =head1 NAME
13 13
 
14
-DBIx::Custom::Query - DBIx::Custom query
14
+DBIx::Custom::Query - query used by DBIx::Custom
15 15
 
16 16
 =head1 SYNOPSIS
17 17
     
18 18
     # New
19 19
     my $query = DBIx::Custom::Query->new;
20 20
     
21
-    # Create by using create_query
22
-    my $query = DBIx::Custom->create_query($template);
23
-    
24 21
 =head1 ATTRIBUTES
25 22
 
26 23
 =head2 sth
27 24
 
28
-Statement handle
25
+Statement handle.
29 26
 
30 27
     $query = $query->sth($sth);
31 28
     $sth   = $query->sth;
32 29
 
33 30
 =head2 sql
34 31
 
35
-SQL
32
+SQL statement.
36 33
 
37 34
     $query = $query->sql($sql);
38 35
     $sql   = $query->sql;
39 36
 
40 37
 =head2 default_filter
41 38
 
42
-Filter excuted when value is bind
39
+Default filter.
43 40
 
44
-    $query          = $query->default_filter($default_filter);
41
+    $query          = $query->default_filter($filter);
45 42
     $default_filter = $query->default_filter;
46 43
 
47 44
 =head2 filter
48 45
 
49
-Filter excuted when value is bind
46
+Filter.
50 47
 
51
-    $query  = $query->filter($filter);
48
+    $query  = $query->filter({%filter});
52 49
     $filter = $query->filter;
53 50
 
54 51
 =head2 columns
55 52
 
56
-Key informations
53
+Column names.
57 54
 
58
-    $query   = $query->columns($columns);
55
+    $query   = $query->columns([@columns]);
59 56
     $columns = $query->columns;
60 57
 
61 58
 =head1 METHODS
62 59
 
63 60
 This class is L<Object::Simple> subclass.
64
-You can use all methods of L<Object::Simple>
65
-
66
-=head2 new
67
-
68
-    my $query = DBIx::Custom::Query->new;
61
+You can use all methods of L<Object::Simple>.
69 62
 
70 63
 =cut
+30 -54
lib/DBIx/Custom/Result.pm
... ...
@@ -10,83 +10,59 @@ use Carp 'croak';
10 10
 __PACKAGE__->attr([qw/sth filters default_filter filter/]);
11 11
 
12 12
 sub fetch {
13
-    my ($self, $type) = @_;
14
-    
15
-    my $sth     = $self->{sth};
16
-    my $filters = $self->{filters} || {};
17
-    my $filter  = $self->{filter} || {};
13
+
14
+    $_[0]->{filters} ||= {};
15
+    $_[0]->{filter}  ||= {};
18 16
     
19 17
     # Fetch
20
-    my $row = $sth->fetchrow_arrayref;
18
+    my @row = $_[0]->{sth}->fetchrow_array;
21 19
     
22 20
     # Cannot fetch
23
-    return unless $row;
21
+    return unless @row;
24 22
 
25
-    # Key
26
-    my $columns  = $sth->{NAME_lc};
27
-    
28 23
     # Filter
29
-    for (my $i = 0; $i < @$columns; $i++) {
30
-        my $fname  = $filter->{$columns->[$i]} || $self->{default_filter} || '';
24
+    for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) {
25
+        my $fname  = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]} 
26
+                  || $_[0]->{default_filter};
27
+        
28
+        croak "Filter \"$fname\" is not registered."
29
+          if $fname && ! exists $_[0]->{filters}->{$fname};
31 30
         
32
-        if ($fname) {
33
-            my $filter;
34
-            
35
-            if (ref $fname) {
36
-                $filter = $fname;
37
-            }
38
-            else {
39
-                croak "Filter \"$fname\" is not registered."
40
-                  unless exists $filters->{$fname};
41
-                  
42
-                $filter = $filters->{$fname};
43
-            }
44
-            $row->[$i] = $filter->($row->[$i]);
45
-        }
31
+        next unless $fname;
32
+        
33
+        $row[$i] = ref $fname
34
+                   ? $fname->($row[$i]) 
35
+                   : $_[0]->{filters}->{$fname}->($row[$i]);
46 36
     }
47 37
 
48
-    return wantarray ? @$row : $row;
38
+    return wantarray ? @row : \@row;
49 39
 }
50 40
 
51 41
 sub fetch_hash {
52
-    my $self = shift;
53 42
 
54
-    my $sth            = $self->{sth};
55
-    my $filters        = $self->{filters} || {};
56
-    my $filter         = $self->{filter} || {};
43
+    $_[0]->{filters} ||= {};
44
+    $_[0]->{filter}  ||= {};
57 45
     
58 46
     # Fetch
59
-    my $row = $sth->fetchrow_arrayref;
47
+    my $row = $_[0]->{sth}->fetchrow_arrayref;
60 48
     
61 49
     # Cannot fetch
62 50
     return unless $row;
63 51
     
64
-    # Keys
65
-    my $columns  = $sth->{NAME_lc};
66
-    
67 52
     # Filter
68 53
     my $row_hash = {};
69
-    for (my $i = 0; $i < @$columns; $i++) {
54
+    for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) {
55
+        
56
+        my $fname  = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]}
57
+                  || $_[0]->{default_filter};
70 58
         
71
-        my $fname  = $filter->{$columns->[$i]} || $self->{default_filter} || '';
59
+        croak "Filter \"$fname\" is not registered."
60
+          if $fname && ! exists $_[0]->{filters}->{$fname};
72 61
         
73
-        if ($fname) {
74
-            my $filter;
75
-            
76
-            if (ref $fname) {
77
-                $filter = $fname;
78
-            }
79
-            else {
80
-                croak "Filter \"$fname\" is not registered."
81
-                  unless exists $filters->{$fname};
82
-                  
83
-                $filter = $filters->{$fname};
84
-            }
85
-            $row_hash->{$columns->[$i]} = $filter->($row->[$i]);
86
-        }
87
-        else {
88
-            $row_hash->{$columns->[$i]} = $row->[$i];
89
-        }
62
+        $row_hash->{$_[0]->{sth}->{NAME_lc}->[$i]}
63
+          = !$fname    ? $row->[$i] :
64
+            ref $fname ? $fname->($row->[$i]) :
65
+            $_[0]->{filters}->{$fname}->($row->[$i]);
90 66
     }
91 67
     
92 68
     return wantarray ? %$row_hash : $row_hash;
lib/DBIx/Custom/SQLTemplate.pm 1000755 → 1000644
File mode changed.
+39 -22
lib/DBIx/Custom/SQLite.pm
... ...
@@ -3,11 +3,17 @@ package DBIx::Custom::SQLite;
3 3
 use strict;
4 4
 use warnings;
5 5
 
6
-use base 'DBIx::Custom::Basic';
6
+use base 'DBIx::Custom';
7
+
7 8
 use Carp 'croak';
8 9
 
10
+__PACKAGE__->attr('database');
11
+
9 12
 sub connect {
10
-    my $self = shift;
13
+    my $proto = shift;
14
+    
15
+    # Create
16
+    my $self = ref $proto ? $proto : $proto->new(@_);
11 17
     
12 18
     # Create data source
13 19
     if (!$self->data_source && (my $database = $self->database)) {
... ...
@@ -60,28 +66,46 @@ sub last_insert_rowid {
60 66
 
61 67
 =head1 NAME
62 68
 
63
-DBIx::Custom::SQLite - DBIx::Custom SQLite implementation
69
+DBIx::Custom::SQLite - a SQLite implementation of DBIx::Custom
64 70
 
65 71
 =head1 SYNOPSYS
66 72
 
67 73
     use DBIx::Custom::SQLite;
68 74
     
69
-    # New
70
-    my $dbi = DBIx::Custom::SQLite->new(user => 'taro', $password => 'kl&@K',
71
-                                        database => 'sample');
75
+    # Connect
76
+    my $dbi = DBIx::Custom::SQLite->connect(user      => 'taro', 
77
+                                            password => 'kl&@K',
78
+                                            database  => 'your_database');
72 79
     
73 80
     # Connect memory database
74
-    my $dbi->connect_memory;
81
+    my $dbi = DBIx::Custom::SQLite->connect_memory;
82
+    
83
+    # Reconnect memory database
84
+    $dbi->reconnect_memory;
85
+    
86
+    # Last insert row ID
87
+    my $id = $dbi->last_insert_rowid;
75 88
     
89
+=head1 ATTRIBUTES
90
+
91
+This class is L<DBIx::Custom> subclass.
92
+You can use all attributes of L<DBIx::Custom>.
93
+
94
+=head2 database
95
+
96
+Database name
97
+
98
+    $dbi      = $dbi->database('your_database');
99
+    $database = $dbi->database;
76 100
 
77 101
 =head1 METHODS
78 102
 
79
-This class is L<DBIx::Custom::Basic> subclass.
80
-You can use all methods of L<DBIx::Custom::Basic>
103
+This class is L<DBIx::Custom> subclass.
104
+You can use all methods of L<DBIx::Custom>.
81 105
 
82
-=head2 connect
106
+=head2 connect - overridden
83 107
 
84
-Connect to database
108
+Connect to database.
85 109
 
86 110
     $dbi->connect;
87 111
 
... ...
@@ -89,29 +113,22 @@ If you set database, host, or port, data source is automatically created.
89 113
 
90 114
 =head2 connect_memory
91 115
 
92
-Connect memory database
116
+Connect memory database.
93 117
 
94 118
     $dbi->connect_memory;
95 119
 
96 120
 =head2 reconnect_memory
97 121
 
98
-Reconnect to memory databsse
122
+Reconnect to memory databsse.
99 123
 
100 124
     $dbi->reconnect_memory;
101 125
 
102 126
 =head2 last_insert_rowid
103 127
 
104
-Get last insert id
128
+Last insert row ID.
105 129
 
106 130
     $last_insert_rowid = $dbi->last_insert_rowid;
107 131
     
108
-The folloing is last_insert_rowid sample.
109
-
110
-    $dbi->insert('books', {title => 'Perl', author => 'taro'});
111
-    $last_insert_rowid = $dbi->last_insert_rowid;
112
-
113
-This is equal to SQLite function
114
-
115
-    last_insert_rowid()
132
+This is equal to SQLite last_insert_rowid() function.
116 133
 
117 134
 =cut
+1 -2
t/00-load.t
... ...
@@ -1,10 +1,9 @@
1 1
 #!perl -T
2 2
 
3
-use Test::More tests => 7;
3
+use Test::More tests => 6;
4 4
 
5 5
 BEGIN {
6 6
 	use_ok( 'DBIx::Custom' );
7
-	use_ok( 'DBIx::Custom::Basic' );
8 7
 	use_ok( 'DBIx::Custom::MySQL' );
9 8
 	use_ok( 'DBIx::Custom::Query' );
10 9
 	use_ok( 'DBIx::Custom::Result' );
+1 -1
t/dbix-custom-core-mysql-private.t
... ...
@@ -12,7 +12,7 @@ plan 'no_plan';
12 12
 use DBIx::Custom;
13 13
 use Scalar::Util 'blessed';
14 14
 {
15
-    my $dbi = DBIx::Custom->new(
15
+    my $dbi = DBIx::Custom->connect(
16 16
         user => $USER,
17 17
         password => $PASSWORD,
18 18
         data_source => "dbi:mysql:dbname=$DATABASE"
+6 -53
t/dbix-custom-core-sqlite.t
... ...
@@ -339,8 +339,6 @@ $dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
339 339
 $rows = $dbi->select(table => 'table1')->fetch_hash_all;
340 340
 is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
341 341
 
342
-__END__
343
-
344 342
 test 'delete error';
345 343
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
346 344
 $dbi->execute($CREATE_TABLE->{0});
... ...
@@ -368,20 +366,20 @@ $rows = $dbi->select(table => 'table1')->fetch_hash_all;
368 366
 is_deeply($rows, [{key1 => 1, key2 => 2},
369 367
                   {key1 => 3, key2 => 4}], "$test : table");
370 368
 
371
-$rows = $dbi->select(table => 'table1', columns => ['key1'])->fetch_hash_all;
369
+$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all;
372 370
 is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
373 371
 
374 372
 $rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all;
375 373
 is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
376 374
 
377
-$rows = $dbi->select(table => 'table1', columns => ['key1'], where => {key1 => 3})->fetch_hash_all;
375
+$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all;
378 376
 is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
379 377
 
380 378
 $rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all;
381 379
 is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
382 380
 
383 381
 $dbi->register_filter(decrement => sub { $_[0] - 1 });
384
-$rows = $dbi->select(table => 'table1', {where => {key1 => 2}, filter => {key1 => 'decrement'})
382
+$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
385 383
             ->fetch_hash_all;
386 384
 is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter");
387 385
 
... ...
@@ -389,57 +387,12 @@ $dbi->execute($CREATE_TABLE->{2});
389 387
 $dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
390 388
 $rows = $dbi->select(
391 389
     table => [qw/table1 table2/],
392
-    columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
390
+    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
393 391
     where   => {'table1.key2' => 2},
394 392
     append  => "where table1.key1 = table2.key1"
395 393
 )->fetch_hash_all;
396 394
 is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
397 395
 
398
-test 'Cache';
399
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
400
-DBIx::Custom->query_cache_max(2);
401
-$dbi->execute($CREATE_TABLE->{0});
402
-delete $DBIx::Custom::CLASS_ATTRS->{_query_caches};
403
-delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys};
404
-$tmpls[0] = "insert into table1 {insert key1 key2}";
405
-$queries[0] = $dbi->create_query($tmpls[0]);
406
-is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
407
-is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first");
408
-is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
409
-
410
-$tmpls[1] = "select * from table1";
411
-$queries[1] = $dbi->create_query($tmpls[1]);
412
-is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
413
-is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first");
414
-is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
415
-is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second");
416
-is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
417
-
418
-$tmpls[2] = "select key1, key2 from table1";
419
-$queries[2] = $dbi->create_query($tmpls[2]);
420
-ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
421
-is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
422
-is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key");
423
-is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
424
-is(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key");
425
-is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
426
-
427
-$queries[1] = $dbi->create_query($tmpls[1]);
428
-ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
429
-is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
430
-is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key");
431
-is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
432
-is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key");
433
-is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
434
-
435
-$query = $dbi->create_query($tmpls[0]);
436
-$query->filter('aaa');
437
-$query = $dbi->create_query($tmpls[0]);
438
-ok(!$query->filter, "$test : only cached sql and columns");
439
-$query->filter('bbb');
440
-$query = $dbi->create_query($tmpls[0]);
441
-ok(!$query->filter, "$test : only cached sql and columns");
442
-
443 396
 test 'fetch filter';
444 397
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
445 398
 $dbi->register_filter(
... ...
@@ -457,8 +410,8 @@ is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"
457 410
 test 'filters';
458 411
 $dbi = DBIx::Custom->new;
459 412
 
460
-ok($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
461
-   'あ', "$test : decode_utf8;);
413
+is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
414
+   'あ', "$test : decode_utf8");
462 415
 
463 416
 is($dbi->filters->{encode_utf8}->('あ'),
464 417
    encode_utf8('あ'), "$test : encode_utf8");
+1 -7
t/dbix-custom-core.t
... ...
@@ -26,7 +26,6 @@ $dbi = DBIx::Custom->new(
26 26
     database => 'a',
27 27
     password => 'b',
28 28
     data_source => 'c',
29
-    options => {d => 1, e => 2},
30 29
     filters => {
31 30
         f => 3,
32 31
     },
... ...
@@ -36,7 +35,7 @@ $dbi = DBIx::Custom->new(
36 35
     sql_template => $SQL_TMPL->{0},
37 36
 );
38 37
 is_deeply($dbi,{user => 'a', database => 'a', password => 'b', data_source => 'c', 
39
-                options => {d => 1, e => 2}, filters => {f => 3}, default_bind_filter => 'f',
38
+                filters => {f => 3}, default_bind_filter => 'f',
40 39
                 default_fetch_filter => 'g', result_class => 'g',
41 40
                 sql_template => $SQL_TMPL->{0}}, $test);
42 41
 isa_ok($dbi, 'DBIx::Custom');
... ...
@@ -102,8 +101,3 @@ test 'register_filters';
102 101
 $dbi = DBIx::Custom->new;
103 102
 $dbi->register_filter(a => sub {1});
104 103
 is($dbi->filters->{a}->(), 1, $test);
105
-
106
-test 'Accessor';
107
-$dbi = DBIx::Custom->new;
108
-$dbi->options({opt1 => 1, opt2 => 2});
109
-is_deeply(scalar $dbi->options, {opt1 => 1, opt2 => 2}, "$test : options");
+6 -1
t/dbix-custom-mysql-private.t
... ...
@@ -44,4 +44,9 @@ $dbi = $CLASS->new(user => $USER, password => $PASSWORD,
44 44
 $dbi->connect;
45 45
 is(ref $dbi->dbh, 'DBI::db', $test);
46 46
 
47
-
47
+test 'attributes';
48
+$dbi = DBIx::Custom::MySQL->new;
49
+$dbi->host('a');
50
+is($dbi->host, 'a', "$test: host");
51
+$dbi->port('b');
52
+is($dbi->port, 'b', "$test: port");
+1 -2
t/dbix-custom-sqlite.t
... ...
@@ -35,8 +35,7 @@ my $db_file;
35 35
 my $id;
36 36
 
37 37
 test 'connect_memory';
38
-$dbi = DBIx::Custom::SQLite->new;
39
-$dbi->connect_memory;
38
+$dbi = DBIx::Custom::SQLite->connect(database => ':memory:');
40 39
 $ret_val = $dbi->execute($CREATE_TABLE->{0});
41 40
 ok(defined $ret_val, $test);
42 41
 $dbi->insert(table => 'table1', param => {key1 => 'a', key2 => 2});