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