Showing 1 changed files with 49 additions and 30 deletions
+49 -30
lib/DBIx/Custom.pm
... ...
@@ -262,12 +262,17 @@ sub delete {
262 262
     croak qq{"where" must be specified}
263 263
       if $swhere eq '' && !$allow_delete_all;
264 264
 
265
-    # Source of SQL
266
-    my $source = "delete from $table $swhere";
267
-    $source .= " $append" if $append;
265
+    # SQL stack
266
+    my @sql;
267
+
268
+    # Delete
269
+    push @sql, "delete from $table $swhere";
270
+    push @sql, $append if $append;
271
+    
272
+    my $sql = join(' ', @sql);
268 273
     
269 274
     # Create query
270
-    my $query = $self->create_query($source);
275
+    my $query = $self->create_query($sql);
271 276
     return $query if $args{query};
272 277
     
273 278
     # Execute query
... ...
@@ -389,13 +394,18 @@ sub insert {
389 394
         push @columns, $column;
390 395
     }
391 396
     
397
+    # SQL stack
398
+    my @sql;
399
+    
400
+    # Insert
401
+    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
402
+    push @sql, $append if $append;
403
+    
392 404
     # SQL
393
-    my $source = "insert into $table {insert_param "
394
-               . join(' ', @columns) . '}';
395
-    $source .= " $append" if $append;
405
+    my $sql = join (' ', @sql);
396 406
     
397 407
     # Create query
398
-    my $query = $self->create_query($source);
408
+    my $query = $self->create_query($sql);
399 409
     return $query if $args{query};
400 410
     
401 411
     # Execute query
... ...
@@ -493,26 +503,26 @@ sub select {
493 503
     my $append   = $args{append};
494 504
     my $filter   = $args{filter};
495 505
     
496
-    # Source of SQL
497
-    my $source = 'select ';
506
+    # SQL stack
507
+    my @sql;
508
+    
509
+    push @sql, 'select';
498 510
     
499 511
     # Column clause
500 512
     if (@$columns) {
501 513
         foreach my $column (@$columns) {
502
-            $source .= "$column, ";
514
+            push @sql, ($column, ',');
503 515
         }
504
-        $source =~ s/, $/ /;
505
-    }
506
-    else {
507
-        $source .= '* ';
516
+        pop @sql if $sql[-1] eq ',';
508 517
     }
518
+    else { push @sql, '*' }
509 519
     
510 520
     # Table
511
-    $source .= 'from ';
521
+    push @sql, 'from';
512 522
     foreach my $table (@$tables) {
513
-        $source .= "$table, ";
523
+        push @sql, ($table, ',');
514 524
     }
515
-    $source =~ s/, $/ /;
525
+    pop @sql if $sql[-1] eq ',';
516 526
     
517 527
     # Where
518 528
     my $w;
... ...
@@ -533,22 +543,25 @@ sub select {
533 543
     
534 544
     # String where
535 545
     my $swhere = "$w";
536
-    $source .= "$swhere ";
546
+    push @sql, $swhere;
537 547
     
538 548
     # Relation
539 549
     if ($relation) {
540
-        $source .= $swhere eq '' ? "where " : "and ";
541
-        foreach my $rkey (keys %$relation) {
542
-            $source .= "$rkey = " . $relation->{$rkey} . " and ";
550
+        push @sql, $swhere eq '' ? 'where' : 'and';
551
+        foreach my $rcolumn (keys %$relation) {
552
+            push @sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
543 553
         }
544 554
     }
545
-    $source =~ s/ and $//;
555
+    pop @sql if $sql[-1] eq 'and';
556
+    
557
+    # Append statement
558
+    push @sql, $append if $append;
546 559
     
547
-    # Append some statement
548
-    $source .= " $append" if $append;
560
+    # SQL
561
+    my $sql = join (' ', @sql);
549 562
     
550 563
     # Create query
551
-    my $query = $self->create_query($source);
564
+    my $query = $self->create_query($sql);
552 565
     return $query if $args{query};
553 566
     
554 567
     # Execute query
... ...
@@ -651,9 +664,12 @@ sub update {
651 664
     croak qq{"where" must be specified}
652 665
       if "$swhere" eq '' && !$allow_update_all;
653 666
     
654
-    # Source of SQL
655
-    my $source = "update $table $update_clause $swhere";
656
-    $source .= " $append" if $append;
667
+    # SQL stack
668
+    my @sql;
669
+    
670
+    # Update
671
+    push @sql, "update $table $update_clause $swhere";
672
+    push @sql, $append if $append;
657 673
     
658 674
     # Rearrange parameters
659 675
     foreach my $wkey (keys %$where) {
... ...
@@ -669,8 +685,11 @@ sub update {
669 685
         }
670 686
     }
671 687
     
688
+    # SQL
689
+    my $sql = join(' ', @sql);
690
+    
672 691
     # Create query
673
-    my $query = $self->create_query($source);
692
+    my $query = $self->create_query($sql);
674 693
     return $query if $args{query};
675 694
     
676 695
     # Execute query