GX/SQL/Builder.pm untested

filter: Statementstmt 0.0% 0 / 307 Branchbran 0.0% 0 / 170 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 563 CC 86 SCAR 46.2
CC
86
Cov
0%
_process_where_expression
306.0
_bind_column_values
132.0
_initialize_where_op_handlers
132.0
CRAP 101.1
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/SQL/Builder.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::SQL::Builder;
7  
8 use GX::Exception;
9 use GX::SQL::Types;
10  
11 use DBI ();
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Class setup
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 use GX::Class::Object;
19  
20 has 'bind_type_map' => (
21 isa => 'Hash',
22 initialize => 1,
23 initializer => '_initialize_bind_type_map',
24 accessors => {
25 '_bind_type_map' => { type => 'get_reference' }
26 }
27 );
28  
29 has 'quote_char' => (
30 isa => 'String',
31 initialize => 1,
32 initializer => '_initialize_quote_char',
33 accessor => { type => 'get' }
34 );
35  
36 has 'where_op_handlers' => (
37 isa => 'Hash',
38 initialize => 1,
39 initializer => '_initialize_where_op_handlers',
40 accessors => {
41 '_where_op_handler' => { type => 'get_value' }
42 }
43 );
44  
45 build;
46  
47  
48 # ----------------------------------------------------------------------------------------------------------------------
49 # Public methods
50 # ----------------------------------------------------------------------------------------------------------------------
51  
52 sub count {
53  
54 my $self = shift;
55 my %args = @_;
56  
57 if ( ! defined $args{'table'} ) {
58 complain "Missing argument (\"table\")";
59 }
60  
61 my $sql;
62 my $bind;
63  
64 eval {
65  
66 $bind = [];
67 $sql = 'SELECT COUNT(*) FROM ' . $self->_quote_table( $args{'table'} );
68  
69 if ( defined $args{'where'} ) {
70 $self->_build_where_clause( $args{'where'}, \$sql, $bind, $args{'bind'} );
71 }
72  
73 };
74  
75 if ( $@ ) {
76 complain $@;
77 }
78  
79 return wantarray ? ( $sql, $bind ) : $sql;
80  
81 }
82  
83 sub delete {
84  
85 my $self = shift;
86 my %args = @_;
87  
88 if ( ! defined $args{'table'} ) {
89 complain "Missing argument (\"table\")";
90 }
91  
92 my $sql;
93 my $bind;
94  
95 eval {
96  
97 $bind = [];
98 $sql = 'DELETE FROM ' . $self->_quote_table( $args{'table'} );
99  
100 if ( defined $args{'where'} ) {
101 $self->_build_where_clause( $args{'where'}, \$sql, $bind, $args{'bind'} );
102 }
103  
104 };
105  
106 if ( $@ ) {
107 complain $@;
108 }
109  
110 return wantarray ? ( $sql, $bind ) : $sql;
111  
112 }
113  
114 sub insert {
115  
116 my $self = shift;
117 my %args = @_;
118  
119 if ( ! defined $args{'table'} ) {
120 complain "Missing argument (\"table\")";
121 }
122  
123 if ( ! defined $args{'columns'} ) {
124 complain "Missing argument (\"columns\")";
125 }
126  
127 if ( ref $args{'columns'} ne 'ARRAY' ) {
128 complain "Invalid argument (\"columns\")";
129 }
130  
131 my $sql;
132 my $bind;
133  
134 eval {
135  
136 my $sql_columns;
137 my $sql_values;
138  
139 ( $bind, $sql_columns, $sql_values ) = $self->_bind_column_values(
140 $args{'columns'},
141 $args{'values'},
142 undef,
143 $args{'bind'}
144 );
145  
146 $sql = 'INSERT INTO ' . $self->_quote_table( $args{'table'} )
147 . ' ( ' . join( ', ', @$sql_columns ) . ' ) '
148 . 'VALUES ( ' . join( ', ', @$sql_values ) . ' )';
149  
150 };
151  
152 if ( $@ ) {
153 complain $@;
154 }
155  
156 return wantarray ? ( $sql, $bind ) : $sql;
157  
158 }
159  
160 sub select {
161  
162 my $self = shift;
163 my %args = @_;
164  
165 if ( ! defined $args{'table'} ) {
166 complain "Missing argument (\"table\")";
167 }
168  
169 my $sql;
170 my $bind;
171  
172 eval {
173  
174 $bind = [];
175 $sql = 'SELECT ';
176  
177 if ( $args{'distinct'} ) {
178 $sql .= 'DISTINCT ';
179 }
180  
181 if ( defined $args{'columns'} ) {
182  
183 if ( ref $args{'columns'} ne 'ARRAY' ) {
184 throw "Invalid argument (\"columns\")";
185 }
186  
187 $sql .= join( ', ', map { $self->_quote_column( $_ ) } @{$args{'columns'}} );
188  
189 }
190 else {
191 $sql .= '*';
192 }
193  
194 $sql .= ' FROM ' . $self->_quote_table( $args{'table'} );
195  
196 if ( defined $args{'where'} ) {
197 $self->_build_where_clause(
198 $args{'where'},
199 \$sql,
200 $bind,
201 $args{'bind'}
202 );
203 }
204  
205 if ( defined $args{'order'} ) {
206 $self->_build_order_clause(
207 $args{'order'},
208 \$sql,
209 $bind,
210 $args{'bind'}
211 );
212 }
213  
214 if ( defined $args{'limit'} ) {
215 $self->_build_limit_clause(
216 $args{'limit'},
217 $args{'offset'},
218 \$sql,
219 $bind,
220 $args{'bind'}
221 );
222 }
223  
224 };
225  
226 if ( $@ ) {
227 complain $@;
228 }
229  
230 return wantarray ? ( $sql, $bind ) : $sql;
231  
232 }
233  
234 sub update {
235  
236 my $self = shift;
237 my %args = @_;
238  
239 if ( ! defined $args{'table'} ) {
240 complain "Missing argument (\"table\")";
241 }
242  
243 if ( ! defined $args{'columns'} ) {
244 complain "Missing argument (\"columns\")";
245 }
246  
247 if ( ref $args{'columns'} ne 'ARRAY' ) {
248 complain "Invalid argument (\"columns\")";
249 }
250  
251 my $sql;
252 my $bind;
253  
254 eval {
255  
256 my $sql_columns;
257 my $sql_values;
258  
259 ( $bind, $sql_columns, $sql_values ) = $self->_bind_column_values(
260 $args{'columns'},
261 $args{'values'},
262 undef,
263 $args{'bind'}
264 );
265  
266 $sql = 'UPDATE ' . $self->_quote_table( $args{'table'} ) . ' SET ';
267  
268 my @sql_set;
269  
270 for ( my $i = 0; $i < @$sql_columns; $i++ ) {
271 push @sql_set, $sql_columns->[$i] . ' = ' . $sql_values->[$i];
272 }
273  
274 $sql .= join( ', ', @sql_set );
275  
276 if ( defined $args{'where'} ) {
277 $self->_build_where_clause( $args{'where'}, \$sql, $bind, $args{'bind'} );
278 }
279  
280 };
281  
282 if ( $@ ) {
283 complain $@;
284 }
285  
286 return wantarray ? ( $sql, $bind ) : $sql;
287  
288 }
289  
290 sub where {
291  
292 my $self = shift;
293 my $where = shift;
294 my %args = @_;
295  
296 if ( ! defined $where ) {
297 complain "Missing argument";
298 }
299  
300 if ( ref $where ne 'ARRAY' ) {
301 complain "Invalid argument";
302 }
303  
304 my $sql;
305 my $bind;
306  
307 eval {
308  
309 $bind = [];
310 $sql = 'WHERE ';
311  
312 $self->_process_where_expression( 0, $where, \$sql, $bind, $args{'bind'} );
313  
314 };
315  
316 if ( $@ ) {
317 complain $@;
318 }
319  
320 return wantarray ? ( $sql, $bind ) : $sql;
321  
322 }
323  
324  
325 # ----------------------------------------------------------------------------------------------------------------------
326 # Private methods
327 # ----------------------------------------------------------------------------------------------------------------------
328  
329 sub _bind_column_value {
330  
331 my ( $self, $column, $value, $bind, $bind_attrs ) = @_;
332  
333 $bind ||= [];
334  
335 if ( $bind_attrs ) {
336  
337 if ( ref $bind_attrs ne 'HASH' ) {
338 throw "Invalid argument (\"bind\")";
339 }
340  
341 if ( exists $bind_attrs->{$column} ) {
342 push @$bind, [ @$bind + 1, $value, $self->_convert_bind_type( $bind_attrs->{$column} ) ];
343 }
344 else {
345 push @$bind, [ @$bind + 1, $value ];
346 }
347  
348 }
349 else {
350 push @$bind, $value;
351 }
352  
353 if ( wantarray ) {
354 return $bind, $self->_quote_column( $column ), '?';
355 }
356 else {
357 return $bind;
358 }
359  
360 }
361  
362 sub _bind_column_values {
363  
364 my ( $self, $columns, $values, $bind, $bind_attrs ) = @_;
365  
366 $bind ||= [];
367  
368 if ( defined $values ) {
369  
370 if ( ref $values ne 'ARRAY' ) {
371 throw "Invalid argument (\"values\")";
372 }
373  
374 if ( @$columns != @$values ) {
375 throw "Invalid number of column values";
376 }
377  
378 if ( defined $bind_attrs ) {
379  
380 if ( ref $bind_attrs ne 'HASH' ) {
381 throw "Invalid argument (\"bind\")";
382 }
383  
384 my $n = @$bind + 1;
385  
386 for ( my $i = 0; $i < @$columns; $i++ ) {
387  
388 my $column = $columns->[$i];
389 my $value = $values->[$i];
390  
391 if ( exists $bind_attrs->{$column} ) {
392 push @$bind, [ $n, $value, $self->_convert_bind_type( $bind_attrs->{$column} ) ];
393 }
394 else {
395 push @$bind, [ $n, $value ];
396 }
397  
398 $n++;
399  
400 }
401  
402 }
403 else {
404 push @$bind, @$values;
405 }
406  
407  
408 }
409 else {
410  
411 if ( defined $bind_attrs ) {
412  
413 if ( ref $bind_attrs ne 'HASH' ) {
414 throw "Invalid argument (\"bind\")";
415 }
416  
417 my $n = @$bind + 1;
418  
419 for my $column ( @$columns ) {
420  
421 if ( exists $bind_attrs->{$column} ) {
422 push @$bind, [ $n, undef, $self->_convert_bind_type( $bind_attrs->{$column} ) ];
423 }
424 else {
425 push @$bind, [ $n, undef ];
426 }
427  
428 $n++;
429  
430 }
431  
432 }
433 else {
434 push @$bind, map { undef } @$columns;
435 }
436  
437 }
438  
439 if ( wantarray ) {
440 return $bind, [ map { $self->_quote_column( $_ ) } @$columns ], [ map { '?' } @$columns ];
441 }
442 else {
443 return $bind;
444 }
445  
446 }
447  
448 sub _build_limit_clause {
449  
450 throw "Invalid argument (\"limit\" is not supported by " . ref( $_[0] ) . ")";
451  
452 }
453  
454 sub _build_order_clause {
455  
456 my ( $self, $order, $sql, $bind, $bind_attrs ) = @_;
457  
458 $$sql .= ' ORDER BY ';
459  
460 if ( ! ref $order ) {
461 $$sql .= $self->_quote_column( $order );
462 }
463 elsif ( ref $order eq 'ARRAY' ) {
464  
465 $$sql .= join(
466 ', ',
467 map {
468 ref( $_ )
469 ? $self->_quote_column( $_->[0] ) . ' ' . $_->[1]
470 : $self->_quote_column( $_ )
471 } @$order
472 );
473  
474 }
475  
476 elsif ( ref $order eq 'SCALAR' ) {
477 $$sql .= $$order;
478 }
479 else {
480 throw "Invalid argument (\"order\")";
481 }
482  
483 return;
484  
485 }
486  
487 sub _build_where_clause {
488  
489 my ( $self, $where, $sql, $bind, $bind_attrs ) = @_;
490  
491 if ( ref $where eq 'ARRAY' ) {
492 $$sql .= ' WHERE ';
493 $self->_process_where_expression( 0, $where, $sql, $bind, $bind_attrs );
494 }
495 elsif ( ref $where eq 'SCALAR' && defined $$where ) {
496 $$sql .= ' WHERE ' . $$where;
497 }
498 elsif ( ! ref $where && defined $where ) {
499 $$sql .= ' WHERE ' . $where;
500 }
501 else {
502 throw "Invalid argument (\"where\")";
503 }
504  
505 return;
506  
507 }
508  
509 sub _convert_bind_type {
510  
511 my $self = shift;
512 my $type = shift;
513  
514 return undef if ! defined $type;
515  
516 return $type if ref $type;
517  
518 my $dbi_type = $self->{'bind_type_map'}{$type};
519  
520 if ( ! defined $dbi_type ) {
521 throw "Unsupported bind type \"$type\"";
522 }
523  
524 return $dbi_type;
525  
526 }
527  
528 sub _process_where_expression {
529  
530 my ( $self, $depth, $block, $sql, $bind, $bind_attrs ) = @_;
531  
532 $$sql .= '( ' if $depth;
533  
534 my @tokens = @$block;
535  
536 while ( @tokens ) {
537  
538 my $token = shift @tokens;
539  
540 if ( ! defined $token ) {
541 throw "Syntax error in \"where\" expression";
542 }
543  
544 if ( ! ref $token ) {
545  
546 # $column => ...
547  
548 my $column = $token;
549  
550 $token = shift @tokens;
551  
552 if ( ! ref $token ) {
553  
554 # $column => $value
555  
556 my $handler = $self->_where_op_handler( '=' );
557  
558 $handler->( $self, $column, '=', $token, $sql, $bind, $bind_attrs );
559  
560 }
561 elsif ( ref $token eq 'HASH' ) {
562  
563 # $column => { ... }
564  
565 if ( keys %$token == 1 ) {
566  
567 # $column => { $operator => $value }
568  
569 my ( $operator ) = keys %$token;
570  
571 my $handler = $self->_where_op_handler( $operator );
572  
573 if ( ! $handler ) {
574 throw "Unsupported operator \"$operator\" in \"where\" expression";
575 }
576  
577 $handler->( $self, $column, %$token, $sql, $bind, $bind_attrs );
578  
579 }
580 else {
581 throw "Syntax error in \"where\" expression";
582 }
583  
584 }
585 elsif ( ref $token eq 'ARRAY' ) {
586  
587 # $column => \@values
588  
589 my $handler = $self->_where_op_handler( 'IN' );
590  
591 if ( ! $handler ) {
592 throw "Unsupported operator \"IN\" in \"where\" expression";
593 }
594  
595 $handler->( $self, $column, 'IN', $token, $sql, $bind, $bind_attrs );
596  
597 }
598 elsif ( ref $token eq 'SCALAR' ) {
599  
600 # $column => \$sql
601  
602 $$sql .= $self->_quote_column( $column ) . ' ' . $$token;
603  
604 }
605 else {
606 throw "Syntax error in \"where\" expression";
607 }
608  
609 }
610 elsif ( ref $token eq 'ARRAY' ) {
611 $self->_process_where_expression( $depth + 1, $token, $sql, $bind, $bind_attrs );
612 }
613 elsif ( ref $token eq 'SCALAR' ) {
614 $$sql .= $$token;
615 }
616 else {
617 throw "Syntax error in \"where\" expression";
618 }
619  
620 if ( @tokens ) {
621  
622 my $next_token = $tokens[0];
623  
624 if ( ! defined $next_token ) {
625 throw "Syntax error in \"where\" expression";
626 }
627  
628 if ( ! ref $next_token && $next_token =~ /^(?:OR|AND)(?: NOT)?$/ ) {
629 $$sql .= ' ' . $next_token . ' ';
630 shift @tokens;
631 }
632 else {
633 $$sql .= ' AND ';
634 }
635  
636 }
637  
638 }
639  
640 $$sql .= ' )' if $depth;
641  
642 return;
643  
644 }
645  
646 sub _quote_column {
647  
648 return $_[1] if ! length $_[0]->{'quote_char'};
649  
650 return join(
651 '.',
652 map(
653 { $_ ne '*' ? $_[0]->{'quote_char'} . $_ . $_[0]->{'quote_char'} : $_ }
654 split( /\./, $_[1] )
655 )
656 );
657  
658 }
659  
660 sub _quote_table {
661  
662 return $_[1] if ! length $_[0]->{'quote_char'};
663  
664 return $_[0]->{'quote_char'} . $_[1] . $_[0]->{'quote_char'};
665  
666 }
667  
668  
669 # ----------------------------------------------------------------------------------------------------------------------
670 # Attribute initializers
671 # ----------------------------------------------------------------------------------------------------------------------
672  
673 sub _initialize_bind_type_map {
674  
675 return {
676 GX::SQL::Types::BIGINT => DBI::SQL_BIGINT,
677 GX::SQL::Types::BINARY => DBI::SQL_BINARY,
678 GX::SQL::Types::BIT => DBI::SQL_BIT,
679 GX::SQL::Types::BLOB => DBI::SQL_BLOB,
680 GX::SQL::Types::BOOLEAN => DBI::SQL_BOOLEAN,
681 GX::SQL::Types::CHAR => DBI::SQL_CHAR,
682 GX::SQL::Types::DATE => DBI::SQL_DATE,
683 GX::SQL::Types::DATETIME => DBI::SQL_DATETIME,
684 GX::SQL::Types::DECIMAL => DBI::SQL_DECIMAL,
685 GX::SQL::Types::DOUBLE => DBI::SQL_DOUBLE,
686 GX::SQL::Types::FLOAT => DBI::SQL_FLOAT,
687 GX::SQL::Types::INTEGER => DBI::SQL_INTEGER,
688 GX::SQL::Types::LONGVARCHAR => DBI::SQL_LONGVARCHAR,
689 GX::SQL::Types::NUMERIC => DBI::SQL_NUMERIC,
690 GX::SQL::Types::REAL => DBI::SQL_REAL,
691 GX::SQL::Types::SMALLINT => DBI::SQL_SMALLINT,
692 GX::SQL::Types::TIME => DBI::SQL_TIME,
693 GX::SQL::Types::TIMESTAMP => DBI::SQL_TIMESTAMP,
694 GX::SQL::Types::TINYINT => DBI::SQL_TINYINT,
695 GX::SQL::Types::VARCHAR => DBI::SQL_VARCHAR
696 };
697  
698 }
699  
700 sub _initialize_quote_char {
701  
702 return '"';
703  
704 }
705  
706 sub _initialize_where_op_handlers {
707  
708 my $self = shift;
709  
710 my %handlers;
711  
712 {
713  
714 my $handler = sub {
715  
716 my ( $self, $column, $operator, $value, $sql, $bind, $bind_attrs ) = @_;
717  
718 if ( ref $value ) {
719  
720 if ( ref $value eq 'SCALAR' && defined $$value ) {
721 $$sql .= $self->_quote_column( $column ) . ' ' . $operator . ' ' . $$value;
722 }
723 else {
724 throw "Syntax error in \"where\" expression";
725 }
726  
727 }
728 elsif ( defined $value ) {
729  
730 $$sql .= $self->_quote_column( $column ) . ' ' . $operator . ' ?';
731  
732 $self->_bind_column_value( $column, $value, $bind, $bind_attrs );
733  
734 }
735 else {
736  
737 if ( $operator eq '=' ) {
738 $operator = 'IS';
739 }
740 elsif ( $operator eq '!=' ) {
741 $operator = 'IS NOT';
742 }
743  
744 $$sql .= $self->_quote_column( $column ) . ' ' . $operator . ' NULL';
745  
746 }
747  
748 return;
749  
750 };
751  
752 for my $operator ( '=', 'IS', '!=', 'IS NOT', '<', '<=', '>', '>=' ) {
753 $handlers{$operator} ||= $handler;
754 }
755  
756 }
757  
758 {
759  
760 $handlers{'IN'} ||= sub {
761  
762 my ( $self, $column, $operator, $value, $sql, $bind, $bind_attrs ) = @_;
763  
764 if ( ref $value ne 'ARRAY' ) {
765 throw "Syntax error in \"where\" expression";
766 }
767  
768 $$sql .= $self->_quote_column( $column ) . ' IN ( '
769 . join( ', ', map { defined( $_ ) ? '?' : 'NULL' } @$value )
770 . ' )';
771  
772 for ( grep { defined } @$value ) {
773 $self->_bind_column_value( $column, $_, $bind, $bind_attrs );
774 }
775  
776 return;
777  
778 };
779  
780 }
781  
782 {
783  
784 $handlers{'BETWEEN'} ||= sub {
785  
786 my ( $self, $column, $operator, $value, $sql, $bind, $bind_attrs ) = @_;
787  
788 if ( ref $value ne 'ARRAY' || @$value != 2 ) {
789 throw "Syntax error in \"where\" expression";
790 }
791  
792 $$sql .= $self->_quote_column( $column ) . ' BETWEEN '
793 . ( defined $value->[0] ? '?' : 'NULL' )
794 . ' AND '
795 . ( defined $value->[1] ? '?' : 'NULL' );
796  
797 for ( grep { defined } @$value ) {
798 $self->_bind_column_value( $column, $_, $bind, $bind_attrs );
799 }
800  
801 return;
802  
803 };
804  
805 }
806  
807 return \%handlers;
808  
809 }
810  
811  
812 1;
813  
814 __END__