DBIx-Custom / lib / DBIx / Custom / QueryBuilder.pm /
Newer Older
363 lines | 8.453kb
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;
10

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

            
cleanup
yuki-kimoto authored on 2010-10-17
15
# Attributes
cleanup
Yuki Kimoto authored on 2011-01-25
16
__PACKAGE__->attr('tags' => sub { {} });
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
17

            
cleanup
yuki-kimoto authored on 2010-10-17
18
sub build_query {
19
    my ($self, $source)  = @_;
20
    
21
    # Parse
22
    my $tree = $self->_parse($source);
23
    
24
    # Build query
25
    my $query = $self->_build_query($tree);
26
    
27
    return $query;
28
}
29

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
30
sub register_tag {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
31
    my $self = shift;
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
32
    
cleanup
Yuki Kimoto authored on 2011-01-25
33
    # Merge tag
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
34
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
35
    $self->tags({%{$self->tags}, %$tags});
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
36
    
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
37
    return $self;
38
}
39

            
cleanup
yuki-kimoto authored on 2010-10-17
40
sub _build_query {
41
    my ($self, $tree) = @_;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
42
    
cleanup
yuki-kimoto authored on 2010-10-17
43
    # SQL
44
    my $sql = '';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
45
    
cleanup
yuki-kimoto authored on 2010-10-17
46
    # All Columns
47
    my $all_columns = [];
48
    
add table tag
Yuki Kimoto authored on 2011-02-09
49
    # Tables
50
    my $tables = [];
51
    
cleanup
yuki-kimoto authored on 2010-10-17
52
    # Build SQL 
53
    foreach my $node (@$tree) {
54
        
55
        # Text
56
        if ($node->{type} eq 'text') { $sql .= $node->{value} }
57
        
58
        # Tag
59
        else {
60
            
61
            # Tag name
62
            my $tag_name = $node->{tag_name};
63
            
64
            # Tag arguments
65
            my $tag_args = $node->{tag_args};
66
            
add table tag
Yuki Kimoto authored on 2011-02-09
67
            # Table
68
            if ($tag_name eq 'table') {
69
                my $table = $tag_args->[0];
70
                push @$tables, $table;
71
                $sql .= $table;
72
                next;
73
            }
74

            
cleanup
Yuki Kimoto authored on 2011-01-25
75
            # Get tag
76
            my $tag = $self->tag_processors->{$tag_name}
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
77
                             || $self->tags->{$tag_name};
cleanup
yuki-kimoto authored on 2010-10-17
78
            
cleanup
Yuki Kimoto authored on 2011-01-25
79
            # Tag is not registered
cleanup
yuki-kimoto authored on 2010-10-17
80
            croak qq{Tag "$tag_name" in "{a }" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-25
81
              unless $tag;
cleanup
yuki-kimoto authored on 2010-10-17
82
            
cleanup
Yuki Kimoto authored on 2011-01-25
83
            # Tag not sub reference
84
            croak qq{Tag "$tag_name" must be sub reference}
85
              unless ref $tag eq 'CODE';
cleanup
yuki-kimoto authored on 2010-10-17
86
            
cleanup
Yuki Kimoto authored on 2011-01-25
87
            # Execute tag
88
            my $r = $tag->(@$tag_args);
cleanup
yuki-kimoto authored on 2010-10-17
89
            
cleanup
Yuki Kimoto authored on 2011-01-25
90
            # Check tag return value
91
            croak qq{Tag "$tag_name" must return [STRING, ARRAY_REFERENCE]}
cleanup
yuki-kimoto authored on 2010-10-17
92
              unless ref $r eq 'ARRAY' && defined $r->[0] && ref $r->[1] eq 'ARRAY';
93
            
94
            # Part of SQL statement and colum names
95
            my ($part, $columns) = @$r;
96
            
97
            # Add columns
98
            push @$all_columns, @$columns;
99
            
100
            # Join part tag to SQL
101
            $sql .= $part;
102
        }
103
    }
104

            
105
    # Check placeholder count
106
    my $placeholder_count = $self->_placeholder_count($sql);
107
    my $column_count      = @$all_columns;
108
    croak qq{Placeholder count in "$sql" must be same as column count $column_count}
109
      unless $placeholder_count eq @$all_columns;
110
    
111
    # Add semicolon
112
    $sql .= ';' unless $sql =~ /;$/;
113
    
114
    # Query
add table tag
Yuki Kimoto authored on 2011-02-09
115
    my $query = DBIx::Custom::Query->new(
116
        sql => $sql,
117
        columns => $all_columns,
118
        tables => $tables
119
    );
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

            
cleanup
Yuki Kimoto authored on 2011-01-25
278
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
279
__PACKAGE__->attr('tag_processors' => sub { {} });
280

            
cleanup
Yuki Kimoto authored on 2011-01-25
281
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
282
sub register_tag_processor {
283
    my $self = shift;
284
    
cleanup
Yuki Kimoto authored on 2011-01-25
285
    # Merge tag
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
286
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
287
    $self->tag_processors({%{$self->tag_processors}, %{$tag_processors}});
288
    
289
    return $self;
290
}
291

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
292
1;
293

            
294
=head1 NAME
295

            
296
DBIx::Custom::QueryBuilder - Query builder
297

            
298
=head1 SYNOPSIS
299
    
300
    my $builder = DBIx::Custom::QueryBuilder->new;
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
301
    my $query = $builder->build_query(
302
        "select from table {= k1} && {<> k2} || {like k3}"
303
    );
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
304

            
305
=head1 ATTRIBUTES
306

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
307
=head2 C<tags>
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
308

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
309
    my $tags = $builder->tags;
cleanup
Yuki Kimoto authored on 2011-01-25
310
    $builder = $builder->tags(\%tags);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
311

            
cleanup
Yuki Kimoto authored on 2011-01-25
312
Tags.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
313

            
314
=head1 METHODS
315

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

            
319
=head2 C<build_query>
320
    
321
    my $query = $builder->build_query($source);
322

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

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

            
330
    'select * from books \\{ something statement \\}'
331

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

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

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

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

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
345
=head2 C<register_tag>
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
346

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
347
    $builder->register_tag(\%tags);
348
    $builder->register_tag(%tags);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349

            
cleanup
Yuki Kimoto authored on 2011-01-25
350
Register tag.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
351

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
354
    $builder->register_tag(
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
355
        '?' => sub {
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
356
            my $column = shift;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
357
            
remove DBIx::Custom::QueryBu...
yuki-kimoto authored on 2010-08-05
358
            return ['?', [$column]];
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
359
        }
360
    );
361

            
cleanup
Yuki Kimoto authored on 2011-01-25
362
See also L<DBIx::Custom::Tag> to know tag.
update document
yuki-kimoto authored on 2010-08-07
363