| ... | ... |
@@ -154,7 +154,7 @@ our $TAG_SYNTAX = <<'EOS'; |
| 154 | 154 |
{update_values} set key1 = ?, key2 = ?, key3 = ?
|
| 155 | 155 |
EOS |
| 156 | 156 |
|
| 157 |
-our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_values/;
|
|
| 157 |
+our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/;
|
|
| 158 | 158 |
sub parse {
|
| 159 | 159 |
my ($self, $template) = @_; |
| 160 | 160 |
$self->template($template); |
| ... | ... |
@@ -221,7 +221,7 @@ sub build_sql {
|
| 221 | 221 |
|
| 222 | 222 |
# Filter Value |
| 223 | 223 |
if ($bind_filter) {
|
| 224 |
- push @bind_values, scalar $bind_filter->($values->{$key});
|
|
| 224 |
+ push @bind_values, scalar $bind_filter->($key, $values->{$key});
|
|
| 225 | 225 |
} |
| 226 | 226 |
else {
|
| 227 | 227 |
push @bind_values, $values->{$key};
|
| ... | ... |
@@ -230,6 +230,52 @@ sub build_sql {
|
| 230 | 230 |
my $place_holder = "$key $tag_name ?"; |
| 231 | 231 |
$sql .= $place_holder; |
| 232 | 232 |
} |
| 233 |
+ elsif ($tag_name eq 'insert_values') {
|
|
| 234 |
+ my $statement_keys = '(';
|
|
| 235 |
+ my $statement_place_holders = '(';
|
|
| 236 |
+ |
|
| 237 |
+ $values = $values->{insert_values};
|
|
| 238 |
+ |
|
| 239 |
+ foreach my $key (sort keys %$values) {
|
|
| 240 |
+ if ($bind_filter) {
|
|
| 241 |
+ push @bind_values, scalar $bind_filter->($key, $values->{$key});
|
|
| 242 |
+ } |
|
| 243 |
+ else {
|
|
| 244 |
+ push @bind_values, $values->{$key};
|
|
| 245 |
+ } |
|
| 246 |
+ |
|
| 247 |
+ $statement_keys .= "$key, "; |
|
| 248 |
+ $statement_place_holders .= "?, "; |
|
| 249 |
+ } |
|
| 250 |
+ |
|
| 251 |
+ $statement_keys =~ s/, $//; |
|
| 252 |
+ $statement_keys .= ')'; |
|
| 253 |
+ |
|
| 254 |
+ $statement_place_holders =~ s/, $//; |
|
| 255 |
+ $statement_place_holders .= ')'; |
|
| 256 |
+ |
|
| 257 |
+ $sql .= "$statement_keys values $statement_place_holders"; |
|
| 258 |
+ } |
|
| 259 |
+ elsif ($tag_name eq 'update_set') {
|
|
| 260 |
+ my $statement = 'set '; |
|
| 261 |
+ |
|
| 262 |
+ $values = $values->{update_set};
|
|
| 263 |
+ |
|
| 264 |
+ foreach my $key (sort keys %$values) {
|
|
| 265 |
+ if ($bind_filter) {
|
|
| 266 |
+ push @bind_values, scalar $bind_filter->($key, $values->{$key});
|
|
| 267 |
+ } |
|
| 268 |
+ else {
|
|
| 269 |
+ push @bind_values, $values->{$key};
|
|
| 270 |
+ } |
|
| 271 |
+ |
|
| 272 |
+ $statement .= "$key = ?, "; |
|
| 273 |
+ } |
|
| 274 |
+ |
|
| 275 |
+ $statement =~ s/, $//; |
|
| 276 |
+ |
|
| 277 |
+ $sql .= $statement; |
|
| 278 |
+ } |
|
| 233 | 279 |
} |
| 234 | 280 |
} |
| 235 | 281 |
$sql .= ';' unless $sql =~ /;$/; |
| ... | ... |
@@ -187,9 +187,69 @@ our ($U, $P, $D) = connect_info(); |
| 187 | 187 |
my $tmpl = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
|
| 188 | 188 |
my $values = {k1 => 'a', k2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'};
|
| 189 | 189 |
|
| 190 |
- my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
| 190 |
+ $dbi->filters(filter => sub {
|
|
| 191 |
+ my ($key, $value) = @_; |
|
| 192 |
+ if ($key eq 'k1' && $value eq 'a') {
|
|
| 193 |
+ return uc $value; |
|
| 194 |
+ } |
|
| 195 |
+ return $value; |
|
| 196 |
+ }); |
|
| 197 |
+ |
|
| 198 |
+ my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 199 |
+ |
|
| 191 | 200 |
is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
| 192 |
- is_deeply(\@bind, ['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
| 201 |
+ is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
| 202 |
+} |
|
| 203 |
+ |
|
| 204 |
+{
|
|
| 205 |
+ # Expand place holer upper case |
|
| 206 |
+ my $dbi = DBI::Custom->new; |
|
| 207 |
+ $dbi->sql_template->upper_case(1); |
|
| 208 |
+ my $tmpl = "select * from table where {like k7}";
|
|
| 209 |
+ my $values = {k7 => 'g'};
|
|
| 210 |
+ |
|
| 211 |
+ my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
| 212 |
+ is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
| 213 |
+ is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
| 214 |
+} |
|
| 215 |
+ |
|
| 216 |
+ |
|
| 217 |
+{
|
|
| 218 |
+ # Insert values |
|
| 219 |
+ my $dbi = DBI::Custom->new; |
|
| 220 |
+ my $tmpl = "insert into table {insert_values}";
|
|
| 221 |
+ my $values = {insert_values => {k1 => 'a', k2 => 'b'}};
|
|
| 222 |
+ |
|
| 223 |
+ $dbi->filters(filter => sub {
|
|
| 224 |
+ my ($key, $value) = @_; |
|
| 225 |
+ if ($key eq 'k1' && $value eq 'a') {
|
|
| 226 |
+ return uc $value; |
|
| 227 |
+ } |
|
| 228 |
+ return $value; |
|
| 229 |
+ }); |
|
| 230 |
+ |
|
| 231 |
+ my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 232 |
+ is($sql, "insert into table (k1, k2) values (?, ?);"); |
|
| 233 |
+ is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 234 |
+} |
|
| 235 |
+ |
|
| 236 |
+{
|
|
| 237 |
+ # Update set |
|
| 238 |
+ my $dbi = DBI::Custom->new; |
|
| 239 |
+ my $tmpl = "update table {update_set}";
|
|
| 240 |
+ my $values = {update_set => {k1 => 'a', k2 => 'b'}};
|
|
| 241 |
+ |
|
| 242 |
+ $dbi->filters(filter => sub {
|
|
| 243 |
+ my ($key, $value) = @_; |
|
| 244 |
+ if ($key eq 'k1' && $value eq 'a') {
|
|
| 245 |
+ return uc $value; |
|
| 246 |
+ } |
|
| 247 |
+ return $value; |
|
| 248 |
+ }); |
|
| 249 |
+ |
|
| 250 |
+ my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
|
|
| 251 |
+ is($sql, "update table set k1 = ?, k2 = ?;"); |
|
| 252 |
+ is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
| 193 | 253 |
} |
| 194 | 254 |
|
| 195 | 255 |
sub connect_info {
|