Showing 6 changed files with 93 additions and 62 deletions
+5
Changes
... ...
@@ -1,5 +1,10 @@
1 1
 0.1637
2 2
   renamed dbi_options to dbi_option. dbi_options is available, but deprecated.
3
+  renamed DBIx::Custom::TagProcessor to DBIx::Custom::Tag, and function names is cleanuped.
4
+  renamed register_tag_processor to register_tag. register_tag_processor is available, but deprecated.
5
+  renamed tag_processors to tags. tag_prosessors is available, but deprecated.
6
+  improved error message
7
+  build all clause if param is undefined.
3 8
 0.1636
4 9
   added tests and cleanup
5 10
 0.1635
+11 -9
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1636';
3
+our $VERSION = '0.1637';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -223,7 +223,7 @@ sub create_query {
223 223
     # Prepare statement handle
224 224
     my $sth;
225 225
     eval { $sth = $self->dbh->prepare($query->{sql})};
226
-    $self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@;
226
+    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
227 227
     
228 228
     # Set statement handle
229 229
     $query->sth($sth);
... ...
@@ -339,7 +339,7 @@ sub execute{
339 339
     my $sth      = $query->sth;
340 340
     my $affected;
341 341
     eval {$affected = $sth->execute(@$binds)};
342
-    $self->_croak($@) if $@;
342
+    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
343 343
     
344 344
     # Return resultset if select statement is executed
345 345
     if ($sth->{NUM_OF_FIELDS}) {
... ...
@@ -465,9 +465,7 @@ sub register_filter {
465 465
     return $invocant;
466 466
 }
467 467
 
468
-sub register_tag_processor {
469
-    return shift->query_builder->register_tag_processor(@_);
470
-}
468
+sub register_tag { shift->query_builder->register_tag(@_) }
471 469
 
472 470
 our %VALID_SELECT_ARGS
473 471
   = map { $_ => 1 } qw/table column where append relation filter query/;
... ...
@@ -730,7 +728,7 @@ sub _croak {
730 728
     }
731 729
 }
732 730
 
733
-# DEPRECATED!
731
+# Following methos are DEPRECATED!
734 732
 __PACKAGE__->attr(
735 733
     dbi_options => sub { {} },
736 734
     filter_check  => 1
... ...
@@ -779,6 +777,10 @@ sub default_fetch_filter {
779 777
     return $self->{default_in_filter};
780 778
 }
781 779
 
780
+sub register_tag_processor {
781
+    return shift->query_builder->register_tag_processor(@_);
782
+}
783
+
782 784
 1;
783 785
 
784 786
 =head1 NAME
... ...
@@ -1180,9 +1182,9 @@ C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1180 1182
 
1181 1183
 =back
1182 1184
 
1183
-=head2 C<register_tag_processor>
1185
+=head2 C<register_tag>
1184 1186
 
1185
-    $dbi->register_tag_processor(
1187
+    $dbi->register_tag(
1186 1188
         limit => sub {
1187 1189
             ...;
1188 1190
         }
+37 -24
lib/DBIx/Custom/QueryBuilder.pm
... ...
@@ -7,27 +7,26 @@ use base 'Object::Simple';
7 7
 
8 8
 use Carp 'croak';
9 9
 use DBIx::Custom::Query;
10
-use DBIx::Custom::TagProcessor;
10
+use DBIx::Custom::Tag;
11 11
 
12 12
 # Carp trust relationship
13 13
 push @DBIx::Custom::CARP_NOT, __PACKAGE__;
14 14
 push @DBIx::Custom::Where::CARP_NOT, __PACKAGE__;
15 15
 
16
-
17 16
 # Attributes
18
-__PACKAGE__->attr('tag_processors' => sub {
17
+__PACKAGE__->attr('tags' => sub {
19 18
     {
20
-        '?'     => \&DBIx::Custom::TagProcessor::expand_placeholder_tag,
21
-        '='     => \&DBIx::Custom::TagProcessor::expand_equal_tag,
22
-        '<>'    => \&DBIx::Custom::TagProcessor::expand_not_equal_tag,
23
-        '>'     => \&DBIx::Custom::TagProcessor::expand_greater_than_tag,
24
-        '<'     => \&DBIx::Custom::TagProcessor::expand_lower_than_tag,
25
-        '>='    => \&DBIx::Custom::TagProcessor::expand_greater_than_equal_tag,
26
-        '<='    => \&DBIx::Custom::TagProcessor::expand_lower_than_equal_tag,
27
-        'like'  => \&DBIx::Custom::TagProcessor::expand_like_tag,
28
-        'in'    => \&DBIx::Custom::TagProcessor::expand_in_tag,
29
-        'insert_param' => \&DBIx::Custom::TagProcessor::expand_insert_param_tag,
30
-        'update_param' => \&DBIx::Custom::TagProcessor::expand_update_param_tag
19
+        '?'     => \&DBIx::Custom::Tag::placeholder,
20
+        '='     => \&DBIx::Custom::Tag::equal,
21
+        '<>'    => \&DBIx::Custom::Tag::not_equal,
22
+        '>'     => \&DBIx::Custom::Tag::greater_than,
23
+        '<'     => \&DBIx::Custom::Tag::lower_than,
24
+        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
25
+        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
26
+        'like'  => \&DBIx::Custom::Tag::like,
27
+        'in'    => \&DBIx::Custom::Tag::in,
28
+        'insert_param' => \&DBIx::Custom::Tag::insert_param,
29
+        'update_param' => \&DBIx::Custom::Tag::update_param
31 30
     }
32 31
 });
33 32
 
... ...
@@ -43,12 +42,12 @@ sub build_query {
43 42
     return $query;
44 43
 }
45 44
 
46
-sub register_tag_processor {
45
+sub register_tag {
47 46
     my $self = shift;
48 47
     
49 48
     # Merge tag processor
50
-    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
51
-    $self->tag_processors({%{$self->tag_processors}, %{$tag_processors}});
49
+    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
50
+    $self->tags({%{$self->tags}, %$tags});
52 51
     
53 52
     return $self;
54 53
 }
... ...
@@ -78,7 +77,8 @@ sub _build_query {
78 77
             my $tag_args = $node->{tag_args};
79 78
             
80 79
             # Get tag processor
81
-            my $tag_processor = $self->tag_processors->{$tag_name};
80
+            my $tag_processor = $self->tag_processors->{$tag_name}
81
+                             || $self->tags->{$tag_name};
82 82
             
83 83
             # Tag processor is not registered
84 84
             croak qq{Tag "$tag_name" in "{a }" is not registered}
... ...
@@ -275,6 +275,19 @@ sub _placeholder_count {
275 275
     return $count;
276 276
 }
277 277
 
278
+# Follwoing methods are DEPRECATED!
279
+__PACKAGE__->attr('tag_processors' => sub { {} });
280
+
281
+sub register_tag_processor {
282
+    my $self = shift;
283
+    
284
+    # Merge tag processor
285
+    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
286
+    $self->tag_processors({%{$self->tag_processors}, %{$tag_processors}});
287
+    
288
+    return $self;
289
+}
290
+
278 291
 1;
279 292
 
280 293
 =head1 NAME
... ...
@@ -290,10 +303,10 @@ DBIx::Custom::QueryBuilder - Query builder
290 303
 
291 304
 =head1 ATTRIBUTES
292 305
 
293
-=head2 C<tag_processors>
306
+=head2 C<tags>
294 307
 
295
-    my $tag_processors = $builder->tag_processors;
296
-    $builder           = $builder->tag_processors(\%tag_processors);
308
+    my $tags = $builder->tags;
309
+    $builder           = $builder->tags(\%tags);
297 310
 
298 311
 Tag processors.
299 312
 
... ...
@@ -328,10 +341,10 @@ Query
328 341
         columns => ['title', 'author', 'price']
329 342
     }
330 343
 
331
-=head2 C<register_tag_processor>
344
+=head2 C<register_tag>
332 345
 
333
-    $builder->register_tag_processor(\%tag_processors);
334
-    $builder->register_tag_processor(%tag_processors);
346
+    $builder->register_tag(\%tags);
347
+    $builder->register_tag(%tags);
335 348
 
336 349
 Register tag processor.
337 350
 
+14 -15
lib/DBIx/Custom/TagProcessor.pm → lib/DBIx/Custom/Tag.pm
... ...
@@ -1,4 +1,4 @@
1
-package DBIx::Custom::TagProcessor;
1
+package DBIx::Custom::Tag;
2 2
 
3 3
 use strict;
4 4
 use warnings;
... ...
@@ -8,11 +8,11 @@ use Carp 'croak';
8 8
 # Carp trust relationship
9 9
 push @DBIx::Custom::QueryBuilder::CARP_NOT, __PACKAGE__;
10 10
 
11
-sub expand_equal_tag              { _expand_basic_tag('=',    @_) }
12
-sub expand_greater_than_equal_tag { _expand_basic_tag('>=',   @_) }
13
-sub expand_greater_than_tag       { _expand_basic_tag('>',    @_) }
11
+sub equal              { _basic('=',  @_) }
12
+sub greater_than_equal { _basic('>=', @_) }
13
+sub greater_than       { _basic('>',  @_) }
14 14
 
15
-sub expand_in_tag {
15
+sub in {
16 16
     my ($column, $count) = @_;
17 17
     
18 18
     # Check arguments
... ...
@@ -34,7 +34,7 @@ sub expand_in_tag {
34 34
     return [$s, $columns];
35 35
 }
36 36
 
37
-sub expand_insert_param_tag {
37
+sub insert_param {
38 38
     my @columns = @_;
39 39
     
40 40
     # Insert parameters
... ...
@@ -50,12 +50,12 @@ sub expand_insert_param_tag {
50 50
     return [$s, \@columns];
51 51
 }
52 52
 
53
-sub expand_like_tag               { _expand_basic_tag('like', @_) }
54
-sub expand_lower_than_equal_tag   { _expand_basic_tag('<=',   @_) }
55
-sub expand_lower_than_tag         { _expand_basic_tag('<',    @_) }
56
-sub expand_not_equal_tag          { _expand_basic_tag('<>',   @_) }
53
+sub like               { _basic('like', @_) }
54
+sub lower_than_equal   { _basic('<=',   @_) }
55
+sub lower_than         { _basic('<',    @_) }
56
+sub not_equal          { _basic('<>',   @_) }
57 57
 
58
-sub expand_placeholder_tag {
58
+sub placeholder {
59 59
     my $column = shift;
60 60
     
61 61
     # Check arguments
... ...
@@ -65,7 +65,7 @@ sub expand_placeholder_tag {
65 65
     return ['?', [$column]];
66 66
 }
67 67
 
68
-sub expand_update_param_tag {
68
+sub update_param {
69 69
     my @columns = @_;
70 70
     
71 71
     # Update paramters
... ...
@@ -76,7 +76,7 @@ sub expand_update_param_tag {
76 76
     return [$s, \@columns];
77 77
 }
78 78
 
79
-sub _expand_basic_tag {
79
+sub _basic {
80 80
     my ($name, $column) = @_;
81 81
     
82 82
     # Check arguments
... ...
@@ -90,5 +90,4 @@ sub _expand_basic_tag {
90 90
 
91 91
 =head1 NAME
92 92
 
93
-DBIx::Custom::TagProcessor - Tag processor
94
-
93
+DBIx::Custom::Tag - Tag processor
+15 -13
lib/DBIx/Custom/Where.pm
... ...
@@ -14,9 +14,8 @@ use Carp 'croak';
14 14
 push @DBIx::Custom::CARP_NOT, __PACKAGE__;
15 15
 
16 16
 __PACKAGE__->attr(
17
-  'query_builder',
17
+  [qw/param query_builder/],
18 18
   clause => sub { [] },
19
-  param => sub { {} }
20 19
 );
21 20
 
22 21
 sub to_string {
... ...
@@ -65,11 +64,8 @@ sub _parse {
65 64
             pop @$where;
66 65
             pop @$where;
67 66
         }
68
-        
69 67
         # End
70
-        else {
71
-            push @$where, ')';
72
-        }
68
+        else { push @$where, ')' }
73 69
     }
74 70
     
75 71
     # String
... ...
@@ -87,15 +83,21 @@ sub _parse {
87 83
         # Push
88 84
         my $param = $self->param;
89 85
         my $pushed;
90
-        if (exists $param->{$column}) {
91
-            if (ref $param->{$column} eq 'ARRAY') {
92
-                $pushed = 1 if exists $param->{$column}->[$count - 1];
93
-            }
94
-            elsif ($count == 1) {
95
-                $pushed = 1;
86
+        if (defined $param) {
87
+            if (exists $param->{$column}) {
88
+                if (ref $param->{$column} eq 'ARRAY') {
89
+                    $pushed = 1 if exists $param->{$column}->[$count - 1];
90
+                }
91
+                elsif ($count == 1) {
92
+                    $pushed = 1;
93
+                }
96 94
             }
95
+            push @$where, $clause if $pushed;
96
+        }
97
+        else {
98
+            push @$where, $clause;
99
+            $pushed = 1;
97 100
         }
98
-        push @$where, $clause if $pushed;
99 101
         
100 102
         return $pushed;
101 103
     }
+11 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -718,7 +718,7 @@ $dbi->execute($CREATE_TABLE->{0});
718 718
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
719 719
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
720 720
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
721
-$dbi->query_builder->register_tag_processor(
721
+$dbi->register_tag(
722 722
     limit => sub {
723 723
         my ($count, $offset) = @_;
724 724
         
... ...
@@ -825,6 +825,9 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
825 825
 $dbi->execute($CREATE_TABLE->{0});
826 826
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
827 827
 $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
828
+$where = $dbi->where->clause(['and', '{= key1}', '{= key2}']);
829
+is("$where", "where ( {= key1} and {= key2} )", 'no param');
830
+
828 831
 $where = $dbi->where
829 832
              ->clause(['and', '{= key1}', '{= key2}'])
830 833
              ->param({key1 => 1});
... ...
@@ -944,6 +947,13 @@ $dbi->register_tag_processor(
944 947
 );
945 948
 is($dbi->query_builder->tag_processors->{a}->(), 1);
946 949
 
950
+test 'register_tag';
951
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
952
+$dbi->register_tag(
953
+    b => sub { 2 }
954
+);
955
+is($dbi->query_builder->tags->{b}->(), 2);
956
+
947 957
 test 'table not specify exception';
948 958
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
949 959
 eval {$dbi->insert};