Showing 3 changed files with 36 additions and 40 deletions
+32 -36
lib/DBIx/Custom.pm
... ...
@@ -84,12 +84,11 @@ sub assign_param {
84 84
     # Create set tag
85 85
     my @params;
86 86
     my $safety = $self->safety_character;
87
-    my $q = $self->_quote;
88 87
     foreach my $column (keys %$param) {
89 88
         croak qq{"$column" is not safety column name } . _subname
90 89
           unless $column =~ /^[$safety\.]+$/;
91
-        my $column_quote = "$q$column$q";
92
-        $column_quote =~ s/\./$q.$q/;
90
+        my $column_quote = $self->_q($column);
91
+        $column_quote =~ s/\./$self->_q(".")/e;
93 92
         push @params, "$column_quote = :$column";
94 93
     }
95 94
     my $tag = join(', ', @params);
... ...
@@ -109,16 +108,14 @@ sub column {
109 108
         $columns ||= $self->model($real_table)->columns;
110 109
     }
111 110
     
112
-    # Reserved word quote
113
-    my $q = $self->_quote;
114
-    
115 111
     # Separator
116 112
     my $separator = $self->separator;
117 113
     
118 114
     # Column clause
119 115
     my @column;
120 116
     $columns ||= [];
121
-    push @column, "$q$table$q.$q$_$q as $q${table}${separator}$_$q"
117
+    push @column, $self->_q($table) . "." . $self->_q($_) .
118
+      " as " . $self->_q("${table}${separator}$_")
122 119
       for @$columns;
123 120
     
124 121
     return join (', ', @column);
... ...
@@ -208,10 +205,9 @@ sub delete {
208 205
 
209 206
     # Delete statement
210 207
     my @sql;
211
-    my $q = $self->_quote;
212 208
     push @sql, "delete";
213 209
     push @sql, $prefix if defined $prefix;
214
-    push @sql, "from $q$table$q $where_clause";
210
+    push @sql, "from " . $self->_q($table) . " $where_clause";
215 211
     push @sql, $append if defined $append;
216 212
     my $sql = join(' ', @sql);
217 213
     
... ...
@@ -334,7 +330,8 @@ sub execute {
334 330
     my $main_table = pop @$tables;
335 331
     $tables = $self->_remove_duplicate_table($tables, $main_table);
336 332
     if (my $q = $self->_quote) {
337
-        $_ =~ s/$q//g for @$tables;
333
+        $q = quotemeta($q);
334
+        $_ =~ s/[$q]//g for @$tables;
338 335
     }
339 336
     
340 337
     # Type rule
... ...
@@ -499,14 +496,11 @@ sub insert {
499 496
         $param = $self->merge_param($id_param, $param);
500 497
     }
501 498
 
502
-    # Reserved word quote
503
-    my $q = $self->_quote;
504
-    
505 499
     # Insert statement
506 500
     my @sql;
507 501
     push @sql, "insert";
508 502
     push @sql, $prefix if defined $prefix;
509
-    push @sql, "into $q$table$q " . $self->insert_param($param);
503
+    push @sql, "into " . $self->_q($table) . " " . $self->insert_param($param);
510 504
     push @sql, $append if defined $append;
511 505
     my $sql = join (' ', @sql);
512 506
     
... ...
@@ -519,14 +513,13 @@ sub insert_param {
519 513
     
520 514
     # Create insert parameter tag
521 515
     my $safety = $self->safety_character;
522
-    my $q = $self->_quote;
523 516
     my @columns;
524 517
     my @placeholders;
525 518
     foreach my $column (keys %$param) {
526 519
         croak qq{"$column" is not safety column name } . _subname
527 520
           unless $column =~ /^[$safety\.]+$/;
528
-        my $column_quote = "$q$column$q";
529
-        $column_quote =~ s/\./$q.$q/;
521
+        my $column_quote = $self->_q($column);
522
+        $column_quote =~ s/\./$self->_q(".")/e;
530 523
         push @columns, $column_quote;
531 524
         push @placeholders, ":$column";
532 525
     }
... ...
@@ -698,9 +691,10 @@ sub mycolumn {
698 691
     
699 692
     # Create column clause
700 693
     my @column;
701
-    my $q = $self->_quote;
702 694
     $columns ||= [];
703
-    push @column, "$q$table$q.$q$_$q as $q$_$q" for @$columns;
695
+    push @column, $self->_q($table) . "." . $self->_q($_) .
696
+      " as " . $self->_q($_)
697
+      for @$columns;
704 698
     
705 699
     return join (', ', @column);
706 700
 }
... ...
@@ -737,7 +731,7 @@ sub not_exists { bless {}, 'DBIx::Custom::NotExists' }
737 731
 
738 732
 sub order {
739 733
     my $self = shift;
740
-    return DBIx::Custom::Order->new(quote => $self->quote, @_);
734
+    return DBIx::Custom::Order->new(dbi => $self, @_);
741 735
 }
742 736
 
743 737
 sub register_filter {
... ...
@@ -787,9 +781,6 @@ sub select {
787 781
     my @sql;
788 782
     push @sql, 'select';
789 783
     
790
-    # Reserved word quote
791
-    my $q = $self->_quote;
792
-    
793 784
     # Prefix
794 785
     push @sql, $prefix if defined $prefix;
795 786
     
... ...
@@ -806,7 +797,7 @@ sub select {
806 797
                     splice @$column, 1, 1;
807 798
                 }
808 799
                 
809
-                $column = join(' ', $column->[0], 'as', $q . $column->[1] . $q);
800
+                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
810 801
             }
811 802
             unshift @$tables, @{$self->_search_tables($column)};
812 803
             push @sql, ($column, ',');
... ...
@@ -820,13 +811,13 @@ sub select {
820 811
     if ($relation) {
821 812
         my $found = {};
822 813
         foreach my $table (@$tables) {
823
-            push @sql, ("$q$table$q", ',') unless $found->{$table};
814
+            push @sql, ($self->_q($table), ',') unless $found->{$table};
824 815
             $found->{$table} = 1;
825 816
         }
826 817
     }
827 818
     else {
828 819
         my $main_table = $tables->[-1] || '';
829
-        push @sql, "$q$main_table$q";
820
+        push @sql, $self->_q($main_table);
830 821
     }
831 822
     pop @sql if ($sql[-1] || '') eq ',';
832 823
     croak "Not found table name " . _subname
... ...
@@ -1060,10 +1051,9 @@ sub update {
1060 1051
     
1061 1052
     # Update statement
1062 1053
     my @sql;
1063
-    my $q = $self->_quote;
1064 1054
     push @sql, "update";
1065 1055
     push @sql, $prefix if defined $prefix;
1066
-    push @sql, "$q$table$q $update_clause $where_clause";
1056
+    push @sql, $self->_q($table) . " $update_clause $where_clause";
1067 1057
     push @sql, $append if defined $append;
1068 1058
     
1069 1059
     # SQL
... ...
@@ -1131,7 +1121,8 @@ sub _create_query {
1131 1121
 
1132 1122
         # Remove reserved word quote
1133 1123
         if (my $q = $self->_quote) {
1134
-            $_ =~ s/$q//g for @{$query->columns}
1124
+            $q = quotemeta($q);
1125
+            $_ =~ s/[$q]//g for @{$query->columns}
1135 1126
         }
1136 1127
 
1137 1128
         # Save query to cache
... ...
@@ -1336,7 +1327,7 @@ sub _push_join {
1336 1327
             my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1337 1328
             $j_clause =~ s/'.+?'//g;
1338 1329
             my $q_re = quotemeta($q);
1339
-            $j_clause =~ s/$q_re//g;
1330
+            $j_clause =~ s/[$q_re]//g;
1340 1331
             my $c = $self->safety_character;
1341 1332
             my $join_re = qr/(?:^|\s)($c+)\.$c+\s+=\s+($c+)\.$c+/;
1342 1333
             if ($j_clause =~ $join_re) {
... ...
@@ -1376,13 +1367,17 @@ sub _quote {
1376 1367
 }
1377 1368
 
1378 1369
 sub _q {
1379
-    my $self = shift;
1370
+    my ($self, $value) = @_;
1380 1371
     
1381 1372
     my $quote = $self->_quote;
1382 1373
     my $q = substr($quote, 0, 1) || '';
1383
-    my $p = substr($quote, 1, 1) || $q || '';
1374
+    my $p;
1375
+    if (defined $quote && length $quote > 1) {
1376
+        $p = substr($quote, 1, 1);
1377
+    }
1378
+    else { $p = $q }
1384 1379
     
1385
-    return ($q, $p);
1380
+    return "$q$value$p";
1386 1381
 }
1387 1382
 
1388 1383
 sub _remove_duplicate_table {
... ...
@@ -1403,7 +1398,8 @@ sub _search_tables {
1403 1398
     my $safety_character = $self->safety_character;
1404 1399
     my $q = $self->_quote;
1405 1400
     my $q_re = quotemeta($q);
1406
-    my $table_re = $q ? qr/(?:^|[^$safety_character])$q_re?([$safety_character]+)$q_re?\./
1401
+    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)");
1402
+    my $table_re = $q ? qr/(?:^|[^$safety_character])$quoted_safety_character_re?\./
1407 1403
                       : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
1408 1404
     while ($source =~ /$table_re/g) {
1409 1405
         push @$tables, $1;
... ...
@@ -1422,8 +1418,8 @@ sub _where_to_obj {
1422 1418
         my $clause = ['and'];
1423 1419
         my $q = $self->_quote;
1424 1420
         foreach my $column (keys %$where) {
1425
-            my $column_quote = "$q$column$q";
1426
-            $column_quote =~ s/\./$q.$q/;
1421
+            my $column_quote = $self->_q($column);
1422
+            $column_quote =~ s/\./$self->_q(".")/e;
1427 1423
             push @$clause, "$column_quote = :$column" for keys %$where;
1428 1424
         }
1429 1425
         $obj = $self->where(clause => $clause, param => $where);
+2 -3
lib/DBIx/Custom/Order.pm
... ...
@@ -7,16 +7,15 @@ use overload
7 7
 
8 8
 
9 9
 has orders => sub { [] },
10
-    quote => '';
10
+    dbi => '';
11 11
 
12 12
 sub prepend {
13 13
     my $self = shift;
14 14
     
15
-    my $q = $self->quote;
16 15
     foreach my $order (reverse @_) {
17 16
         if (ref $order eq 'ARRAY') {
18 17
             my $column = shift @$order;
19
-            $column = "$q$column$q" if defined $column;
18
+            $column = $self->dbi->_q($column) if defined $column;
20 19
             my $derection = shift @$order;
21 20
             $order = $column;
22 21
             $order .= " $derection" if $derection;
+2 -1
lib/DBIx/Custom/Where.pm
... ...
@@ -107,7 +107,8 @@ sub _parse {
107 107
         # Remove quote
108 108
         my $column = $columns->[0];
109 109
         if (my $q = $self->quote) {
110
-            $column =~ s/$q//g;
110
+            $q = quotemeta($q);
111
+            $column =~ s/[$q]//g;
111 112
         }
112 113
         
113 114
         # Check safety