blib/lib/DBIx/Struct.pm untested

filter: Statementstmt 0.0% 0 / 1163 Branchbran 0.0% 0 / 464 Conditioncond 0.0% 0 / 217 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 113 Podpod 0.0% 0 / 75 totaltotal 0.0% 0 / 2032 SCAR 108.8
CC
230
Cov
0%
_build_complex_query
3422.0
setup_row
1482.0
execute
870.0
CRAP 53130.0
1 package DBIx::Struct::JSON::Array;
2 use strict;
3 use warnings;
4  
5 sub TIEARRAY {
6 bless [$_[1], $_[2], $_[3]], $_[0];
7 }
8  
9 sub FETCHSIZE {
10 scalar @{$_[0][0]};
11 }
12  
13 sub STORESIZE {
14 $_[0][1]{$_[0][2]} = undef;
15 $#{$_[0][0]} = $_[1] - 1;
16 }
17  
18 sub STORE {
19 $_[0][1]{$_[0][2]} = undef;
20 $_[0][0][$_[1]] = $_[2];
21 }
22 sub FETCH {$_[0][0][$_[1]]}
23  
24 sub CLEAR {
25 $_[0][1]{$_[0][2]} = undef;
26 @{$_[0][0]} = ();
27 }
28  
29 sub POP {
30 $_[0][1]{$_[0][2]} = undef;
31 pop(@{$_[0][0]});
32 }
33  
34 sub PUSH {
35 my $o = shift;
36 $o->[1]{$o->[2]} = undef;
37 push(@{$o->[0]}, @_);
38 }
39  
40 sub SHIFT {
41 $_[0][1]{$_[0][2]} = undef;
42 shift(@{$_[0][0]});
43 }
44  
45 sub UNSHIFT {
46 my $o = shift;
47 $o->[1]{$o->[2]} = undef;
48 unshift(@$o, @_);
49 }
50 sub EXISTS {exists $_[0][0]->[$_[1]]}
51 sub DELETE {delete $_[0][0]->[$_[1]]}
52  
53 sub SPLICE {
54 my $ob = shift;
55 my $sz = $ob->FETCHSIZE;
56 my $off = @_ ? shift : 0;
57 $off += $sz if $off < 0;
58 my $len = @_ ? shift : $sz - $off;
59 $ob->[1]{$ob->[2]} = undef;
60 return splice(@{$ob->[0]}, $off, $len, @_);
61 }
62  
63 package DBIx::Struct::JSON::Hash;
64 use strict;
65 use warnings;
66  
67 sub TIEHASH {
68 bless [$_[1], $_[2], $_[3]], $_[0];
69 }
70  
71 sub STORE {
72 $_[0][1]{$_[0][2]} = undef;
73 $_[0][0]->{$_[1]} = $_[2];
74 }
75  
76 sub FETCH {
77 $_[0][0]->{$_[1]};
78 }
79  
80 sub FIRSTKEY {
81 my $a = scalar keys %{$_[0][0]};
82 each %{$_[0][0]};
83 }
84  
85 sub NEXTKEY {
86 each %{$_[0][0]};
87 }
88  
89 sub EXISTS {
90 exists $_[0][0]->{$_[1]};
91 }
92  
93 sub DELETE {
94 $_[0][1]{$_[0][2]} = undef;
95 delete $_[0][0]->{$_[1]};
96 }
97  
98 sub CLEAR {
99 $_[0][1]{$_[0][2]} = undef;
100 %{$_[0][0]} = ();
101 }
102  
103 sub SCALAR {
104 scalar %{$_[0][0]};
105 }
106  
107 package DBIx::Struct::JSON;
108  
109 use strict;
110 use warnings;
111 use JSON;
112  
113 sub factory {
114 my ($class, $value_ref, $update_hash, $hash_key) = @_;
115 my $self;
116 if (not ref $$value_ref) {
117 my $jv = from_json($$value_ref) if $$value_ref;
118 $$value_ref = $jv if $jv;
119 }
120 if (not defined $$value_ref) {
121 $self = [undef, undef];
122 } elsif ('HASH' eq ref $$value_ref) {
123 my %h;
124 tie %h, 'DBIx::Struct::JSON::Hash', $$value_ref, $update_hash, $hash_key;
125 $self = [\%h, $$value_ref];
126 } elsif ('ARRAY' eq ref $$value_ref) {
127 my @a;
128 tie @a, 'DBIx::Struct::JSON::Array', $$value_ref, $update_hash, $hash_key;
129 $self = [\@a, $$value_ref];
130 }
131 $$value_ref = bless $self, $class;
132 }
133  
134 sub revert {
135 $_[0] = defined($_[0][1]) ? to_json $_[0][1] : undef;
136 }
137  
138 sub data {
139 $_[0][1];
140 }
141  
142 sub accessor {
143 $_[0][0];
144 }
145  
146 package DBIx::Struct::Connector;
147 use strict;
148 use warnings;
149 use base 'DBIx::Connector';
150  
151 our $db_reconnect_timeout = 30;
152  
153 sub _connect {
154 my ($self, @args) = @_;
155 for my $try (1 .. $db_reconnect_timeout) {
156 my $dbh = eval {$self->SUPER::_connect(@args)};
157 return $dbh if $dbh;
158 sleep 1 if $try != $db_reconnect_timeout;
159 }
160 die $@ if $@;
161 die "DB connect error";
162 }
163  
164 package DBIx::Struct::Error::String;
165 use strict;
166 use warnings;
167 use Carp;
168  
169 sub error_message (+%) {
170 my $msg = $_[0];
171 delete $msg->{result};
172 my $message = delete $msg->{message};
173 croak join "; ", $message, map {"$_: $msg->{$_}"} keys %$msg;
174 }
175  
176 package DBIx::Struct::Error::Hash;
177 use strict;
178 use warnings;
179  
180 sub error_message (+%) {
181 die $_[0];
182 }
183  
184 package DBIx::Struct;
185 use strict;
186 use warnings;
187 use SQL::Abstract;
188 use Digest::MD5;
189 use Data::Dumper;
190 use Scalar::Util 'refaddr';
191 use base 'Exporter';
192 use v5.14;
193  
194 our $VERSION = '0.49';
195  
196 our @EXPORT = qw{
197 one_row
198 all_rows
199 for_rows
200 new_row
201 };
202  
203 our @EXPORT_OK = qw{
204 connector
205 hash_ref_slice
206 };
207  
208 sub ccmap ($) {
209 my $name = $_[0];
210 $name =~ s/([[:upper:]])/_\l$1/g;
211 $name =~ s/^_//;
212 return $name;
213 }
214  
215 our $camel_case_map = \&ccmap;
216 our $conn;
217 our $update_on_destroy = 1;
218 our $connector_module = 'DBIx::Struct::Connector';
219 our $connector_constructor = 'new';
220 our $connector_pool;
221 our $connector_pool_method = 'get_connector';
222 our $connector_args = [];
223 our $connector_driver;
224 our $user_schema_namespace;
225 our $table_classes_namespace = 'DBC';
226 our $query_classes_namespace = 'DBQ';
227 our $error_message_class = 'DBIx::Struct::Error::String';
228 our %driver_pk_insert;
229  
230 sub error_message (+%) {
231 goto &DBIx::Struct::Error::String::error_message;
232 }
233  
234 %driver_pk_insert = (
235 _returning => sub {
236 my ($table, $pk_row_data, $pk_returninig) = @_;
237 my $ret;
238 if ($pk_row_data) {
239 $ret = <<INS;
240 ($pk_row_data) =
241 \$_->selectrow_array(\$insert . " $pk_returninig", undef, \@bind)
242 INS
243 } else {
244 $ret = <<INS;
245 \$_->do(\$insert, undef, \@bind)
246 INS
247 }
248 $ret .= <<INS;
249 or DBIx::Struct::error_message {
250 result => 'SQLERR',
251 message => 'error '.\$_->errstr.' inserting into table $table'
252 };
253 INS
254 $ret;
255 },
256 _last_id_undef => sub {
257 my ($table, $pk_row_data) = @_;
258 my $ret;
259 $ret = <<INS;
260 \$_->do(\$insert, undef, \@bind)
261 or DBIx::Struct::error_message {
262 result => 'SQLERR',
263 message => 'error '.\$_->errstr.' inserting into table $table'
264 };
265 INS
266 if ($pk_row_data) {
267 $ret .= <<INS;
268 $pk_row_data = \$_->last_insert_id(undef, undef, undef, undef);
269 INS
270 }
271 $ret;
272 },
273 _last_id_empty => sub {
274 my ($table, $pk_row_data) = @_;
275 my $ret;
276 $ret = <<INS;
277 \$_->do(\$insert, undef, \@bind)
278 or DBIx::Struct::error_message {
279 result => 'SQLERR',
280 message => 'error '.\$_->errstr.' inserting into table $table'
281 };
282 INS
283 if ($pk_row_data) {
284 $ret .= <<INS;
285 $pk_row_data = \$_->last_insert_id("", "", "", "");
286 INS
287 }
288 $ret;
289 }
290 );
291  
292 $driver_pk_insert{Pg} = $driver_pk_insert{_returning};
293 $driver_pk_insert{mysql} = $driver_pk_insert{_last_id_undef};
294 $driver_pk_insert{SQLite} = $driver_pk_insert{_last_id_empty};
295  
296 sub hash_ref_slice($@) {
297 my ($hashref, @slice) = @_;
298 error_message {
299 message => "first parameter is not hash reference",
300 result => 'INTERR',
301 }
302 if 'HASH' ne ref $hashref;
303 map {$_ => $hashref->{$_}} @slice;
304 }
305  
306 my @already_exported_to;
307  
308 sub connector {
309 $conn;
310 }
311  
312 sub connector_from_pool {
313 $connector_pool->$connector_pool_method();
314 }
315  
316 sub set_connector_pool {
317 $connector_pool = $_[0];
318 if (\&connector != \&connector_from_pool) {
319 no warnings 'redefine';
320 no strict 'refs';
321 *connector = \&connector_from_pool;
322 for my $aep (@already_exported_to) {
323 *{"$aep\::connector"} = \&connector;
324 }
325 }
326 }
327  
328 sub set_user_schema_namespace {
329 $user_schema_namespace = $_[0];
330 }
331  
332 sub set_connector_pool_method {
333 $connector_pool_method = $_[0];
334 }
335  
336 sub set_connector_object {
337 *conn = \$_[0];
338 }
339  
340 sub set_camel_case_map {
341 error_message {
342 message => "CamelCaseMap must be code reference",
343 result => 'SQLERR',
344 } if 'CODE' ne ref $_[0];
345 $camel_case_map = $_[0];
346 }
347  
348 sub check_package_scalar {
349 my ($package, $scalar) = @_;
350 no strict 'refs';
351 my $pr = \%{$package . '::'};
352 my $er = $$pr{$scalar};
353 return unless $er;
354 defined *{$er}{'SCALAR'};
355 }
356  
357 sub import {
358 my ($class, @args) = @_;
359 my $defconn = 0;
360 my $_emc = 0;
361 my $_cp = 0;
362 my $_c = 0;
363 for (my $i = 0; $i < @args; ++$i) {
364 if ($args[$i] eq 'connector_module') {
365 (undef, $connector_module) = splice @args, $i, 2;
366 --$i;
367 if (not $defconn and check_package_scalar($connector_module, 'conn')) {
368 no strict 'refs';
369 set_connector_object(${$connector_module . '::conn'});
370 }
371 } elsif ($args[$i] eq 'connector_constructor') {
372 (undef, $connector_constructor) = splice @args, $i, 2;
373 --$i;
374 } elsif ($args[$i] eq 'camel_case_map' && 'CODE' eq ref $args[$i]) {
375 (undef, $camel_case_map) = splice @args, $i, 2;
376 --$i;
377 } elsif ($args[$i] eq 'user_schema_namespace') {
378 (undef, $user_schema_namespace) = splice @args, $i, 2;
379 --$i;
380 } elsif ($args[$i] eq 'table_classes_namespace') {
381 (undef, $table_classes_namespace) = splice @args, $i, 2;
382 --$i;
383 } elsif ($args[$i] eq 'query_classes_namespace') {
384 (undef, $query_classes_namespace) = splice @args, $i, 2;
385 --$i;
386 } elsif ($args[$i] eq 'connect_timeout') {
387 (undef, $db_reconnect_timeout) = splice @args, $i, 2;
388 --$i;
389 } elsif ($args[$i] eq 'error_class') {
390 my (undef, $emc) = splice @args, $i, 2;
391 $error_message_class = $emc;
392 $_emc = 1;
393 --$i;
394 } elsif ($args[$i] eq 'connector_pool') {
395 (undef, $connector_pool) = splice @args, $i, 2;
396 $_cp = 1;
397 --$i;
398 } elsif ($args[$i] eq 'connector_pool_method') {
399 (undef, $connector_pool_method) = splice @args, $i, 2;
400 --$i;
401 } elsif ($args[$i] eq 'connector_args') {
402 (undef, $connector_args) = splice @args, $i, 2;
403 --$i;
404 } elsif ($args[$i] eq 'connector') {
405 $_c = 1;
406 } elsif ($args[$i] eq 'connector_object') {
407 $defconn = 1;
408 set_connector_object($args[$i + 1]);
409 splice @args, $i, 2;
410 --$i;
411 }
412 }
413 if ($_emc) {
414 no warnings 'redefine';
415 no strict 'refs';
416 *error_message = \&{$error_message_class . "::error_message"};
417 }
418 if ($_cp) {
419 no warnings 'redefine';
420 no strict 'refs';
421 *connector = \&connector_from_pool;
422 for my $aep (@already_exported_to) {
423 *{"$aep\::connector"} = \&connector;
424 }
425 }
426 my $callpkg = caller;
427 push @already_exported_to, $callpkg if $_c;
428 my %imps = map {$_ => undef} @args, @EXPORT;
429 $class->export_to_level(1, $class, keys %imps);
430 }
431  
432 sub _not_yet_connected {
433 if (!$connector_pool && !$conn) {
434 my ($dsn, $user, $password) = @_;
435 if ($dsn && $dsn !~ /^dbi:/i) {
436 $dsn = "dbi:Pg:dbname=$dsn";
437 }
438 my $connect_attrs = {
439 AutoCommit => 1,
440 PrintError => 0,
441 AutoInactiveDestroy => 1,
442 RaiseError => 0,
443 };
444 if ($dsn) {
445 my ($driver) = $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i;
446 if ($driver) {
447 if ($driver eq 'Pg') {
448 $connect_attrs->{pg_enable_utf8} = 1;
449 } elsif ($driver eq 'mysql') {
450 $connect_attrs->{mysql_enable_utf8} = 1;
451 } elsif ($driver eq 'SQLite') {
452 $connect_attrs->{sqlite_unicode} = 1;
453 }
454 }
455 }
456 if (!@$connector_args) {
457 @$connector_args = ($dsn, $user, $password, $connect_attrs);
458 }
459 $conn = $connector_module->$connector_constructor(@$connector_args)
460 or error_message {
461 message => "DB connect error",
462 result => 'SQLERR',
463 };
464 $conn->mode('fixup');
465 }
466 '' =~ /()/;
467 $connector_driver = connector->driver->{driver};
468 no warnings 'redefine';
469 *connect = \&connector;
470 populate();
471 connector;
472 }
473  
474 sub connect {
475 goto &_not_yet_connected;
476 }
477  
478 {
479 my $md5 = Digest::MD5->new;
480  
481 sub make_name {
482 my ($table) = @_;
483 my $simple_table = (index($table, " ") == -1);
484 my $ncn;
485 if ($simple_table) {
486 $ncn = $table_classes_namespace . "::" . join('', map {ucfirst($_)} split(/[^a-zA-Z0-9]/, $table));
487 } else {
488 $md5->add($table);
489 $ncn = $query_classes_namespace . "::" . "G" . $md5->hexdigest;
490 $md5->reset;
491 }
492 $ncn;
493 }
494 }
495  
496 sub populate {
497 my @tables;
498 DBIx::Struct::connect->run(
499 sub {
500 my $sth = $_->table_info('', '%', '%', "TABLE");
501 return if not $sth;
502 my $tables = $sth->fetchall_arrayref;
503 @tables = map {
504 (my $t = $_->[2]) =~ s/"//g;
505 $t;
506 } grep {
507 $_->[3] eq 'TABLE' and $_->[2] !~ /^sql_/
508 } @$tables;
509 }
510 );
511 for (@tables) {
512 my $ncn = setup_row($_);
513 if ($user_schema_namespace) {
514 (my $uncn = $ncn) =~ s/^.*:://;
515 eval "use ${user_schema_namespace}::${uncn}";
516 no strict 'refs';
517 eval {
518 if (
519 keys %{"${user_schema_namespace}::${uncn}::"}
520 and ( not ${"${user_schema_namespace}::${uncn}::"}{ISA}
521 or not "${user_schema_namespace}::${uncn}"->isa($ncn))
522 )
523 {
524 unshift @{"${user_schema_namespace}::${uncn}::ISA"}, $ncn;
525 }
526 };
527 }
528 }
529 }
530  
531 #<<<
532 my @uneq = (
533 qr/LessThanEqual$/, '<=',
534 qr/LessThan$/, '<',
535 qr/GreaterThanEqual$/, '>=',
536 qr/GreaterThan$/, '>',
537 qr/IsNull$/, sub {"'$_[0]' => {'=', undef}"},
538 qr/IsNotNull$/, sub {"'$_[0]' => {'!=', undef}"},
539 qr/IsNot$/, '!=',
540 qr/NotNull$/, sub {"'$_[0]' => {'!=', undef}"},
541 qr/NotEquals$/, '!=',
542 qr/NotIn$/, '-not_in',
543 qr/NotLike$/, '-not_like',
544 qr/IsEqualTo$/, '=',
545 qr/IsTrue$/, sub {"-bool => '$_[0]'"},
546 qr/IsFalse$/, sub {"-not_bool => '$_[0]'"},
547 qr/Equals$/, '=',
548 qr/True$/, sub {"-bool => '$_[0]'"},
549 qr/False$/, sub {"-not_bool => '$_[0]'"},
550 qr/Like$/, '-like',
551 qr/Is$/, '=',
552 qr/Not$/, '!=',
553 qr/In$/, '-in',
554 );
555 #>>>
556  
557 sub _parse_find_by {
558 my ($table, $find) = @_;
559 $find =~ s/^find(?<what>.*?)By(?![[:lower:]])// || $find =~ s/^find(?<what>.*)// or die "bad pattern: $find";
560 my $what = $+{what} || 'All';
561 $what =~ s/(?<distinct>Distinct)(?![[:lower:]])//;
562 my $distinct = $+{distinct} // 0;
563 $what =~ s/((?<type>(All|One|First))(?<limit>\d+)?)(?![[:lower:]])//;
564 my $type = $+{type} // 'All';
565 my $limit = $+{limit};
566 $what =~ s/(?<column>\w+)//;
567 my $column = $camel_case_map->($+{column} // '');
568 $find =~ s/OrderBy(?<order>.*?)(?<asc>Asc|Desc)(?=[[:upper:]]|$)// || $find =~ s/OrderBy(?<order>.*?)$//;
569 my $order = $+{order};
570 my $asc = $+{asc} || 'Asc';
571 my $where = $find;
572  
573 if ($type eq 'First' && !$limit) {
574 $limit = 1;
575 }
576 if ($limit && $limit == 1) {
577 $type = 'One';
578 }
579 my $pi = 1;
580 my $pp = sub {
581 my ($param) = @_;
582 my $found;
583 for (my $i = 0; $i < @uneq; $i += 2) {
584 if ($param =~ s/$uneq[$i]//) {
585 $found = $i + 1;
586 last;
587 }
588 }
589 $param = $camel_case_map->($param);
590 my $ret;
591 if ($found) {
592 if ('CODE' eq ref $uneq[$found]) {
593 $ret = $uneq[$found]->($param);
594 } else {
595 $ret = "'$param' => { '$uneq[$found]' => \$_[$pi]}";
596 ++$pi;
597 }
598 } else {
599 $ret = "'$param' => \$_[$pi]";
600 ++$pi;
601 }
602 $ret;
603 };
604 #<<<
605 my $conds = join(
606 ", ",
607 map {
608 /And(?![[:lower:]])/
609 ? '-and => [' . join(", ", map {$pp->($_)} split /And(?![[:lower:]])/x, $_) . ']'
610 : $pp->($_);
611 } split /Or(?![[:lower:]])/, $where
612 );
613 #>>>
614 my $obj = $type eq 'One' ? 'DBIx::Struct::one_row' : 'DBIx::Struct::all_rows';
615 my $flags = $column ? ", -column => '$column'" : '';
616 $flags = $distinct ? $flags ? ", -distinct => '$column'" : ", '-distinct'" : $flags;
617 $order =
618 $order
619 ? $asc eq 'Asc'
620 ? ", -order_by => '" . $camel_case_map->($order) . "'"
621 : ", -order_by => {-desc => '" . $camel_case_map->($order) . "'}"
622 : '';
623 $where = $conds ? ", -where => [$conds]" : '';
624 $limit = $limit && $limit > 1 && $type ne 'One' ? ", -limit => $limit" : '';
625 my $tspec = "'$table'" . $flags;
626 $tspec = "[$tspec]" if $column;
627 $tspec .= $where . $order . $limit;
628 return "sub { $obj($tspec) }";
629 }
630  
631 sub _row_data () {0}
632 sub _row_updates () {1}
633  
634 sub make_object_new {
635 my ($table, $required, $pk_row_data, $pk_returninig) = @_;
636 my $new = <<NEW;
637 sub new {
638 my \$class = \$_[0];
639 my \$self = [ [] ];
640 bless \$self, \$class;
641 if(CORE::defined(\$_[1]) && CORE::ref(\$_[1]) eq 'ARRAY') {
642 \$self->[@{[_row_data]}] = \$_[1];
643 }
644 NEW
645 if (not ref $table) {
646 $new .= <<NEW;
647 else {
648 my \%insert;
649 for(my \$i = 1; \$i < \@_; \$i += 2) {
650 if (CORE::exists \$fields{\$_[\$i]}) {
651 my \$f = \$_[\$i];
652 \$self->[@{[_row_data]}]->[\$fields{\$_[\$i]}] = \$_[\$i + 1];
653 \$insert{\$f} = \$_[\$i + 1];
654 } else {
655 DBIx::Struct::error_message {
656 result => 'SQLERR',
657 message => "unknown column \$_[\$i] inserting into table $table"
658 }
659 }
660 }
661 my (\@insert, \@values, \@bind);
662 \@insert =
663 CORE::map {
664 if(CORE::ref(\$insert{\$_}) eq 'ARRAY' and CORE::ref(\$insert{\$_}[0]) eq 'SCALAR') {
665 CORE::push \@bind, \@{\$insert{\$_}}[1..\$#{\$insert{\$_}}];
666 CORE::push \@values, \${\$insert{\$_}[0]};
667 DBIx::Struct::connect->dbh->quote_identifier(\$_);
668 } elsif(CORE::ref(\$insert{\$_}) eq 'REF' and CORE::ref(\${\$insert{\$_}}) eq 'ARRAY') {
669 if(CORE::defined \${\$insert{\$_}}->[0]) {
670 CORE::push \@bind, \@{\${\$insert{\$_}}}[1..\$#{\${\$insert{\$_}}}];
671 CORE::push \@values, \${\$insert{\$_}}->[0];
672 DBIx::Struct::connect->dbh->quote_identifier(\$_);
673 } else {
674 CORE::push \@values, "null";
675 DBIx::Struct::connect->dbh->quote_identifier(\$_)
676 }
677 } elsif(CORE::ref(\$insert{\$_}) eq 'SCALAR') {
678 CORE::push \@values, \${\$insert{\$_}};
679 DBIx::Struct::connect->dbh->quote_identifier(\$_);
680 } elsif(CORE::exists (\$json_fields{\$_})
681 && (CORE::ref(\$insert{\$_}) eq 'ARRAY' || CORE::ref(\$insert{\$_}) eq 'HASH')) {
682 CORE::push \@bind, JSON::to_json(\$insert{\$_});
683 CORE::push \@values, "?";
684 DBIx::Struct::connect->dbh->quote_identifier(\$_);
685 } else {
686 CORE::push \@bind, \$insert{\$_};
687 CORE::push \@values, "?";
688 DBIx::Struct::connect->dbh->quote_identifier(\$_);
689 }
690 } CORE::keys \%insert;
691 my \$insert;
692 if(\%insert){
693 \$insert = "insert into $table (" . CORE::join( ", ", \@insert) . ") values ("
694 . CORE::join( ", ", \@values) . ")";
695 } else {
696 \$insert = "insert into $table values (default)";
697 }
698 NEW
699 if ($required) {
700 $new .= <<NEW;
701 for my \$r ($required) {
702 DBIx::Struct::error_message {
703 result => 'SQLERR',
704 message => "required field \$r is absent for table $table"
705 } if not CORE::exists \$insert{\$r};
706 }
707 NEW
708 }
709 $new .= <<NEW;
710 DBIx::Struct::connect->run(
711 sub {
712 NEW
713 $new .= $driver_pk_insert{$connector_driver}->($table, $pk_row_data, $pk_returninig);
714 $new .= <<NEW;
715 });
716 }
717 NEW
718 }
719 $new .= <<NEW;
720 \$self;
721 }
722 NEW
723 $new;
724 }
725  
726 sub make_object_filter_timestamp {
727 my ($timestamps) = @_;
728 my $filter_timestamp = <<FTS;
729 sub filter_timestamp {
730 my \$self = \$_[0];
731 if(\@_ == 1) {
732 for my \$f ($timestamps) {
733 if(\$self->[@{[_row_data]}][\$fields{\$f}]) {
734 \$self->[@{[_row_data]}][\$fields{\$f}] =~ s/\\.\\d+(\$|\\+|\\-)/\$1/;
735 \$self->[@{[_row_data]}][\$fields{\$f}] =~ s/(\\+|\\-)(\\d{2})\$/\$1\${2}00/;
736 }
737 }
738 } else {
739 for my \$f (\@_[1..\$#_]) {
740 if(\$self->[@{[_row_data]}][\$fields{\$f}]) {
741 \$self->[@{[_row_data]}][\$fields{\$f}] =~ s/\\.\\d+(\$|\\+|\\-)/\$1/;
742 \$self->[@{[_row_data]}][\$fields{\$f}] =~ s/(\\+|\\-)(\\d{2})\$/\$1\${2}00/;
743 }
744 }
745 }
746 '' =~ /()/;
747 \$self;
748 }
749 FTS
750 $filter_timestamp;
751 }
752  
753 sub make_object_set {
754 my $table = $_[0];
755 my $set = <<SET;
756 sub set {
757 my \$self = \$_[0];
758 my \@unknown_columns;
759 if(CORE::defined(\$_[1])) {
760 if(CORE::ref(\$_[1]) eq 'ARRAY') {
761 \$self->[@{[_row_data]}] = \$_[1];
762 \$self->[@{[_row_updates]}] = {};
763 } elsif(CORE::ref(\$_[1]) eq 'HASH') {
764 for my \$f (CORE::keys \%{\$_[1]}) {
765 if (CORE::exists \$fields{\$f}) {
766 \$self->\$f(\$_[1]->{\$f});
767 } else {
768 CORE::push \@unknown_columns, \$f;
769 }
770 }
771 } elsif(not CORE::ref(\$_[1])) {
772 for(my \$i = 1; \$i < \@_; \$i += 2) {
773 if (CORE::exists \$fields{\$_[\$i]}) {
774 my \$f = \$_[\$i];
775 \$self->\$f(\$_[\$i + 1]);
776 } else {
777 CORE::push \@unknown_columns, \$_[\$i];
778 }
779 }
780 }
781 }
782 DBIx::Struct::error_message {
783 result => 'SQLERR',
784 message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' for $table->data'
785 } if \@unknown_columns;
786 \$self;
787 }
788 SET
789 $set;
790 }
791  
792 sub make_object_data {
793 my $table = $_[0];
794 my $data = <<DATA;
795 sub data {
796 my \$self = \$_[0];
797 my \@ret_keys;
798 my \$ret;
799 if(CORE::defined(\$_[1])) {
800 if(CORE::ref(\$_[1]) eq 'ARRAY') {
801 if(!\@{\$_[1]}) {
802 \$ret = \$self->[@{[_row_data]}];
803 } else {
804 \$ret = [CORE::map {\$self->[@{[_row_data]}]->[\$fields{\$_}] } \@{\$_[1]}];
805 }
806 } else {
807 for my \$k (\@_[1..\$#_]) {
808 CORE::push \@ret_keys, \$k if CORE::exists \$fields{\$k};
809 }
810 }
811 } else {
812 \@ret_keys = keys \%fields;
813 }
814 my \@unknown_columns = CORE::grep {not CORE::exists \$fields{\$_}} \@ret_keys;
815 DBIx::Struct::error_message {
816 result => 'SQLERR',
817 message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' for $table->data'
818 } if \@unknown_columns;
819 \$ret = {
820 CORE::map {\$_ => \$self->\$_} \@ret_keys
821 } if not CORE::defined \$ret;
822 \$ret;
823 }
824 DATA
825 $data;
826 }
827  
828 sub make_object_update {
829 my ($table, $pk_where, $pk_row_data) = @_;
830 my $update;
831 if (not ref $table) {
832  
833 # means this is just one simple table
834 $update = <<UPD;
835 sub update {
836 my \$self = \$_[0];
837 if(\@_ > 1 && CORE::ref(\$_[1]) eq 'HASH') {
838 my (\$set, \$where, \@bind, \@bind_where);
839 {
840 no strict 'vars';
841 local *set_hash = \$_[1];
842 my \@unknown_columns = CORE::grep {not CORE::exists \$fields{\$_}} CORE::keys %set_hash;
843 DBIx::Struct::error_message {
844 result => 'SQLERR',
845 message => 'unknown columns '.CORE::join(", ", \@unknown_columns).' updating table $table'
846 } if \@unknown_columns;
847 \$set =
848 CORE::join ", ",
849 CORE::map {
850 if(CORE::ref(\$set_hash{\$_}) eq 'ARRAY' and CORE::ref(\$set_hash{\$_}[0]) eq 'SCALAR') {
851 CORE::push \@bind, \@{\$set_hash{\$_}}[1..\$#{\$set_hash{\$_}}];
852 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}[0]};
853 } elsif(CORE::ref(\$set_hash{\$_}) eq 'REF' and CORE::ref(\${\$set_hash{\$_}}) eq 'ARRAY') {
854 if(CORE::defined \${\$set_hash{\$_}}->[0]) {
855 CORE::push \@bind, \@{\${\$set_hash{\$_}}}[1..\$#{\${\$set_hash{\$_}}}];
856 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}}->[0];
857 } else {
858 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = null"
859 }
860 } elsif(CORE::ref(\$set_hash{\$_}) eq 'SCALAR') {
861 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$set_hash{\$_}};
862 } elsif(CORE::exists(\$json_fields{\$_})
863 && (CORE::ref(\$set_hash{\$_}) eq 'ARRAY' || CORE::ref(\$set_hash{\$_}) eq 'HASH')) {
864 CORE::push \@bind, JSON::to_json(\$set_hash{\$_});
865 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?"
866 } else {
867 CORE::push \@bind, \$set_hash{\$_};
868 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?"
869 }
870 } CORE::keys \%set_hash;
871 }
872 if(\@_ > 2) {
873 my \$cond = \$_[2];
874 if(not CORE::ref(\$cond)) {
875 \$cond = {(selectKeys)[0] => \$_[2]};
876 }
877 (\$where, \@bind_where) = SQL::Abstract->new->where(\$cond);
878 }
879 return DBIx::Struct::connect->run(sub {
880 \$_->do(qq{update $table set \$set \$where}, undef, \@bind, \@bind_where)
881 or DBIx::Struct::error_message {
882 result => 'SQLERR',
883 message => 'error '.\$_->errstr.' updating table $table'
884 }
885 });
886 } elsif (CORE::ref(\$self) && \@\$self > 1 && \%{\$self->[@{[_row_updates]}]}) {
887 my (\$set, \@bind);
888 {
889 no strict 'vars';
890 \$set =
891 CORE::join ", ",
892 CORE::map {
893 local *column_value = \\\$self->[@{[_row_data]}][\$fields{\$_}];
894 if(CORE::ref(\$column_value) eq 'ARRAY' and CORE::ref(\$column_value->[0]) eq 'SCALAR') {
895 CORE::push \@bind, \@{\$column_value}[1..\$#\$column_value];
896 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$column_value->[0]};
897 } elsif(CORE::ref(\$column_value) eq 'REF' and CORE::ref(\${\$column_value}) eq 'ARRAY') {
898 if(CORE::defined \${\$column_value}->[0]) {
899 CORE::push \@bind, \@{\${\$column_value}}[1..\$#{\${\$column_value}}];
900 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \${\$column_value}->[0];
901 } else {
902 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = null"
903 }
904 } elsif(CORE::ref(\$column_value) && CORE::ref(\$column_value) =~ /^DBIx::Struct::JSON/) {
905 \$column_value->revert;
906 CORE::push \@bind, \$column_value;
907 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?"
908 } elsif(CORE::ref(\$column_value) eq 'SCALAR') {
909 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = " . \$\$column_value;
910 } else {
911 CORE::push \@bind, \$column_value;
912 DBIx::Struct::connect->dbh->quote_identifier(\$_) . " = ?"
913 }
914 } CORE::keys \%{\$self->[@{[_row_updates]}]};
915 }
916 my \$update_query = qq{update $table set \$set where $pk_where};
917 DBIx::Struct::connect->run(
918 sub {
919 \$_->do(\$update_query, undef, \@bind, $pk_row_data)
920 or DBIx::Struct::error_message {
921 result => 'SQLERR',
922 message => 'error '.\$_->errstr.' updating table $table',
923 query => \$update_query,
924 bind => \\\@bind,
925 }
926 }
927 );
928 \$#\$self = @{[_row_data]};
929 }
930 \$self;
931 }
932 UPD
933 } else {
934 $update = <<UPD;
935 sub update {}
936 UPD
937 }
938 $update;
939 }
940  
941 sub make_object_delete {
942 my ($table, $pk_where, $pk_row_data) = @_;
943 my $delete;
944 if (not ref $table) {
945 $delete = <<DEL;
946 sub delete {
947 my \$self = \$_[0];
948 if(Scalar::Util::blessed \$self) {
949 DBIx::Struct::connect->run(
950 sub {
951 \$_->do(qq{delete from $table where $pk_where}, undef, $pk_row_data)
952 or DBIx::Struct::error_message {
953 result => 'SQLERR',
954 message => 'error '.\$_->errstr.' updating table $table'
955 }
956 });
957 return \$self;
958 }
959 my \$where = '';
960 my \@bind;
961 my \$cond = \$_[1] if \@_ > 1;
962 if(not CORE::ref(\$cond)) {
963 \$cond = {};
964 my \@keys = selectKeys();
965 for(my \$i = 1; \$i < \@_; ++\$i) {
966 DBIx::Struct::error_message {
967 result => 'SQLERR',
968 message => "Too many keys to delete for $table"
969 } if not CORE::defined \$keys[\$i-1];
970 \$cond->{\$keys[\$i-1]} = \$_[\$i];
971 }
972 }
973 my \@rpar = ();
974 if(\$cond) {
975 (\$where, \@bind) = SQL::Abstract->new->where(\$cond);
976 \@rpar = (undef, \@bind);
977 }
978 return DBIx::Struct::connect->run(sub {
979 \$_->do(qq{delete from $table \$where}, \@rpar)
980 or DBIx::Struct::error_message {
981 result => 'SQLERR',
982 message => 'error '.\$_->errstr.' updating table $table'
983 }
984 });
985 }
986 DEL
987 } else {
988 $delete = <<DEL
989 sub delete {}
990 DEL
991 }
992 $delete;
993 }
994  
995 sub make_object_autoload_find {
996 my ($table, $pk_where, $pk_row_data) = @_;
997 my $find = '';
998 if (not ref $table) {
999 $find = <<AUTOLOAD;
1000 sub AUTOLOAD {
1001 my \$self = \$_[0];
1002 ( my \$method = \$AUTOLOAD ) =~ s{.*::}{};
1003
1004 if(Scalar::Util::blessed \$self) {
1005 \$self = CORE::ref \$self;
1006 }
1007 DBIx::Struct::error_message {
1008 result => 'SQLERR',
1009 message => "Unknown method \$method for $table"
1010 } if !\$self || !"\$self"->can("tableName") || \$method !~ /^find/;
1011 my \$func = DBIx::Struct::_parse_find_by('$table', \$method);
1012 my \$ncn = DBIx::Struct::make_name('$table');
1013 {
1014 no strict 'refs';
1015 *{\$ncn."::".\$method} = eval \$func;
1016 DBIx::Struct::error_message {
1017 result => 'SQLERR',
1018 message => "Error creating method \$method for $table: \$\@"
1019 } if \$\@;
1020 }
1021 goto &{\$ncn."::".\$method};
1022 }
1023 AUTOLOAD
1024 }
1025 $find;
1026 }
1027  
1028 sub make_object_fetch {
1029 my ($table, $pk_where, $pk_row_data) = @_;
1030 my $fetch;
1031 if (not ref $table) {
1032 $fetch = <<FETCH;
1033 sub fetch {
1034 my \$self = \$_[0];
1035 if(\@_ > 1) {
1036 my (\$where, \@bind);
1037 my \$cond = \$_[1];
1038 if(not CORE::ref(\$cond)) {
1039 \$cond = {(selectKeys)[0] => \$_[1]};
1040 }
1041 (\$where, \@bind) = SQL::Abstract->new->where(\$cond);
1042 DBIx::Struct::connect->run(sub {
1043 my \$rowref = \$_->selectrow_arrayref(qq{select * from $table \$where}, undef, \@bind)
1044 or DBIx::Struct::error_message {
1045 result => 'SQLERR',
1046 message => 'error '.\$_->errstr.' fetching table $table'
1047 };
1048 \$self->[@{[_row_data]}] = [\@\$rowref];
1049 });
1050 } else {
1051 DBIx::Struct::connect->run(
1052 sub {
1053 my \$rowref = \$_->selectrow_arrayref(qq{select * from $table where $pk_where}, undef, $pk_row_data)
1054 or DBIx::Struct::error_message {
1055 result => 'SQLERR',
1056 message => 'error '.\$_->errstr.' fetching table $table'
1057 };
1058 \$self->[@{[_row_data]}] = [\@\$rowref];
1059 });
1060 }
1061 \$self;
1062 }
1063 FETCH
1064 } else {
1065 $fetch = <<FETCH;
1066 sub fetch { \$_[0] }
1067 FETCH
1068 }
1069 $fetch;
1070 }
1071  
1072 sub _exists_row ($) {
1073 my $ncn = $_[0];
1074 no strict "refs";
1075 if (grep {!/::$/} keys %{"${ncn}::"}) {
1076 return 1;
1077 }
1078 return;
1079 }
1080  
1081 sub _parse_interface ($) {
1082 my $interface = $_[0];
1083 my %ret;
1084 $interface = [$interface] if not ref $interface;
1085 if ('ARRAY' eq ref $interface) {
1086 for my $i (@$interface) {
1087 my $dbc_name = make_name($i);
1088 error_message {
1089 result => 'SQLERR',
1090 message => "Unknown base interface table $i",
1091 }
1092 unless _exists_row $dbc_name;
1093 no strict 'refs';
1094 my $href = \%{"${dbc_name}::fkfuncs"};
1095 if ($href && %$href) {
1096 my @i = keys %$href;
1097 @ret{@i} = @{$href}{@i};
1098 }
1099 }
1100 } elsif ('HASH' eq ref $interface) {
1101 for my $i (keys %$interface) {
1102 my $dbc_name = make_name($i);
1103 error_message {
1104 result => 'SQLERR',
1105 message => "Unknown base interface table $i",
1106 }
1107 unless _exists_row $dbc_name;
1108 no strict 'refs';
1109 my $href = \%{"${dbc_name}::fkfuncs"};
1110 next if not $href or not %$href;
1111 my $fl = $interface->{$i};
1112 $fl = [$fl] if not ref $fl;
1113 if ('ARRAY' eq ref $fl) {
1114  
1115 for my $m (@$fl) {
1116 $ret{$m} = $href->{$m} if exists $href->{$m};
1117 }
1118 } else {
1119 error_message {
1120 result => 'SQLERR',
1121 message => "Usupported interface structure",
1122 };
1123 }
1124 }
1125 } else {
1126 error_message {
1127 result => 'SQLERR',
1128 message => "Unknown interface structure: " . ref($interface),
1129 };
1130 }
1131 \%ret;
1132 }
1133  
1134 sub make_object_to_json {
1135 my ($table, $field_types, $fields) = @_;
1136 my @to_types = map {[
1137 $_,
1138 qq|!defined(\$self->[@{[_row_data]}][$fields->{$_}])? undef: |
1139 . (
1140 $field_types->{$_} eq 'number' ? "0+\$self->[@{[_row_data]}][$fields->{$_}]"
1141 : $field_types->{$_} eq 'boolean' ? "\$self->[@{[_row_data]}][$fields->{$_}]? \\1: \\0"
1142 : $field_types->{$_} eq 'json'
1143 ? "CORE::ref(\$self->[@{[_row_data]}]->[$fields->{$_}])? \$self->[@{[_row_data]}][$fields->{$_}]->data"
1144 . ": JSON::from_json(\$self->[@{[_row_data]}][$fields->{$_}])"
1145 : "\"\$self->[@{[_row_data]}][$fields->{$_}]\""
1146 )
1147 ]
1148 } keys %$field_types;
1149 my $field_to_types = join ",\n\t\t\t\t ", map {qq|"$_->[0]" => $_->[1]|} @to_types;
1150 my $sub_to_types = '';
1151 for my $tt (@to_types) {
1152 my $k = $tt->[0];
1153 $k =~ s/[^\w\d]/_/g;
1154 $sub_to_types .= <<JSTT
1155 sub _to_json_$k { my \$self = \$_[0]; $tt->[1] }
1156 JSTT
1157 }
1158 my $to_json = <<TOJSON;
1159 sub TO_JSON {
1160 my \$self = shift;
1161 my \$ret;
1162 my \@columns = CORE::grep { not ref } \@_;
1163 my \@refs = CORE::grep { 'HASH' eq ref } \@_;
1164 if(\@columns) {
1165 \$ret = +{
1166 map {
1167 DBIx::Struct::error_message {
1168 result => 'SQLERR',
1169 message => "unknown column \$_ in table $table"
1170 } if not CORE::exists \$fields{\$_};
1171 my \$k = \$_;
1172 \$k =~ s/[^\\w\\d]/_/g;
1173 my \$m = "_to_json_\$k";
1174 \$_ => \$self->\$m
1175 } \@columns
1176 };
1177 } else {
1178 \$ret = +{
1179 $field_to_types
1180 };
1181 }
1182 if(\@refs) {
1183 \$ret = +{ %\$ret, map { %\$_ } \@refs };
1184 }
1185 return \$ret;
1186 }
1187 TOJSON
1188 return $sub_to_types . $to_json;
1189 }
1190  
1191 sub _field_type_from_name {
1192 my $type_name = $_[0];
1193 return 'string' if not defined $type_name;
1194 if ( $type_name =~ /int(\d+)?$/i
1195 || $type_name =~ /integer/i
1196 || $type_name =~ /bit$/
1197 || $type_name =~ /float|double|real|decimal|numeric/i)
1198 {
1199 return 'number';
1200 } elsif ($type_name =~ /json/i) {
1201 return 'json';
1202 } elsif ($type_name =~ /bool/i) {
1203 return 'boolean';
1204 } elsif ($type_name =~ /date/i && $type_name =~ /time/i) {
1205 return 'datetime';
1206 } elsif ($type_name =~ /date/i) {
1207 return 'date';
1208 } elsif ($type_name =~ /time/i) {
1209 return 'time';
1210 } else {
1211 return 'string';
1212 }
1213 }
1214  
1215 sub _schema_name {
1216 my $ncn = $_[0];
1217 if ($user_schema_namespace) {
1218 (my $uncn = $ncn) =~ s/^.*:://;
1219 no strict 'refs';
1220 eval {
1221 if (${"${user_schema_namespace}::${uncn}::"}{ISA}
1222 && "${user_schema_namespace}::${uncn}"->isa($ncn))
1223 {
1224 $ncn = "${user_schema_namespace}::${uncn}";
1225 }
1226 };
1227 }
1228 $ncn;
1229 }
1230  
1231 sub setup_row {
1232 my ($table, $ncn, $interface) = @_;
1233 error_message {
1234 result => 'SQLERR',
1235 message => "Unsupported driver $connector_driver",
1236 }
1237 unless exists $driver_pk_insert{$connector_driver};
1238 $ncn ||= make_name($table);
1239 return $ncn if _exists_row $ncn ;
1240 my %fields;
1241 my @fields;
1242 my @timestamp_fields;
1243 my @required;
1244 my @pkeys;
1245 my @fkeys;
1246 my @refkeys;
1247 my %json_fields;
1248 my $connector = DBIx::Struct::connect;
1249 my %field_types;
1250  
1251 if (not ref $table) {
1252 # means this is just one simple table
1253 $connector->run(
1254 sub {
1255 my $ssth = $_->prepare('select * from ' . $_->quote_identifier($table) . ' where 0 = 1');
1256 error_message {
1257 result => 'SQLERR',
1258 message => "Unknown table $table",
1259 }
1260 if not $ssth;
1261 $ssth->execute
1262 or error_message {
1263 result => 'SQLERR',
1264 message => "Probably unknown table $table: " . $_->errstr,
1265  
1266 };
1267 my $cih = $_->column_info(undef, undef, $table, undef);
1268 error_message {
1269 result => 'SQLERR',
1270 message => "Unknown table $table",
1271 }
1272 if not $cih;
1273 my $i = 0;
1274 while (my $chr = $cih->fetchrow_hashref) {
1275 $chr->{COLUMN_NAME} =~ s/"//g;
1276 $chr->{COLUMN_NAME} = lc $chr->{COLUMN_NAME};
1277 push @fields, $chr->{COLUMN_NAME};
1278 if ($chr->{TYPE_NAME} =~ /^time/i) {
1279 push @timestamp_fields, $chr->{COLUMN_NAME};
1280 }
1281 if ($chr->{TYPE_NAME} =~ /^json/i) {
1282 $json_fields{$chr->{COLUMN_NAME}} = undef;
1283 }
1284 $chr->{COLUMN_DEF} //= $chr->{mysql_is_auto_increment};
1285 if ($chr->{NULLABLE} == 0 && !defined($chr->{COLUMN_DEF})) {
1286 push @required, $chr->{COLUMN_NAME};
1287 }
1288 $fields{$chr->{COLUMN_NAME}} = $i++;
1289 $field_types{$chr->{COLUMN_NAME}} = _field_type_from_name($chr->{TYPE_NAME});
1290 }
1291 @pkeys = map {lc} $_->primary_key(undef, undef, $table);
1292 if (!@pkeys && @required) {
1293 my $ukh = $_->statistics_info(undef, undef, $table, 1, 1);
1294 my %req = map {$_ => undef} @required;
1295 my %pkeys;
1296 while (my $ukr = $ukh->fetchrow_hashref) {
1297 if (not exists $req{$ukr->{COLUMN_NAME}} or defined $ukr->{FILTER_CONDITION}) {
1298 $pkeys{lc $ukr->{INDEX_NAME}}{drop} = 1;
1299 } else {
1300 $pkeys{lc $ukr->{INDEX_NAME}}{fields}{lc $ukr->{COLUMN_NAME}} = undef;
1301 }
1302 }
1303 my @d = grep {exists $pkeys{$_}{drop}} keys %pkeys;
1304 delete $pkeys{$_} for @d;
1305 if (%pkeys) {
1306 my @spk = sort {scalar(keys %{$pkeys{$a}{fields}}) <=> scalar(keys %{$pkeys{$b}{fields}})}
1307 keys %pkeys;
1308 @pkeys = keys %{$pkeys{$spk[0]}{fields}};
1309 }
1310 }
1311 my $sth = $_->foreign_key_info(undef, undef, undef, undef, undef, $table);
1312 if ($sth) {
1313 @fkeys =
1314 grep {($_->{PKTABLE_NAME} || $_->{UK_TABLE_NAME}) && $_->{FK_COLUMN_NAME} !~ /[^a-z_0-9]/}
1315 map {
1316 $_->{FK_COLUMN_NAME} = $_->{FKCOLUMN_NAME}
1317 if $_->{FKCOLUMN_NAME};
1318 $_->{FK_TABLE_NAME} = $_->{FKTABLE_NAME} if $_->{FKTABLE_NAME};
1319 $_->{FK_TABLE_NAME} = lc $_->{FK_TABLE_NAME};
1320 $_->{FK_COLUMN_NAME} = lc $_->{FK_COLUMN_NAME};
1321 $_->{PKTABLE_NAME} ||= $_->{UK_TABLE_NAME};
1322 $_->{PKCOLUMN_NAME} ||= $_->{UK_COLUMN_NAME};
1323 $_->{PKTABLE_NAME} = lc $_->{PKTABLE_NAME} if $_->{PKTABLE_NAME};
1324 $_->{PKCOLUMN_NAME} = lc $_->{PKCOLUMN_NAME} if $_->{PKCOLUMN_NAME};
1325 $_
1326 } @{$sth->fetchall_arrayref({})};
1327 }
1328 $sth = $_->foreign_key_info(undef, undef, $table, undef, undef, undef);
1329 if ($sth) {
1330 @refkeys =
1331 grep {($_->{PKTABLE_NAME} || $_->{UK_TABLE_NAME}) && $_->{FK_COLUMN_NAME} !~ /[^a-z_0-9]/}
1332 map {
1333 $_->{FK_COLUMN_NAME} = $_->{FKCOLUMN_NAME}
1334 if $_->{FKCOLUMN_NAME};
1335 $_->{FK_TABLE_NAME} = $_->{FKTABLE_NAME} if $_->{FKTABLE_NAME};
1336 $_->{FK_TABLE_NAME} = lc $_->{FK_TABLE_NAME};
1337 $_->{FK_COLUMN_NAME} = lc $_->{FK_COLUMN_NAME};
1338 $_->{PKTABLE_NAME} = lc($_->{PKTABLE_NAME} || $_->{UK_TABLE_NAME});
1339 $_->{PKCOLUMN_NAME} = lc($_->{PKCOLUMN_NAME} || $_->{UK_COLUMN_NAME});
1340 $_
1341 } @{$sth->fetchall_arrayref({})};
1342 }
1343 }
1344 );
1345 } else {
1346  
1347 # means this is a query
1348 my %tnh = %{$table->{NAME_lc_hash}};
1349 for my $k (keys %tnh) {
1350 my $fk = $k;
1351 $fk =~ s/[^\w ].*$//;
1352 $fields{$fk} = $tnh{$k};
1353 }
1354 $connector->run(
1355 sub {
1356 for (my $cn = 0; $cn < @{$table->{NAME}}; ++$cn) {
1357 my $ti = $_->type_info($table->{TYPE}->[$cn]);
1358 my $field = lc $table->{NAME}->[$cn];
1359 $field =~ s/[^\w ].*$//;
1360 $field_types{$field} = _field_type_from_name($ti->{TYPE_NAME});
1361 push @timestamp_fields, $field
1362 if $ti->{TYPE_NAME} && $ti->{TYPE_NAME} =~ /^time/;
1363 $json_fields{$field} = undef
1364 if $ti->{TYPE_NAME} && $ti->{TYPE_NAME} =~ /^json/;
1365 }
1366 }
1367 );
1368 }
1369 my $field_types = join ", ", map {qq|"$_" => '$field_types{$_}'|} keys %field_types;
1370 my $fields = join ", ", map {qq|"$_" => $fields{$_}|} keys %fields;
1371 my $json_fields = join ", ", map {qq|"$_" => undef|} keys %json_fields;
1372 my $required = '';
1373 if (@required) {
1374 $required = join(", ", map {qq|"$_"|} @required);
1375 }
1376 my $timestamps = '';
1377 if (@timestamp_fields) {
1378 $timestamps = join(", ", map {qq|"$_"|} @timestamp_fields);
1379 } else {
1380 $timestamps = "()";
1381 }
1382 my %keywords = (
1383 new => undef,
1384 set => undef,
1385 data => undef,
1386 delete => undef,
1387 fetch => undef,
1388 update => undef,
1389 DESTROY => undef,
1390 filter_timestamp => undef,
1391 );
1392 my $pk_row_data = '';
1393 my $pk_returninig = '';
1394 my $pk_where = '';
1395 my $select_keys = '';
1396 my %pk_fields;
1397 if (@pkeys) {
1398 @pk_fields{@pkeys} = undef;
1399 $pk_row_data = join(", ", map {qq|\$self->[@{[_row_data]}]->[$fields{"$_"}]|} @pkeys);
1400 $pk_returninig = 'returning ' . join(", ", @pkeys);
1401 $pk_where = join(" and ", map {"$_ = ?"} @pkeys);
1402 my $sk_list = join(", ", map {qq|"$_"|} @pkeys);
1403 $select_keys = <<SK;
1404 sub selectKeys () {
1405 ($sk_list)
1406 }
1407 SK
1408 } else {
1409 if (@fields) {
1410 my $sk_list = join(", ", map {qq|"$_"|} @fields);
1411 $select_keys = <<SK;
1412 sub selectKeys () {
1413 ($sk_list)
1414 }
1415 SK
1416 } else {
1417 $select_keys = <<SK;
1418 sub selectKeys () { () }
1419 SK
1420 }
1421 }
1422 my $foreign_tables = '';
1423 my %foreign_tables;
1424 my %fkfuncs;
1425 for my $fk (@fkeys) {
1426 (my $pt = $fk->{PKTABLE_NAME} || $fk->{UK_TABLE_NAME}) =~ s/"//g;
1427 (my $pk = $fk->{PKCOLUMN_NAME} || $fk->{UK_COLUMN_NAME}) =~ s/"//g;
1428 my $fn = $pt;
1429 $fn =~ tr/_/-/;
1430 $fn =~ s/\b(\w)/\u$1/g;
1431 $fn =~ tr/-//d;
1432 $fk->{FK_COLUMN_NAME} =~ s/"//g;
1433 my $fn_suffix = $fk->{FK_COLUMN_NAME};
1434 $fn_suffix =~ s/^${pk}_*//i or $fn_suffix =~ s/_$pk(?=[^a-z]|$)//i or $fn_suffix =~ s/$pk(?=[^a-z]|$)//i;
1435 $fn_suffix =~ tr/_/-/;
1436 $fn_suffix =~ s/\b(\w)/\u$1/g;
1437 $fn_suffix =~ tr/-//d;
1438 $fn_suffix =~ s/$fn//;
1439 $fn .= $fn_suffix;
1440 $fkfuncs{$fn} = undef;
1441 $foreign_tables .= <<FKT;
1442 sub $fn {
1443 if(CORE::defined(\$_[0]->$fk->{FK_COLUMN_NAME})) {
1444 return DBIx::Struct::one_row("$pt", {$pk => \$_[0]->$fk->{FK_COLUMN_NAME}});
1445 } else {
1446 return
1447 }
1448 }
1449 FKT
1450 $foreign_tables{$pt} = [$fk->{FK_COLUMN_NAME} => $pk];
1451 }
1452 for my $ft (keys %foreign_tables) {
1453 my $ucft = ucfirst $ft;
1454 $fkfuncs{"foreignKey$ucft"} = undef;
1455 $foreign_tables .= <<FKT;
1456 sub foreignKey$ucft () {("$foreign_tables{$ft}[0]" => "$foreign_tables{$ft}[1]")}
1457 FKT
1458 }
1459 my $references_tables = '';
1460 for my $rk (@refkeys) {
1461 $rk->{FK_TABLE_NAME} =~ s/"//g;
1462 my $ft = $rk->{FK_TABLE_NAME};
1463 (my $fk = $rk->{FK_COLUMN_NAME}) =~ s/"//g;
1464 (my $pt = $rk->{PKTABLE_NAME} || $rk->{UK_TABLE_NAME}) =~ s/"//g;
1465 (my $pk = $rk->{PKCOLUMN_NAME} || $rk->{UK_COLUMN_NAME}) =~ s/"//g;
1466 if ($pk ne $fk) {
1467 my $fn = $fk;
1468 $fn =~ s/^${pk}_*//i or $fn =~ s/_$pk(?=[^a-z]|$)//i or $fn =~ s/$pk(?=[^a-z]|$)//i;
1469 $fn =~ s/$pt//i;
1470 $ft .= "_$fn" if $fn;
1471 }
1472 $ft =~ tr/_/-/;
1473 $ft =~ s/\b(\w)/\u$1/g;
1474 $ft =~ tr/-//d;
1475 $fkfuncs{"ref${ft}s"} = undef;
1476 $fkfuncs{"ref${ft}"} = undef;
1477 $references_tables .= <<RT;
1478 sub ref${ft}s {
1479 my (\$self, \@cond) = \@_;
1480 my \%cond;
1481 if(\@cond) {
1482 if(not CORE::ref \$cond[0]) {
1483 \%cond = \@cond;
1484 } else {
1485 \%cond = \%{\$cond[0]};
1486 }
1487 }
1488 \$cond{$fk} = \$self->$pk;
1489 return DBIx::Struct::all_rows("$rk->{FK_TABLE_NAME}", \\\%cond);
1490 }
1491 sub ref${ft} {
1492 my (\$self, \@cond) = \@_;
1493 my \%cond;
1494 if(\@cond) {
1495 if(not CORE::ref \$cond[0]) {
1496 \%cond = \@cond;
1497 } else {
1498 \%cond = \%{\$cond[0]};
1499 }
1500 }
1501 \$cond{$fk} = \$self->$pk;
1502 return DBIx::Struct::one_row("$rk->{FK_TABLE_NAME}", \\\%cond);
1503 }
1504 RT
1505 }
1506 my $accessors = <<ACC;
1507 sub markUpdated {
1508 \$_[0]->[@{[_row_updates]}]{\$_[1]} = undef if CORE::exists \$fields{\$_[1]};
1509 \$_[0];
1510 }
1511 ACC
1512 for my $k (keys %fields) {
1513 next if exists $keywords{$k};
1514 next if $k =~ /^\d/;
1515 $k =~ s/[^\w\d]/_/g;
1516 if (!exists $json_fields{$k}) {
1517 if (!exists($pk_fields{$k}) && (not ref $table)) {
1518 $accessors .= <<ACC;
1519 sub _$k {
1520 if(\@_ > 1) {
1521 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = \$_[1];
1522 }
1523 \$_[0]->[@{[_row_data]}]->[$fields{$k}];
1524 }
1525 sub $k {
1526 if(\@_ > 1) {
1527 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = \$_[1];
1528 \$_[0]->[@{[_row_updates]}]{"$k"} = undef;
1529 }
1530 \$_[0]->[@{[_row_data]}]->[$fields{$k}];
1531 }
1532 ACC
1533 } else {
1534 $accessors .= <<ACC;
1535 sub $k {
1536 \$_[0]->[@{[_row_data]}]->[$fields{$k}];
1537 }
1538 ACC
1539 }
1540 } else {
1541 if (!exists($pk_fields{$k}) && (not ref $table)) {
1542 $accessors .= <<ACC;
1543 sub _$k {
1544 if(\@_ > 1) {
1545 if(not CORE::ref \$_[1]) {
1546 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = \$_[1];
1547 } else {
1548 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = JSON::to_json(\$_[1]);
1549 }
1550 }
1551 if(not CORE::ref \$_[0]->[@{[_row_data]}]->[$fields{$k}]) {
1552 \$_[0]->[@{[_row_updates]}] = {} if not \$_[0]->[@{[_row_updates]}];
1553 \$_[0]->[@{[_row_data]}]->[$fields{$k}] =
1554 DBIx::Struct::JSON->factory(\\\$_[0]->[@{[_row_data]}]->[$fields{$k}], \$_[0]->[@{[_row_updates]}], "$k");
1555 }
1556 \$_[0]->[@{[_row_data]}]->[$fields{$k}]->accessor;
1557 }
1558 sub $k {
1559 if(\@_ > 1) {
1560 if(not CORE::ref \$_[1]) {
1561 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = \$_[1];
1562 } else {
1563 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = JSON::to_json(\$_[1]);
1564 }
1565 \$_[0]->[@{[_row_updates]}]{"$k"} = undef;
1566 }
1567 if(not CORE::ref \$_[0]->[@{[_row_data]}]->[$fields{$k}]) {
1568 \$_[0]->[@{[_row_updates]}] = {} if not \$_[0]->[@{[_row_updates]}];
1569 \$_[0]->[@{[_row_data]}]->[$fields{$k}] =
1570 DBIx::Struct::JSON->factory(\\\$_[0]->[@{[_row_data]}]->[$fields{$k}], \$_[0]->[@{[_row_updates]}], "$k");
1571 }
1572 \$_[0]->[@{[_row_data]}]->[$fields{$k}]->accessor;
1573 }
1574 ACC
1575 } else {
1576 $accessors .= <<ACC;
1577 sub $k {
1578 if(\$_[0]->[@{[_row_data]}]->[$fields{$k}] and not CORE::ref \$_[0]->[@{[_row_data]}]->[$fields{$k}]) {
1579 \$_[0]->[@{[_row_data]}]->[$fields{$k}] = JSON::from_json(\$_[0]->[@{[_row_data]}]->[$fields{$k}]);
1580 }
1581 \$_[0]->[@{[_row_data]}]->[$fields{$k}];
1582 }
1583 ACC
1584 }
1585 }
1586 }
1587 my $package_header = <<PHD;
1588 package ${ncn};
1589 use strict;
1590 use warnings;
1591 use Carp;
1592 use SQL::Abstract;
1593 use JSON;
1594 use Scalar::Util 'blessed';
1595 use vars qw(\$AUTOLOAD);
1596 our \%field_types = ($field_types);
1597 our \%fields = ($fields);
1598 our \%json_fields = ($json_fields);
1599 PHD
1600 if (not ref $table) {
1601 if (%fkfuncs) {
1602 my $fkfuncs = join ",", map {qq{"$_" => \\&${ncn}::$_}} keys %fkfuncs;
1603 $package_header .= <<PHD;
1604 our \%fkfuncs = ($fkfuncs);
1605 PHD
1606 } else {
1607 $package_header .= <<PHD;
1608 our \%fkfuncs = ();
1609 PHD
1610 }
1611 $package_header .= <<PHD;
1612 sub tableName () {"$table"}
1613 PHD
1614 } else {
1615 $package_header .= <<PHD;
1616 sub tableName () {"\\\$query\\\$$ncn"}
1617 PHD
1618 }
1619 my $new = make_object_new($table, $required, $pk_row_data, $pk_returninig);
1620 my $filter_timestamp = make_object_filter_timestamp($timestamps);
1621 my $set = make_object_set($table);
1622 my $data = make_object_data($table);
1623 my $update = make_object_update($table, $pk_where, $pk_row_data);
1624 my $delete = make_object_delete($table, $pk_where, $pk_row_data);
1625 my $fetch = make_object_fetch($table, $pk_where, $pk_row_data);
1626 my $autoload = make_object_autoload_find($table, $pk_where, $pk_row_data);
1627 my $to_json = make_object_to_json($table, \%field_types, \%fields);
1628 my $destroy;
1629  
1630 if (not ref $table) {
1631 $destroy = <<DESTROY;
1632 sub DESTROY {
1633 no warnings 'once';
1634 \$_[0]->update if \$DBIx::Struct::update_on_destroy;
1635 }
1636 DESTROY
1637 } else {
1638 $destroy = '';
1639 }
1640 my $eval_code = join "", $package_header, $select_keys, $new,
1641 $set, $data, $fetch, $autoload, $to_json, $filter_timestamp,
1642 $update, $delete, $destroy, $accessors, $foreign_tables, $references_tables;
1643  
1644 # print $eval_code;
1645 eval $eval_code;
1646 error_message {
1647 result => 'SQLERR',
1648 message => "Unknown error: $@",
1649 } if $@;
1650 if ($interface) {
1651 my $ifuncs = _parse_interface $interface;
1652 no strict 'refs';
1653 for my $f (keys %$ifuncs) {
1654 *{"${ncn}::$f"} = $ifuncs->{$f};
1655 }
1656 }
1657 '' =~ /()/;
1658 return $ncn;
1659 }
1660  
1661 my %cache_complex_query;
1662 my $json_canonical = JSON->new->canonical->convert_blessed;
1663  
1664 sub _cached_complex_query {
1665 my $key = $json_canonical->encode(\@_);
1666 my ($ret, $is_one_column);
1667 if (exists $cache_complex_query{$key}) {
1668 ($ret, $is_one_column) = @{$cache_complex_query{$key}};
1669 } else {
1670 ($ret, $is_one_column) = _build_complex_query(@_);
1671 $cache_complex_query{$key} = [($ret, $is_one_column)];
1672 }
1673 if (wantarray) {
1674 return ($ret, $is_one_column);
1675 } else {
1676 return $ret;
1677 }
1678 }
1679  
1680 sub _table_name() {0}
1681 sub _table_alias() {1}
1682 sub _table_join() {2}
1683 sub _table_join_on() {3}
1684  
1685 my $sql_abstract = SQL::Abstract->new;
1686 my $tblnum;
1687  
1688 sub _build_complex_query {
1689 my ($table, $query_bind, $where) = @_;
1690 return $table if not ref $table;
1691 my @from;
1692 my @columns;
1693 my @linked_list = (
1694 ref($table) eq 'ARRAY'
1695 ? @$table
1696 : error_message {
1697 result => 'SQLERR',
1698 message => "Unsupported type of query: " . ref($table)
1699 }
1700 );
1701 my ($conditions, $groupby, $having, $limit, $offset, $orderby);
1702 my $one_column = 0;
1703 my $distinct = 0;
1704 my $count = 0;
1705 my $all = 0;
1706  
1707 for (my $i = 0; $i < @linked_list; ++$i) {
1708 my $le = $linked_list[$i];
1709 if ('ARRAY' eq ref $le) {
1710 my $subfrom = _build_complex_query($le, $query_bind);
1711 my $ta = "t$tblnum";
1712 ++$tblnum;
1713 push @from, ["($subfrom)", $ta];
1714 next;
1715 }
1716 if (substr($le, 0, 1) ne '-') {
1717 my ($tn, $ta) = split ' ', $le;
1718 $ta = $tn if not $ta;
1719 my $ncn = make_name($tn);
1720 $ncn = _schema_name($ncn);
1721 error_message {
1722 result => 'SQLERR',
1723 message => "Unknown table $tn"
1724 }
1725 unless setup_row($tn, $ncn);
1726 push @from, [$tn, $ta];
1727 } else {
1728 my $cmd = substr($le, 1);
1729 if ($cmd eq 'left') {
1730 $from[-1][_table_join] = 'left join';
1731 } elsif ($cmd eq 'right') {
1732 $from[-1][_table_join] = 'right join';
1733 } elsif ($cmd eq 'join') {
1734 $from[-1][_table_join] = 'join';
1735 } elsif ($cmd eq 'on') {
1736 $from[-1][_table_join_on] = ["on", $linked_list[++$i]];
1737 } elsif ($cmd eq 'using') {
1738 $from[-1][_table_join_on] = ["using", $linked_list[++$i]];
1739 } elsif ($cmd eq 'as') {
1740 $from[-1][_table_alias] = $linked_list[++$i];
1741 } elsif ($cmd eq 'where') {
1742 $conditions = $linked_list[++$i];
1743 } elsif ($cmd eq 'group_by') {
1744 $groupby = $linked_list[++$i];
1745 } elsif ($cmd eq 'order_by') {
1746 $orderby = $linked_list[++$i];
1747 } elsif ($cmd eq 'having') {
1748 $having = $linked_list[++$i];
1749 } elsif ($cmd eq 'limit') {
1750 $limit = 0 + $linked_list[++$i];
1751 } elsif ($cmd eq 'offset') {
1752 $offset = 0 + $linked_list[++$i];
1753 } elsif ($cmd eq 'columns'
1754 || $cmd eq 'column'
1755 || $cmd eq 'distinct'
1756 || $cmd eq 'count'
1757 || $cmd eq 'all')
1758 {
1759 if ($cmd eq 'all') {
1760 $all = 1;
1761 }
1762 if ($cmd eq 'distinct') {
1763 $distinct = 1;
1764 }
1765 if ($cmd eq 'count') {
1766 $count = 1;
1767 }
1768 if ($i + 1 < @linked_list && substr($linked_list[$i + 1], 0, 1) ne '-') {
1769 my $cols = $linked_list[++$i];
1770 if ($cols && $cols !~ /^\d|^true$/) {
1771 if ('ARRAY' eq ref($cols)) {
1772 push @columns, @$cols;
1773 } else {
1774 push @columns, $cols;
1775 }
1776 } elsif (($cols =~ /^\d+$/ && $cols == 0) || $cols eq '') {
1777 $distinct = 0 if $cmd eq 'distinct';
1778 }
1779 }
1780 if ($cmd eq 'column') {
1781 ++$one_column;
1782 } else {
1783 $one_column += 2;
1784 }
1785  
1786 } else {
1787 error_message {
1788 result => 'SQLERR',
1789 message => "Unknown directive $le"
1790 };
1791 }
1792 }
1793 }
1794 error_message {
1795 result => 'SQLERR',
1796 message => "No table to build query on"
1797 } if !@from;
1798 for (my $idx = 1; $idx < @from; ++$idx) {
1799 next if $from[$idx][_table_join_on] or not $from[$idx - 1][_table_join];
1800 next if substr($from[$idx][_table_name], 0, 1) eq "(";
1801 my $cta = $from[$idx][_table_alias];
1802 my $cto = make_name($from[$idx][_table_name]);
1803 my $ucct = ucfirst $from[$idx][_table_name];
1804 my @join;
1805 for (my $i = $idx - 1; $i >= 0; --$i) {
1806 next if not $from[$i][_table_join];
1807 my $ptn = $from[$i][_table_name];
1808 next if substr($ptn, 0, 1) eq "(";
1809 my $ucfptn = ucfirst $ptn;
1810 if ($cto->can("foreignKey$ucfptn")) {
1811 my $fkfn = "foreignKey$ucfptn";
1812 my ($ctf, $ptk) = $cto->$fkfn;
1813 push @join, "$cta.$ctf = " . $from[$i][_table_alias] . ".$ptk";
1814 } else {
1815 my $ptno = make_name($ptn);
1816 if ($ptno->can("foreignKey$ucct")) {
1817 my $fkfn = "foreignKey$ucct";
1818 my ($ptf, $ctk) = $ptno->$fkfn;
1819 push @join, "$cta.$ctk = " . $from[$i][_table_alias] . ".$ptf";
1820 }
1821 }
1822 }
1823 $from[$idx][_table_join_on] = ["on", join(" and ", @join)];
1824 }
1825 my $from = '';
1826 @columns = ('*') if not @columns;
1827 @columns = map {('SCALAR' eq ref) ? DBIx::Struct::connect->dbh->quote_identifier($$_) : $_} @columns;
1828 my $joined = 0;
1829 for (my $idx = 0; $idx < @from; ++$idx) {
1830 if (not $joined) {
1831 $from .= " " . $from[$idx][_table_name];
1832 $from .= " " . $from[$idx][_table_alias] if $from[$idx][_table_alias] ne $from[$idx][_table_name];
1833 }
1834 if ($from[$idx][_table_join]) {
1835 my $nt = $from[$idx + 1];
1836 $from .= " " . $from[$idx][_table_join];
1837 $from .= " " . $nt->[_table_name];
1838 $from .= " " . $nt->[_table_alias] if $nt->[_table_alias] ne $nt->[_table_name];
1839 my $using_on = $nt->[_table_join_on][0];
1840 if ($using_on eq 'on' and ref $nt->[_table_join_on][1]) {
1841 my ($on_where, @on_bind) = $sql_abstract->where($nt->[_table_join_on][1]);
1842 $on_where =~ s/WHERE //;
1843 push @$query_bind, @on_bind;
1844 $from .= " $using_on(" . $on_where . ")";
1845 } else {
1846 $from .= " $using_on(" . $nt->[_table_join_on][1] . ")";
1847 }
1848 $joined = 1;
1849 } else {
1850 $from .= "," if $idx != $#from;
1851 $joined = 0;
1852 }
1853 }
1854 my $what = join(", ", @columns);
1855 if ($count) {
1856 $one_column = 1;
1857 if ($distinct) {
1858 $what = $from[0][_table_alias] . ".*" if $what eq '*';
1859 $what = "count(distinct $what)";
1860 } elsif ($all) {
1861 $what = $from[0][_table_alias] . ".*" if $what eq '*';
1862 $what = "count(all $what)";
1863 } else {
1864 $what = "count(*)";
1865 }
1866 } else {
1867 if ($distinct) {
1868 $what = "distinct $what";
1869 }
1870 }
1871 my $ret = "select $what from" . $from;
1872 if (not defined $where) {
1873 my $sql_grp = _parse_groupby($groupby);
1874 my $having_bind = [];
1875 if ($sql_grp && defined $having) {
1876 my $sql_having;
1877 ($sql_having, $having_bind) = _parse_having($having);
1878 $sql_grp .= " $sql_having";
1879 }
1880 if ($conditions) {
1881 my @where_bind;
1882 ($where, @where_bind) = $sql_abstract->where($conditions);
1883 push @$query_bind, @where_bind;
1884 } else {
1885 $where = '';
1886 }
1887 if (defined $sql_grp) {
1888 $where .= " $sql_grp";
1889 push @$query_bind, @$having_bind;
1890 }
1891 $where .= " limit $limit" if defined $limit;
1892 $where .= " offset $offset" if $offset;
1893 }
1894 $ret .= " $where" if $where;
1895 if (wantarray) {
1896 return ($ret, $one_column == 1);
1897 } else {
1898 return $ret;
1899 }
1900 }
1901  
1902 sub _parse_groupby {
1903 my $groupby = $_[0];
1904 my $sql_grp;
1905 if (defined $groupby) {
1906 $sql_grp = "GROUP BY ";
1907 my @groupby =
1908 map {/^\d+$/ ? $_ : /^[a-z][\w ]*$/i ? "\"$_\"" : "$_"} (ref($groupby) ? @$groupby : ($groupby));
1909 $sql_grp .= join(", ", @groupby);
1910 }
1911 $sql_grp;
1912 }
1913  
1914 sub _parse_having {
1915 my $having = $_[0];
1916 my $sql_having;
1917 my @having_bind;
1918 if (defined $having) {
1919 ($sql_having, @having_bind) = $sql_abstract->where($having);
1920 $sql_having =~ s/\bWHERE\b/HAVING/;
1921 }
1922 ($sql_having, \@having_bind);
1923 }
1924  
1925 sub execute {
1926 my ($groupby, $having, $up_conditions, $up_order, $up_limit, $up_offset, $up_interface, $sql, $dry_run);
1927 my $distinct = '';
1928 for (my $i = 2; $i < @_; ++$i) {
1929 next unless defined $_[$i] and not ref $_[$i];
1930 if ($_[$i] eq '-group_by') {
1931 (undef, $groupby) = splice @_, $i, 2;
1932 --$i;
1933 } elsif ($_[$i] eq '-distinct') {
1934 $distinct = ' distinct';
1935 splice @_, $i, 1;
1936 --$i;
1937 } elsif ($_[$i] eq '-having') {
1938 (undef, $having) = splice @_, $i, 2;
1939 --$i;
1940 } elsif ($_[$i] eq '-order_by') {
1941 (undef, $up_order) = splice @_, $i, 2;
1942 --$i;
1943 } elsif ($_[$i] eq '-where') {
1944 (undef, $up_conditions) = splice @_, $i, 2;
1945 --$i;
1946 } elsif ($_[$i] eq '-limit') {
1947 (undef, $up_limit) = splice @_, $i, 2;
1948 --$i;
1949 } elsif ($_[$i] eq '-interface') {
1950 (undef, $up_interface) = splice @_, $i, 2;
1951 --$i;
1952 } elsif ($_[$i] eq '-offset') {
1953 (undef, $up_offset) = splice @_, $i, 2;
1954 --$i;
1955 } elsif ($_[$i] eq '-sql') {
1956 (undef, $sql) = splice @_, $i, 2;
1957 --$i;
1958 } elsif ($_[$i] eq '-dry_run') {
1959 (undef, $dry_run) = splice @_, $i, 2;
1960 --$i;
1961 } elsif (substr($_[$i], 0, 1) eq '-') {
1962 error_message {
1963 result => 'SQLERR',
1964 message => "Unknown directive $_[$i]"
1965 };
1966 }
1967 }
1968 $tblnum = 1;
1969 my $sql_grp = _parse_groupby($groupby);
1970 my $having_bind = [];
1971 if ($sql_grp && defined $having) {
1972 my $sql_having;
1973 ($sql_having, $having_bind) = _parse_having($having);
1974 $sql_grp .= " $sql_having";
1975 }
1976 my ($code, $table, $conditions, $order, $limit, $offset) = @_;
1977 my $have_conditions = @_ > 2;
1978 $conditions //= $up_conditions;
1979 $order //= $up_order;
1980 $limit //= $up_limit;
1981 $offset //= $up_offset;
1982 my $where;
1983 my $need_where = 0;
1984 my @where_bind;
1985 my $simple_table = (not ref $table and index($table, " ") == -1);
1986 my $ncn;
1987  
1988 if ($simple_table) {
1989 $ncn = make_name($table);
1990 $ncn = _schema_name($ncn);
1991 setup_row($table, $ncn);
1992 if ($have_conditions and not ref $conditions) {
1993 my $id = ($ncn->selectKeys())[0]
1994 or error_message {
1995 result => 'SQLERR',
1996 message => "unknown primary key",
1997 query => "select * from $table",
1998 };
1999 if (defined $conditions) {
2000 $where = "where $id = ?";
2001 @where_bind = ($conditions);
2002 } else {
2003 $where = "where $id is null";
2004 }
2005 } else {
2006 $need_where = 1;
2007 }
2008 } else {
2009 $need_where = 1;
2010 }
2011 if ($need_where) {
2012 ($where, @where_bind) = $sql_abstract->where($conditions);
2013 }
2014 if (defined $sql_grp) {
2015 $where .= " $sql_grp";
2016 push @where_bind, @$having_bind;
2017 }
2018 if ($order) {
2019 my ($order_sql, @order_bind) = $sql_abstract->where(undef, $order);
2020 $where .= " $order_sql";
2021 push @where_bind, @order_bind;
2022 }
2023 if (defined($limit)) {
2024 $limit += 0;
2025 $where .= " limit $limit";
2026 }
2027 if (defined($offset)) {
2028 $offset += 0;
2029 $where .= " offset $offset" if $offset;
2030 }
2031 my $query;
2032 my @query_bind;
2033 my $one_column = 0;
2034 if ($simple_table) {
2035 $query = qq{select$distinct * from $table $where};
2036 } else {
2037 if (not ref $table) {
2038 $query = "$table $where";
2039 } else {
2040 ($query, $one_column) = _cached_complex_query($table, \@query_bind, $where);
2041 }
2042 $ncn = make_name($query);
2043 }
2044 if ($sql) {
2045 if ('CODE' eq ref $sql) {
2046 $sql->($query, \@where_bind);
2047 } elsif ('SCALAR' eq ref $sql) {
2048 $$sql = $query;
2049 }
2050 }
2051 return if $dry_run;
2052 '' =~ /()/;
2053 my $sth;
2054 return DBIx::Struct::connect->run(
2055 sub {
2056 $sth = $_->prepare($query)
2057 or error_message {
2058 result => 'SQLERR',
2059 message => $_->errstr,
2060 query => $query,
2061 };
2062 $sth->execute(@query_bind, @where_bind)
2063 or error_message {
2064 result => 'SQLERR',
2065 message => $_->errstr,
2066 query => $query,
2067 where_bind => Dumper(\@where_bind),
2068 query_bind => Dumper(\@query_bind),
2069 conditions => Dumper($conditions),
2070 };
2071 setup_row($sth, $ncn, $up_interface);
2072 return $code->($sth, $ncn, $one_column);
2073 }
2074 );
2075 }
2076  
2077 sub one_row {
2078 return execute(
2079 sub {
2080 my ($sth, $ncn, $one_column) = @_;
2081 my $data = $sth->fetchrow_arrayref;
2082 $sth->finish;
2083 return if not $data;
2084 if ($one_column) {
2085 #<<<
2086 # json type is not working yet here
2087 # no strict 'refs';
2088 # my @f = %{$ncn . "::field_types"};
2089 # if ($f[1] eq 'json') {
2090 # return (defined($data->[0]) ? from_json($data->[0]) : undef);
2091 # } else {
2092 return $data->[0];
2093 #>>> }
2094 }
2095 return $ncn->new([@$data]);
2096 },
2097 @_
2098 );
2099 }
2100  
2101 sub all_rows {
2102 my $mapfunc;
2103 for (my $i = 0; $i < @_; ++$i) {
2104 if (ref($_[$i]) eq 'CODE') {
2105 $mapfunc = splice @_, $i, 1;
2106 last;
2107 }
2108 }
2109 return execute(
2110 sub {
2111 my ($sth, $ncn, $one_column) = @_;
2112 my @rows;
2113 my $row;
2114 if ($mapfunc) {
2115 while ($row = $sth->fetch) {
2116 local $_ = $ncn->new([@$row]);
2117 push @rows, $mapfunc->();
2118 }
2119 } else {
2120 if ($one_column) {
2121 #<<<
2122 # json type is not working yet here
2123 # no strict 'refs';
2124 # my @f = %{$ncn . "::field_types"};
2125 # if ($f[1] eq 'json') {
2126 # push @rows, (defined($row->[0]) ? from_json($row->[0]) : undef) while ($row = $sth->fetch);
2127 # } else {
2128 push @rows, $row->[0] while ($row = $sth->fetch);
2129 # }
2130 #>>>
2131 } else {
2132 push @rows, $ncn->new([@$row]) while ($row = $sth->fetch);
2133 }
2134 }
2135 return \@rows;
2136 },
2137 @_
2138 );
2139 }
2140  
2141 sub for_rows {
2142 my $itemfunc;
2143 for (my $i = 0; $i < @_; ++$i) {
2144 if (ref($_[$i]) eq 'CODE') {
2145 $itemfunc = splice @_, $i, 1;
2146 last;
2147 }
2148 }
2149 error_message {
2150 result => 'SQLERR',
2151 message => "Item function is required",
2152 query => "(not parsed)",
2153 where_bind => "(not parsed)",
2154 query_bind => "(not parsed)",
2155 conditions => "(not parsed)",
2156 }
2157 if not $itemfunc;
2158 return execute(
2159 sub {
2160 my ($sth, $ncn) = @_;
2161 my $rows = 0;
2162 my $row;
2163 my $dbh = $_;
2164 local $dbh->{mysql_use_result} = 1 if $connector_driver eq 'mysql';
2165 local $_;
2166 while ($row = $sth->fetch) {
2167 ++$rows;
2168 $_ = $ncn->new([@$row]);
2169 last if not $itemfunc->();
2170 }
2171 return $rows;
2172 },
2173 @_
2174 );
2175 }
2176  
2177 sub new_row {
2178 my ($table, @data) = @_;
2179 my $simple_table = (index($table, " ") == -1);
2180 error_message {
2181 result => 'SQLERR',
2182 message => "insert row can't work for queries"
2183 }
2184 unless $simple_table;
2185 my $ncn = make_name($table);
2186 $ncn = _schema_name($ncn);
2187 $ncn = setup_row($table, $ncn);
2188 return $ncn->new(@data);
2189 }
2190  
2191 1;