renamed default_query_filter...
|
1 |
package DBIx::Custom::QueryBuilder; |
2 | ||
updatedd pod
|
3 |
use Object::Simple -base; |
renamed default_query_filter...
|
4 | |
5 |
use Carp 'croak'; |
|
6 |
use DBIx::Custom::Query; |
|
cleanup
|
7 |
use DBIx::Custom::Util '_subname'; |
renamed default_query_filter...
|
8 | |
fixed Carp trast relationshi...
|
9 |
# Carp trust relationship |
10 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
updated document
|
11 |
push @DBIx::Custom::Where::CARP_NOT, __PACKAGE__; |
12 | ||
- update_param_tag is DEPREC...
|
13 |
# Parameter regex |
14 |
our $PARAM_RE = qr/(^|[^\.\w]):([\.\w]+)([^\.\w]|$)/sm; |
|
renamed default_query_filter...
|
15 | |
cleanup
|
16 |
sub build_query { |
- update_param_tag is DEPREC...
|
17 |
my ($self, $source) = @_; |
cleanup
|
18 |
|
- update_param_tag is DEPREC...
|
19 |
my $query; |
20 |
|
|
21 |
# Parse tag. tag is DEPRECATED! |
|
22 |
if ($source =~ /\{/ && $source =~ /\}/) { |
|
23 |
$query = $self->_parse_tag($source); |
|
24 |
my $tag_count = delete $query->{tag_count}; |
|
fixed DEPRECATED message bug
|
25 |
warn qq/Tag system such as {? name} is DEPRECATED! / . |
26 |
qq/use parameter system such as :name instead/ |
|
- update_param_tag is DEPREC...
|
27 |
if $tag_count; |
28 |
my $query2 = $self->_parse_parameter($query->sql); |
|
29 |
$query->sql($query2->sql); |
|
30 |
for (my $i =0; $i < @{$query->columns}; $i++) { |
|
31 |
my $column = $query->columns->[$i]; |
|
32 |
if ($column eq 'RESERVED_PARAMETER') { |
|
33 |
my $column2 = shift @{$query2->columns}; |
|
34 |
croak ":name syntax is wrong" |
|
35 |
unless defined $column2; |
|
36 |
$query->columns->[$i] = $column2; |
|
37 |
} |
|
38 |
} |
|
39 |
} |
|
cleanup
|
40 |
|
- update_param_tag is DEPREC...
|
41 |
# Parse parameter |
42 |
else { |
|
43 |
$query = $self->_parse_parameter($source); |
|
44 |
} |
|
cleanup
|
45 |
|
- update_param_tag is DEPREC...
|
46 |
my $sql = $query->sql; |
47 |
$sql .= ';' unless $source =~ /;$/; |
|
48 |
$query->sql($sql); |
|
49 | ||
50 |
# Check placeholder count |
|
51 |
croak qq{Placeholder count in "$sql" must be same as column count} |
|
52 |
. _subname |
|
53 |
unless $self->_placeholder_count($sql) eq @{$query->columns}; |
|
54 |
|
|
cleanup
|
55 |
return $query; |
56 |
} |
|
57 | ||
- update_param_tag is DEPREC...
|
58 |
sub _placeholder_count { |
59 |
my ($self, $sql) = @_; |
|
changed argument of tag proc...
|
60 |
|
- update_param_tag is DEPREC...
|
61 |
# Count |
62 |
$sql ||= ''; |
|
63 |
my $count = 0; |
|
64 |
my $pos = -1; |
|
65 |
while (($pos = index($sql, '?', $pos + 1)) != -1) { |
|
66 |
$count++; |
|
67 |
} |
|
68 |
return $count; |
|
renamed default_query_filter...
|
69 |
} |
70 | ||
- update_param_tag is DEPREC...
|
71 |
sub _parse_parameter { |
72 |
my ($self, $source) = @_; |
|
add table tag
|
73 |
|
- update_param_tag is DEPREC...
|
74 |
# Get and replace parameters |
75 |
my $sql = $source || ''; |
|
76 |
my $columns = []; |
|
77 |
while ($source =~ /$PARAM_RE/g) { |
|
78 |
push @$columns, $2; |
|
cleanup
|
79 |
} |
- update_param_tag is DEPREC...
|
80 |
$sql =~ s/$PARAM_RE/$1?$3/g; |
cleanup
|
81 | |
- update_param_tag is DEPREC...
|
82 |
# Create query |
add table tag
|
83 |
my $query = DBIx::Custom::Query->new( |
84 |
sql => $sql, |
|
- update_param_tag is DEPREC...
|
85 |
columns => $columns |
add table tag
|
86 |
); |
renamed default_query_filter...
|
87 |
|
88 |
return $query; |
|
89 |
} |
|
90 | ||
- update_param_tag is DEPREC...
|
91 |
# DEPRECATED! |
cleanup
|
92 |
has tags => sub { {} }; |
- update_param_tag is DEPREC...
|
93 | |
94 |
# DEPRECATED! |
|
95 |
sub register_tag { |
|
96 |
my $self = shift; |
|
renamed default_query_filter...
|
97 |
|
added warnings
|
98 |
warn "register_tag is DEPRECATED!"; |
99 |
|
|
- update_param_tag is DEPREC...
|
100 |
# Merge tag |
101 |
my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
102 |
$self->tags({%{$self->tags}, %$tags}); |
|
103 |
|
|
104 |
return $self; |
|
105 |
} |
|
106 | ||
107 |
# DEPRECATED! |
|
108 |
sub _parse_tag { |
|
109 |
my ($self, $source) = @_; |
|
remove DBIx::Custom::QueryBu...
|
110 |
# Source |
renamed default_query_filter...
|
111 |
$source ||= ''; |
remove DBIx::Custom::QueryBu...
|
112 |
# Tree |
removed DBIx::Custom::Query ...
|
113 |
my @tree; |
114 |
# Value |
|
115 |
my $value = ''; |
|
116 |
# State |
|
renamed default_query_filter...
|
117 |
my $state = 'text'; |
removed DBIx::Custom::Query ...
|
118 |
# Before charactor |
119 |
my $before = ''; |
|
120 |
# Position |
|
fixed DBIx::Custom::QueryBui...
|
121 |
my $pos = 0; |
renamed default_query_filter...
|
122 |
# Parse |
added tests
|
123 |
my $original = $source; |
- update_param_tag is DEPREC...
|
124 |
my $tag_count = 0; |
fixed DBIx::Custom::QueryBui...
|
125 |
while (defined(my $c = substr($source, $pos, 1))) { |
126 |
# Last |
|
127 |
last unless length $c; |
|
- update_param_tag is DEPREC...
|
128 |
# Parameter |
129 |
if ($c eq ':' && (substr($source, $pos + 1, 1) || '') =~ /\w/) { |
|
130 |
push @tree, {type => 'param'};; |
|
131 |
} |
|
removed DBIx::Custom::Query ...
|
132 |
# State is text |
133 |
if ($state eq 'text') { |
|
134 |
# Tag start charactor |
|
135 |
if ($c eq '{') { |
|
136 |
# Escaped charactor |
|
137 |
if ($before eq "\\") { |
|
138 |
substr($value, -1, 1, ''); |
|
139 |
$value .= $c; |
|
140 |
} |
|
141 |
# Tag start |
|
142 |
else { |
|
143 |
# Change state |
|
144 |
$state = 'tag'; |
|
145 |
# Add text |
|
146 |
push @tree, {type => 'text', value => $value} |
|
147 |
if $value; |
|
148 |
# Clear |
|
149 |
$value = ''; |
|
150 |
} |
|
151 |
} |
|
152 |
# Tag end charactor |
|
153 |
elsif ($c eq '}') { |
|
154 |
# Escaped charactor |
|
155 |
if ($before eq "\\") { |
|
156 |
substr($value, -1, 1, ''); |
|
157 |
$value .= $c; |
|
158 |
} |
|
159 |
# Unexpected |
|
160 |
else { |
|
improved error messages
|
161 |
croak qq{Parsing error. unexpected "\}". } |
cleanup
|
162 |
. qq{pos $pos of "$original" } . _subname |
removed DBIx::Custom::Query ...
|
163 |
} |
164 |
} |
|
165 |
# Normal charactor |
|
166 |
else { $value .= $c } |
|
167 |
} |
|
168 |
# State is tags |
|
added tests
|
169 |
else { |
removed DBIx::Custom::Query ...
|
170 |
# Tag start charactor |
171 |
if ($c eq '{') { |
|
172 |
# Escaped charactor |
|
173 |
if ($before eq "\\") { |
|
174 |
substr($value, -1, 1, ''); |
|
175 |
$value .= $c; |
|
176 |
} |
|
177 |
# Unexpected |
|
178 |
else { |
|
improved error messages
|
179 |
croak qq{Parsing error. unexpected "\{". } |
cleanup
|
180 |
. qq{pos $pos of "$original" } . _subname |
removed DBIx::Custom::Query ...
|
181 |
} |
182 |
} |
|
183 |
# Tag end charactor |
|
184 |
elsif ($c eq '}') { |
|
185 |
# Escaped charactor |
|
186 |
if ($before eq "\\") { |
|
187 |
substr($value, -1, 1, ''); |
|
188 |
$value .= $c; |
|
189 |
} |
|
190 |
# Tag end |
|
191 |
else { |
|
192 |
# Change state |
|
193 |
$state = 'text'; |
|
194 |
# Add tag |
|
195 |
my ($tag_name, @tag_args) = split /\s+/, $value; |
|
196 |
push @tree, {type => 'tag', tag_name => $tag_name, |
|
197 |
tag_args => \@tag_args}; |
|
198 |
# Clear |
|
199 |
$value = ''; |
|
- update_param_tag is DEPREC...
|
200 |
# Countup |
201 |
$tag_count++; |
|
removed DBIx::Custom::Query ...
|
202 |
} |
203 |
} |
|
204 |
# Normal charactor |
|
205 |
else { $value .= $c } |
|
renamed default_query_filter...
|
206 |
} |
removed DBIx::Custom::Query ...
|
207 |
# Save before charactor |
208 |
$before = $c; |
|
209 |
# increment position |
|
210 |
$pos++; |
|
renamed default_query_filter...
|
211 |
} |
removed DBIx::Custom::Query ...
|
212 |
# Tag not finished |
cleanup
|
213 |
croak qq{Tag not finished. "$original" } . _subname |
removed DBIx::Custom::Query ...
|
214 |
if $state eq 'tag'; |
- update_param_tag is DEPREC...
|
215 |
# Not contains tag |
216 |
return DBIx::Custom::Query->new(sql => $source, tag_count => $tag_count) |
|
217 |
if $tag_count == 0; |
|
removed DBIx::Custom::Query ...
|
218 |
# Add rest text |
219 |
push @tree, {type => 'text', value => $value} |
|
220 |
if $value; |
|
- update_param_tag is DEPREC...
|
221 |
# SQL |
222 |
my $sql = ''; |
|
223 |
# All Columns |
|
224 |
my $all_columns = []; |
|
225 |
# Tables |
|
226 |
my $tables = []; |
|
227 |
# Build SQL |
|
228 |
foreach my $node (@tree) { |
|
229 |
# Text |
|
230 |
if ($node->{type} eq 'text') { $sql .= $node->{value} } |
|
231 |
# Parameter |
|
232 |
elsif ($node->{type} eq 'param') { |
|
233 |
push @$all_columns, 'RESERVED_PARAMETER'; |
|
234 |
} |
|
235 |
# Tag |
|
236 |
else { |
|
237 |
# Tag name |
|
238 |
my $tag_name = $node->{tag_name}; |
|
239 |
# Tag arguments |
|
240 |
my $tag_args = $node->{tag_args}; |
|
241 |
# Table |
|
242 |
if ($tag_name eq 'table') { |
|
243 |
my $table = $tag_args->[0]; |
|
244 |
push @$tables, $table; |
|
245 |
$sql .= $table; |
|
246 |
next; |
|
247 |
} |
|
248 |
# Get tag |
|
249 |
my $tag = $self->tag_processors->{$tag_name} |
|
250 |
|| $self->tags->{$tag_name}; |
|
251 |
# Tag is not registered |
|
252 |
croak qq{Tag "$tag_name" is not registered } . _subname |
|
253 |
unless $tag; |
|
254 |
# Tag not sub reference |
|
255 |
croak qq{Tag "$tag_name" must be sub reference } . _subname |
|
256 |
unless ref $tag eq 'CODE'; |
|
257 |
# Execute tag |
|
258 |
my $r = $tag->(@$tag_args); |
|
259 |
# Check tag return value |
|
260 |
croak qq{Tag "$tag_name" must return [STRING, ARRAY_REFERENCE] } |
|
261 |
. _subname |
|
262 |
unless ref $r eq 'ARRAY' && defined $r->[0] && ref $r->[1] eq 'ARRAY'; |
|
263 |
# Part of SQL statement and colum names |
|
264 |
my ($part, $columns) = @$r; |
|
265 |
# Add columns |
|
266 |
push @$all_columns, @$columns; |
|
267 |
# Join part tag to SQL |
|
268 |
$sql .= $part; |
|
269 |
} |
|
renamed default_query_filter...
|
270 |
} |
- update_param_tag is DEPREC...
|
271 |
# Query |
272 |
my $query = DBIx::Custom::Query->new( |
|
273 |
sql => $sql, |
|
274 |
columns => $all_columns, |
|
275 |
tables => $tables, |
|
276 |
tag_count => $tag_count |
|
277 |
); |
|
278 |
return $query; |
|
renamed default_query_filter...
|
279 |
} |
280 | ||
cleanup
|
281 |
# DEPRECATED! |
cleanup
|
282 |
has tag_processors => sub { {} }; |
renamed DBIx::Custom::TagPro...
|
283 | |
cleanup
|
284 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
285 |
sub register_tag_processor { |
286 |
my $self = shift; |
|
- update_param_tag is DEPREC...
|
287 |
warn "register_tag_processor is DEPRECATED! use register_tag instead"; |
cleanup
|
288 |
# Merge tag |
renamed DBIx::Custom::TagPro...
|
289 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
290 |
$self->tag_processors({%{$self->tag_processors}, %{$tag_processors}}); |
|
291 |
return $self; |
|
292 |
} |
|
293 | ||
renamed default_query_filter...
|
294 |
1; |
295 | ||
296 |
=head1 NAME |
|
297 | ||
298 |
DBIx::Custom::QueryBuilder - Query builder |
|
299 | ||
300 |
=head1 SYNOPSIS |
|
301 |
|
|
302 |
my $builder = DBIx::Custom::QueryBuilder->new; |
|
remove DBIx::Custom::QueryBu...
|
303 |
my $query = $builder->build_query( |
- update_param_tag is DEPREC...
|
304 |
"select from table title = :title and author = :author" |
remove DBIx::Custom::QueryBu...
|
305 |
); |
renamed default_query_filter...
|
306 | |
307 |
=head1 METHODS |
|
308 | ||
remove DBIx::Custom::QueryBu...
|
309 |
L<DBIx::Custom::QueryBuilder> inherits all methods from L<Object::Simple> |
310 |
and implements the following new ones. |
|
renamed default_query_filter...
|
311 | |
312 |
=head2 C<build_query> |
|
313 |
|
|
314 |
my $query = $builder->build_query($source); |
|
315 | ||
remove DBIx::Custom::QueryBu...
|
316 |
Create a new L<DBIx::Custom::Query> object from SQL source. |
update document
|
317 | |
- update_param_tag is DEPREC...
|
318 |
=cut |