... | ... |
@@ -11,6 +11,7 @@ my $builder = Module::Build->new( |
11 | 11 |
'Test::More' => 0, |
12 | 12 |
}, |
13 | 13 |
requires => { |
14 |
+ 'Encode' => 0, |
|
14 | 15 |
'DBIx::Custom' => 0.0101 |
15 | 16 |
}, |
16 | 17 |
recommends => { |
... | ... |
@@ -1,22 +1,24 @@ |
1 | 1 |
package DBIx::Custom::Basic; |
2 |
+use 5.008001; |
|
2 | 3 |
use base 'DBIx::Custom'; |
3 | 4 |
use Encode qw/decode encode/; |
4 | 5 |
|
5 | 6 |
use warnings; |
6 | 7 |
use strict; |
7 | 8 |
|
8 |
-our $VERSION = '0.0101'; |
|
9 |
+our $VERSION = '0.0201'; |
|
9 | 10 |
|
10 | 11 |
my $class = __PACKAGE__; |
11 | 12 |
|
12 | 13 |
$class->add_filter( |
13 |
- default_bind_filter => sub { encode('UTF-8', $_[1]) }, |
|
14 |
- default_fetch_filter => sub { decode('UTF-8', $_[1]) } |
|
14 |
+ encode_utf8 => sub { |
|
15 |
+ my $value = shift; |
|
16 |
+ utf8::upgrade($value) unless Encode::is_utf8($value); |
|
17 |
+ return encode('UTF-8', $value); |
|
18 |
+ }, |
|
19 |
+ decode_utf8 => sub { decode('UTF-8', shift) } |
|
15 | 20 |
); |
16 | 21 |
|
17 |
-$class->bind_filter($class->filters->{default_bind_filter}); |
|
18 |
-$class->fetch_filter($class->filters->{default_fetch_filter}); |
|
19 |
- |
|
20 | 22 |
$class->add_format( |
21 | 23 |
'SQL99_date' => '%Y-%m-%d', |
22 | 24 |
'SQL99_datetime' => '%Y-%m-%d %H:%M:%S', |
... | ... |
@@ -26,6 +28,13 @@ $class->add_format( |
26 | 28 |
'ISO-8601_time' => '%H:%M:%S', |
27 | 29 |
); |
28 | 30 |
|
31 |
+# Methods |
|
32 |
+sub utf8_filter_on { |
|
33 |
+ my $self = shift; |
|
34 |
+ $self->bind_filter($self->filters->{encode_utf8}); |
|
35 |
+ $self->fetch_filter($self->filters->{decode_utf8}); |
|
36 |
+} |
|
37 |
+ |
|
29 | 38 |
1; |
30 | 39 |
|
31 | 40 |
=head1 NAME |
... | ... |
@@ -34,7 +43,55 @@ DBIx::Custom::Basic - DBIx::Custom basic class |
34 | 43 |
|
35 | 44 |
=head1 VERSION |
36 | 45 |
|
37 |
-Version 0.0101 |
|
46 |
+Version 0.0201 |
|
47 |
+ |
|
48 |
+=head1 FILTERS |
|
49 |
+ |
|
50 |
+=head2 encode_utf8 |
|
51 |
+ |
|
52 |
+ # Encode to UTF-8 byte stream (utf8::upgrade is done if need) |
|
53 |
+ $dbi->filters->{encode_utf8}->($value); |
|
54 |
+ |
|
55 |
+This filter is generally used as bind filter |
|
56 |
+ |
|
57 |
+ $dbi->bind_filter($dbi->filters->{encode_utf8}); |
|
58 |
+ |
|
59 |
+=head2 decode_utf8 |
|
60 |
+ |
|
61 |
+ # Decode to perl internal string |
|
62 |
+ $dbi->filters->{decode_utf8}->($value); |
|
63 |
+ |
|
64 |
+This filter is generally used as fetch filter |
|
65 |
+ |
|
66 |
+ $dbi->fetch_filter($dbi->filters->{decode_utf8}); |
|
67 |
+ |
|
68 |
+=head2 FORMATS |
|
69 |
+ |
|
70 |
+strptime formats is available |
|
71 |
+ |
|
72 |
+ # format name format |
|
73 |
+ 'SQL99_date' '%Y-%m-%d', |
|
74 |
+ 'SQL99_datetime' '%Y-%m-%d %H:%M:%S', |
|
75 |
+ 'SQL99_time' '%H:%M:%S', |
|
76 |
+ 'ISO-8601_date' '%Y-%m-%d', |
|
77 |
+ 'ISO-8601_datetime' '%Y-%m-%dT%H:%M:%S', |
|
78 |
+ 'ISO-8601_time' '%H:%M:%S', |
|
79 |
+ |
|
80 |
+You get format as the following |
|
81 |
+ |
|
82 |
+ my $format = $dbi->formats->{$format_name}; |
|
83 |
+ |
|
84 |
+=head1 METHOD |
|
85 |
+ |
|
86 |
+=head2 utf8_filter_on |
|
87 |
+ |
|
88 |
+ # Encode and decode utf8 filter on |
|
89 |
+ $dbi->utf8_filter_on; |
|
90 |
+ |
|
91 |
+This equel to |
|
92 |
+ |
|
93 |
+ $dbi->bind_filter($dbi->filters->{encode_utf8}); |
|
94 |
+ $dbi->fetch_filter($dbi->filters->{decode_utf8}); |
|
38 | 95 |
|
39 | 96 |
=head1 AUTHOR |
40 | 97 |
|
... | ... |
@@ -49,12 +49,14 @@ use DBIx::Custom::Basic; |
49 | 49 |
|
50 | 50 |
test 'Filter'; |
51 | 51 |
$dbi = DBIx::Custom::Basic->new($NEW_ARGS->{0}); |
52 |
-ok($dbi->filters->{default_bind_filter}, "$test : exists default_bind_filter"); |
|
53 |
-ok($dbi->filters->{default_fetch_filter}, "$test : exists default_fetch_filter"); |
|
54 |
-is($dbi->bind_filter, $dbi->filters->{default_bind_filter}, 'default bind filter'); |
|
55 |
-is($dbi->fetch_filter, $dbi->filters->{default_fetch_filter}, 'default fetch filter'); |
|
52 |
+ok($dbi->filters->{encode_utf8}, "$test : exists default_bind_filter"); |
|
53 |
+ok($dbi->filters->{decode_utf8}, "$test : exists default_fetch_filter"); |
|
54 |
+ |
|
55 |
+$dbi->utf8_filter_on; |
|
56 |
+is($dbi->bind_filter, $dbi->filters->{encode_utf8}, 'default bind filter'); |
|
57 |
+is($dbi->fetch_filter, $dbi->filters->{decode_utf8}, 'default fetch filter'); |
|
56 | 58 |
|
57 | 59 |
$decoded_str = 'あ'; |
58 |
-$encoded_str = $dbi->bind_filter->('', $decoded_str); |
|
60 |
+$encoded_str = $dbi->bind_filter->($decoded_str); |
|
59 | 61 |
is($encoded_str, encode('UTF-8', $decoded_str), 'encode utf8'); |
60 | 62 |
is($decoded_str, $dbi->fetch_filter->('', $encoded_str), "$test : fetch_filter"); |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom::Result; |
2 | 2 |
use Object::Simple; |
3 | 3 |
|
4 |
-our $VERSION = '0.0101'; |
|
4 |
+our $VERSION = '0.0201'; |
|
5 | 5 |
|
6 | 6 |
use Carp 'croak'; |
7 | 7 |
|
... | ... |
@@ -34,7 +34,7 @@ sub fetch { |
34 | 34 |
my $types = $sth->{TYPE}; |
35 | 35 |
for (my $i = 0; $i < @$keys; $i++) { |
36 | 36 |
next if $self->_no_fetch_filters_map->{$keys->[$i]}; |
37 |
- $row->[$i]= $fetch_filter->($keys->[$i], $row->[$i], $types->[$i], |
|
37 |
+ $row->[$i]= $fetch_filter->($row->[$i], $keys->[$i], $types->[$i], |
|
38 | 38 |
$sth, $i); |
39 | 39 |
} |
40 | 40 |
} |
... | ... |
@@ -66,7 +66,7 @@ sub fetch_hash { |
66 | 66 |
} |
67 | 67 |
else { |
68 | 68 |
$row_hash->{$keys->[$i]} |
69 |
- = $fetch_filter->($keys->[$i], $row->[$i], |
|
69 |
+ = $fetch_filter->($row->[$i], $keys->[$i], |
|
70 | 70 |
$types->[$i], $sth, $i); |
71 | 71 |
} |
72 | 72 |
} |
... | ... |
@@ -198,7 +198,7 @@ DBIx::Custom::Result - Resultset for DBIx::Custom |
198 | 198 |
|
199 | 199 |
=head1 VERSION |
200 | 200 |
|
201 |
-Version 0.0101 |
|
201 |
+Version 0.0201 |
|
202 | 202 |
|
203 | 203 |
=head1 SYNOPSIS |
204 | 204 |
|
... | ... |
@@ -208,7 +208,7 @@ is_deeply(\@rows, [[1, 2], [3, 4]], $test); |
208 | 208 |
|
209 | 209 |
test 'fetch filter'; |
210 | 210 |
$fetch_filter = sub { |
211 |
- my ($key, $value, $type, $sth, $i) = @_; |
|
211 |
+ my ($value, $key, $type, $sth, $i) = @_; |
|
212 | 212 |
if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
213 | 213 |
return $value * 3; |
214 | 214 |
} |
... | ... |
@@ -323,8 +323,8 @@ sub _build_bind_values { |
323 | 323 |
!$no_bind_filters_map->{$original_key}) |
324 | 324 |
{ |
325 | 325 |
push @bind_values, |
326 |
- $bind_filter->($original_key, |
|
327 |
- $root_params->[$current_key->[0]], |
|
326 |
+ $bind_filter->($root_params->[$current_key->[0]], |
|
327 |
+ $original_key, |
|
328 | 328 |
$table, $column); |
329 | 329 |
} |
330 | 330 |
# Not filtering |
... | ... |
@@ -344,8 +344,8 @@ sub _build_bind_values { |
344 | 344 |
!$no_bind_filters_map->{$original_key}) |
345 | 345 |
{ |
346 | 346 |
push @bind_values, |
347 |
- $bind_filter->($original_key, |
|
348 |
- $root_params->{$current_key}, |
|
347 |
+ $bind_filter->($root_params->{$current_key}, |
|
348 |
+ $original_key, |
|
349 | 349 |
$table, $column); |
350 | 350 |
} |
351 | 351 |
# Not filtering |
... | ... |
@@ -163,7 +163,7 @@ $insert_tmpl = "insert into table1 {insert key1 key2}"; |
163 | 163 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub { |
164 | 164 |
my $query = shift; |
165 | 165 |
$query->bind_filter(sub { |
166 |
- my ($key, $value) = @_; |
|
166 |
+ my ($value, $key) = @_; |
|
167 | 167 |
if ($key eq 'key2') { |
168 | 168 |
return $value + 1; |
169 | 169 |
} |
... | ... |
@@ -182,7 +182,7 @@ $dbi->do($CREATE_TABLE->{0}); |
182 | 182 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
183 | 183 |
$insert_query = $dbi->create_query($insert_tmpl); |
184 | 184 |
$insert_query->bind_filter(sub { |
185 |
- my ($key, $value, $table, $column) = @_; |
|
185 |
+ my ($value, $key, $table, $column) = @_; |
|
186 | 186 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
187 | 187 |
return $value * 2; |
188 | 188 |
} |
... | ... |
@@ -191,7 +191,7 @@ $insert_query->bind_filter(sub { |
191 | 191 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
192 | 192 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
193 | 193 |
$select_query->fetch_filter(sub { |
194 |
- my ($key, $value, $type, $sth, $i) = @_; |
|
194 |
+ my ($value, $key, $type, $sth, $i) = @_; |
|
195 | 195 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
196 | 196 |
return $value * 3; |
197 | 197 |
} |
... | ... |
@@ -214,7 +214,7 @@ $dbi->do($CREATE_TABLE->{0}); |
214 | 214 |
$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
215 | 215 |
$insert_query = $dbi->create_query($insert_tmpl); |
216 | 216 |
$insert_query->bind_filter(sub { |
217 |
- my ($key, $value, $table, $column) = @_; |
|
217 |
+ my ($value, $key, $table, $column) = @_; |
|
218 | 218 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
219 | 219 |
return $value * 3; |
220 | 220 |
} |
... | ... |
@@ -233,7 +233,7 @@ $dbi->execute($insert_query, {key1 => 2, key2 => 4}); |
233 | 233 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
234 | 234 |
$select_query = $dbi->create_query($select_tmpl); |
235 | 235 |
$select_query->bind_filter(sub { |
236 |
- my ($key, $value, $table, $column) = @_; |
|
236 |
+ my ($value, $key, $table, $column) = @_; |
|
237 | 237 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') { |
238 | 238 |
return $value * 2; |
239 | 239 |
} |
... | ... |
@@ -484,7 +484,7 @@ $dbi->do('delete from table1'); |
484 | 484 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub { |
485 | 485 |
my $query = shift; |
486 | 486 |
$query->bind_filter(sub { |
487 |
- my ($key, $value) = @_; |
|
487 |
+ my ($value, $key) = @_; |
|
488 | 488 |
if ($key eq 'key1') { |
489 | 489 |
return $value * 3; |
490 | 490 |
} |
... | ... |
@@ -532,7 +532,7 @@ $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}) |
532 | 532 |
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub { |
533 | 533 |
my $query = shift; |
534 | 534 |
$query->bind_filter(sub { |
535 |
- my ($key, $value) = @_; |
|
535 |
+ my ($value, $key) = @_; |
|
536 | 536 |
if ($key eq 'key2') { |
537 | 537 |
return $value * 2; |
538 | 538 |
} |
... | ... |
@@ -570,7 +570,7 @@ $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}) |
570 | 570 |
$dbi->update_all('table1', {key2 => 10}, sub { |
571 | 571 |
my $query = shift; |
572 | 572 |
$query->bind_filter(sub { |
573 |
- my ($key, $value) = @_; |
|
573 |
+ my ($value, $key) = @_; |
|
574 | 574 |
return $value * 2; |
575 | 575 |
}) |
576 | 576 |
}); |
... | ... |
@@ -597,7 +597,7 @@ $dbi->insert('table1', {key1 => 3, key2 => 4}); |
597 | 597 |
$dbi->delete('table1', {key2 => 1}, sub { |
598 | 598 |
my $query = shift; |
599 | 599 |
$query->bind_filter(sub { |
600 |
- my ($key, $value) = @_; |
|
600 |
+ my ($value, $key) = @_; |
|
601 | 601 |
return $value * 2; |
602 | 602 |
}); |
603 | 603 |
}); |
... | ... |
@@ -660,7 +660,7 @@ is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
660 | 660 |
$rows = $dbi->select('table1', {key1 => 2}, sub { |
661 | 661 |
my $query = shift; |
662 | 662 |
$query->bind_filter(sub { |
663 |
- my ($key, $value) = @_; |
|
663 |
+ my ($value, $key) = @_; |
|
664 | 664 |
if ($key eq 'key1') { |
665 | 665 |
return $value - 1; |
666 | 666 |
} |