DBIx-Custom / lib / DBIx / Custom / QueryBuilder.pm /
Newer Older
418 lines | 10.033kb
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1
package DBIx::Custom::QueryBuilder;
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9
use DBIx::Custom::Query;
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
10
use DBIx::Custom::QueryBuilder::TagProcessors;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
11

            
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
12
# Carp trust relationship
13
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
updated document
Yuki Kimoto authored on 2011-01-20
14
push @DBIx::Custom::Where::CARP_NOT, __PACKAGE__;
15

            
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
16

            
cleanup
yuki-kimoto authored on 2010-10-17
17
# Attributes
cleanup (removed undocumente...
yuki-kimoto authored on 2010-11-10
18
__PACKAGE__->attr('tag_processors' => sub {
19
    {
20
        '?'     => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_placeholder_tag,
21
        '='     => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_equal_tag,
22
        '<>'    => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_not_equal_tag,
23
        '>'     => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_greater_than_tag,
24
        '<'     => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_lower_than_tag,
25
        '>='    => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_greater_than_equal_tag,
26
        '<='    => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_lower_than_equal_tag,
27
        'like'  => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_like_tag,
28
        'in'    => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_in_tag,
29
        'insert_param' => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_insert_param_tag,
30
        'update_param' => \&DBIx::Custom::QueryBuilder::TagProcessors::expand_update_param_tag
31
    }
32
});
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
33

            
cleanup
yuki-kimoto authored on 2010-10-17
34
sub build_query {
35
    my ($self, $source)  = @_;
36
    
37
    # Parse
38
    my $tree = $self->_parse($source);
39
    
40
    # Build query
41
    my $query = $self->_build_query($tree);
42
    
43
    return $query;
44
}
45

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
46
sub register_tag_processor {
47
    my $self = shift;
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
48
    
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
49
    # Merge tag processor
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
50
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
51
    $self->tag_processors({%{$self->tag_processors}, %{$tag_processors}});
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
52
    
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
53
    return $self;
54
}
55

            
cleanup
yuki-kimoto authored on 2010-10-17
56
sub _build_query {
57
    my ($self, $tree) = @_;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
58
    
cleanup
yuki-kimoto authored on 2010-10-17
59
    # SQL
60
    my $sql = '';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
61
    
cleanup
yuki-kimoto authored on 2010-10-17
62
    # All Columns
63
    my $all_columns = [];
64
    
65
    # Build SQL 
66
    foreach my $node (@$tree) {
67
        
68
        # Text
69
        if ($node->{type} eq 'text') { $sql .= $node->{value} }
70
        
71
        # Tag
72
        else {
73
            
74
            # Tag name
75
            my $tag_name = $node->{tag_name};
76
            
77
            # Tag arguments
78
            my $tag_args = $node->{tag_args};
79
            
80
            # Get tag processor
81
            my $tag_processor = $self->tag_processors->{$tag_name};
82
            
83
            # Tag processor is not registered
84
            croak qq{Tag "$tag_name" in "{a }" is not registered}
85
              unless $tag_processor;
86
            
87
            # Tag processor not sub reference
88
            croak qq{Tag processor "$tag_name" must be sub reference}
89
              unless ref $tag_processor eq 'CODE';
90
            
91
            # Execute tag processor
92
            my $r = $tag_processor->(@$tag_args);
93
            
94
            # Check tag processor return value
95
            croak qq{Tag processor "$tag_name" must return [STRING, ARRAY_REFERENCE]}
96
              unless ref $r eq 'ARRAY' && defined $r->[0] && ref $r->[1] eq 'ARRAY';
97
            
98
            # Part of SQL statement and colum names
99
            my ($part, $columns) = @$r;
100
            
101
            # Add columns
102
            push @$all_columns, @$columns;
103
            
104
            # Join part tag to SQL
105
            $sql .= $part;
106
        }
107
    }
108

            
109
    # Check placeholder count
110
    my $placeholder_count = $self->_placeholder_count($sql);
111
    my $column_count      = @$all_columns;
112
    croak qq{Placeholder count in "$sql" must be same as column count $column_count}
113
      unless $placeholder_count eq @$all_columns;
114
    
115
    # Add semicolon
116
    $sql .= ';' unless $sql =~ /;$/;
117
    
118
    # Query
119
    my $query = DBIx::Custom::Query->new(sql => $sql, columns => $all_columns);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
120
    
121
    return $query;
122
}
123

            
124
sub _parse {
125
    my ($self, $source) = @_;
126
    
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
127
    # Source
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
128
    $source ||= '';
fixed tests
yuki-kimoto authored on 2010-08-06
129

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
130
    # Tree
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
131
    my @tree;
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
132
    
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
133
    # Value
134
    my $value = '';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
135
    
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
136
    # State
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
137
    my $state = 'text';
138
    
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
139
    # Before charactor
140
    my $before = '';
141

            
142
    # Position
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
143
    my $pos = 0;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
144
    
145
    # Parse
added tests
yuki-kimoto authored on 2010-08-12
146
    my $original = $source;
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
147
    while (defined(my $c = substr($source, $pos, 1))) {
148
        
149
        # Last
150
        last unless length $c;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
151
        
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
152
        # State is text
153
        if ($state eq 'text') {
154
            
155
            # Tag start charactor
156
            if ($c eq '{') {
157
                
158
                # Escaped charactor
159
                if ($before eq "\\") {
160
                    substr($value, -1, 1, '');
161
                    $value .= $c;
162
                }
163
                
164
                # Tag start
165
                else {
166
                    
167
                    # Change state
168
                    $state = 'tag';
169
                    
170
                    # Add text
171
                    push @tree, {type => 'text', value => $value}
172
                      if $value;
173
                    
174
                    # Clear
175
                    $value = '';
176
                }
177
            }
178
            
179
            # Tag end charactor
180
            elsif ($c eq '}') {
181
            
182
                # Escaped charactor
183
                if ($before eq "\\") {
184
                    substr($value, -1, 1, '');
185
                    $value .= $c;
186
                }
187
                
188
                # Unexpected
189
                else {
190
                    croak qq/Parsing error. unexpected "}". / .
added tests
yuki-kimoto authored on 2010-08-12
191
                          qq/pos $pos of "$original"/;
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
192
                }
193
            }
194
            
195
            # Normal charactor
196
            else { $value .= $c }
197
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
198
        
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
199
        # State is tags
added tests
yuki-kimoto authored on 2010-08-12
200
        else {
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
201
            
202
            # Tag start charactor
203
            if ($c eq '{') {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
204
            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
205
                # Escaped charactor
206
                if ($before eq "\\") {
207
                    substr($value, -1, 1, '');
208
                    $value .= $c;
209
                }
210
                
211
                # Unexpected
212
                else {
213
                    croak qq/Parsing error. unexpected "{". / .
added tests
yuki-kimoto authored on 2010-08-12
214
                          qq/pos $pos of "$original"/;
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
215
                }
216
            }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
217
            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
218
            # Tag end charactor
219
            elsif ($c eq '}') {
220
                
221
                # Escaped charactor
222
                if ($before eq "\\") {
223
                    substr($value, -1, 1, '');
224
                    $value .= $c;
225
                }
226
                
227
                # Tag end
228
                else {
229
                
230
                    # Change state
231
                    $state = 'text';
232
                    
233
                    # Add tag
234
                    my ($tag_name, @tag_args) = split /\s+/, $value;
235
                    push @tree, {type => 'tag', tag_name => $tag_name, 
236
                                 tag_args => \@tag_args};
237
                    
238
                    # Clear
239
                    $value = '';
240
                }
241
            }
242
            
243
            # Normal charactor
244
            else { $value .= $c }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
245
        }
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
246
        
247
        # Save before charactor
248
        $before = $c;
249
        
250
        # increment position
251
        $pos++;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
252
    }
253
    
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
254
    # Tag not finished
added tests
yuki-kimoto authored on 2010-08-12
255
    croak qq{Tag not finished. "$original"}
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
256
      if $state eq 'tag';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
257
    
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
258
    # Add rest text
259
    push @tree, {type => 'text', value => $value}
260
      if $value;
261
    
262
    return \@tree;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
263
}
264

            
265
sub _placeholder_count {
266
    my ($self, $expand) = @_;
267
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
268
    # Count
269
    $expand ||= '';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
270
    my $count = 0;
271
    my $pos   = -1;
272
    while (($pos = index($expand, '?', $pos + 1)) != -1) {
273
        $count++;
274
    }
275
    return $count;
276
}
277

            
278
1;
279

            
280
=head1 NAME
281

            
282
DBIx::Custom::QueryBuilder - Query builder
283

            
284
=head1 SYNOPSIS
285
    
286
    my $builder = DBIx::Custom::QueryBuilder->new;
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
287
    my $query = $builder->build_query(
288
        "select from table {= k1} && {<> k2} || {like k3}"
289
    );
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
290

            
291
=head1 ATTRIBUTES
292

            
293
=head2 C<tag_processors>
294

            
295
    my $tag_processors = $builder->tag_processors;
296
    $builder           = $builder->tag_processors(\%tag_processors);
297

            
298
Tag processors.
299

            
300
=head1 METHODS
301

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
302
L<DBIx::Custom::QueryBuilder> inherits all methods from L<Object::Simple>
303
and implements the following new ones.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
304

            
305
=head2 C<build_query>
306
    
307
    my $query = $builder->build_query($source);
308

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
309
Create a new L<DBIx::Custom::Query> object from SQL source.
310
SQL source contains tags, such as {= title}, {like author}.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
311

            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
312
C<{> and C<}> is reserved. If you use these charactors,
313
you must escape them using '\'. Note that '\' is
314
already perl escaped charactor, so you must write '\\'. 
315

            
316
    'select * from books \\{ something statement \\}'
317

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
318
B<Example:>
319

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
320
SQL source
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
321

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
322
      "select * from table where {= title} && {like author} || {<= price}"
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
323

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
324
Query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
325

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
326
    {
327
        sql     => "select * from table where title = ? && author like ? price <= ?;"
328
        columns => ['title', 'author', 'price']
329
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
330

            
331
=head2 C<register_tag_processor>
332

            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
333
    $builder->register_tag_processor(\%tag_processors);
334
    $builder->register_tag_processor(%tag_processors);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
335

            
336
Register tag processor.
337

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
338
B<Example:>
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
339

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
340
    $builder->register_tag_processor(
341
        '?' => sub {
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
342
            my $column = shift;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
343
            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
344
            return ['?', [$column]];
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
345
        }
346
    );
347

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
348
See also L<DBIx::Custom::QueryBuilder::TagProcessors> to know tag processor.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349

            
350
=head1 Tags
351

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
352
The following tags is available.
update document
yuki-kimoto authored on 2010-08-07
353

            
354
=head2 C<?>
355

            
356
Placeholder tag.
357

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
358
    {? NAME}    ->   ?
update document
yuki-kimoto authored on 2010-08-07
359

            
360
=head2 C<=>
361

            
362
Equal tag.
363

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
364
    {= NAME}    ->   NAME = ?
update document
yuki-kimoto authored on 2010-08-07
365

            
366
=head2 C<E<lt>E<gt>>
367

            
368
Not equal tag.
369

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
370
    {<> NAME}   ->   NAME <> ?
update document
yuki-kimoto authored on 2010-08-07
371

            
372
=head2 C<E<lt>>
373

            
374
Lower than tag
375

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
376
    {< NAME}    ->   NAME < ?
update document
yuki-kimoto authored on 2010-08-07
377

            
378
=head2 C<E<gt>>
379

            
380
Greater than tag
381

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
382
    {> NAME}    ->   NAME > ?
update document
yuki-kimoto authored on 2010-08-07
383

            
384
=head2 C<E<gt>=>
385

            
386
Greater than or equal tag
387

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
388
    {>= NAME}   ->   NAME >= ?
update document
yuki-kimoto authored on 2010-08-07
389

            
390
=head2 C<E<lt>=>
391

            
392
Lower than or equal tag
393

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
394
    {<= NAME}   ->   NAME <= ?
update document
yuki-kimoto authored on 2010-08-07
395

            
396
=head2 C<like>
397

            
398
Like tag
399

            
400
    {like NAME}   ->   NAME like ?
401

            
402
=head2 C<in>
403

            
404
In tag.
405

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
406
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
update document
yuki-kimoto authored on 2010-08-07
407

            
updated document
yuki-kimoto authored on 2010-08-09
408
=head2 C<insert_param>
update document
yuki-kimoto authored on 2010-08-07
409

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
410
Insert parameter tag.
update document
yuki-kimoto authored on 2010-08-07
411

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
412
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
update document
yuki-kimoto authored on 2010-08-07
413

            
updated document
yuki-kimoto authored on 2010-08-09
414
=head2 C<update_param>
update document
yuki-kimoto authored on 2010-08-07
415

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
416
Updata parameter tag.
update document
yuki-kimoto authored on 2010-08-07
417

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
418
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?