DBIx-Custom / lib / DBIx / Custom / QueryBuilder.pm /
Newer Older
416 lines | 9.982kb
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__;
14

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

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

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

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

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

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

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

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

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

            
276
1;
277

            
278
=head1 NAME
279

            
280
DBIx::Custom::QueryBuilder - Query builder
281

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

            
289
=head1 ATTRIBUTES
290

            
291
=head2 C<tag_processors>
292

            
293
    my $tag_processors = $builder->tag_processors;
294
    $builder           = $builder->tag_processors(\%tag_processors);
295

            
296
Tag processors.
297

            
298
=head1 METHODS
299

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

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

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

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

            
314
    'select * from books \\{ something statement \\}'
315

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

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

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

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

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

            
329
=head2 C<register_tag_processor>
330

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

            
334
Register tag processor.
335

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

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

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

            
348
=head1 Tags
349

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

            
352
=head2 C<?>
353

            
354
Placeholder tag.
355

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

            
358
=head2 C<=>
359

            
360
Equal tag.
361

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

            
364
=head2 C<E<lt>E<gt>>
365

            
366
Not equal tag.
367

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

            
370
=head2 C<E<lt>>
371

            
372
Lower than tag
373

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

            
376
=head2 C<E<gt>>
377

            
378
Greater than tag
379

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

            
382
=head2 C<E<gt>=>
383

            
384
Greater than or equal tag
385

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

            
388
=head2 C<E<lt>=>
389

            
390
Lower than or equal tag
391

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

            
394
=head2 C<like>
395

            
396
Like tag
397

            
398
    {like NAME}   ->   NAME like ?
399

            
400
=head2 C<in>
401

            
402
In tag.
403

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

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

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

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

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

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

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