update pod
|
1 |
=encoding utf8 |
2 | ||
added DBIx::Custom::Guides
|
3 |
=head1 NAME |
4 | ||
update pod
|
5 |
DBIx::Custom::Guide - DBIx::Custom Guide |
added DBIx::Custom::Guides
|
6 | |
pod fix
|
7 |
=head1 GUIDE |
added DBIx::Custom::Guides
|
8 | |
update pod
|
9 |
B<This guide is now writing.> |
update pod
|
10 | |
update pod
|
11 |
L<DBIx::Custom> is the class to make easy to execute SQL. |
12 |
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>. |
|
13 |
You can do thing more easy than L<DBIx::Class>, more flexible |
|
14 |
than L<DBIx::Simple>. |
|
added DBIx::Custom::Guides
|
15 | |
update pod
|
16 |
L<DBIx::Custom> is not O/R mapper, O/R mapper is usefule, but |
update pod
|
17 |
you must learn many things. Created SQL is sometimes inefficient, |
18 |
and in many cases you create raw SQL because |
|
19 |
O/R mapper can't make complex SQL |
|
added DBIx::Custom::Guides
|
20 | |
update pod
|
21 |
L<DBIx::Custom> is opposit of O/R mapper. |
22 |
The main purpose is that we respect SQL |
|
23 |
and make easy difficult works if you use only L<DBI>. |
|
24 |
If you already learn SQL, it is easy to use L<DBIx::Custom>. |
|
deprecated DBIx::Custom::MyS...
|
25 | |
update pod
|
26 |
I explain L<DBIx::Custom> a little in this section. |
27 |
In L<DBIx::Custom>, you embbed tag in SQL. |
|
deprecated DBIx::Custom::MyS...
|
28 | |
update pod
|
29 |
select * from book where {= title} and {=author}; |
deprecated DBIx::Custom::MyS...
|
30 | |
update pod
|
31 |
The part arround {} is tag. |
32 |
This SQL is converted to the one which contains place holder. |
|
33 | ||
34 |
select * from book where title = ? and author = ?; |
|
35 | ||
36 |
Maybe you ask me that this conversion is meaningful. |
|
37 |
On the top of this, usuful features is implemented. |
|
38 |
See the following descriptions. |
|
39 | ||
40 |
=over 4 |
|
41 | ||
42 |
=item 1. Specify place holder binding value as hash refernce |
|
43 | ||
44 |
If you use L<DBI>, you must specify place holder binding value |
|
45 |
as array. |
|
46 | ||
47 |
$sth->execute(@bind); |
|
48 | ||
49 |
If you use L<DBIx::Custom>, you specify it as hash reference. |
|
50 |
|
|
51 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
52 |
$dbi->execute($sql, $param); |
|
53 | ||
54 |
=item 2. Filtering |
|
55 | ||
56 |
L<DBIx::Custom> provides filtering system. |
|
57 |
For example, You think that about date value you want to |
|
58 |
manipulate it as date object like L<Time::Piece> in Perl, |
|
59 |
and want to convert it to database DATE format. |
|
60 |
and want to do reverse. |
|
61 | ||
62 |
You can use filtering system. |
|
63 | ||
64 |
At first, register filter. |
|
65 | ||
66 |
$dbi->register_filter( |
|
67 |
tp_to_date => sub { |
|
68 |
... |
|
69 |
}, |
|
70 |
date_to_tp => sub { |
|
71 |
... |
|
72 |
} |
|
73 |
); |
|
74 | ||
75 |
next, apply this filter to each column. |
|
76 | ||
77 |
$dbi->apply_filter('book', |
|
78 |
'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'} |
|
79 |
); |
|
80 | ||
81 |
C<out> is perl-to-database way. C<in> is perl-from-database way. |
|
82 | ||
83 |
This filter is automatically enabled in many method. |
|
84 | ||
85 |
$dbi->insert(table => 'book', param => {issue_date => $tp}); |
|
86 | ||
87 | ||
88 |
=item 3. Selective search condition |
|
89 | ||
90 |
It is difficult to create selective where clause in L<DBI>. |
|
91 |
For example, If C<title> and C<author> is specified, we create |
|
92 |
the following SQL. |
|
93 | ||
94 |
select * from book where title = ? and author = ?; |
|
95 | ||
96 |
If only C<title> is specified, the following one |
|
97 | ||
98 |
select * from book where title = ?; |
|
99 | ||
100 |
If only C<author> is specified, the following one, |
|
101 | ||
102 |
select * from book where author = ?; |
|
103 | ||
104 |
This is hard work. Generally we use modules like L<SQL::Abstract>. |
|
105 |
L<DBIx::Custom> prepare the way to make it easy. |
|
106 | ||
107 |
# Where object |
|
108 |
my $where = $dbi->where; |
|
109 |
|
|
110 |
# Search condition |
|
111 |
$where->clause( |
|
112 |
['and', '{= title}', {'= author'}] |
|
113 |
); |
|
114 |
|
|
115 |
# Setting to automatically select needed column |
|
116 |
$where->param({title => 'Perl'}); |
|
117 | ||
118 |
# Embbed where clause to SQL |
|
119 |
my $sql = "select * from book $where"; |
|
120 | ||
121 |
You can create where clause which has selected search condition. |
|
122 |
You can write nesting of where clause and C<or> condition |
|
123 | ||
update pod
|
124 |
=item 4. Methods for insert, update, delete, select |
update pod
|
125 | |
update pod
|
126 |
L<DBIx::Custom> provides methods for insert, update, delete, select |
127 |
There are C<insert()>, C<update()>, C<delete()>,C<select()>. |
|
update pod
|
128 | |
129 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
130 |
$dbi->insert(table => 'book', param => $param); |
|
131 | ||
update pod
|
132 |
=item 5. Register method for table. |
deprecated DBIx::Custom::MyS...
|
133 | |
update pod
|
134 |
You can register method for table. |
update pod
|
135 | |
update pod
|
136 |
$dbi->table('book')->method( |
update pod
|
137 |
list => sub { |
138 |
... |
|
139 |
}, |
|
update pod
|
140 |
something => sub { |
141 |
... |
|
update pod
|
142 |
} |
143 |
); |
|
144 | ||
update pod
|
145 |
use the mehtod. |
update pod
|
146 | |
147 |
$dbi->table('book')->list; |
|
148 | ||
update pod
|
149 |
Many O/R mapper must create class for table, |
150 |
but L<DBIx::Custom> make it easy. |
|
update pod
|
151 | |
152 |
=back |
|
153 | ||
update pod
|
154 |
L<DBIx::Custom> is very useful. |
155 |
See the following if you are interested in it. |
|
update pod
|
156 | |
update pod
|
157 |
=head2 1. Connect to database |
update pod
|
158 | |
update pod
|
159 |
Load L<DBIx::Custom>. |
update pod
|
160 | |
161 |
use DBIx::Custom; |
|
162 | ||
update pod
|
163 |
use C<connect()> to connect to database. |
164 |
Return value is L<DBIx::Custom> object. |
|
update pod
|
165 | |
166 |
my $dbi = DBIx::Custom->connect( |
|
update pod
|
167 |
data_source => "dbi:mysql:database=bookstore", |
update pod
|
168 |
user => 'ken', |
169 |
password => '!LFKD%$&', |
|
170 |
dbi_options => {mysql_enable_utf8 => 1} |
|
171 |
); |
|
172 | ||
update pod
|
173 |
C<data_source> must be one corresponding to the database system. |
174 |
The following ones are data source example. |
|
update pod
|
175 | |
176 |
B<MySQL> |
|
remove DBIx::Custom::Model
|
177 | |
178 |
"dbi:mysql:database=$database" |
|
179 |
"dbi:mysql:database=$database;host=$hostname;port=$port" |
|
180 | ||
update pod
|
181 |
B<SQLite> |
182 | ||
183 |
"dbi:SQLite:dbname=$database" |
|
184 |
"dbi:SQLite:dbname=:memory:" |
|
185 | ||
186 |
B<PostgreSQL> |
|
deprecated DBIx::Custom::MyS...
|
187 | |
188 |
"dbi:Pg:dbname=$dbname" |
|
189 | ||
update pod
|
190 |
B<Oracle> |
deprecated DBIx::Custom::MyS...
|
191 | |
192 |
"dbi:Oracle:$dbname" |
|
193 |
"dbi:Oracle:host=$host;sid=$sid" |
|
194 | ||
update pod
|
195 |
B<ODBC(Microsoft Access)> |
deprecated DBIx::Custom::MyS...
|
196 | |
197 |
"dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb" |
|
198 | ||
update pod
|
199 |
B<ODBC(SQL Server)> |
deprecated DBIx::Custom::MyS...
|
200 | |
201 |
"dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;" |
|
added DBIx::Custom::Guides
|
202 | |
update pod
|
203 |
If authentication is needed, you can specify C<user> and C<password> |
204 | ||
205 |
L<DBIx::Custom> is wrapper class of L<DBI>. |
|
206 |
You can use all methods of L<DBI> from L<DBIx::Custom> object. |
|
207 | ||
208 |
$dbi->do(...); |
|
209 |
$dbi->begin_work; |
|
update pod
|
210 | |
update pod
|
211 |
use C<dhb()> to get database handle of L<DBI> |
added DBIx::Custom::Guides
|
212 | |
update pod
|
213 |
my $dbh = $dbi->dbh; |
added DBIx::Custom::Guides
|
214 | |
update pod
|
215 |
By default, the following ones is set to database handle attributes. |
216 | ||
217 |
RaiseError -> 1 |
|
218 |
PrintError -> 0 |
|
219 |
AutoCommit -> 1 |
|
added DBIx::Custom::Guides
|
220 | |
update pod
|
221 |
If fatal error occuer, program terminate. |
222 |
If SQL is executed, commit is executed automatically. |
|
update pod
|
223 | |
update pod
|
224 |
=head2 2. Methods for insert, update, delete, or insert |
update pod
|
225 | |
update pod
|
226 |
There are following methods. |
update pod
|
227 | |
update pod
|
228 |
=head3 C<insert()> |
update pod
|
229 | |
update pod
|
230 |
use C<insert()> to insert row into database |
added DBIx::Custom::Guides
|
231 | |
remove DBIx::Custom::Model
|
232 |
$dbi->insert(table => 'book', |
added DBIx::Custom::Guides
|
233 |
param => {title => 'Perl', author => 'Ken'}); |
234 | ||
update pod
|
235 |
C<table> is table name, C<param> is insert data. |
added DBIx::Custom::Guides
|
236 | |
update pod
|
237 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
238 | |
update pod
|
239 |
insert into (title, author) values (?, ?); |
added DBIx::Custom::Guides
|
240 | |
update pod
|
241 |
=head3 C<update()> |
added DBIx::Custom::Guides
|
242 | |
update pod
|
243 |
use C<update()> to update row in database. |
added DBIx::Custom::Guides
|
244 | |
remove DBIx::Custom::Model
|
245 |
$dbi->update(table => 'book', |
added DBIx::Custom::Guides
|
246 |
param => {title => 'Perl', author => 'Ken'}, |
247 |
where => {id => 5}); |
|
248 | ||
update pod
|
249 |
C<table> is table name, C<param> is update data, C<where> is condition. |
added DBIx::Custom::Guides
|
250 | |
update pod
|
251 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
252 | |
update pod
|
253 |
update book set title = ?, author = ?; |
added DBIx::Custom::Guides
|
254 | |
update pod
|
255 |
You can't execute C<update()> without C<where> for safety. |
256 |
use C<update_all()> if you want to update all rows. |
|
added DBIx::Custom::Guides
|
257 | |
update pod
|
258 |
$dbi->update_all(table => 'book', |
259 |
param => {title => 'Perl', author => 'Ken'}); |
|
added DBIx::Custom::Guides
|
260 | |
update pod
|
261 |
=head3 C<delete()> |
added DBIx::Custom::Guides
|
262 | |
update pod
|
263 |
use C<delete()> to delete rows from database. |
added DBIx::Custom::Guides
|
264 | |
remove DBIx::Custom::Model
|
265 |
$dbi->delete(table => 'book', |
added DBIx::Custom::Guides
|
266 |
where => {author => 'Ken'}); |
267 | ||
update pod
|
268 |
C<table> is table name, C<where> is condition. |
added DBIx::Custom::Guides
|
269 | |
update pod
|
270 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
271 | |
update pod
|
272 |
delete from book where id = ?; |
added DBIx::Custom::Guides
|
273 | |
update pod
|
274 |
You can't execute C<delete()> without C<where> for safety. |
275 |
use C<delete_all()> if you want to delete all rows. |
|
added DBIx::Custom::Guides
|
276 | |
update pod
|
277 |
$dbi->delete_all(table => 'book'); |
added DBIx::Custom::Guides
|
278 | |
update pod
|
279 |
=head3 C<select()> |
added DBIx::Custom::Guides
|
280 | |
update pod
|
281 |
use C<select()> to select rows from database |
added DBIx::Custom::Guides
|
282 | |
remove DBIx::Custom::Model
|
283 |
my $result = $dbi->select(table => 'book'); |
added DBIx::Custom::Guides
|
284 | |
update pod
|
285 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
286 | |
remove DBIx::Custom::Model
|
287 |
select * from book; |
added DBIx::Custom::Guides
|
288 | |
update pod
|
289 |
Return value is L<DBIx::Custom::Result> object. |
290 |
use C<fetch()> to fetch row. |
|
added DBIx::Custom::Guides
|
291 | |
292 |
while (my $row = $result->fetch) { |
|
293 |
my $title = $row->[0]; |
|
294 |
my $author = $row->[1]; |
|
295 |
} |
|
296 | ||
update pod
|
297 |
See L<3. Fetch row/"3. Fetch row"> about L<DBIx::Custom::Result>. |
added DBIx::Custom::Guides
|
298 | |
update pod
|
299 |
Continue more examples. |
added DBIx::Custom::Guides
|
300 | |
301 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
302 |
table => 'book', |
update pod
|
303 |
column => ['author', 'title'], |
added DBIx::Custom::Guides
|
304 |
where => {author => 'Ken'} |
305 |
); |
|
306 | ||
update pod
|
307 |
C<column> is column names, C<where> is condition. |
308 | ||
309 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
310 | |
remove DBIx::Custom::Model
|
311 |
select author, title from book where author = ?; |
added DBIx::Custom::Guides
|
312 | |
update pod
|
313 |
Next example. |
added DBIx::Custom::Guides
|
314 | |
315 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
316 |
table => ['book', 'rental'], |
update pod
|
317 |
where => {'book.name' => 'Perl'}, |
remove DBIx::Custom::Model
|
318 |
relation => {'book.id' => 'rental.book_id'} |
added DBIx::Custom::Guides
|
319 |
); |
320 | ||
update pod
|
321 |
C<relation> is relation of tables. This is inner join. |
322 | ||
323 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
324 | |
update pod
|
325 |
select * from book, rental where book.name = ? and book.id = rental.book_id; |
added DBIx::Custom::Guides
|
326 | |
update pod
|
327 |
Next example. |
added DBIx::Custom::Guides
|
328 | |
329 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
330 |
table => 'book', |
added DBIx::Custom::Guides
|
331 |
where => {author => 'Ken'}, |
update pod
|
332 |
append => 'for update', |
added DBIx::Custom::Guides
|
333 |
); |
334 | ||
update pod
|
335 |
C<append> is string appending to end of SQL. |
336 | ||
337 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
338 | |
update pod
|
339 |
select * book where author = ? for update; |
added DBIx::Custom::Guides
|
340 | |
update pod
|
341 |
C<appned> is also used at C<insert()>, C<update()>, C<update_all()> |
342 |
C<delete()>, C<delete_all()>, and C<select()>. |
|
added DBIx::Custom::Guides
|
343 | |
update pod
|
344 |
Instead of C<column> and C<table>, |
345 |
you can use C<selection>. |
|
346 |
This is used to specify column names and table names at once |
|
347 | ||
348 |
my $selection = <<"EOS"; |
|
349 |
title, author, company_name |
|
350 |
from book inner join company on book.company_id = company.id |
|
351 |
EOS |
|
352 | ||
353 |
$dbi->select(selection => $selection); |
|
354 | ||
355 |
Note that you can't use where clause in C<selection>. |
|
356 |
use clause like "inner join". |
|
357 | ||
update pod
|
358 |
=head3 C<execute()> |
added DBIx::Custom::Guides
|
359 | |
update pod
|
360 |
use C<execute()> to execute SQL |
update pod
|
361 | |
362 |
$dbi->execute("select * from book;"); |
|
363 | ||
update pod
|
364 |
Process tag and execute SQL. |
update pod
|
365 | |
366 |
$dbi->execute( |
|
367 |
"select * from book {= title} and {= author};" |
|
368 |
param => {title => 'Perl', author => 'Ken'} |
|
369 |
); |
|
370 | ||
update pod
|
371 |
Following SQL is executed. |
update pod
|
372 | |
373 |
select * from book title = ? and author = ?; |
|
374 | ||
update pod
|
375 |
Values of title and author is embbdeded into placeholder. |
update pod
|
376 | |
update pod
|
377 |
See L<5. Tag/"5. Tag"> about tag. |
update pod
|
378 | |
update pod
|
379 |
You don't have to wirte last semicolon in C<execute()>. |
update pod
|
380 | |
381 |
$dbi->execute('select * from book'); |
|
382 | ||
update pod
|
383 |
=head2 3. Fetch row |
update pod
|
384 | |
update pod
|
385 |
Return value of C<select()> is L<DBIx::Custom::Result> object. |
386 |
There are many methods to fetch row. |
|
update pod
|
387 | |
update pod
|
388 |
=head3 Fetch a row (array) : C<fetch()> |
update pod
|
389 | |
update pod
|
390 |
use C<fetch()> to fetch a row and assign it into array reference. |
391 | ||
392 |
my $row = $result->fetch; |
|
393 | ||
394 |
You can get all rows. |
|
added DBIx::Custom::Guides
|
395 | |
396 |
while (my $row = $result->fetch) { |
|
update pod
|
397 |
my $title = $row->[0]; |
398 |
my $author = $row->[1]; |
|
added DBIx::Custom::Guides
|
399 |
} |
400 | ||
update pod
|
401 |
=head3 Fetch only first row (array) : C<fetch_first()> |
update pod
|
402 | |
update pod
|
403 |
use C<fetch_first()> to fetch only first row. |
added DBIx::Custom::Guides
|
404 | |
405 |
my $row = $result->fetch_first; |
|
406 | ||
update pod
|
407 |
You can't fetch rest rows |
408 |
because statement handle C<finish()> is executed. |
|
added DBIx::Custom::Guides
|
409 | |
update pod
|
410 |
=head3 Fetch rows (array) : C<fetch_multi()> |
update pod
|
411 | |
update pod
|
412 |
use C<fetch_multi()> to fetch rows and assign it into |
413 |
array reference which has array references as element. |
|
update pod
|
414 | |
415 |
while (my $rows = $result->fetch_multi(2)) { |
|
416 |
my $title0 = $rows->[0][0]; |
|
417 |
my $author0 = $rows->[0][1]; |
|
418 |
|
|
419 |
my $title1 = $rows->[1][0]; |
|
420 |
my $author1 = $rows->[1][1]; |
|
added DBIx::Custom::Guides
|
421 |
} |
update pod
|
422 | |
update pod
|
423 |
Specify row count as argument. |
update pod
|
424 | |
update pod
|
425 |
You can get the following data. |
update pod
|
426 | |
427 |
[ |
|
428 |
['Perl', 'Ken'], |
|
429 |
['Ruby', 'Mark'] |
|
430 |
] |
|
431 | ||
update pod
|
432 |
=head3 Fetch all rows (array) : C<fetch_all> |
update pod
|
433 | |
update pod
|
434 |
use C<fetch_all()> to fetch all rows and assign it into |
435 |
array reference which has array reference as element. |
|
added DBIx::Custom::Guides
|
436 | |
437 |
my $rows = $result->fetch_all; |
|
438 | ||
update pod
|
439 |
You can get the following data. |
update pod
|
440 | |
441 |
[ |
|
442 |
['Perl', 'Ken'], |
|
443 |
['Ruby', 'Mark'] |
|
444 |
] |
|
445 | ||
update pod
|
446 |
=head3 Fetch a row (hash) : C<fetch_hash()> |
update pod
|
447 | |
update pod
|
448 |
use C<fetch_hash()> to fetch a row and assign it into hash reference. |
added DBIx::Custom::Guides
|
449 | |
450 |
while (my $row = $result->fetch_hash) { |
|
451 |
my $title = $row->{title}; |
|
452 |
my $author = $row->{author}; |
|
453 |
} |
|
454 | ||
update pod
|
455 |
=head3 Fetch only first row (hash) : C<fetch_hash_first()> |
update pod
|
456 | |
update pod
|
457 |
use C<fetch_hash_first()> to fetch only first row |
458 |
and assign it into hash reference. |
|
added DBIx::Custom::Guides
|
459 | |
460 |
my $row = $result->fetch_hash_first; |
|
update pod
|
461 | |
update pod
|
462 |
You can't fetch rest rows |
463 |
because statement handle C<finish()> is executed. |
|
update pod
|
464 | |
update pod
|
465 |
=head3 Fetch rows (hash) : C<fetch_hash_multi()> |
update pod
|
466 | |
update pod
|
467 |
use C<fetch_hash_multi()> to fetch rows and |
468 |
assign it into array reference which has hash references as element. |
|
added DBIx::Custom::Guides
|
469 | |
470 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
update pod
|
471 |
my $title0 = $rows->[0]{title}; |
472 |
my $author0 = $rows->[0]{author}; |
|
473 |
my $title1 = $rows->[1]{title}; |
|
474 |
my $author1 = $rows->[1]{author}; |
|
added DBIx::Custom::Guides
|
475 |
} |
update pod
|
476 | |
update pod
|
477 |
Specify row count as argument. |
update pod
|
478 | |
update pod
|
479 |
You can get the following data. |
update pod
|
480 | |
481 |
[ |
|
482 |
{title => 'Perl', author => 'Ken'}, |
|
483 |
{title => 'Ruby', author => 'Mark'} |
|
484 |
] |
|
485 | ||
update pod
|
486 |
=head3 Fetch all rows (hash) : C<fetch_hash_all()> |
update pod
|
487 | |
update pod
|
488 |
use C<fetch_hash_all()> to fetch all rows and |
489 |
assign it into array reference which has hash |
|
490 |
references as element. |
|
added DBIx::Custom::Guides
|
491 | |
492 |
my $rows = $result->fetch_hash_all; |
|
493 | ||
update pod
|
494 |
You can get the following data. |
update pod
|
495 | |
496 |
[ |
|
497 |
{title => 'Perl', author => 'Ken'}, |
|
498 |
{title => 'Ruby', author => 'Mark'} |
|
499 |
] |
|
500 | ||
update pod
|
501 |
=head3 Statement handle : C<sth()> |
update pod
|
502 | |
update pod
|
503 |
use <sth()> to get statement handle. |
added DBIx::Custom::Guides
|
504 | |
505 |
my $sth = $result->sth; |
|
506 | ||
update pod
|
507 |
=head2 4. Filtering |
update pod
|
508 | |
update pod
|
509 |
L<DBIx::Custom> provide value filtering. |
510 |
For example, You maybe want to convert L<Time::Piece> object to |
|
511 |
database date format when register data into database. |
|
512 |
and convert database date fromat to L<Time::Piece> object |
|
513 |
when get data from database. |
|
update pod
|
514 | |
update pod
|
515 |
=head3 Register filter : C<register_filter()> |
update pod
|
516 | |
update pod
|
517 |
use C<register_filter()> to register filter. |
update pod
|
518 | |
519 |
$dbi->register_filter( |
|
520 |
# Time::Piece object to DATE format |
|
521 |
tp_to_date => sub { |
|
522 |
my $date = shift; |
|
523 | ||
524 |
return '0000-00-00' unless $tp; |
|
525 |
return $tp->strftime('%Y-%m-%d'); |
|
526 |
}, |
|
527 |
|
|
528 |
# DATE to Time::Piece object |
|
529 |
date_to_tp => sub { |
|
530 |
my $date = shift; |
|
531 | ||
532 |
return if $date eq '0000-00-00'; |
|
533 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
534 |
}, |
|
535 |
); |
|
added DBIx::Custom::Guides
|
536 | |
update pod
|
537 |
Registered filter is used by C<apply_filter()> or etc. |
added DBIx::Custom::Guides
|
538 | |
update pod
|
539 |
=head3 Apply filter : C<apply_filter()> |
added DBIx::Custom::Guides
|
540 | |
update pod
|
541 |
use C<apply_filter()> to apply registered filter. |
update pod
|
542 | |
543 |
$dbi->apply_filter('book', |
|
544 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp'}, |
|
545 |
first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'} |
|
added DBIx::Custom::Guides
|
546 |
); |
547 | ||
update pod
|
548 |
First argument is table name. Arguments after first argument are pairs of column |
549 |
name and fitering rule. C<out> of filtering rule is filter which is used when data |
|
550 |
is send to database. C<in> of filtering rule is filter which is used when data |
|
551 |
is got from database. |
|
552 | ||
553 |
You can specify code reference as filter. |
|
update pod
|
554 | |
555 |
issue_date => {out => sub { ... }, in => sub { ... }} |
|
556 | ||
update pod
|
557 |
Applied filter become effective at insert()>, C<update()>, C<update_all()>, |
558 |
C<delete()>, C<delete_all()>, C<select()>. |
|
update pod
|
559 | |
560 |
my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d'); |
|
update pod
|
561 |
my $result = $dbi->select(table => 'book', where => {issue_date => $tp}); |
update pod
|
562 | |
update pod
|
563 |
When data is send to database, L<Time::Piece> object is converted |
564 |
to database date format "2010-10-14" |
|
update pod
|
565 | |
update pod
|
566 |
When data is fetched, database date format is |
567 |
converted to L<Time::Piece> object. |
|
update pod
|
568 | |
569 |
my $row = $resutl->fetch_hash_first; |
|
570 |
my $tp = $row->{issue_date}; |
|
571 | ||
update pod
|
572 |
You can also use column name which contains table name. |
update pod
|
573 | |
574 |
$dbi->select( |
|
575 |
table => 'book', |
|
update pod
|
576 |
where => {'book.issue_date' => $tp} |
update pod
|
577 |
); |
578 | ||
update pod
|
579 |
In fetching, Filter is effective if you use "TABLE__COLUMN" as column name. |
580 | ||
581 |
my $result = $dbi->execute( |
|
582 |
"select issue_date as book__issue_date from book"); |
|
583 | ||
584 |
You can apply C<end> filter execute after C<in> filter. |
|
585 | ||
586 |
$dbi->apply_filter('book', |
|
587 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp', |
|
588 |
end => 'tp_to_displaydate'}, |
|
589 |
); |
|
590 | ||
update pod
|
591 |
=head3 Individual filter C<filter> |
update pod
|
592 | |
update pod
|
593 |
You can apply individual filter . |
594 |
This filter overwrite the filter by C<apply_filter()> |
|
update pod
|
595 | |
update pod
|
596 |
use C<filter> option to apply individual filter |
597 |
when data is send to database. |
|
598 |
This option is used at C<insert()>, C<update()>, |
|
599 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
600 |
C<execute()>. |
|
update pod
|
601 | |
update pod
|
602 |
C<insert()> example: |
update pod
|
603 | |
604 |
$dbi->insert( |
|
605 |
table => 'book', |
|
606 |
param => {issue_date => $tp, first_issue_date => $tp}, |
|
607 |
filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'} |
|
608 |
); |
|
added DBIx::Custom::Guides
|
609 | |
update pod
|
610 |
C<execute()> example: |
update pod
|
611 | |
612 |
my $sql = <<"EOS"; |
|
613 |
select YEAR(issue_date) as issue_year |
|
614 |
from book |
|
615 |
where YEAR(issue_date) = {? issue_year} |
|
616 |
EOS |
|
617 |
|
|
added DBIx::Custom::Guides
|
618 |
my $result = $dbi->execute( |
update pod
|
619 |
$sql, |
620 |
param => {issue_year => '2010'}, |
|
621 |
filter => {issue_year => 'tp_to_year'} |
|
added DBIx::Custom::Guides
|
622 |
); |
623 | ||
update pod
|
624 |
You can also apply indivisual filter when you fetch row. |
625 |
use C<DBIx::Custom::Result>'s C<filter()>. |
|
added DBIx::Custom::Guides
|
626 | |
update pod
|
627 |
$result->filter(issue_year => 'year_to_tp'); |
added DBIx::Custom::Guides
|
628 | |
update pod
|
629 |
=head3 End filtering : C<end_filter()> |
update pod
|
630 | |
update pod
|
631 |
You can add filter at end. |
632 |
It is useful to create last output. |
|
633 |
use C<end_filter()> to add end filter. |
|
update pod
|
634 | |
635 |
$result->end_filter(issue_date => sub { |
|
636 |
my $tp = shift; |
|
637 |
|
|
638 |
return '' unless $tp; |
|
639 |
return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)'); |
|
640 |
}); |
|
641 | ||
update pod
|
642 |
In this example, L<Time::Piece> object is converted to readable format. |
update pod
|
643 | |
update pod
|
644 |
=head3 Automate applying filter : C<each_column()> |
update pod
|
645 | |
update pod
|
646 |
It is useful to apply filter automatically at date type columns. |
647 |
You can use C<each_column()> to process all column infos. |
|
update pod
|
648 | |
649 |
$dbi->each_column( |
|
650 |
sub { |
|
651 |
my ($self, $table, $column, $info) = @_; |
|
652 |
|
|
653 |
my $type = $info->{TYPE_NAME}; |
|
654 |
|
|
655 |
my $filter = $type eq 'DATE' ? {out => 'tp_to_date', in => 'date_to_tp'} |
|
656 |
: $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'} |
|
657 |
: undef; |
|
658 |
|
|
659 |
$self->apply_filter($table, $column, $filter) |
|
660 |
if $filter; |
|
661 |
} |
|
662 |
); |
|
663 | ||
update pod
|
664 |
C<each_column()> receive callback. |
update pod
|
665 |
callback arguments are L<DBIx::Custom> object, table name, column name, column information. |
666 |
Filter is applied automatically by column type. |
|
update pod
|
667 | |
update pod
|
668 |
=head2 5. Tag |
update pod
|
669 | |
update pod
|
670 |
=head3 Basic of Tag |
update pod
|
671 | |
update pod
|
672 |
You can embedd tag into SQL. |
update pod
|
673 | |
674 |
select * from book where {= title} and {like author}; |
|
675 | ||
update pod
|
676 |
{= title} and {like author} are tag. Tag has the folloring format. |
update pod
|
677 | |
update pod
|
678 |
{TAG_NAME ARG1 ARG2 ...} |
update pod
|
679 | |
update pod
|
680 |
Tag start C<{> and end C<}>. |
update pod
|
681 |
Don't insert space between C<{}> and tag name. |
update pod
|
682 | |
683 |
C<{> and C<}> are reserved word. |
|
684 |
If you want to use these, escape it by '\'; |
|
update pod
|
685 | |
686 |
select from book \\{ ... \\} |
|
687 | ||
update pod
|
688 |
\ is perl's escape character, you need two \. |
689 | ||
690 |
Tag is expanded before executing SQL. |
|
update pod
|
691 | |
692 |
select * from book where title = ? and author like ?; |
|
693 | ||
update pod
|
694 |
use C<execute()> to execute SQL which contains tag |
update pod
|
695 | |
696 |
my $sql = "select * from book where {= author} and {like title};" |
|
697 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}); |
|
698 | ||
update pod
|
699 |
You can specify values embedded into place holder as hash reference using |
700 |
C<param> option. |
|
update pod
|
701 | |
update pod
|
702 |
You can specify C<filter()> at C<execute()>. |
update pod
|
703 | |
704 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
705 |
filter => {title => 'to_something'); |
|
706 | ||
update pod
|
707 |
Note that at C<execute()> the filter applied by C<apply_filter()> |
708 |
don't has effective to columns. |
|
update pod
|
709 |
You have to use C<table> tag in SQL |
update pod
|
710 | |
update pod
|
711 |
my $sql = "select * from {table book} where {= author} and {like title};" |
update pod
|
712 | |
update pod
|
713 |
=head3 Tag list |
update pod
|
714 | |
update pod
|
715 |
The following tag is available. |
update pod
|
716 | |
update pod
|
717 |
=head4 C<table> |
718 | ||
719 |
{table NAME} -> NAME |
|
720 | ||
721 |
This is used to specify table name in SQL. |
|
722 |
If you specify table name, Filtering by |
|
723 |
C<apply_filter()> is effective. |
|
724 | ||
update pod
|
725 |
=head4 C<?> |
update pod
|
726 | |
727 |
{? NAME} -> ? |
|
728 | ||
update pod
|
729 |
=head4 C<=> |
update pod
|
730 | |
731 |
{= NAME} -> NAME = ? |
|
732 | ||
update pod
|
733 |
=head4 C<E<lt>E<gt>> |
update pod
|
734 | |
735 |
{<> NAME} -> NAME <> ? |
|
736 | ||
update pod
|
737 |
=head4 C<E<lt>> |
update pod
|
738 | |
739 |
{< NAME} -> NAME < ? |
|
740 | ||
update pod
|
741 |
=head4 C<E<gt>> |
update pod
|
742 | |
743 |
{> NAME} -> NAME > ? |
|
744 | ||
update pod
|
745 |
=head4 C<E<gt>=> |
update pod
|
746 | |
747 |
{>= NAME} -> NAME >= ? |
|
748 | ||
update pod
|
749 |
=head4 C<E<lt>=> |
update pod
|
750 | |
751 |
{<= NAME} -> NAME <= ? |
|
752 | ||
update pod
|
753 |
=head4 C<like> |
update pod
|
754 | |
755 |
{like NAME} -> NAME like ? |
|
756 | ||
update pod
|
757 |
=head4 C<in> |
update pod
|
758 | |
759 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
760 | ||
update pod
|
761 |
=head4 C<insert_param> |
update pod
|
762 | |
added DBIx::Custom::Guides
|
763 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
update pod
|
764 | |
update pod
|
765 |
=head4 C<update_param> |
update pod
|
766 | |
added DBIx::Custom::Guides
|
767 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
768 | ||
update pod
|
769 |
=head3 Manipulate same name's columns |
added DBIx::Custom::Guides
|
770 | |
update pod
|
771 |
It is ok if there are same name's columns. |
772 |
Let's think two date comparison. |
|
added DBIx::Custom::Guides
|
773 | |
update pod
|
774 |
my $sql = "select * from table where {> date} and {< date};"; |
added DBIx::Custom::Guides
|
775 | |
update pod
|
776 |
In this case, You specify paramter values as array reference. |
added DBIx::Custom::Guides
|
777 | |
update pod
|
778 |
my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']}); |
added DBIx::Custom::Guides
|
779 | |
update pod
|
780 |
=head3 Register Tag : C<register_tag()> |
update pod
|
781 | |
update pod
|
782 |
You can register custom tag. |
update pod
|
783 |
use C<register_tag()> to register tag. |
update pod
|
784 | |
785 |
$dbi->register_tag( |
|
786 |
'=' => sub { |
|
787 |
my $column = shift; |
|
788 |
|
|
789 |
return ["$column = ?", [$column]]; |
|
added DBIx::Custom::Guides
|
790 |
} |
791 |
); |
|
792 | ||
update pod
|
793 |
This is implementation of C<=> tag. |
794 |
Tag format is the following one. |
|
added experimental DBIx::Cus...
|
795 | |
update pod
|
796 |
{TAG_NAME ARG1 ARG2 ...} |
added experimental DBIx::Cus...
|
797 | |
update pod
|
798 |
In case C<=> tag. Format is |
cleanup
|
799 | |
update pod
|
800 |
{= title} |
added experimental DBIx::Cus...
|
801 | |
update pod
|
802 |
So subroutine receive one argument "title". |
803 |
You have to return array reference in the following format. |
|
update pod
|
804 | |
805 |
[ |
|
update pod
|
806 |
String after expanding, |
807 |
[COLUMN1(This is used for place holder), COLUMN2 , ...] |
|
update pod
|
808 |
] |
809 | ||
update pod
|
810 |
First element is expanded stirng. In this example, |
update pod
|
811 | |
812 |
'title = ?' |
|
813 | ||
update pod
|
814 |
Secount element is array reference which is used to embedd value to |
815 |
place holder. In this example, |
|
update pod
|
816 | |
817 |
['title'] |
|
818 | ||
update pod
|
819 |
If there are more than one placeholders, |
820 |
This elements is multipul. |
|
update pod
|
821 | |
update pod
|
822 |
You return the following array reference. |
update pod
|
823 | |
824 |
['title = ?', ['title']] |
|
added DBIx::Custom::Guides
|
825 | |
update pod
|
826 |
See source of L<DBIx::Custom::Tag> to see many implementation. |
added DBIx::Custom::Guides
|
827 | |
update pod
|
828 |
=head2 6. Dinamically create where clause |
update pod
|
829 | |
update pod
|
830 |
=head3 Dinamically create where clause : where() |
update pod
|
831 | |
update pod
|
832 |
You want to search multiple conditions in many times. |
833 |
Let's think the following three cases. |
|
update pod
|
834 | |
update pod
|
835 |
Case1: Search only C<title> |
update pod
|
836 | |
837 |
where {= title} |
|
838 | ||
update pod
|
839 |
Case2: Search only C<author> |
update pod
|
840 | |
841 |
where {= author} |
|
842 | ||
update pod
|
843 |
Case3: Search C<title> and C<author> |
update pod
|
844 | |
845 |
where {= title} and {=author} |
|
846 | ||
update pod
|
847 |
L<DBIx::Custom> support dinamic where clause creating. |
848 |
At first, create L<DBIx::Custom::Where> object by C<where()>. |
|
update pod
|
849 | |
850 |
my $where = $dbi->where; |
|
851 | ||
update pod
|
852 |
Set clause by C<clause()> |
update pod
|
853 | |
854 |
$where->clause( |
|
855 |
['and', '{= title'}, '{= author}'] |
|
added DBIx::Custom::Guides
|
856 |
); |
857 | ||
update pod
|
858 |
C<clause> is the following format. |
update pod
|
859 | |
update pod
|
860 |
['or' or 'and', TAG1, TAG2, TAG3] |
added DBIx::Custom::Guides
|
861 | |
update pod
|
862 |
First argument is 'or' or 'and'. |
863 |
Later than first argument are tag names. |
|
update pod
|
864 | |
update pod
|
865 |
You can write more complex format. |
update pod
|
866 | |
867 |
['and', |
|
868 |
'{= title}', |
|
869 |
['or', '{= author}', '{like date}'] |
|
870 |
] |
|
871 | ||
update pod
|
872 |
This mean "{=title} and ( {=author} or {like date} )". |
873 | ||
874 |
After setting C<clause>, set C<param>. |
|
added DBIx::Custom::Guides
|
875 |
|
update pod
|
876 |
$where->param({title => 'Perl'}); |
added DBIx::Custom::Guides
|
877 | |
update pod
|
878 |
In this example, parameter contains only title. |
added DBIx::Custom::Guides
|
879 | |
update pod
|
880 |
If you execute C<string_to()>, you can get where clause |
881 |
which contain only parameter name. |
|
update pod
|
882 | |
883 |
my $where_clause = $where->to_string; |
|
884 | ||
update pod
|
885 |
Parameter name is only title, the following where clause is created. |
update pod
|
886 | |
887 |
where {= title} |
|
888 | ||
update pod
|
889 |
You can also create where clause by stringification. |
update pod
|
890 | |
891 |
my $where_clause = "$where"; |
|
892 | ||
update pod
|
893 |
This is useful to embbed it into SQL. |
added DBIx::Custom::Guides
|
894 | |
update pod
|
895 |
=head3 In case where clause contains same name columns |
check arguments of connect m...
|
896 | |
update pod
|
897 |
Even if same name tags exists, you can create where clause. |
898 |
Let's think that there are starting date and ending date. |
|
added experimental DBIx::Cus...
|
899 | |
update pod
|
900 |
my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
added experimental DBIx::Cus...
|
901 | |
update pod
|
902 |
In this case, you set parameter value as array reference. |
update pod
|
903 | |
904 |
my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
905 | ||
update pod
|
906 |
You can embbed these values into same name tags. |
update pod
|
907 | |
908 |
$where->clause( |
|
909 |
['and', '{> date}', '{< date}'] |
|
added experimental DBIx::Cus...
|
910 |
); |
update pod
|
911 |
$where->param($p); |
912 | ||
update pod
|
913 |
If starting date isn't exists, create the following parameter. |
update pod
|
914 | |
915 |
my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
916 | ||
update pod
|
917 |
You can get DBIx::Custom::NotExists object by C<not_exists()> |
918 |
This mean correnspondinf value isn't exists. |
|
update pod
|
919 | |
update pod
|
920 |
If ending date isn't exists, create the following parameter. |
update pod
|
921 | |
922 |
my $p = {date => ['2010-11-15']}; |
|
923 | ||
update pod
|
924 |
If both date isn't exists, create the following parameter. |
update pod
|
925 | |
926 |
my $p = {date => []}; |
|
927 | ||
update pod
|
928 |
This logic is a little difficut. See the following ones. |
update pod
|
929 | |
930 |
my @date; |
|
931 |
push @date, exists $param->{start_date} ? $param->{start_date} |
|
932 |
: $dbi->not_exists; |
|
933 |
push @date, $param->{end_date} if exists $param->{end_date}; |
|
934 |
my $p = {date => \@date}; |
|
935 | ||
update pod
|
936 |
=head3 With C<select()> |
update pod
|
937 | |
update pod
|
938 |
You can pass L<DBIx::Custom::Where> object to C<where> of C<select()>. |
update pod
|
939 |
|
940 |
my $where = $dbi->where; |
|
update pod
|
941 |
$where->clause(['and', '{= title}', '{= author}']); |
942 |
$where->param({title => 'Perl'}); |
|
update pod
|
943 |
my $result = $dbi->select(table => 'book', where => $where); |
944 | ||
update pod
|
945 |
You can also pass it to C<where> of C<update()>�AC<delete()> |
update pod
|
946 | |
update pod
|
947 |
=head3 With C<execute()> |
added experimental DBIx::Cus...
|
948 | |
update pod
|
949 |
L<DBIx::Custom::Where> object is embedded into SQL. |
added experimental DBIx::Cus...
|
950 | |
update pod
|
951 |
my $where = $dbi->where; |
update pod
|
952 |
$where->clause(['and', '{= title}', '{= author}']); |
953 |
$where->param({title => 'Perl'}); |
|
update pod
|
954 | |
update pod
|
955 |
my $sql = <<"EOS"; |
956 |
select * from {table book}; |
|
update pod
|
957 |
$where |
958 |
EOS |
|
959 | ||
960 |
$dbi->execute($sql, param => $param); |
|
961 | ||
add feture. all model class ...
|
962 |
=head2 7. Model |
update pod
|
963 | |
add feture. all model class ...
|
964 |
=head3 Model |
update pod
|
965 | |
add feture. all model class ...
|
966 |
you can define model extending L<DBIx::Custom::Model> |
removed experimental base_ta...
|
967 |
to improve source code view. |
update pod
|
968 | |
add feture. all model class ...
|
969 |
package MyModel::book; |
970 |
use base 'DBIx::Custom::Model'; |
|
removed experimental base_ta...
|
971 |
|
972 |
sub insert { ... } |
|
973 |
sub list { ... } |
|
update pod
|
974 | |
add feture. all model class ...
|
975 |
You can include and instantiate this class |
update pod
|
976 | |
add feture. all model class ...
|
977 |
$dbi->include_model( |
978 |
MyModel => [ |
|
removed experimental base_ta...
|
979 |
'book', |
980 |
] |
|
981 |
); |
|
update pod
|
982 | |
removed experimental base_ta...
|
983 |
First argument is name space. |
984 |
Second argument is array reference of class base names. |
|
update pod
|
985 | |
add feture. all model class ...
|
986 |
If you don't specify second argument, All models under name space is |
987 |
included. |
|
removed experimental base_ta...
|
988 | |
add feture. all model class ...
|
989 |
$dbi->include_model('MyModel'); |
removed experimental base_ta...
|
990 | |
add feture. all model class ...
|
991 |
Note that in this case name spece module is needed. |
removed experimental base_ta...
|
992 | |
add feture. all model class ...
|
993 |
# MyModel.pm |
994 |
package MyModel; |
|
995 |
|
|
996 |
use base 'DBIx::Custom::Model'; |
|
997 | ||
998 |
The follwoing modules location is needed. |
|
999 | ||
1000 |
MyModel.pm |
|
1001 |
MyModel / book.pm |
|
1002 |
/ company.pm |
|
1003 |
|
|
1004 |
You can use model like this. |
|
1005 | ||
1006 |
my $result = $dbi->model('book')->list; |
|
1007 | ||
1008 |
In mode, You can use such as methods, |
|
1009 |
C<insert()>, C<update()>, C<update_all()>, |
|
1010 |
C<delete()>, C<delete_all()>, C<select()> |
|
1011 |
without C<table> option. |
|
1012 | ||
1013 |
$dbi->model('book')->insert(param => $param); |
|
update pod
|
1014 | |
add feture. all model class ...
|
1015 |
Model is L<DBIx::Custom::Model>. |
update pod
|
1016 | |
add feture. all model class ...
|
1017 |
If you need table name�Ayou can get it by C<table()>. |
update pod
|
1018 | |
add feture. all model class ...
|
1019 |
my $table = $model->table; |
update pod
|
1020 | |
add feture. all model class ...
|
1021 |
You can get L<DBIx::Custom>. |
update pod
|
1022 | |
add feture. all model class ...
|
1023 |
my $dbi = $model->dbi; |
removed experimental base_ta...
|
1024 | |
add feture. all model class ...
|
1025 |
You can also call all methods of L<DBIx::Custom> and L<DBI>. |
added experimental DBIx::Cus...
|
1026 | |
update pod
|
1027 |
# DBIx::Custom method |
add feture. all model class ...
|
1028 |
$model->execute($sql); |
update pod
|
1029 |
|
1030 |
# DBI method |
|
add feture. all model class ...
|
1031 |
$model->begin_work; |
1032 |
$model->commit; |
|
added experimental DBIx::Cus...
|
1033 | |
add feture. all model class ...
|
1034 |
=head2 Model Examples |
added DBIx::Custom::Guides
|
1035 | |
add feture. all model class ...
|
1036 |
Model examples |
added DBIx::Custom::Guides
|
1037 | |
update pod
|
1038 |
package MyDBI; |
1039 |
|
|
1040 |
use base 'DBIx::Custom'; |
|
added DBIx::Custom::Guides
|
1041 |
|
update pod
|
1042 |
sub connect { |
1043 |
my $self = shift->SUPER::connect(@_); |
|
1044 |
|
|
add feture. all model class ...
|
1045 |
$self->include_model( |
1046 |
MyModel => [ |
|
removed experimental base_ta...
|
1047 |
'book', |
1048 |
'company' |
|
1049 |
] |
|
update pod
|
1050 |
); |
1051 |
} |
|
removed experimental base_ta...
|
1052 |
|
add feture. all model class ...
|
1053 |
package MyModel::book; |
1054 |
use base 'DBIx::Custom::Model'; |
|
removed experimental base_ta...
|
1055 |
|
1056 |
sub insert { ... } |
|
1057 |
sub list { ... } |
|
1058 |
|
|
add feture. all model class ...
|
1059 |
package MyModel::company; |
1060 |
use base 'DBIx::Custom::Model'; |
|
removed experimental base_ta...
|
1061 |
|
1062 |
sub insert { ... } |
|
1063 |
sub list { ... } |
|
update pod
|
1064 | |
update pod
|
1065 |
=head2 8. Improve performance |
update pod
|
1066 | |
update pod
|
1067 |
=head3 Create query |
update pod
|
1068 | |
update pod
|
1069 |
If you can't get performance, create query by C<query> option. |
1070 |
For example, many insert is needed. |
|
update pod
|
1071 | |
update pod
|
1072 |
my $params = [ |
1073 |
{title => 'Perl', author => 'Ken'}, |
|
1074 |
{title => 'Good day', author => 'Tom'} |
|
1075 |
] |
|
1076 |
my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1); |
|
added DBIx::Custom::Guides
|
1077 | |
update pod
|
1078 |
Return value is L<DBIx::Custom::Query> object. |
1079 |
This query is executed by C<execute()>. |
|
added DBIx::Custom::Guides
|
1080 | |
update pod
|
1081 |
foreach my $param (@$params) { |
1082 |
$dbi->execute($query, $param); |
|
added DBIx::Custom::Guides
|
1083 |
} |
1084 | ||
update pod
|
1085 |
Performance is improved because statement handle is reused |
1086 |
C<query> option is used in C<insert()>, C<update()>, C<update_all()>, |
|
1087 |
C<delete()>, C<delete_all()>. |
|
1088 | ||
1089 |
Note that parameters count is same as method for creating query and C<execute()>. |
|
added DBIx::Custom::Guides
|
1090 | |
update pod
|
1091 |
You can create query from any SQL by C<create_query()>. |
added DBIx::Custom::Guides
|
1092 | |
update pod
|
1093 |
my $query = $dbi->create_query( |
1094 |
"insert into book {insert_param title author};"; |
|
1095 |
); |
|
added DBIx::Custom::Guides
|
1096 | |
update pod
|
1097 |
=head2 9. Other features |
added DBIx::Custom::Guides
|
1098 | |
update pod
|
1099 |
=head3 Add method |
added DBIx::Custom::Guides
|
1100 | |
update pod
|
1101 |
You can add method to L<DBIx::Custom> object. |
update pod
|
1102 |
use C<method()>. |
added DBIx::Custom::Guides
|
1103 | |
update pod
|
1104 |
$dbi->method( |
1105 |
update_or_insert => sub { |
|
1106 |
my $self = shift; |
|
1107 |
# something |
|
1108 |
}, |
|
1109 |
find_or_create => sub { |
|
1110 |
my $self = shift; |
|
1111 |
# something |
|
1112 |
} |
|
1113 |
); |
|
1114 | ||
update pod
|
1115 |
You can call these methods from L<DBIx::Custom> object. |
update pod
|
1116 | |
1117 |
$dbi->update_or_insert; |
|
1118 |
$dbi->find_or_create; |
|
1119 | ||
update pod
|
1120 |
=head3 Change result class |
added DBIx::Custom::Guides
|
1121 | |
update pod
|
1122 |
You can change result class. By default it is L<DBIx::Custom::Result>. |
added DBIx::Custom::Guides
|
1123 | |
update pod
|
1124 |
package MyResult; |
added DBIx::Custom::Guides
|
1125 |
use base 'DBIx::Custom::Result'; |
1126 |
|
|
1127 |
sub some_method { ... } |
|
1128 | ||
1129 |
1; |
|
1130 |
|
|
1131 |
package main; |
|
1132 |
|
|
update pod
|
1133 |
use MyResult; |
added DBIx::Custom::Guides
|
1134 |
|
1135 |
my $dbi = DBIx::Custom->connect(...); |
|
update pod
|
1136 |
$dbi->result_class('MyResult'); |
added DBIx::Custom::Guides
|
1137 | |
update pod
|
1138 |
=head3 Caching |
added DBIx::Custom::Guides
|
1139 | |
update pod
|
1140 |
SQL after parsing tag is cached for performance. |
1141 |
You can set C<cache()>. By default, chaching is true. |
|
added DBIx::Custom::Guides
|
1142 | |
update pod
|
1143 |
$dbi->cache(1); |
added DBIx::Custom::Guides
|
1144 | |
update pod
|
1145 |
The way to cache is changed by C<cache_method()>. |
1146 |
Default method is the following one. |
|
1147 |
Cache is saved to memory. |
|
update pod
|
1148 | |
1149 |
$dbi->cache_method(sub { |
|
1150 |
sub { |
|
added DBIx::Custom::Guides
|
1151 |
my $self = shift; |
update pod
|
1152 |
|
1153 |
$self->{_cached} ||= {}; |
|
1154 |
|
|
1155 |
if (@_ > 1) { |
|
update pod
|
1156 |
# Save cache |
update pod
|
1157 |
$self->{_cached}{$_[0]} = $_[1] |
1158 |
} |
|
1159 |
else { |
|
update pod
|
1160 |
# Get cache |
update pod
|
1161 |
return $self->{_cached}{$_[0]} |
1162 |
} |
|
added DBIx::Custom::Guides
|
1163 |
} |
update pod
|
1164 |
}); |
1165 |
|
|
update pod
|
1166 |
First argument is L<DBIx::Custom> object. |
1167 |
Second argument is SQL before parsing. |
|
1168 |
Third argument is SQL information after parsing. This is hash reference. |
|
added DBIx::Custom::Guides
|
1169 | |
update pod
|
1170 |
If third argument exists, you save cache, |
1171 |
and if third argument isn't exists, you get chace. |
|
added DBIx::Custom::Guides
|
1172 | |
pod fix
|
1173 |
=head1 EXAMPLES |
1174 | ||
update pod
|
1175 |
You can see exsamples in the following wiki. |
1176 | ||
pod fix
|
1177 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
add examples
|
1178 | |
added DBIx::Custom::Guides
|
1179 |
=cut |