added DBIx::Cusotm::Order prepend method automatically...
...quoted syntax
... | ... |
@@ -737,7 +737,7 @@ sub not_exists { bless {}, 'DBIx::Custom::NotExists' } |
737 | 737 |
|
738 | 738 |
sub order { |
739 | 739 |
my $self = shift; |
740 |
- return DBIx::Custom::Order->new(@_); |
|
740 |
+ return DBIx::Custom::Order->new(quote => $self->quote, @_); |
|
741 | 741 |
} |
742 | 742 |
|
743 | 743 |
sub register_filter { |
... | ... |
@@ -5,9 +5,27 @@ use overload |
5 | 5 |
'""' => sub { shift->to_string }, |
6 | 6 |
fallback => 1; |
7 | 7 |
|
8 |
-has orders => sub { [] }; |
|
9 | 8 |
|
10 |
-sub prepend { unshift @{shift->orders}, @_ } |
|
9 |
+has orders => sub { [] }, |
|
10 |
+ quote => ''; |
|
11 |
+ |
|
12 |
+sub prepend { |
|
13 |
+ my $self = shift; |
|
14 |
+ |
|
15 |
+ my $q = $self->quote; |
|
16 |
+ foreach my $order (reverse @_) { |
|
17 |
+ if (ref $order eq 'ARRAY') { |
|
18 |
+ my $column = shift @$order; |
|
19 |
+ $column = "$q$column$q" if defined $column; |
|
20 |
+ my $derection = shift @$order; |
|
21 |
+ $order = $column; |
|
22 |
+ $order .= " $derection" if $derection; |
|
23 |
+ } |
|
24 |
+ unshift @{$self->orders}, $order; |
|
25 |
+ } |
|
26 |
+ |
|
27 |
+ return $self; |
|
28 |
+} |
|
11 | 29 |
|
12 | 30 |
sub to_string { |
13 | 31 |
my $self = shift; |
... | ... |
@@ -61,6 +79,18 @@ and implements the following new ones. |
61 | 79 |
|
62 | 80 |
Prepend order parts to C<orders>. |
63 | 81 |
|
82 |
+You can pass array reference, which contain column name and direction. |
|
83 |
+Column name is quoted properly |
|
84 |
+ |
|
85 |
+ # Column name and direction |
|
86 |
+ $order->prepend(['book-title']); |
|
87 |
+ $order->prepend([qw/book-title desc/]); |
|
88 |
+ |
|
89 |
+This is expanded to the following way. |
|
90 |
+ |
|
91 |
+ "book-title" |
|
92 |
+ "book-title" desc |
|
93 |
+ |
|
64 | 94 |
=head2 C<to_string> |
65 | 95 |
|
66 | 96 |
my $order_by = $order->to_string; |
... | ... |
@@ -3325,6 +3325,16 @@ $dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
3325 | 3325 |
$result = $dbi->select(table => 'table1', append => "$order"); |
3326 | 3326 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
3327 | 3327 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
3328 |
+ |
|
3329 |
+ $order = $dbi->order; |
|
3330 |
+ $order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
3331 |
+ $result = $dbi->select(table => 'table1', |
|
3332 |
+ column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
3333 |
+ append => "$order"); |
|
3334 |
+ is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
3335 |
+ {'table1-key1' => 1, 'table1-key2' => 1}, |
|
3336 |
+ {'table1-key1' => 2, 'table1-key2' => 4}, |
|
3337 |
+ {'table1-key1' => 2, 'table1-key2' => 2}]); |
|
3328 | 3338 |
} |
3329 | 3339 |
|
3330 | 3340 |
test 'tag_parse'; |