renamed dbi_options to dbi_option. dbi_options is avail...
...able, but deprecated.
| ... | ... |
@@ -1,3 +1,5 @@ |
| 1 |
+0.1637 |
|
| 2 |
+ renamed dbi_options to dbi_option. dbi_options is available, but deprecated. |
|
| 1 | 3 |
0.1636 |
| 2 | 4 |
added tests and cleanup |
| 3 | 5 |
0.1635 |
| ... | ... |
@@ -18,8 +18,8 @@ use Encode qw/encode_utf8 decode_utf8/; |
| 18 | 18 |
|
| 19 | 19 |
__PACKAGE__->attr( |
| 20 | 20 |
[qw/data_source dbh password user/], |
| 21 |
- dbi_options => sub { {} },
|
|
| 22 | 21 |
cache => 1, |
| 22 |
+ dbi_option => sub { {} },
|
|
| 23 | 23 |
filter_check => 1, |
| 24 | 24 |
query_builder => sub {DBIx::Custom::QueryBuilder->new},
|
| 25 | 25 |
result_class => 'DBIx::Custom::Result', |
| ... | ... |
@@ -165,7 +165,7 @@ sub connect {
|
| 165 | 165 |
unless $data_source; |
| 166 | 166 |
my $user = $self->user; |
| 167 | 167 |
my $password = $self->password; |
| 168 |
- my $dbi_options = $self->dbi_options || {};
|
|
| 168 |
+ my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
|
|
| 169 | 169 |
|
| 170 | 170 |
# Connect |
| 171 | 171 |
my $dbh = eval {DBI->connect(
|
| ... | ... |
@@ -176,7 +176,7 @@ sub connect {
|
| 176 | 176 |
RaiseError => 1, |
| 177 | 177 |
PrintError => 0, |
| 178 | 178 |
AutoCommit => 1, |
| 179 |
- %$dbi_options |
|
| 179 |
+ %$dbi_option |
|
| 180 | 180 |
} |
| 181 | 181 |
)}; |
| 182 | 182 |
|
| ... | ... |
@@ -731,6 +731,10 @@ sub _croak {
|
| 731 | 731 |
} |
| 732 | 732 |
} |
| 733 | 733 |
|
| 734 |
+# DEPRECATED! |
|
| 735 |
+ |
|
| 736 |
+__PACKAGE__->attr(dbi_options => sub { {} });
|
|
| 737 |
+ |
|
| 734 | 738 |
sub default_bind_filter {
|
| 735 | 739 |
my $self = shift; |
| 736 | 740 |
|
| ... | ... |
@@ -782,67 +786,44 @@ DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
| 782 | 786 |
|
| 783 | 787 |
=head1 SYNOPSYS |
| 784 | 788 |
|
| 785 |
-Connect to the database. |
|
| 786 |
- |
|
| 787 | 789 |
use DBIx::Custom; |
| 788 | 790 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
| 789 |
- user => 'ken', password => '!LFKD%$&'); |
|
| 790 |
- |
|
| 791 |
-Insert, update, and delete |
|
| 791 |
+ user => 'ken', password => '!LFKD%$&', |
|
| 792 |
+ dbi_option => {mysql_enable_utf8 => 1});
|
|
| 792 | 793 |
|
| 793 | 794 |
# Insert |
| 794 | 795 |
$dbi->insert(table => 'book', |
| 795 | 796 |
param => {title => 'Perl', author => 'Ken'},
|
| 796 |
- filter => {title => 'encode_utf8'});
|
|
| 797 |
+ filter => {title => 'to_something'});
|
|
| 797 | 798 |
|
| 798 | 799 |
# Update |
| 799 | 800 |
$dbi->update(table => 'book', |
| 800 | 801 |
param => {title => 'Perl', author => 'Ken'},
|
| 801 | 802 |
where => {id => 5},
|
| 802 |
- filter => {title => 'encode_utf8'});
|
|
| 803 |
+ filter => {title => 'to_something'});
|
|
| 803 | 804 |
|
| 804 | 805 |
# Update all |
| 805 | 806 |
$dbi->update_all(table => 'book', |
| 806 | 807 |
param => {title => 'Perl'},
|
| 807 |
- filter => {title => 'encode_utf8'});
|
|
| 808 |
+ filter => {title => 'to_something'});
|
|
| 808 | 809 |
|
| 809 | 810 |
# Delete |
| 810 | 811 |
$dbi->delete(table => 'book', |
| 811 | 812 |
where => {author => 'Ken'},
|
| 812 |
- filter => {title => 'encode_utf8'});
|
|
| 813 |
+ filter => {title => 'to_something'});
|
|
| 813 | 814 |
|
| 814 | 815 |
# Delete all |
| 815 | 816 |
$dbi->delete_all(table => 'book'); |
| 816 | 817 |
|
| 817 |
-Select |
|
| 818 |
- |
|
| 819 | 818 |
# Select |
| 820 |
- my $result = $dbi->select(table => 'book'); |
|
| 821 |
- |
|
| 822 |
- # Select, more complex |
|
| 823 | 819 |
my $result = $dbi->select( |
| 824 | 820 |
table => 'book', |
| 825 | 821 |
column => [qw/author title/], |
| 826 | 822 |
where => {author => 'Ken'},
|
| 823 |
+ relation => {'book.id' => 'rental.book_id'},
|
|
| 827 | 824 |
append => 'order by id limit 5', |
| 828 |
- filter => {title => 'encode_utf8'}
|
|
| 829 |
- ); |
|
| 830 |
- |
|
| 831 |
- # Select, join table |
|
| 832 |
- my $result = $dbi->select( |
|
| 833 |
- table => ['book', 'rental'], |
|
| 834 |
- column => ['book.name as book_name'] |
|
| 835 |
- relation => {'book.id' => 'rental.book_id'}
|
|
| 825 |
+ filter => {title => 'to_something'}
|
|
| 836 | 826 |
); |
| 837 |
- |
|
| 838 |
- # Select, more flexible where |
|
| 839 |
- my $result = $dbi->select( |
|
| 840 |
- table => 'book', |
|
| 841 |
- where => ['{= author} and {like title}',
|
|
| 842 |
- {author => 'Ken', title => '%Perl%'}]
|
|
| 843 |
- ); |
|
| 844 |
- |
|
| 845 |
-Execute SQL |
|
| 846 | 827 |
|
| 847 | 828 |
# Execute SQL |
| 848 | 829 |
$dbi->execute("select title from book");
|
| ... | ... |
@@ -850,7 +831,7 @@ Execute SQL |
| 850 | 831 |
# Execute SQL with hash binding and filtering |
| 851 | 832 |
$dbi->execute("select id from book where {= author} and {like title}",
|
| 852 | 833 |
param => {author => 'ken', title => '%Perl%'},
|
| 853 |
- filter => {title => 'encode_utf8'});
|
|
| 834 |
+ filter => {title => 'to_something'});
|
|
| 854 | 835 |
|
| 855 | 836 |
# Create query and execute it |
| 856 | 837 |
my $query = $dbi->create_query( |
| ... | ... |
@@ -858,13 +839,9 @@ Execute SQL |
| 858 | 839 |
); |
| 859 | 840 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
|
| 860 | 841 |
|
| 861 |
-Other features. |
|
| 862 |
- |
|
| 863 | 842 |
# Get DBI object |
| 864 | 843 |
my $dbh = $dbi->dbh; |
| 865 | 844 |
|
| 866 |
-Fetch row. |
|
| 867 |
- |
|
| 868 | 845 |
# Fetch |
| 869 | 846 |
while (my $row = $result->fetch) {
|
| 870 | 847 |
# ... |
| ... | ... |
@@ -932,10 +909,10 @@ C<connect()> method use this value to connect the database. |
| 932 | 909 |
|
| 933 | 910 |
L<DBI> object. You can call all methods of L<DBI>. |
| 934 | 911 |
|
| 935 |
-=head2 C<dbi_options> |
|
| 912 |
+=head2 C<dbi_option> |
|
| 936 | 913 |
|
| 937 |
- my $dbi_options = $dbi->dbi_options; |
|
| 938 |
- $dbi = $dbi->dbi_options($dbi_options); |
|
| 914 |
+ my $dbi_option = $dbi->dbi_option; |
|
| 915 |
+ $dbi = $dbi->dbi_option($dbi_option); |
|
| 939 | 916 |
|
| 940 | 917 |
DBI options. |
| 941 | 918 |
C<connect()> method use this value to connect the database. |
| ... | ... |
@@ -947,9 +924,6 @@ Default filter when row is fetched. |
| 947 | 924 |
my $filters = $dbi->filters; |
| 948 | 925 |
$dbi = $dbi->filters(\%filters); |
| 949 | 926 |
|
| 950 |
-Filter functions. |
|
| 951 |
-"encode_utf8" and "decode_utf8" is registered by default. |
|
| 952 |
- |
|
| 953 | 927 |
=head2 C<filter_check> |
| 954 | 928 |
|
| 955 | 929 |
my $filter_check = $dbi->filter_check; |
| ... | ... |
@@ -1068,18 +1042,6 @@ Query is L<DBIx::Custom::Query> object. |
| 1068 | 1042 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
| 1069 | 1043 |
or the count of affected rows if insert, update, delete statement is executed. |
| 1070 | 1044 |
|
| 1071 |
-B<Example:> |
|
| 1072 |
- |
|
| 1073 |
- my $result = $dbi->execute( |
|
| 1074 |
- "select * from book where {= author} and {like title}",
|
|
| 1075 |
- param => {author => 'Ken', title => '%Perl%'}
|
|
| 1076 |
- ); |
|
| 1077 |
- |
|
| 1078 |
- while (my $row = $result->fetch) {
|
|
| 1079 |
- my $author = $row->[0]; |
|
| 1080 |
- my $title = $row->[1]; |
|
| 1081 |
- } |
|
| 1082 |
- |
|
| 1083 | 1045 |
=head2 C<(experimental) expand> |
| 1084 | 1046 |
|
| 1085 | 1047 |
my %expand = $dbi->expand($source); |
| ... | ... |
@@ -1114,13 +1076,6 @@ C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as re |
| 1114 | 1076 |
default to 0. This is experimental. |
| 1115 | 1077 |
Return value of C<delete()> is the count of affected rows. |
| 1116 | 1078 |
|
| 1117 |
-B<Example:> |
|
| 1118 |
- |
|
| 1119 |
- $dbi->delete(table => 'book', |
|
| 1120 |
- where => {id => 5},
|
|
| 1121 |
- append => 'some statement', |
|
| 1122 |
- filter => {id => 'encode_utf8'});
|
|
| 1123 |
- |
|
| 1124 | 1079 |
=head2 C<delete_all> |
| 1125 | 1080 |
|
| 1126 | 1081 |
$dbi->delete_all(table => $table); |
| ... | ... |
@@ -1130,10 +1085,6 @@ Arguments is same as C<delete> method, |
| 1130 | 1085 |
except that C<delete_all> don't have C<where> argument. |
| 1131 | 1086 |
Return value of C<delete_all()> is the count of affected rows. |
| 1132 | 1087 |
|
| 1133 |
-B<Example:> |
|
| 1134 |
- |
|
| 1135 |
- $dbi->delete_all(table => 'book'); |
|
| 1136 |
- |
|
| 1137 | 1088 |
=head2 C<(experimental) helper> |
| 1138 | 1089 |
|
| 1139 | 1090 |
$dbi->helper( |
| ... | ... |
@@ -1172,13 +1123,6 @@ default to 0. This is experimental. |
| 1172 | 1123 |
This is overwrites C<default_bind_filter>. |
| 1173 | 1124 |
Return value of C<insert()> is the count of affected rows. |
| 1174 | 1125 |
|
| 1175 |
-B<Example:> |
|
| 1176 |
- |
|
| 1177 |
- $dbi->insert(table => 'book', |
|
| 1178 |
- param => {title => 'Perl', author => 'Taro'},
|
|
| 1179 |
- append => "some statement", |
|
| 1180 |
- filter => {title => 'encode_utf8'})
|
|
| 1181 |
- |
|
| 1182 | 1126 |
=head2 C<new> |
| 1183 | 1127 |
|
| 1184 | 1128 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
| ... | ... |
@@ -1235,25 +1179,6 @@ C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
| 1235 | 1179 |
|
| 1236 | 1180 |
=back |
| 1237 | 1181 |
|
| 1238 |
-B<Example:> |
|
| 1239 |
- |
|
| 1240 |
- $dbi->register_filter( |
|
| 1241 |
- encode_utf8 => sub {
|
|
| 1242 |
- my $value = shift; |
|
| 1243 |
- |
|
| 1244 |
- require Encode; |
|
| 1245 |
- |
|
| 1246 |
- return Encode::encode('UTF-8', $value);
|
|
| 1247 |
- }, |
|
| 1248 |
- decode_utf8 => sub {
|
|
| 1249 |
- my $value = shift; |
|
| 1250 |
- |
|
| 1251 |
- require Encode; |
|
| 1252 |
- |
|
| 1253 |
- return Encode::decode('UTF-8', $value)
|
|
| 1254 |
- } |
|
| 1255 |
- ); |
|
| 1256 |
- |
|
| 1257 | 1182 |
=head2 C<register_tag_processor> |
| 1258 | 1183 |
|
| 1259 | 1184 |
$dbi->register_tag_processor( |
| ... | ... |
@@ -1291,30 +1216,6 @@ C<filter> is filters when parameter binding is executed. |
| 1291 | 1216 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
| 1292 | 1217 |
default to 0. This is experimental. |
| 1293 | 1218 |
|
| 1294 |
-B<Example:> |
|
| 1295 |
- |
|
| 1296 |
- # select * from book; |
|
| 1297 |
- my $result = $dbi->select(table => 'book'); |
|
| 1298 |
- |
|
| 1299 |
- # select * from book where title = ?; |
|
| 1300 |
- my $result = $dbi->select(table => 'book', where => {title => 'Perl'});
|
|
| 1301 |
- |
|
| 1302 |
- # select title, author from book where id = ? for update; |
|
| 1303 |
- my $result = $dbi->select( |
|
| 1304 |
- table => 'book', |
|
| 1305 |
- column => ['title', 'author'], |
|
| 1306 |
- where => {id => 1},
|
|
| 1307 |
- appned => 'for update' |
|
| 1308 |
- ); |
|
| 1309 |
- |
|
| 1310 |
- # select book.name as book_name from book, rental |
|
| 1311 |
- # where book.id = rental.book_id; |
|
| 1312 |
- my $result = $dbi->select( |
|
| 1313 |
- table => ['book', 'rental'], |
|
| 1314 |
- column => ['book.name as book_name'] |
|
| 1315 |
- relation => {'book.id' => 'rental.book_id'}
|
|
| 1316 |
- ); |
|
| 1317 |
- |
|
| 1318 | 1219 |
If you use more complex condition, |
| 1319 | 1220 |
you can specify a array reference to C<where> argument. |
| 1320 | 1221 |
|
| ... | ... |
@@ -1351,14 +1252,6 @@ default to 0. This is experimental. |
| 1351 | 1252 |
This is overwrites C<default_bind_filter>. |
| 1352 | 1253 |
Return value of C<update()> is the count of affected rows. |
| 1353 | 1254 |
|
| 1354 |
-B<Example:> |
|
| 1355 |
- |
|
| 1356 |
- $dbi->update(table => 'book', |
|
| 1357 |
- param => {title => 'Perl', author => 'Taro'},
|
|
| 1358 |
- where => {id => 5},
|
|
| 1359 |
- append => "some statement", |
|
| 1360 |
- filter => {title => 'encode_utf8'});
|
|
| 1361 |
- |
|
| 1362 | 1255 |
=head2 C<(experimental) txn_scope> |
| 1363 | 1256 |
|
| 1364 | 1257 |
{
|
| ... | ... |
@@ -1398,12 +1291,6 @@ Arguments is same as C<update> method, |
| 1398 | 1291 |
except that C<update_all> don't have C<where> argument. |
| 1399 | 1292 |
Return value of C<update_all()> is the count of affected rows. |
| 1400 | 1293 |
|
| 1401 |
-B<Example:> |
|
| 1402 |
- |
|
| 1403 |
- $dbi->update_all(table => 'book', |
|
| 1404 |
- param => {author => 'taro'},
|
|
| 1405 |
- filter => {author => 'encode_utf8'});
|
|
| 1406 |
- |
|
| 1407 | 1294 |
=head2 C<(experimental) where> |
| 1408 | 1295 |
|
| 1409 | 1296 |
my $where = $dbi->where; |
| ... | ... |
@@ -1417,23 +1304,6 @@ Create a new L<DBIx::Custom::Where> object. |
| 1417 | 1304 |
|
| 1418 | 1305 |
Method to set and get caches. |
| 1419 | 1306 |
|
| 1420 |
-B<Example:> |
|
| 1421 |
- |
|
| 1422 |
- $dbi->cache_method( |
|
| 1423 |
- sub {
|
|
| 1424 |
- my $self = shift; |
|
| 1425 |
- |
|
| 1426 |
- $self->{_cached} ||= {};
|
|
| 1427 |
- |
|
| 1428 |
- if (@_ > 1) {
|
|
| 1429 |
- $self->{_cached}{$_[0]} = $_[1]
|
|
| 1430 |
- } |
|
| 1431 |
- else {
|
|
| 1432 |
- return $self->{_cached}{$_[0]}
|
|
| 1433 |
- } |
|
| 1434 |
- } |
|
| 1435 |
- ); |
|
| 1436 |
- |
|
| 1437 | 1307 |
=head1 STABILITY |
| 1438 | 1308 |
|
| 1439 | 1309 |
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature. |
| ... | ... |
@@ -77,7 +77,7 @@ L<DBIx::Custom>を利用するのであればハッシュリファレンスで |
| 77 | 77 |
次にテーブルの各列にこのフィルタを適用します。 |
| 78 | 78 |
|
| 79 | 79 |
$dbi->apply_filter('book',
|
| 80 |
- 'publish_date' => {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 80 |
+ 'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 81 | 81 |
); |
| 82 | 82 |
|
| 83 | 83 |
outはPerlからデータベースに保存する方向、inはデータベースからPerlに取得する方向です。 |
| ... | ... |
@@ -482,7 +482,90 @@ C<fetch_hash_first()>を使用します。 |
| 482 | 482 |
ステートメントハンドルから |
| 483 | 483 |
利用できる速度の速いメソッドを利用することができます。 |
| 484 | 484 |
|
| 485 |
-=head2 4. タグ |
|
| 485 |
+=head2 4. フィルタリング |
|
| 486 |
+ |
|
| 487 |
+データベースにデータを登録するときやデータベースからデータを取得する |
|
| 488 |
+ときに自動的に値の変換を行いたい場合が多いと思います。 |
|
| 489 |
+たとえば、日付を表現する列の場合は、 |
|
| 490 |
+データベースに登録する場合はL<Time::Piece>オブジェクトから |
|
| 491 |
+データベースの日付のフォーマットに、 |
|
| 492 |
+データベースからデータを取得するときは、その逆を行えると便利です。 |
|
| 493 |
+ |
|
| 494 |
+=head3 フィルタの登録 |
|
| 495 |
+ |
|
| 496 |
+フィルタを登録するにはC<register_filter()>を使用します。 |
|
| 497 |
+ |
|
| 498 |
+ $dbi->register_filter( |
|
| 499 |
+ # Time::Piece object to DATE format |
|
| 500 |
+ tp_to_date => sub {
|
|
| 501 |
+ my $date = shift; |
|
| 502 |
+ |
|
| 503 |
+ return '0000-00-00' unless $tp; |
|
| 504 |
+ return $tp->strftime('%Y-%m-%d');
|
|
| 505 |
+ }, |
|
| 506 |
+ |
|
| 507 |
+ # DATE to Time::Piece object |
|
| 508 |
+ date_to_tp => sub {
|
|
| 509 |
+ my $date = shift; |
|
| 510 |
+ |
|
| 511 |
+ return if $date eq '0000-00-00'; |
|
| 512 |
+ return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
| 513 |
+ }, |
|
| 514 |
+ ); |
|
| 515 |
+ |
|
| 516 |
+登録したフィルタはC<apply_filter()>などで利用することができます。 |
|
| 517 |
+ |
|
| 518 |
+=head3 フィルタの適用 |
|
| 519 |
+ |
|
| 520 |
+作成したフィルタを適用するには、C<apply_filter()>を使用します。 |
|
| 521 |
+ |
|
| 522 |
+ $dbi->apply_filter('book',
|
|
| 523 |
+ issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
|
|
| 524 |
+ first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
|
|
| 525 |
+ ); |
|
| 526 |
+ |
|
| 527 |
+第一引数はテーブル名です。第二引数以降は、列名とフィルタルールのペアを記述します。 |
|
| 528 |
+フィルタルールのoutには、データベースにデータを送信するときに適用するフィルタを、 |
|
| 529 |
+フィルタルールのinには、データベースからデータを取得するときに適用するフィルタを |
|
| 530 |
+記述します。outがデータベースに送信する方向、inがデータベースから取り出す方向です。 |
|
| 531 |
+フィルタには、C<register_filter>で登録したフィルタ名の他に、コードリファレンスを |
|
| 532 |
+指定することもできます。 |
|
| 533 |
+ |
|
| 534 |
+ issue_date => {out => sub { ... }, in => sub { ... }}
|
|
| 535 |
+ |
|
| 536 |
+適用されたフィルタはC<insert()>、C<update()>、C<update_all()>、C<delete()>、 |
|
| 537 |
+C<delete_all()>、C<select()>で有効になります。 |
|
| 538 |
+ |
|
| 539 |
+ my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d');
|
|
| 540 |
+ my $result = $dbi->select(table => 'book', where => {issu_date => $tp});
|
|
| 541 |
+ |
|
| 542 |
+データベースにデータが送信されるときに、L<Time::Piece>オブジェクトは |
|
| 543 |
+データベースの日付のフォーマット「2010-10-14」に変換されます。 |
|
| 544 |
+ |
|
| 545 |
+また逆にデータをフェッチするときには、データベースの日付のフォーマットは |
|
| 546 |
+タイムピースオブジェクトに変換されます。 |
|
| 547 |
+ |
|
| 548 |
+ my $row = $resutl->fetch_hash_first; |
|
| 549 |
+ my $tp = $row->{issue_date};
|
|
| 550 |
+ |
|
| 551 |
+このような自動的に実行されるフィルタを登録できることがL<DBIx::Custom>の |
|
| 552 |
+特徴のひとつです。 |
|
| 553 |
+ |
|
| 554 |
+=head3 個別にフィルタを適用する |
|
| 555 |
+ |
|
| 556 |
+C<apply_filter()>を使って最初にすべてのテーブルの列について |
|
| 557 |
+フィルタを定義しておくこともできますが、 |
|
| 558 |
+さらに個別にフィルタを適用することもできます。 |
|
| 559 |
+個別のフィルタはC<apply_filter()>で適用したフィルタを上書きます。 |
|
| 560 |
+ |
|
| 561 |
+行をフェッチするときに個別にフィルタを適用するには、 |
|
| 562 |
+C<DBIx::Custom::Result>クラスのC<filter>メソッドを使用します。 |
|
| 563 |
+ |
|
| 564 |
+ $result->filter(issue_date => 'date_to_tp', first_issue_date => 'date_to_tp'); |
|
| 565 |
+ |
|
| 566 |
+これは、C<apply_filter()>のフィルタルールのinで定義されたフィルタを上書きます。 |
|
| 567 |
+ |
|
| 568 |
+=head2 5. タグ |
|
| 486 | 569 |
|
| 487 | 570 |
L<DBIx::Custom>はハッシュパラメタバインドを提供します。 |
| 488 | 571 |
|
| ... | ... |
@@ -547,75 +630,6 @@ C<{>とC<}>は予約語です。これらの文字を使いたい場合は
|
| 547 | 630 |
|
| 548 | 631 |
'select * from book \\{ something statement \\}'
|
| 549 | 632 |
|
| 550 |
-=head2 5. フィルタリング |
|
| 551 |
- |
|
| 552 |
-=head3 パラメタバインド時のフィルタリング |
|
| 553 |
- |
|
| 554 |
-データベースに登録するデータをフィルタリングしたい場合 |
|
| 555 |
-があります。たとえば、内部文字列で文字列を保持している場合は |
|
| 556 |
-データベースにデータを登録する前に、バイト文字列に変換する |
|
| 557 |
-必要があります。L<DBIx::Custom>のフィルタリングシステムは |
|
| 558 |
-あるデータを他のデータに変換するのを手助けしてくれます。 |
|
| 559 |
- |
|
| 560 |
-フィルタリングを利用するにはまず、 |
|
| 561 |
-C<register_filter()>メソッドを使用して |
|
| 562 |
-フィルタを登録しておく必要があります。 |
|
| 563 |
- |
|
| 564 |
- $dbi->register_filter( |
|
| 565 |
- to_upper_case => sub {
|
|
| 566 |
- my $value = shift; |
|
| 567 |
- return uc $value; |
|
| 568 |
- } |
|
| 569 |
- ); |
|
| 570 |
- |
|
| 571 |
-デフォルトのフィルタとしてC<encode_utf8>とC<decode_utf8> |
|
| 572 |
-が登録されています。 |
|
| 573 |
- |
|
| 574 |
-登録されているフィルタはC<execute()>メソッドのC<filter>オプション |
|
| 575 |
-で指定することができます。 |
|
| 576 |
- |
|
| 577 |
- my $result = $dbi->execute( |
|
| 578 |
- "select * from book where {= author} and {like title};"
|
|
| 579 |
- param => {author => 'Ken', title => '%Perl%'},
|
|
| 580 |
- filter => {author => 'to_upper_case, title => 'encode_utf8'}
|
|
| 581 |
- ); |
|
| 582 |
- |
|
| 583 |
-この例ではC<author>の値はバインドされるときに大文字に変換され、 |
|
| 584 |
-C<title>の値はバイト文字列に変換されます。 |
|
| 585 |
- |
|
| 586 |
-C<filter>オプションは |
|
| 587 |
-C<insert()>、C<update()>、 C<update_all()>, |
|
| 588 |
-C<delete()>、C<select()> |
|
| 589 |
-メソッドにおいても使用することができます。 |
|
| 590 |
- |
|
| 591 |
- # insert() with filter option |
|
| 592 |
- $dbi->insert(table => 'book', |
|
| 593 |
- param => {title => 'Perl', author => 'Ken'},
|
|
| 594 |
- filter => {title => 'encode_utf8'});
|
|
| 595 |
- |
|
| 596 |
- # select() with filter option |
|
| 597 |
- my $result = $dbi->select( |
|
| 598 |
- table => 'book', |
|
| 599 |
- column => [qw/author title/], |
|
| 600 |
- where => {author => 'Ken'},
|
|
| 601 |
- append => 'order by id limit 1', |
|
| 602 |
- filter => {title => 'encode_utf8'}
|
|
| 603 |
- ); |
|
| 604 |
- |
|
| 605 |
-=head3 行のフェッチ時のフィルタリング |
|
| 606 |
- |
|
| 607 |
-行をフェッチするときのフィルタも設定することができます。 |
|
| 608 |
-これはL<DBIx::Custom::Result>クラスのC<filter>メソッドを使って |
|
| 609 |
-行います。 |
|
| 610 |
- |
|
| 611 |
- my $result = $dbi->select(table => 'book'); |
|
| 612 |
- $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
|
|
| 613 |
- |
|
| 614 |
-フェッチのためのフィルタにおいて、 |
|
| 615 |
-たとえ、列名が大文字を含む場合であっても |
|
| 616 |
-列名は小文字であることに注意してください。 |
|
| 617 |
-これはデータベースシステムに依存させないための要件です。 |
|
| 618 |
- |
|
| 619 | 633 |
=head2 6. パフォーマンスの改善 |
| 620 | 634 |
|
| 621 | 635 |
=head3 シュガーメソッドを使わない |
| ... | ... |
@@ -664,7 +678,7 @@ C<execute>メソッドの第一引数にクエリオブジェトを渡すこと |
| 664 | 678 |
C<begin_work()>、C<commit()>、C<rollback()> |
| 665 | 679 |
という三つのメソッドが容易されています。 |
| 666 | 680 |
これはL<DBI>の同名のメソッドと同じ機能を持ちます。 |
| 667 |
- |
|
| 681 |
+fc |
|
| 668 | 682 |
$dbi->begin_work; |
| 669 | 683 |
|
| 670 | 684 |
eval {
|
| ... | ... |
@@ -730,7 +744,7 @@ L<DBIx::Custom>オブジェクトから直接呼び出すことができます |
| 730 | 744 |
$dbi->update_or_insert; |
| 731 | 745 |
$dbi->find_or_create; |
| 732 | 746 |
|
| 733 |
-=head3 ユーティリティメソッド(実験的) |
|
| 747 |
+=head3 ユーティリティメソッド |
|
| 734 | 748 |
|
| 735 | 749 |
C<expand>メソッドを使用すると次のようなハッシュに含まれる |
| 736 | 750 |
テーブル名と列名を結合することができます。 |
| ... | ... |
@@ -936,9 +936,9 @@ $where = $dbi->where |
| 936 | 936 |
eval{$where->to_string};
|
| 937 | 937 |
like($@, qr/one column/); |
| 938 | 938 |
|
| 939 |
-test 'dbi_options default'; |
|
| 939 |
+test 'dbi_option default'; |
|
| 940 | 940 |
$dbi = DBIx::Custom->new; |
| 941 |
-is_deeply($dbi->dbi_options, {});
|
|
| 941 |
+is_deeply($dbi->dbi_option, {});
|
|
| 942 | 942 |
|
| 943 | 943 |
test 'register_tag_processor'; |
| 944 | 944 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
| ... | ... |
@@ -1046,4 +1046,13 @@ is($result->default_filter->(), 1); |
| 1046 | 1046 |
|
| 1047 | 1047 |
$dbi->table('book');
|
| 1048 | 1048 |
eval{$dbi->table('book')->no_exists};
|
| 1049 |
-like($@, qr/locate/); |
|
| 1049 |
+like($@, qr/locate/); |
|
| 1050 |
+ |
|
| 1051 |
+test 'dbi_option'; |
|
| 1052 |
+$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
| 1053 |
+ dbi_option => {PrintError => 1});
|
|
| 1054 |
+ok($dbi->dbh->{PrintError});
|
|
| 1055 |
+$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
|
| 1056 |
+ dbi_options => {PrintError => 1});
|
|
| 1057 |
+ok($dbi->dbh->{PrintError});
|
|
| 1058 |
+ |