... | ... |
@@ -1,5 +1,8 @@ |
1 | 1 |
0.1628 |
2 |
+ remove DBIx::Custom::Model |
|
3 |
+ move table method and table_class attribute to DBIx::Custom |
|
2 | 4 |
added examples |
5 |
+ fixed connect method bug |
|
3 | 6 |
0.1627 |
4 | 7 |
added insert, update, update_all, delete, delete_all, select method to DBIx::Custom::Table |
5 | 8 |
added experimental txn_scope |
... | ... |
@@ -44,6 +44,7 @@ __PACKAGE__->attr(filters => sub { |
44 | 44 |
__PACKAGE__->attr(filter_check => 1); |
45 | 45 |
__PACKAGE__->attr(query_builder => sub {DBIx::Custom::QueryBuilder->new}); |
46 | 46 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
47 |
+__PACKAGE__->attr(table_class => 'DBIx::Custom::Table'); |
|
47 | 48 |
|
48 | 49 |
# DBI methods |
49 | 50 |
foreach my $method (qw/begin_work commit rollback/) { |
... | ... |
@@ -173,7 +174,7 @@ sub connect { |
173 | 174 |
} |
174 | 175 |
} |
175 | 176 |
else { |
176 |
- $self = $proto->new(@_); |
|
177 |
+ $self = $proto->SUPER::new(@_); |
|
177 | 178 |
} |
178 | 179 |
|
179 | 180 |
# Information |
... | ... |
@@ -606,6 +607,30 @@ sub select { |
606 | 607 |
return $result; |
607 | 608 |
} |
608 | 609 |
|
610 |
+sub table { |
|
611 |
+ my $self = shift; |
|
612 |
+ my $name = shift; |
|
613 |
+ |
|
614 |
+ # Table class |
|
615 |
+ my $table_class = $self->table_class; |
|
616 |
+ croak qq{Invalid table class name "$table_class"} |
|
617 |
+ unless $table_class =~ /^[\w:]+$/; |
|
618 |
+ unless ($table_class->can('isa')) { |
|
619 |
+ eval "require $table_class"; |
|
620 |
+ croak $@ if $@; |
|
621 |
+ } |
|
622 |
+ # Create table |
|
623 |
+ $self->{_tables} ||= {}; |
|
624 |
+ $self->{_tables}->{$name} |
|
625 |
+ = $table_class->new(name => $name, dbi => $self) |
|
626 |
+ unless defined $self->{_tables}->{$name}; |
|
627 |
+ |
|
628 |
+ # Helper |
|
629 |
+ $self->{_tables}->{$name}->helper(@_) if @_; |
|
630 |
+ |
|
631 |
+ return $self->{_tables}{$name}; |
|
632 |
+} |
|
633 |
+ |
|
609 | 634 |
sub txn_scope { |
610 | 635 |
my $self = shift; |
611 | 636 |
|
... | ... |
@@ -1351,6 +1376,18 @@ Rollback is automatically done. |
1351 | 1376 |
Note that this is feature of L<DBIx::TransactionManager> |
1352 | 1377 |
L<DBIx::TransactionManager> is required. |
1353 | 1378 |
|
1379 |
+=head2 C<table> |
|
1380 |
+ |
|
1381 |
+ $dbi->table('book', |
|
1382 |
+ insert => sub { ... }, |
|
1383 |
+ update => sub { ... } |
|
1384 |
+ ); |
|
1385 |
+ |
|
1386 |
+ my $table = $dbi->table('book'); |
|
1387 |
+ |
|
1388 |
+Create a L<DBIx::Custom::Table> object, |
|
1389 |
+or get a L<DBIx::Custom::Table> object. |
|
1390 |
+ |
|
1354 | 1391 |
=head2 C<update_all> |
1355 | 1392 |
|
1356 | 1393 |
$dbi->update_all(table => $table, |
... | ... |
@@ -6,25 +6,25 @@ DBIx::Custom::Guides - DBIx::Custom Guides |
6 | 6 |
|
7 | 7 |
=head2 1. Connect to the database |
8 | 8 |
|
9 |
-C<connect()> method create a new L<DBIx::Custom> |
|
10 |
-object and connect to the database. |
|
11 |
- |
|
12 | 9 |
use DBIx::Custom; |
13 |
- my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
10 |
+ my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=$database", |
|
14 | 11 |
user => 'ken', password => '!LFKD%$&'); |
15 | 12 |
|
16 |
-B<Data source exmaples:> |
|
17 |
- |
|
18 |
-MySQL |
|
13 |
+use C<connect()> to connect to the database. |
|
14 |
+You can sepecfiy C<data_soruce>, C<user>, and C<password>. |
|
19 | 15 |
|
20 |
- "dbi:mysql:database=$database" |
|
21 |
- "dbi:mysql:database=$database;host=$hostname;port=$port" |
|
16 |
+The following ones are data source exmaple in variouse dabase system. |
|
22 | 17 |
|
23 | 18 |
SQLite |
24 | 19 |
|
25 | 20 |
"dbi:SQLite:dbname=$database" |
26 | 21 |
"dbi:SQLite:dbname=:memory:" |
27 | 22 |
|
23 |
+MySQL |
|
24 |
+ |
|
25 |
+ "dbi:mysql:database=$database" |
|
26 |
+ "dbi:mysql:database=$database;host=$hostname;port=$port" |
|
27 |
+ |
|
28 | 28 |
PostgreSQL |
29 | 29 |
|
30 | 30 |
"dbi:Pg:dbname=$dbname" |
... | ... |
@@ -52,7 +52,7 @@ You don't have to create SQL statements. |
52 | 52 |
|
53 | 53 |
Execute insert statement. |
54 | 54 |
|
55 |
- $dbi->insert(table => 'books', |
|
55 |
+ $dbi->insert(table => 'book', |
|
56 | 56 |
param => {title => 'Perl', author => 'Ken'}); |
57 | 57 |
|
58 | 58 |
The following SQL is executed. |
... | ... |
@@ -68,13 +68,13 @@ See also "METHODS" section. |
68 | 68 |
|
69 | 69 |
Execute update statement. |
70 | 70 |
|
71 |
- $dbi->update(table => 'books', |
|
71 |
+ $dbi->update(table => 'book', |
|
72 | 72 |
param => {title => 'Perl', author => 'Ken'}, |
73 | 73 |
where => {id => 5}); |
74 | 74 |
|
75 | 75 |
The following SQL is executed. |
76 | 76 |
|
77 |
- update books set title = ?, author = ?; |
|
77 |
+ update book set title = ?, author = ?; |
|
78 | 78 |
|
79 | 79 |
The values of C<title> and C<author> is embedded into the placeholders. |
80 | 80 |
|
... | ... |
@@ -87,12 +87,12 @@ If you want to update all rows, use C<update_all()> method. |
87 | 87 |
|
88 | 88 |
Execute delete statement. |
89 | 89 |
|
90 |
- $dbi->delete(table => 'books', |
|
90 |
+ $dbi->delete(table => 'book', |
|
91 | 91 |
where => {author => 'Ken'}); |
92 | 92 |
|
93 | 93 |
The following SQL is executed. |
94 | 94 |
|
95 |
- delete from books where id = ?; |
|
95 |
+ delete from book where id = ?; |
|
96 | 96 |
|
97 | 97 |
The value of C<id> is embedded into the placehodler. |
98 | 98 |
|
... | ... |
@@ -105,11 +105,11 @@ If you want to delete all rows, use C<delete_all()> method. |
105 | 105 |
|
106 | 106 |
Execute select statement, only C<table> argument specified : |
107 | 107 |
|
108 |
- my $result = $dbi->select(table => 'books'); |
|
108 |
+ my $result = $dbi->select(table => 'book'); |
|
109 | 109 |
|
110 | 110 |
The following SQL is executed. |
111 | 111 |
|
112 |
- select * from books; |
|
112 |
+ select * from book; |
|
113 | 113 |
|
114 | 114 |
the result of C<select()> method is L<DBIx::Custom::Result> object. |
115 | 115 |
You can fetch a row by C<fetch()> method. |
... | ... |
@@ -125,42 +125,42 @@ See "3. Fetch row". |
125 | 125 |
C<column> and C<where> arguments specified. |
126 | 126 |
|
127 | 127 |
my $result = $dbi->select( |
128 |
- table => 'books', |
|
128 |
+ table => 'book', |
|
129 | 129 |
column => [qw/author title/], |
130 | 130 |
where => {author => 'Ken'} |
131 | 131 |
); |
132 | 132 |
|
133 | 133 |
The following SQL is executed. |
134 | 134 |
|
135 |
- select author, title from books where author = ?; |
|
135 |
+ select author, title from book where author = ?; |
|
136 | 136 |
|
137 | 137 |
the value of C<author> is embdded into the placeholder. |
138 | 138 |
|
139 | 139 |
If you want to join tables, specify C<relation> argument. |
140 | 140 |
|
141 | 141 |
my $result = $dbi->select( |
142 |
- table => ['books', 'rental'], |
|
143 |
- column => ['books.name as book_name'] |
|
144 |
- relation => {'books.id' => 'rental.book_id'} |
|
142 |
+ table => ['book', 'rental'], |
|
143 |
+ column => ['book.name as book_name'] |
|
144 |
+ relation => {'book.id' => 'rental.book_id'} |
|
145 | 145 |
); |
146 | 146 |
|
147 | 147 |
The following SQL is executed. |
148 | 148 |
|
149 |
- select books.name as book_name from books, rental |
|
150 |
- where books.id = rental.book_id; |
|
149 |
+ select book.name as book_name from book, rental |
|
150 |
+ where book.id = rental.book_id; |
|
151 | 151 |
|
152 | 152 |
If you want to add some string to the end of SQL statement, |
153 | 153 |
use C<append> argument. |
154 | 154 |
|
155 | 155 |
my $result = $dbi->select( |
156 |
- table => 'books', |
|
156 |
+ table => 'book', |
|
157 | 157 |
where => {author => 'Ken'}, |
158 | 158 |
append => 'order by price limit 5', |
159 | 159 |
); |
160 | 160 |
|
161 | 161 |
The following SQL is executed. |
162 | 162 |
|
163 |
- select * books where author = ? order by price limit 5; |
|
163 |
+ select * book where author = ? order by price limit 5; |
|
164 | 164 |
|
165 | 165 |
C<filter> argument can be specified. |
166 | 166 |
see also "METHODS" section. |
... | ... |
@@ -238,7 +238,7 @@ At frist, I show normal parameter binding. |
238 | 238 |
use DBI; |
239 | 239 |
my $dbh = DBI->connect(...); |
240 | 240 |
my $sth = $dbh->prepare( |
241 |
- "select * from books where author = ? and title like ?;" |
|
241 |
+ "select * from book where author = ? and title like ?;" |
|
242 | 242 |
); |
243 | 243 |
$sth->execute('Ken', '%Perl%'); |
244 | 244 |
|
... | ... |
@@ -249,7 +249,7 @@ L<DBIx::Custom> hash parameter binding system improve |
249 | 249 |
normal parameter binding to use hash parameter. |
250 | 250 |
|
251 | 251 |
my $result = $dbi->execute( |
252 |
- "select * from books where {= author} and {like title};" |
|
252 |
+ "select * from book where {= author} and {like title};" |
|
253 | 253 |
param => {author => 'Ken', title => '%Perl%'} |
254 | 254 |
); |
255 | 255 |
|
... | ... |
@@ -257,8 +257,8 @@ This is same as the normal way, execpt that the parameter is hash. |
257 | 257 |
{= author} and {like title} is called C<tag>. |
258 | 258 |
tag is expand to placeholder string internally. |
259 | 259 |
|
260 |
- select * from books where {= author} and {like title} |
|
261 |
- -> select * from books where author = ? and title like ?; |
|
260 |
+ select * from book where {= author} and {like title} |
|
261 |
+ -> select * from book where author = ? and title like ?; |
|
262 | 262 |
|
263 | 263 |
The following tags is available. |
264 | 264 |
|
... | ... |
@@ -284,7 +284,7 @@ C<{> and C<}> is reserved. If you use these charactors, |
284 | 284 |
you must escape them using '\'. Note that '\' is |
285 | 285 |
already perl escaped charactor, so you must write '\\'. |
286 | 286 |
|
287 |
- 'select * from books \\{ something statement \\}' |
|
287 |
+ 'select * from book \\{ something statement \\}' |
|
288 | 288 |
|
289 | 289 |
=head2 5. Filtering |
290 | 290 |
|
... | ... |
@@ -307,7 +307,7 @@ C<encode_utf8> and C<decode_utf8> filter is registerd by default. |
307 | 307 |
You can specify these filters to C<filter> argument of C<execute()> method. |
308 | 308 |
|
309 | 309 |
my $result = $dbi->execute( |
310 |
- "select * from books where {= author} and {like title};" |
|
310 |
+ "select * from book where {= author} and {like title};" |
|
311 | 311 |
param => {author => 'Ken', title => '%Perl%'}, |
312 | 312 |
filter => {author => 'to_upper_case, title => 'encode_utf8'} |
313 | 313 |
); |
... | ... |
@@ -317,13 +317,13 @@ C<insert()>, C<update()>, C<update_all()>, |
317 | 317 |
C<delete()>, C<delete_all()>, C<select()>. |
318 | 318 |
|
319 | 319 |
# insert(), having filter argument |
320 |
- $dbi->insert(table => 'books', |
|
320 |
+ $dbi->insert(table => 'book', |
|
321 | 321 |
param => {title => 'Perl', author => 'Ken'}, |
322 | 322 |
filter => {title => 'encode_utf8'}); |
323 | 323 |
|
324 | 324 |
# select(), having filter argument |
325 | 325 |
my $result = $dbi->select( |
326 |
- table => 'books', |
|
326 |
+ table => 'book', |
|
327 | 327 |
column => [qw/author title/], |
328 | 328 |
where => {author => 'Ken'}, |
329 | 329 |
append => 'order by id limit 1', |
... | ... |
@@ -338,7 +338,7 @@ C<filter()> argument overwrites this default filter. |
338 | 338 |
|
339 | 339 |
$dbi->default_bind_filter('encode_utf8'); |
340 | 340 |
$dbi->insert( |
341 |
- table => 'books', |
|
341 |
+ table => 'book', |
|
342 | 342 |
param => {title => 'Perl', author => 'Ken', price => 1000}, |
343 | 343 |
filter => {author => 'to_upper_case', price => undef} |
344 | 344 |
); |
... | ... |
@@ -346,14 +346,14 @@ C<filter()> argument overwrites this default filter. |
346 | 346 |
This is same as the following example. |
347 | 347 |
|
348 | 348 |
$dbi->insert( |
349 |
- table => 'books', |
|
349 |
+ table => 'book', |
|
350 | 350 |
param => {title => 'Perl', author => 'Ken', price => 1000}, |
351 | 351 |
filter => {title => 'encode_uft8' author => 'to_upper_case'} |
352 | 352 |
); |
353 | 353 |
|
354 | 354 |
You can also specify filter when the row is fetched. This is reverse of bind filter. |
355 | 355 |
|
356 |
- my $result = $dbi->select(table => 'books'); |
|
356 |
+ my $result = $dbi->select(table => 'book'); |
|
357 | 357 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
358 | 358 |
|
359 | 359 |
Filter works each column value, but you prepare a default filter |
... | ... |
@@ -366,7 +366,7 @@ overwrites this default filter. |
366 | 366 |
|
367 | 367 |
$dbi->default_fetch_filter('decode_utf8'); |
368 | 368 |
my $result = $dbi->select( |
369 |
- table => 'books', |
|
369 |
+ table => 'book', |
|
370 | 370 |
columns => ['title', 'author', 'price'] |
371 | 371 |
); |
372 | 372 |
$result->filter({author => 'to_upper_case', price => undef}); |
... | ... |
@@ -374,7 +374,7 @@ overwrites this default filter. |
374 | 374 |
This is same as the following one. |
375 | 375 |
|
376 | 376 |
my $result = $dbi->select( |
377 |
- table => 'books', |
|
377 |
+ table => 'book', |
|
378 | 378 |
columns => ['title', 'author', 'price'] |
379 | 379 |
); |
380 | 380 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
... | ... |
@@ -431,17 +431,6 @@ SQLite |
431 | 431 |
|
432 | 432 |
=head2 6. Get high performance |
433 | 433 |
|
434 |
-=head3 Disable filter checking |
|
435 |
- |
|
436 |
-Filter checking is executed by default. |
|
437 |
-This is done to check right filter name is specified, |
|
438 |
-but sometimes damage performance. |
|
439 |
- |
|
440 |
-If you disable this filter checking, |
|
441 |
-Set C<filter_check> attribute to 0. |
|
442 |
- |
|
443 |
- $dbi->filter_check(0); |
|
444 |
- |
|
445 | 434 |
=head3 Use execute() method instead suger methods |
446 | 435 |
|
447 | 436 |
If you execute insert statement by C<insert()> method, |
... | ... |
@@ -453,65 +442,30 @@ is created every time. |
453 | 442 |
In that case, you can prepare a query by C<create_query()> method. |
454 | 443 |
|
455 | 444 |
my $query = $dbi->create_query( |
456 |
- "insert into books {insert_param title author};" |
|
445 |
+ "insert into book {insert_param title author};" |
|
457 | 446 |
); |
458 | 447 |
|
459 | 448 |
Return value of C<create_query()> is L<DBIx::Custom::Query> object. |
460 | 449 |
This keep the information of SQL and column names. |
461 | 450 |
|
462 | 451 |
{ |
463 |
- sql => 'insert into books (title, author) values (?, ?);', |
|
452 |
+ sql => 'insert into book (title, author) values (?, ?);', |
|
464 | 453 |
columns => ['title', 'author'] |
465 | 454 |
} |
466 | 455 |
|
467 | 456 |
Execute query repeatedly. |
468 | 457 |
|
469 |
- my $inputs = [ |
|
458 |
+ my $params = [ |
|
470 | 459 |
{title => 'Perl', author => 'Ken'}, |
471 | 460 |
{title => 'Good days', author => 'Mike'} |
472 | 461 |
]; |
473 | 462 |
|
474 |
- foreach my $input (@$inputs) { |
|
475 |
- $dbi->execute($query, $input); |
|
463 |
+ foreach my $param (@$params) { |
|
464 |
+ $dbi->execute($query, $param); |
|
476 | 465 |
} |
477 | 466 |
|
478 | 467 |
This is faster than C<insert()> method. |
479 | 468 |
|
480 |
-=head3 caching |
|
481 |
- |
|
482 |
-C<execute()> method caches the parsed result of the source of SQL. |
|
483 |
-Default to 1 |
|
484 |
- |
|
485 |
- $dbi->cache(1); |
|
486 |
- |
|
487 |
-Caching is on memory, but you can change this by C<cache_method()>. |
|
488 |
-First argument is L<DBIx::Custom> object. |
|
489 |
-Second argument is a source of SQL, |
|
490 |
-such as "select * from books where {= title} and {= author};"; |
|
491 |
-Third argument is parsed result, such as |
|
492 |
-{sql => "select * from books where title = ? and author = ?", |
|
493 |
- columns => ['title', 'author']}, this is hash reference. |
|
494 |
-If arguments is more than two, this method is called to set cache. |
|
495 |
-If not, this method is called to get cache. |
|
496 |
- |
|
497 |
- $dbi->cache_method(sub { |
|
498 |
- sub { |
|
499 |
- my $self = shift; |
|
500 |
- |
|
501 |
- $self->{_cached} ||= {}; |
|
502 |
- |
|
503 |
- # Set cache |
|
504 |
- if (@_ > 1) { |
|
505 |
- $self->{_cached}{$_[0]} = $_[1] |
|
506 |
- } |
|
507 |
- |
|
508 |
- # Get cache |
|
509 |
- else { |
|
510 |
- return $self->{_cached}{$_[0]} |
|
511 |
- } |
|
512 |
- } |
|
513 |
- }); |
|
514 |
- |
|
515 | 469 |
=head2 7. More features |
516 | 470 |
|
517 | 471 |
=head3 Get DBI object |
... | ... |
@@ -94,7 +94,7 @@ C<insert()>、C<update()>、C<delete()>、C<select()> |
94 | 94 |
|
95 | 95 |
C<insert>メソッドです。データベースにデータを挿入します。 |
96 | 96 |
|
97 |
- $dbi->insert(table => 'books', |
|
97 |
+ $dbi->insert(table => 'book', |
|
98 | 98 |
param => {title => 'Perl', author => 'Ken'}); |
99 | 99 |
|
100 | 100 |
これは次のL<DBI>の操作と同じです。 |
... | ... |
@@ -106,14 +106,14 @@ C<insert>メソッドです。データベースにデータを挿入します |
106 | 106 |
|
107 | 107 |
C<update>メソッドです。データベースのデータを更新します。 |
108 | 108 |
|
109 |
- $dbi->update(table => 'books', |
|
109 |
+ $dbi->update(table => 'book', |
|
110 | 110 |
param => {title => 'Perl', author => 'Ken'}, |
111 | 111 |
where => {id => 5}); |
112 | 112 |
|
113 | 113 |
これは次のL<DBI>の操作と同じです。 |
114 | 114 |
|
115 | 115 |
my $sth = $dbh->prepare( |
116 |
- 'update books set title = ?, author = ? where id = ?;'); |
|
116 |
+ 'update book set title = ?, author = ? where id = ?;'); |
|
117 | 117 |
$sth->execute('Perl', 'Ken', 5); |
118 | 118 |
|
119 | 119 |
C<update>メソッドは安全のため |
... | ... |
@@ -121,19 +121,19 @@ where句のないSQLを発行することを許可していません。 |
121 | 121 |
もしすべての行を更新したい場合は |
122 | 122 |
C<update_all()>メソッドを使用してください。 |
123 | 123 |
|
124 |
- $dbi->update_all(table => 'books', |
|
124 |
+ $dbi->update_all(table => 'book', |
|
125 | 125 |
param => {title => 'Perl', author => 'Ken'}); |
126 | 126 |
|
127 | 127 |
=head3 C<delete()> |
128 | 128 |
|
129 | 129 |
C<delete>メソッドです。データベースのデータを削除します。 |
130 | 130 |
|
131 |
- $dbi->delete(table => 'books', |
|
131 |
+ $dbi->delete(table => 'book', |
|
132 | 132 |
where => {author => 'Ken'}); |
133 | 133 |
|
134 | 134 |
これは次のL<DBI>の操作と同じです。 |
135 | 135 |
|
136 |
- my $sth = $dbh->prepare('delete from books where id = ?;'); |
|
136 |
+ my $sth = $dbh->prepare('delete from book where id = ?;'); |
|
137 | 137 |
$sth->execute('Ken'); |
138 | 138 |
|
139 | 139 |
C<delete>メソッドは安全のため |
... | ... |
@@ -141,17 +141,17 @@ where句のないSQLを発行することを許可していません。 |
141 | 141 |
もしすべての行を削除したい場合は |
142 | 142 |
C<delete_all()>メソッドを使用してください。 |
143 | 143 |
|
144 |
- $dbi->delete_all(table => 'books'); |
|
144 |
+ $dbi->delete_all(table => 'book'); |
|
145 | 145 |
|
146 | 146 |
=head3 C<select()> |
147 | 147 |
|
148 | 148 |
C<select>メソッドです。テーブル名だけを指定しています。 |
149 | 149 |
|
150 |
- my $result = $dbi->select(table => 'books'); |
|
150 |
+ my $result = $dbi->select(table => 'book'); |
|
151 | 151 |
|
152 | 152 |
これは次のL<DBI>の操作と同じです。 |
153 | 153 |
|
154 |
- my $sth = $dbh->prepare('select * from books;); |
|
154 |
+ my $sth = $dbh->prepare('select * from book;); |
|
155 | 155 |
$sth->execute; |
156 | 156 |
|
157 | 157 |
C<select()>メソッドの戻り値はL<DBIx::Custom::Result> |
... | ... |
@@ -166,7 +166,7 @@ C<select()>メソッドの戻り値はL<DBIx::Custom::Result> |
166 | 166 |
次のC<select>は行の名前とwhere句を指定したものです。 |
167 | 167 |
|
168 | 168 |
my $result = $dbi->select( |
169 |
- table => 'books', |
|
169 |
+ table => 'book', |
|
170 | 170 |
column => [qw/author title/], |
171 | 171 |
where => {author => 'Ken'} |
172 | 172 |
); |
... | ... |
@@ -174,28 +174,28 @@ C<select()>メソッドの戻り値はL<DBIx::Custom::Result> |
174 | 174 |
次のL<DBI>の操作と同じです。 |
175 | 175 |
|
176 | 176 |
my $sth = $dbh->prepare( |
177 |
- 'select author, title from books where author = ?;'); |
|
177 |
+ 'select author, title from book where author = ?;'); |
|
178 | 178 |
$sht->execute('Ken'); |
179 | 179 |
|
180 | 180 |
テーブルをjoinしたい場合はC<relation>を使用します。 |
181 | 181 |
|
182 | 182 |
my $result = $dbi->select( |
183 |
- table => ['books', 'rental'], |
|
184 |
- column => ['books.name as book_name'] |
|
185 |
- relation => {'books.id' => 'rental.book_id'} |
|
183 |
+ table => ['book', 'rental'], |
|
184 |
+ column => ['book.name as book_name'] |
|
185 |
+ relation => {'book.id' => 'rental.book_id'} |
|
186 | 186 |
); |
187 | 187 |
|
188 | 188 |
次のL<DBI>の操作と同じです。 |
189 | 189 |
|
190 | 190 |
my $sth = $dbh->prepare( |
191 |
- 'select books.name as book_name from books, rental' . |
|
192 |
- 'where books.id = rental.book_id;'); |
|
191 |
+ 'select book.name as book_name from book, rental' . |
|
192 |
+ 'where book.id = rental.book_id;'); |
|
193 | 193 |
$sth->execute; |
194 | 194 |
|
195 | 195 |
SQL文の末尾に文字列を追加したい場合は<append>オプションを使用します。 |
196 | 196 |
|
197 | 197 |
my $result = $dbi->select( |
198 |
- table => 'books', |
|
198 |
+ table => 'book', |
|
199 | 199 |
where => {author => 'Ken'}, |
200 | 200 |
append => 'order by price limit 5', |
201 | 201 |
); |
... | ... |
@@ -203,7 +203,7 @@ SQL文の末尾に文字列を追加したい場合は<append>オプションを |
203 | 203 |
次のL<DBI>の操作と同じです。 |
204 | 204 |
|
205 | 205 |
my $sth = $dbh->prepare( |
206 |
- 'select * books where author = ? order by price limit 5;'); |
|
206 |
+ 'select * book where author = ? order by price limit 5;'); |
|
207 | 207 |
$sth->execute; |
208 | 208 |
|
209 | 209 |
C<append>オプションは、C<insert()>、C<update()>、C<update_all()> |
... | ... |
@@ -213,7 +213,7 @@ C<delete()>、C<select>メソッドで使用することが |
213 | 213 |
この後のフィルタリングの解説で詳しく扱いますが、値をフィルタリングしたい |
214 | 214 |
場合はC<filter>オプションを使用することができます。 |
215 | 215 |
|
216 |
- $dbi->insert(table => 'books', |
|
216 |
+ $dbi->insert(table => 'book', |
|
217 | 217 |
param => {title => 'Perl', author => 'Ken'}); |
218 | 218 |
filter => {title => 'encode_utf8', |
219 | 219 |
author => 'encode_utf8'}); |
... | ... |
@@ -228,7 +228,7 @@ C<select()>メソッドのC<where>オプションではハッシュの代わり |
228 | 228 |
|
229 | 229 |
# Select, more flexible where |
230 | 230 |
my $result = $dbi->select( |
231 |
- table => 'books', |
|
231 |
+ table => 'book', |
|
232 | 232 |
where => ['{= author} and {like title}', |
233 | 233 |
{author => 'Ken', title => '%Perl%'}] |
234 | 234 |
); |
... | ... |
@@ -328,7 +328,7 @@ L<DBIx::Custom>はハッシュパラメタバインドを提供します。 |
328 | 328 |
use DBI; |
329 | 329 |
my $dbh = DBI->connect(...); |
330 | 330 |
my $sth = $dbh->prepare( |
331 |
- "select * from books where author = ? and title like ?;" |
|
331 |
+ "select * from book where author = ? and title like ?;" |
|
332 | 332 |
); |
333 | 333 |
$sth->execute('Ken', '%Perl%'); |
334 | 334 |
|
... | ... |
@@ -342,18 +342,18 @@ L<DBIx::Custom>はこれを改善して、ハッシュで |
342 | 342 |
パラメタを指定できるようにしました。 |
343 | 343 |
|
344 | 344 |
my $result = $dbi->execute( |
345 |
- "select * from books where {= author} and {like title};" |
|
345 |
+ "select * from book where {= author} and {like title};" |
|
346 | 346 |
param => {author => 'Ken', title => '%Perl%'} |
347 | 347 |
); |
348 | 348 |
|
349 | 349 |
C<{= author}>とC<{like title}>はタグと呼ばれます。 |
350 | 350 |
タグは内部ではプレースホルダを含む文字列に置き換えられます。 |
351 | 351 |
|
352 |
- select * from books where {= author} and {like title} |
|
352 |
+ select * from book where {= author} and {like title} |
|
353 | 353 |
|
354 | 354 |
という文は以下のSQLに置き換えられます。 |
355 | 355 |
|
356 |
- select * from books where author = ? and title like ?; |
|
356 |
+ select * from book where author = ? and title like ?; |
|
357 | 357 |
|
358 | 358 |
このようにタグを使ってSQL文を表現するのがL<DBIx::Custom>の |
359 | 359 |
特徴です。以下のタグが利用可能です。 |
... | ... |
@@ -382,7 +382,7 @@ C<{>とC<}>は予約語です。これらの文字を使いたい場合は |
382 | 382 |
エスケープするためには'\\'と書く必要があることに注意 |
383 | 383 |
してください。 |
384 | 384 |
|
385 |
- 'select * from books \\{ something statement \\}' |
|
385 |
+ 'select * from book \\{ something statement \\}' |
|
386 | 386 |
|
387 | 387 |
=head2 5. フィルタリング |
388 | 388 |
|
... | ... |
@@ -412,7 +412,7 @@ C<register_filter()>メソッドを使用して |
412 | 412 |
で指定することができます。 |
413 | 413 |
|
414 | 414 |
my $result = $dbi->execute( |
415 |
- "select * from books where {= author} and {like title};" |
|
415 |
+ "select * from book where {= author} and {like title};" |
|
416 | 416 |
param => {author => 'Ken', title => '%Perl%'}, |
417 | 417 |
filter => {author => 'to_upper_case, title => 'encode_utf8'} |
418 | 418 |
); |
... | ... |
@@ -426,13 +426,13 @@ C<delete()>、C<select()> |
426 | 426 |
メソッドにおいても使用することができます。 |
427 | 427 |
|
428 | 428 |
# insert() with filter option |
429 |
- $dbi->insert(table => 'books', |
|
429 |
+ $dbi->insert(table => 'book', |
|
430 | 430 |
param => {title => 'Perl', author => 'Ken'}, |
431 | 431 |
filter => {title => 'encode_utf8'}); |
432 | 432 |
|
433 | 433 |
# select() with filter option |
434 | 434 |
my $result = $dbi->select( |
435 |
- table => 'books', |
|
435 |
+ table => 'book', |
|
436 | 436 |
column => [qw/author title/], |
437 | 437 |
where => {author => 'Ken'}, |
438 | 438 |
append => 'order by id limit 1', |
... | ... |
@@ -449,7 +449,7 @@ C<filter()>オプションで指定されたフィルタは、 |
449 | 449 |
|
450 | 450 |
$dbi->default_bind_filter('encode_utf8'); |
451 | 451 |
$dbi->insert( |
452 |
- table => 'books', |
|
452 |
+ table => 'book', |
|
453 | 453 |
param => {title => 'Perl', author => 'Ken', price => 1000}, |
454 | 454 |
filter => {author => 'to_upper_case', price => undef} |
455 | 455 |
); |
... | ... |
@@ -510,7 +510,7 @@ SQLite |
510 | 510 |
これはL<DBIx::Custom::Result>クラスのC<filter>メソッドを使って |
511 | 511 |
行います。 |
512 | 512 |
|
513 |
- my $result = $dbi->select(table => 'books'); |
|
513 |
+ my $result = $dbi->select(table => 'book'); |
|
514 | 514 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
515 | 515 |
|
516 | 516 |
フィルタはそれぞれの列の値に作用しますが、 |
... | ... |
@@ -522,7 +522,7 @@ C<filter>メソッドで設定されたフィルタはデフォルトのフィ |
522 | 522 |
|
523 | 523 |
$dbi->default_fetch_filter('decode_utf8'); |
524 | 524 |
my $result = $dbi->select( |
525 |
- table => 'books', |
|
525 |
+ table => 'book', |
|
526 | 526 |
columns => ['title', 'author', 'price'] |
527 | 527 |
); |
528 | 528 |
$result->filter({author => 'to_upper_case', price => undef}); |
... | ... |
@@ -561,7 +561,7 @@ C<insert()>メソッドは、SQL文とステートメントハンドルを |
561 | 561 |
クエリを用意しておくことができます。 |
562 | 562 |
|
563 | 563 |
my $query = $dbi->create_query( |
564 |
- "insert into books {insert_param title author};" |
|
564 |
+ "insert into book {insert_param title author};" |
|
565 | 565 |
); |
566 | 566 |
|
567 | 567 |
戻り値はL<DBIx::Custom::Query>オブジェクトです。 |
... | ... |
@@ -569,7 +569,7 @@ C<insert()>メソッドは、SQL文とステートメントハンドルを |
569 | 569 |
保持しています。またステートメントハンドルも保持しています。 |
570 | 570 |
|
571 | 571 |
{ |
572 |
- sql => 'insert into books (title, author) values (?, ?);', |
|
572 |
+ sql => 'insert into book (title, author) values (?, ?);', |
|
573 | 573 |
columns => ['title', 'author'], |
574 | 574 |
sth => $sth |
575 | 575 |
} |
... | ... |
@@ -617,10 +617,10 @@ C<execute()>メソッドはデフォルトでSQL文への解析結果をキャ |
617 | 617 |
}); |
618 | 618 |
|
619 | 619 |
最初の引数はL<DBIx::Custom>です。第二引数は、 |
620 |
-"select * from books where {= title} and {= author};"; |
|
620 |
+"select * from book where {= title} and {= author};"; |
|
621 | 621 |
のようなSQL文の源です。 |
622 | 622 |
第三引数は |
623 |
-{sql => "select * from books where title = ? and author = ?", |
|
623 |
+{sql => "select * from book where title = ? and author = ?", |
|
624 | 624 |
columns => ['title', 'author']} |
625 | 625 |
のような解析後のデータです。これはハッシュリファレンスである必要 |
626 | 626 |
があります。 |
... | ... |
@@ -711,18 +711,18 @@ C<expand>メソッドを使用すると次のようなハッシュに含まれ |
711 | 711 |
|
712 | 712 |
以下のハッシュ |
713 | 713 |
|
714 |
- {books => {title => 'Perl', author => 'Ken'}} |
|
714 |
+ {book => {title => 'Perl', author => 'Ken'}} |
|
715 | 715 |
|
716 | 716 |
は次のように展開されます。 |
717 | 717 |
|
718 |
- ('books.title' => 'Perl', 'books.author' => 'Ken') |
|
718 |
+ ('book.title' => 'Perl', 'book.author' => 'Ken') |
|
719 | 719 |
|
720 | 720 |
これはテーブル名を含むselect文で利用すると便利です。 |
721 | 721 |
|
722 | 722 |
my $param = {title => 'Perl', author => '%Ken%'}; |
723 | 723 |
$dbi->execute( |
724 |
- 'select * from books where {= books.title} && {like books.author};', |
|
725 |
- param => {$dbi->expand({books => $param})} |
|
724 |
+ 'select * from book where {= book.title} && {like book.author};', |
|
725 |
+ param => {$dbi->expand({book => $param})} |
|
726 | 726 |
); |
727 | 727 |
|
728 | 728 |
=cut |
... | ... |
@@ -1,73 +0,0 @@ |
1 |
-package DBIx::Custom::Model; |
|
2 |
- |
|
3 |
-use strict; |
|
4 |
-use warnings; |
|
5 |
- |
|
6 |
-use base 'Object::Simple'; |
|
7 |
- |
|
8 |
-use Carp 'croak'; |
|
9 |
- |
|
10 |
-__PACKAGE__->attr(dbi => sub { DBIx::Custom->new }); |
|
11 |
-__PACKAGE__->attr(table_class => 'DBIx::Custom::Table'); |
|
12 |
-__PACKAGE__->attr(tables => sub { {} }); |
|
13 |
- |
|
14 |
-sub table { |
|
15 |
- my ($self, $name) = @_; |
|
16 |
- |
|
17 |
- # Table class |
|
18 |
- my $table_class = $self->table_class; |
|
19 |
- croak qq{Invalid table class name "$table_class"} |
|
20 |
- unless $table_class =~ /^[\w:]+$/; |
|
21 |
- unless ($table_class->can('isa')) { |
|
22 |
- eval "require $table_class"; |
|
23 |
- croak $@ if $@; |
|
24 |
- } |
|
25 |
- # Create table |
|
26 |
- $self->tables->{$name} |
|
27 |
- = $table_class->new(name => $name, dbi => $self->dbi, model => $self) |
|
28 |
- unless defined $self->tables->{$name}; |
|
29 |
- |
|
30 |
- return $self->{tables}{$name}; |
|
31 |
-} |
|
32 |
- |
|
33 |
-1; |
|
34 |
- |
|
35 |
-=head1 NAME |
|
36 |
- |
|
37 |
-DBIx::Custom::Model - Table class(experimental) |
|
38 |
- |
|
39 |
-=head1 SYNOPSIS |
|
40 |
- |
|
41 |
-use MyModel; |
|
42 |
- |
|
43 |
-use base 'DBIx::Custom::Model'; |
|
44 |
- |
|
45 |
-sub new { |
|
46 |
- my $self = shift->SUPER::new(@_); |
|
47 |
- |
|
48 |
- my $dbi = DBIx::Custom->connect(...); |
|
49 |
- |
|
50 |
- $self->dbi($dbi); |
|
51 |
- |
|
52 |
- $self->table('book')->helper( |
|
53 |
- insert => sub { |
|
54 |
- my $self = shift; |
|
55 |
- |
|
56 |
- return $self->dbi->insert(table => $self->name, @_); |
|
57 |
- } |
|
58 |
- ); |
|
59 |
- |
|
60 |
- return $self; |
|
61 |
-} |
|
62 |
- |
|
63 |
-=head1 METHODS |
|
64 |
- |
|
65 |
-L<DBIx::Custom> inherits all methods from L<Object::Simple> |
|
66 |
-and implements the following new ones. |
|
67 |
- |
|
68 |
-=head2 C<table> |
|
69 |
- |
|
70 |
- my $table = $model->table('book'); |
|
71 |
- |
|
72 |
-Get a L<DBIx::Custom::Table>, or create a L<DBIx::Custom::Table> object if not exists. |
|
73 |
- |
... | ... |
@@ -62,7 +62,6 @@ my $insert_query; |
62 | 62 |
my $update_query; |
63 | 63 |
my $ret_val; |
64 | 64 |
my $infos; |
65 |
-my $model; |
|
66 | 65 |
my $table; |
67 | 66 |
|
68 | 67 |
# Prepare table |
... | ... |
@@ -749,27 +748,10 @@ is_deeply($infos, |
749 | 748 |
, $test |
750 | 749 |
); |
751 | 750 |
|
752 |
-test 'model'; |
|
753 |
-{ |
|
754 |
- package MyModel1; |
|
755 |
- |
|
756 |
- use base 'DBIx::Custom::Model'; |
|
757 |
- |
|
758 |
- sub new { |
|
759 |
- my $self = shift->SUPER::new(@_); |
|
760 |
- |
|
761 |
- my $dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
762 |
- |
|
763 |
- $self->dbi($dbi); |
|
764 |
- |
|
765 |
- return $self; |
|
766 |
- } |
|
767 |
-} |
|
768 |
-$model = MyModel1->new; |
|
769 |
-$model->dbi->execute($CREATE_TABLE->{0}); |
|
770 |
-$table = $model->table('table1'); |
|
771 |
-is($table, $model->table('table1')); |
|
772 |
-is($table->model, $model); |
|
751 |
+test 'table'; |
|
752 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
753 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
754 |
+$table = $dbi->table('table1'); |
|
773 | 755 |
$table->insert(param => {key1 => 1, key2 => 2}); |
774 | 756 |
$table->insert(param => {key1 => 3, key2 => 4}); |
775 | 757 |
$rows = $table->select->fetch_hash_all; |
... | ... |
@@ -794,6 +776,14 @@ $table->delete_all; |
794 | 776 |
$rows = $table->select->fetch_hash_all; |
795 | 777 |
is_deeply($rows, [], "$test: delete_all"); |
796 | 778 |
|
779 |
+$dbi->dbh->do($CREATE_TABLE->{2}); |
|
780 |
+$dbi->table('table2', ppp => sub { |
|
781 |
+ my $self = shift; |
|
782 |
+ |
|
783 |
+ return $self->name; |
|
784 |
+}); |
|
785 |
+is($dbi->table('table2')->ppp, 'table2', "$test : helper"); |
|
786 |
+ |
|
797 | 787 |
test 'limit'; |
798 | 788 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
799 | 789 |
$dbi->execute($CREATE_TABLE->{0}); |
... | ... |
@@ -830,3 +820,30 @@ $rows = $dbi->select( |
830 | 820 |
)->fetch_hash_all; |
831 | 821 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
832 | 822 |
|
823 |
+test 'connect super'; |
|
824 |
+{ |
|
825 |
+ package MyDBI; |
|
826 |
+ |
|
827 |
+ use base 'DBIx::Custom'; |
|
828 |
+ sub connect { |
|
829 |
+ my $self = shift->SUPER::connect(@_); |
|
830 |
+ |
|
831 |
+ return $self; |
|
832 |
+ } |
|
833 |
+ |
|
834 |
+ sub new { |
|
835 |
+ my $self = shift->SUPER::connect(@_); |
|
836 |
+ |
|
837 |
+ return $self; |
|
838 |
+ } |
|
839 |
+} |
|
840 |
+ |
|
841 |
+$dbi = MyDBI->connect($NEW_ARGS->{0}); |
|
842 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
843 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
844 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |
|
845 |
+ |
|
846 |
+$dbi = MyDBI->new($NEW_ARGS->{0}); |
|
847 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
848 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
849 |
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test); |