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( |
|
DBIx::Custom::Model select()...
|
316 |
table => 'book', |
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 |
=head3 insert by using primary key : C<insert_at()> |
384 | ||
385 |
To insert row by using primary key, use C<insert_at()> |
|
386 | ||
387 |
$dbi->insert_at( |
|
388 |
table => 'book', primary_key => ['id'], |
|
389 |
where => ['123'], param => {name => 'Ken'} |
|
390 |
); |
|
391 | ||
392 |
In this example, row which id column is 123 is inserted. |
|
393 |
NOTE that you must pass array reference as C<where>. |
|
394 |
If C<param> contains primary key, the key and value is delete from C<param>. |
|
395 | ||
add experimental update_at()...
|
396 |
=head3 Update by using primary key : C<update_at()> |
397 | ||
398 |
To update row by using primary key, use C<update_at()> |
|
399 | ||
400 |
$dbi->update_at( |
|
401 |
table => 'book', primary_key => ['id'], |
|
402 |
where => ['123'], param => {name => 'Ken'} |
|
403 |
); |
|
404 | ||
405 |
In this example, row which id column is 123 is updated. |
|
406 |
NOTE that you must pass array reference as C<where>. |
|
407 |
If C<param> contains primary key, the key and value is delete from C<param>. |
|
408 | ||
409 |
=head3 Delete by using primary key : C<delete_at()> |
|
410 | ||
411 |
To delete row by using primary key, use C<delete_at()> |
|
412 | ||
413 |
$dbi->delete_at(table => 'book', primary_key => ['id'], where => ['123']); |
|
414 | ||
415 |
In this example, row which id column is 123 is deleted. |
|
416 |
NOTE that you must pass array reference as C<where>. |
|
417 | ||
418 |
You can also write arguments like this. |
|
419 | ||
420 |
$dbi->delete_at(table => 'book', primary_key => ['id'], param => {id => '123'}); |
|
421 | ||
422 |
=head3 Select by using primary key : C<select_at()> |
|
423 | ||
424 |
To select row by using primary key, use C<select_at()>. |
|
425 | ||
426 |
$dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']); |
|
427 | ||
428 |
In this example, row which id colunm is 123 is selected. |
|
429 |
NOTE that you must pass array reference as C<where>. |
|
430 | ||
431 |
You can also write arguments like this. |
|
432 | ||
433 |
$dbi->select_at(table => 'book', primary_key => ['id'], param => {id => '123'}); |
|
434 | ||
update pod
|
435 |
=head2 3. Fetch row |
update pod
|
436 | |
update pod
|
437 |
Return value of C<select()> is L<DBIx::Custom::Result> object. |
438 |
There are many methods to fetch row. |
|
update pod
|
439 | |
update pod
|
440 |
=head3 Fetch a row (array) : C<fetch()> |
update pod
|
441 | |
update pod
|
442 |
use C<fetch()> to fetch a row and assign it into array reference. |
443 | ||
444 |
my $row = $result->fetch; |
|
445 | ||
446 |
You can get all rows. |
|
added DBIx::Custom::Guides
|
447 | |
448 |
while (my $row = $result->fetch) { |
|
update pod
|
449 |
my $title = $row->[0]; |
450 |
my $author = $row->[1]; |
|
added DBIx::Custom::Guides
|
451 |
} |
452 | ||
update pod
|
453 |
=head3 Fetch only first row (array) : C<fetch_first()> |
update pod
|
454 | |
update pod
|
455 |
use C<fetch_first()> to fetch only first row. |
added DBIx::Custom::Guides
|
456 | |
457 |
my $row = $result->fetch_first; |
|
458 | ||
update pod
|
459 |
You can't fetch rest rows |
460 |
because statement handle C<finish()> is executed. |
|
added DBIx::Custom::Guides
|
461 | |
update pod
|
462 |
=head3 Fetch rows (array) : C<fetch_multi()> |
update pod
|
463 | |
update pod
|
464 |
use C<fetch_multi()> to fetch rows and assign it into |
465 |
array reference which has array references as element. |
|
update pod
|
466 | |
467 |
while (my $rows = $result->fetch_multi(2)) { |
|
468 |
my $title0 = $rows->[0][0]; |
|
469 |
my $author0 = $rows->[0][1]; |
|
470 |
|
|
471 |
my $title1 = $rows->[1][0]; |
|
472 |
my $author1 = $rows->[1][1]; |
|
added DBIx::Custom::Guides
|
473 |
} |
update pod
|
474 | |
update pod
|
475 |
Specify row count as argument. |
update pod
|
476 | |
update pod
|
477 |
You can get the following data. |
update pod
|
478 | |
479 |
[ |
|
480 |
['Perl', 'Ken'], |
|
481 |
['Ruby', 'Mark'] |
|
482 |
] |
|
483 | ||
update pod
|
484 |
=head3 Fetch all rows (array) : C<fetch_all> |
update pod
|
485 | |
update pod
|
486 |
use C<fetch_all()> to fetch all rows and assign it into |
487 |
array reference which has array reference as element. |
|
added DBIx::Custom::Guides
|
488 | |
489 |
my $rows = $result->fetch_all; |
|
490 | ||
update pod
|
491 |
You can get the following data. |
update pod
|
492 | |
493 |
[ |
|
494 |
['Perl', 'Ken'], |
|
495 |
['Ruby', 'Mark'] |
|
496 |
] |
|
497 | ||
update pod
|
498 |
=head3 Fetch a row (hash) : C<fetch_hash()> |
update pod
|
499 | |
update pod
|
500 |
use C<fetch_hash()> to fetch a row and assign it into hash reference. |
added DBIx::Custom::Guides
|
501 | |
502 |
while (my $row = $result->fetch_hash) { |
|
503 |
my $title = $row->{title}; |
|
504 |
my $author = $row->{author}; |
|
505 |
} |
|
506 | ||
update pod
|
507 |
=head3 Fetch only first row (hash) : C<fetch_hash_first()> |
update pod
|
508 | |
update pod
|
509 |
use C<fetch_hash_first()> to fetch only first row |
510 |
and assign it into hash reference. |
|
added DBIx::Custom::Guides
|
511 | |
512 |
my $row = $result->fetch_hash_first; |
|
update pod
|
513 | |
update pod
|
514 |
You can't fetch rest rows |
515 |
because statement handle C<finish()> is executed. |
|
update pod
|
516 | |
update pod
|
517 |
=head3 Fetch rows (hash) : C<fetch_hash_multi()> |
update pod
|
518 | |
update pod
|
519 |
use C<fetch_hash_multi()> to fetch rows and |
520 |
assign it into array reference which has hash references as element. |
|
added DBIx::Custom::Guides
|
521 | |
522 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
update pod
|
523 |
my $title0 = $rows->[0]{title}; |
524 |
my $author0 = $rows->[0]{author}; |
|
525 |
my $title1 = $rows->[1]{title}; |
|
526 |
my $author1 = $rows->[1]{author}; |
|
added DBIx::Custom::Guides
|
527 |
} |
update pod
|
528 | |
update pod
|
529 |
Specify row count as argument. |
update pod
|
530 | |
update pod
|
531 |
You can get the following data. |
update pod
|
532 | |
533 |
[ |
|
534 |
{title => 'Perl', author => 'Ken'}, |
|
535 |
{title => 'Ruby', author => 'Mark'} |
|
536 |
] |
|
537 | ||
update pod
|
538 |
=head3 Fetch all rows (hash) : C<fetch_hash_all()> |
update pod
|
539 | |
update pod
|
540 |
use C<fetch_hash_all()> to fetch all rows and |
541 |
assign it into array reference which has hash |
|
542 |
references as element. |
|
added DBIx::Custom::Guides
|
543 | |
544 |
my $rows = $result->fetch_hash_all; |
|
545 | ||
update pod
|
546 |
You can get the following data. |
update pod
|
547 | |
548 |
[ |
|
549 |
{title => 'Perl', author => 'Ken'}, |
|
550 |
{title => 'Ruby', author => 'Mark'} |
|
551 |
] |
|
552 | ||
update pod
|
553 |
=head3 Statement handle : C<sth()> |
update pod
|
554 | |
update pod
|
555 |
use <sth()> to get statement handle. |
added DBIx::Custom::Guides
|
556 | |
557 |
my $sth = $result->sth; |
|
558 | ||
update pod
|
559 |
=head2 4. Filtering |
update pod
|
560 | |
update pod
|
561 |
L<DBIx::Custom> provide value filtering. |
562 |
For example, You maybe want to convert L<Time::Piece> object to |
|
563 |
database date format when register data into database. |
|
564 |
and convert database date fromat to L<Time::Piece> object |
|
565 |
when get data from database. |
|
update pod
|
566 | |
update pod
|
567 |
=head3 Register filter : C<register_filter()> |
update pod
|
568 | |
update pod
|
569 |
use C<register_filter()> to register filter. |
update pod
|
570 | |
571 |
$dbi->register_filter( |
|
572 |
# Time::Piece object to DATE format |
|
573 |
tp_to_date => sub { |
|
574 |
my $date = shift; |
|
575 | ||
576 |
return '0000-00-00' unless $tp; |
|
577 |
return $tp->strftime('%Y-%m-%d'); |
|
578 |
}, |
|
579 |
|
|
580 |
# DATE to Time::Piece object |
|
581 |
date_to_tp => sub { |
|
582 |
my $date = shift; |
|
583 | ||
584 |
return if $date eq '0000-00-00'; |
|
585 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
586 |
}, |
|
587 |
); |
|
added DBIx::Custom::Guides
|
588 | |
update pod
|
589 |
Registered filter is used by C<apply_filter()> or etc. |
added DBIx::Custom::Guides
|
590 | |
update pod
|
591 |
=head3 Apply filter : C<apply_filter()> |
added DBIx::Custom::Guides
|
592 | |
update pod
|
593 |
use C<apply_filter()> to apply registered filter. |
update pod
|
594 | |
595 |
$dbi->apply_filter('book', |
|
596 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp'}, |
|
597 |
first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'} |
|
added DBIx::Custom::Guides
|
598 |
); |
599 | ||
update pod
|
600 |
First argument is table name. Arguments after first argument are pairs of column |
601 |
name and fitering rule. C<out> of filtering rule is filter which is used when data |
|
602 |
is send to database. C<in> of filtering rule is filter which is used when data |
|
603 |
is got from database. |
|
604 | ||
605 |
You can specify code reference as filter. |
|
update pod
|
606 | |
607 |
issue_date => {out => sub { ... }, in => sub { ... }} |
|
608 | ||
update pod
|
609 |
Applied filter become effective at insert()>, C<update()>, C<update_all()>, |
610 |
C<delete()>, C<delete_all()>, C<select()>. |
|
update pod
|
611 | |
612 |
my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d'); |
|
update pod
|
613 |
my $result = $dbi->select(table => 'book', where => {issue_date => $tp}); |
update pod
|
614 | |
update pod
|
615 |
When data is send to database, L<Time::Piece> object is converted |
616 |
to database date format "2010-10-14" |
|
update pod
|
617 | |
update pod
|
618 |
When data is fetched, database date format is |
619 |
converted to L<Time::Piece> object. |
|
update pod
|
620 | |
621 |
my $row = $resutl->fetch_hash_first; |
|
622 |
my $tp = $row->{issue_date}; |
|
623 | ||
update pod
|
624 |
You can also use column name which contains table name. |
update pod
|
625 | |
626 |
$dbi->select( |
|
627 |
table => 'book', |
|
update pod
|
628 |
where => {'book.issue_date' => $tp} |
update pod
|
629 |
); |
630 | ||
update pod
|
631 |
In fetching, Filter is effective if you use "TABLE__COLUMN" as column name. |
632 | ||
633 |
my $result = $dbi->execute( |
|
634 |
"select issue_date as book__issue_date from book"); |
|
635 | ||
636 |
You can apply C<end> filter execute after C<in> filter. |
|
637 | ||
638 |
$dbi->apply_filter('book', |
|
639 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp', |
|
640 |
end => 'tp_to_displaydate'}, |
|
641 |
); |
|
642 | ||
update pod
|
643 |
=head3 Individual filter C<filter> |
update pod
|
644 | |
update pod
|
645 |
You can apply individual filter . |
646 |
This filter overwrite the filter by C<apply_filter()> |
|
update pod
|
647 | |
update pod
|
648 |
use C<filter> option to apply individual filter |
649 |
when data is send to database. |
|
650 |
This option is used at C<insert()>, C<update()>, |
|
651 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
652 |
C<execute()>. |
|
update pod
|
653 | |
update pod
|
654 |
C<insert()> example: |
update pod
|
655 | |
656 |
$dbi->insert( |
|
657 |
table => 'book', |
|
658 |
param => {issue_date => $tp, first_issue_date => $tp}, |
|
659 |
filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'} |
|
660 |
); |
|
added DBIx::Custom::Guides
|
661 | |
update pod
|
662 |
C<execute()> example: |
update pod
|
663 | |
664 |
my $sql = <<"EOS"; |
|
665 |
select YEAR(issue_date) as issue_year |
|
666 |
from book |
|
667 |
where YEAR(issue_date) = {? issue_year} |
|
668 |
EOS |
|
669 |
|
|
added DBIx::Custom::Guides
|
670 |
my $result = $dbi->execute( |
update pod
|
671 |
$sql, |
672 |
param => {issue_year => '2010'}, |
|
673 |
filter => {issue_year => 'tp_to_year'} |
|
added DBIx::Custom::Guides
|
674 |
); |
675 | ||
update pod
|
676 |
You can also apply indivisual filter when you fetch row. |
677 |
use C<DBIx::Custom::Result>'s C<filter()>. |
|
added DBIx::Custom::Guides
|
678 | |
update pod
|
679 |
$result->filter(issue_year => 'year_to_tp'); |
added DBIx::Custom::Guides
|
680 | |
update pod
|
681 |
=head3 End filtering : C<end_filter()> |
update pod
|
682 | |
update pod
|
683 |
You can add filter at end. |
684 |
It is useful to create last output. |
|
685 |
use C<end_filter()> to add end filter. |
|
update pod
|
686 | |
687 |
$result->end_filter(issue_date => sub { |
|
688 |
my $tp = shift; |
|
689 |
|
|
690 |
return '' unless $tp; |
|
691 |
return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)'); |
|
692 |
}); |
|
693 | ||
update pod
|
694 |
In this example, L<Time::Piece> object is converted to readable format. |
update pod
|
695 | |
added experimental DBIx::Cus...
|
696 |
You can remove end_filter by C<end_filter> |
697 | ||
698 |
$result->remove_end_filter; |
|
699 | ||
update pod
|
700 |
=head3 Automate applying filter : C<each_column()> |
update pod
|
701 | |
update pod
|
702 |
It is useful to apply filter automatically at date type columns. |
703 |
You can use C<each_column()> to process all column infos. |
|
update pod
|
704 | |
705 |
$dbi->each_column( |
|
706 |
sub { |
|
707 |
my ($self, $table, $column, $info) = @_; |
|
708 |
|
|
709 |
my $type = $info->{TYPE_NAME}; |
|
710 |
|
|
711 |
my $filter = $type eq 'DATE' ? {out => 'tp_to_date', in => 'date_to_tp'} |
|
712 |
: $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'} |
|
713 |
: undef; |
|
714 |
|
|
715 |
$self->apply_filter($table, $column, $filter) |
|
716 |
if $filter; |
|
717 |
} |
|
718 |
); |
|
719 | ||
update pod
|
720 |
C<each_column()> receive callback. |
update pod
|
721 |
callback arguments are L<DBIx::Custom> object, table name, column name, column information. |
722 |
Filter is applied automatically by column type. |
|
update pod
|
723 | |
update pod
|
724 |
=head2 5. Tag |
update pod
|
725 | |
update pod
|
726 |
=head3 Basic of Tag |
update pod
|
727 | |
update pod
|
728 |
You can embedd tag into SQL. |
update pod
|
729 | |
730 |
select * from book where {= title} and {like author}; |
|
731 | ||
update pod
|
732 |
{= title} and {like author} are tag. Tag has the folloring format. |
update pod
|
733 | |
update pod
|
734 |
{TAG_NAME ARG1 ARG2 ...} |
update pod
|
735 | |
update pod
|
736 |
Tag start C<{> and end C<}>. |
update pod
|
737 |
Don't insert space between C<{}> and tag name. |
update pod
|
738 | |
739 |
C<{> and C<}> are reserved word. |
|
740 |
If you want to use these, escape it by '\'; |
|
update pod
|
741 | |
742 |
select from book \\{ ... \\} |
|
743 | ||
update pod
|
744 |
\ is perl's escape character, you need two \. |
745 | ||
746 |
Tag is expanded before executing SQL. |
|
update pod
|
747 | |
748 |
select * from book where title = ? and author like ?; |
|
749 | ||
update pod
|
750 |
use C<execute()> to execute SQL which contains tag |
update pod
|
751 | |
752 |
my $sql = "select * from book where {= author} and {like title};" |
|
753 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}); |
|
754 | ||
update pod
|
755 |
You can specify values embedded into place holder as hash reference using |
756 |
C<param> option. |
|
update pod
|
757 | |
update pod
|
758 |
You can specify C<filter()> at C<execute()>. |
update pod
|
759 | |
760 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
761 |
filter => {title => 'to_something'); |
|
762 | ||
update pod
|
763 |
Note that at C<execute()> the filter applied by C<apply_filter()> |
764 |
don't has effective to columns. |
|
update pod
|
765 |
You have to use C<table> tag in SQL |
update pod
|
766 | |
update pod
|
767 |
my $sql = "select * from {table book} where {= author} and {like title};" |
update pod
|
768 | |
update pod
|
769 |
=head3 Tag list |
update pod
|
770 | |
update pod
|
771 |
The following tag is available. |
update pod
|
772 | |
update pod
|
773 |
=head4 C<table> |
774 | ||
775 |
{table NAME} -> NAME |
|
776 | ||
777 |
This is used to specify table name in SQL. |
|
778 |
If you specify table name, Filtering by |
|
779 |
C<apply_filter()> is effective. |
|
780 | ||
update pod
|
781 |
=head4 C<?> |
update pod
|
782 | |
783 |
{? NAME} -> ? |
|
784 | ||
update pod
|
785 |
=head4 C<=> |
update pod
|
786 | |
787 |
{= NAME} -> NAME = ? |
|
788 | ||
update pod
|
789 |
=head4 C<E<lt>E<gt>> |
update pod
|
790 | |
791 |
{<> NAME} -> NAME <> ? |
|
792 | ||
update pod
|
793 |
=head4 C<E<lt>> |
update pod
|
794 | |
795 |
{< NAME} -> NAME < ? |
|
796 | ||
update pod
|
797 |
=head4 C<E<gt>> |
update pod
|
798 | |
799 |
{> NAME} -> NAME > ? |
|
800 | ||
update pod
|
801 |
=head4 C<E<gt>=> |
update pod
|
802 | |
803 |
{>= NAME} -> NAME >= ? |
|
804 | ||
update pod
|
805 |
=head4 C<E<lt>=> |
update pod
|
806 | |
807 |
{<= NAME} -> NAME <= ? |
|
808 | ||
update pod
|
809 |
=head4 C<like> |
update pod
|
810 | |
811 |
{like NAME} -> NAME like ? |
|
812 | ||
update pod
|
813 |
=head4 C<in> |
update pod
|
814 | |
815 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
816 | ||
update pod
|
817 |
=head4 C<insert_param> |
update pod
|
818 | |
added DBIx::Custom::Guides
|
819 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
update pod
|
820 | |
update pod
|
821 |
=head4 C<update_param> |
update pod
|
822 | |
added DBIx::Custom::Guides
|
823 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
824 | ||
update pod
|
825 |
=head3 Manipulate same name's columns |
added DBIx::Custom::Guides
|
826 | |
update pod
|
827 |
It is ok if there are same name's columns. |
828 |
Let's think two date comparison. |
|
added DBIx::Custom::Guides
|
829 | |
update pod
|
830 |
my $sql = "select * from table where {> date} and {< date};"; |
added DBIx::Custom::Guides
|
831 | |
update pod
|
832 |
In this case, You specify paramter values as array reference. |
added DBIx::Custom::Guides
|
833 | |
update pod
|
834 |
my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']}); |
added DBIx::Custom::Guides
|
835 | |
update pod
|
836 |
=head3 Register Tag : C<register_tag()> |
update pod
|
837 | |
update pod
|
838 |
You can register custom tag. |
update pod
|
839 |
use C<register_tag()> to register tag. |
update pod
|
840 | |
841 |
$dbi->register_tag( |
|
842 |
'=' => sub { |
|
843 |
my $column = shift; |
|
844 |
|
|
845 |
return ["$column = ?", [$column]]; |
|
added DBIx::Custom::Guides
|
846 |
} |
847 |
); |
|
848 | ||
update pod
|
849 |
This is implementation of C<=> tag. |
850 |
Tag format is the following one. |
|
added experimental DBIx::Cus...
|
851 | |
update pod
|
852 |
{TAG_NAME ARG1 ARG2 ...} |
added experimental DBIx::Cus...
|
853 | |
update pod
|
854 |
In case C<=> tag. Format is |
cleanup
|
855 | |
update pod
|
856 |
{= title} |
added experimental DBIx::Cus...
|
857 | |
update pod
|
858 |
So subroutine receive one argument "title". |
859 |
You have to return array reference in the following format. |
|
update pod
|
860 | |
861 |
[ |
|
update pod
|
862 |
String after expanding, |
863 |
[COLUMN1(This is used for place holder), COLUMN2 , ...] |
|
update pod
|
864 |
] |
865 | ||
update pod
|
866 |
First element is expanded stirng. In this example, |
update pod
|
867 | |
868 |
'title = ?' |
|
869 | ||
update pod
|
870 |
Secount element is array reference which is used to embedd value to |
871 |
place holder. In this example, |
|
update pod
|
872 | |
873 |
['title'] |
|
874 | ||
update pod
|
875 |
If there are more than one placeholders, |
876 |
This elements is multipul. |
|
update pod
|
877 | |
update pod
|
878 |
You return the following array reference. |
update pod
|
879 | |
880 |
['title = ?', ['title']] |
|
added DBIx::Custom::Guides
|
881 | |
update pod
|
882 |
See source of L<DBIx::Custom::Tag> to see many implementation. |
added DBIx::Custom::Guides
|
883 | |
update pod
|
884 |
=head2 6. Dinamically create where clause |
update pod
|
885 | |
update pod
|
886 |
=head3 Dinamically create where clause : where() |
update pod
|
887 | |
update pod
|
888 |
You want to search multiple conditions in many times. |
889 |
Let's think the following three cases. |
|
update pod
|
890 | |
update pod
|
891 |
Case1: Search only C<title> |
update pod
|
892 | |
893 |
where {= title} |
|
894 | ||
update pod
|
895 |
Case2: Search only C<author> |
update pod
|
896 | |
897 |
where {= author} |
|
898 | ||
update pod
|
899 |
Case3: Search C<title> and C<author> |
update pod
|
900 | |
901 |
where {= title} and {=author} |
|
902 | ||
update pod
|
903 |
L<DBIx::Custom> support dinamic where clause creating. |
904 |
At first, create L<DBIx::Custom::Where> object by C<where()>. |
|
update pod
|
905 | |
906 |
my $where = $dbi->where; |
|
907 | ||
update pod
|
908 |
Set clause by C<clause()> |
update pod
|
909 | |
910 |
$where->clause( |
|
911 |
['and', '{= title'}, '{= author}'] |
|
added DBIx::Custom::Guides
|
912 |
); |
913 | ||
update pod
|
914 |
C<clause> is the following format. |
update pod
|
915 | |
update pod
|
916 |
['or' or 'and', TAG1, TAG2, TAG3] |
added DBIx::Custom::Guides
|
917 | |
update pod
|
918 |
First argument is 'or' or 'and'. |
919 |
Later than first argument are tag names. |
|
update pod
|
920 | |
update pod
|
921 |
You can write more complex format. |
update pod
|
922 | |
923 |
['and', |
|
924 |
'{= title}', |
|
925 |
['or', '{= author}', '{like date}'] |
|
926 |
] |
|
927 | ||
update pod
|
928 |
This mean "{=title} and ( {=author} or {like date} )". |
929 | ||
930 |
After setting C<clause>, set C<param>. |
|
added DBIx::Custom::Guides
|
931 |
|
update pod
|
932 |
$where->param({title => 'Perl'}); |
added DBIx::Custom::Guides
|
933 | |
update pod
|
934 |
In this example, parameter contains only title. |
added DBIx::Custom::Guides
|
935 | |
update pod
|
936 |
If you execute C<string_to()>, you can get where clause |
937 |
which contain only parameter name. |
|
update pod
|
938 | |
939 |
my $where_clause = $where->to_string; |
|
940 | ||
update pod
|
941 |
Parameter name is only title, the following where clause is created. |
update pod
|
942 | |
943 |
where {= title} |
|
944 | ||
update pod
|
945 |
You can also create where clause by stringification. |
update pod
|
946 | |
947 |
my $where_clause = "$where"; |
|
948 | ||
update pod
|
949 |
This is useful to embbed it into SQL. |
added DBIx::Custom::Guides
|
950 | |
update pod
|
951 |
=head3 In case where clause contains same name columns |
check arguments of connect m...
|
952 | |
update pod
|
953 |
Even if same name tags exists, you can create where clause. |
954 |
Let's think that there are starting date and ending date. |
|
added experimental DBIx::Cus...
|
955 | |
update pod
|
956 |
my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
added experimental DBIx::Cus...
|
957 | |
update pod
|
958 |
In this case, you set parameter value as array reference. |
update pod
|
959 | |
960 |
my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
961 | ||
update pod
|
962 |
You can embbed these values into same name tags. |
update pod
|
963 | |
964 |
$where->clause( |
|
965 |
['and', '{> date}', '{< date}'] |
|
added experimental DBIx::Cus...
|
966 |
); |
update pod
|
967 |
$where->param($p); |
968 | ||
update pod
|
969 |
If starting date isn't exists, create the following parameter. |
update pod
|
970 | |
971 |
my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
972 | ||
update pod
|
973 |
You can get DBIx::Custom::NotExists object by C<not_exists()> |
974 |
This mean correnspondinf value isn't exists. |
|
update pod
|
975 | |
update pod
|
976 |
If ending date isn't exists, create the following parameter. |
update pod
|
977 | |
978 |
my $p = {date => ['2010-11-15']}; |
|
979 | ||
update pod
|
980 |
If both date isn't exists, create the following parameter. |
update pod
|
981 | |
982 |
my $p = {date => []}; |
|
983 | ||
update pod
|
984 |
This logic is a little difficut. See the following ones. |
update pod
|
985 | |
986 |
my @date; |
|
987 |
push @date, exists $param->{start_date} ? $param->{start_date} |
|
988 |
: $dbi->not_exists; |
|
989 |
push @date, $param->{end_date} if exists $param->{end_date}; |
|
990 |
my $p = {date => \@date}; |
|
991 | ||
update pod
|
992 |
=head3 With C<select()> |
update pod
|
993 | |
update pod
|
994 |
You can pass L<DBIx::Custom::Where> object to C<where> of C<select()>. |
update pod
|
995 |
|
996 |
my $where = $dbi->where; |
|
update pod
|
997 |
$where->clause(['and', '{= title}', '{= author}']); |
998 |
$where->param({title => 'Perl'}); |
|
update pod
|
999 |
my $result = $dbi->select(table => 'book', where => $where); |
1000 | ||
update pod
|
1001 |
You can also pass it to C<where> of C<update()>�AC<delete()> |
update pod
|
1002 | |
update pod
|
1003 |
=head3 With C<execute()> |
added experimental DBIx::Cus...
|
1004 | |
update pod
|
1005 |
L<DBIx::Custom::Where> object is embedded into SQL. |
added experimental DBIx::Cus...
|
1006 | |
update pod
|
1007 |
my $where = $dbi->where; |
update pod
|
1008 |
$where->clause(['and', '{= title}', '{= author}']); |
1009 |
$where->param({title => 'Perl'}); |
|
update pod
|
1010 | |
update pod
|
1011 |
my $sql = <<"EOS"; |
1012 |
select * from {table book}; |
|
update pod
|
1013 |
$where |
1014 |
EOS |
|
1015 | ||
1016 |
$dbi->execute($sql, param => $param); |
|
1017 | ||
add feture. all model class ...
|
1018 |
=head2 7. Model |
update pod
|
1019 | |
add feture. all model class ...
|
1020 |
=head3 Model |
update pod
|
1021 | |
add feture. all model class ...
|
1022 |
you can define model extending L<DBIx::Custom::Model> |
removed experimental base_ta...
|
1023 |
to improve source code view. |
update pod
|
1024 | |
add experimental DBIx::Custo...
|
1025 |
At first, you create basic model class extending <DBIx::Custom::Model>. |
update pod
|
1026 | |
add experimental DBIx::Custo...
|
1027 |
package MyModel; |
1028 |
|
|
1029 |
use base 'DBIx::Custom::Model'; |
|
update pod
|
1030 | |
add experimental DBIx::Custo...
|
1031 |
Next, you create each model classes. |
update pod
|
1032 | |
add experimental DBIx::Custo...
|
1033 |
MyModel::book |
removed experimental base_ta...
|
1034 | |
add experimental DBIx::Custo...
|
1035 |
package MyModel::book; |
1036 |
|
|
1037 |
use base 'MyModel'; |
|
1038 |
|
|
1039 |
sub insert { ... } |
|
1040 |
sub list { ... } |
|
removed experimental base_ta...
|
1041 | |
add experimental DBIx::Custo...
|
1042 |
MyModel::company |
removed experimental base_ta...
|
1043 | |
add experimental DBIx::Custo...
|
1044 |
package MyModel::company; |
add feture. all model class ...
|
1045 |
|
add experimental DBIx::Custo...
|
1046 |
use base 'MyModel'; |
1047 |
|
|
1048 |
sub insert { ... } |
|
1049 |
sub list { ... } |
|
add feture. all model class ...
|
1050 | |
1051 |
The follwoing modules location is needed. |
|
1052 | ||
1053 |
MyModel.pm |
|
1054 |
MyModel / book.pm |
|
1055 |
/ company.pm |
|
add experimental DBIx::Custo...
|
1056 | |
1057 |
You can include these models by C<include_model()> |
|
1058 | ||
1059 |
$dbi->include_model('MyModel'); |
|
1060 | ||
1061 |
First argument is name space of model. |
|
1062 | ||
add feture. all model class ...
|
1063 |
You can use model like this. |
1064 | ||
1065 |
my $result = $dbi->model('book')->list; |
|
1066 | ||
1067 |
In mode, You can use such as methods, |
|
1068 |
C<insert()>, C<update()>, C<update_all()>, |
|
1069 |
C<delete()>, C<delete_all()>, C<select()> |
|
1070 |
without C<table> option. |
|
1071 | ||
1072 |
$dbi->model('book')->insert(param => $param); |
|
update pod
|
1073 | |
add feture. all model class ...
|
1074 |
Model is L<DBIx::Custom::Model>. |
update pod
|
1075 | |
add feture. all model class ...
|
1076 |
If you need table name�Ayou can get it by C<table()>. |
update pod
|
1077 | |
add feture. all model class ...
|
1078 |
my $table = $model->table; |
update pod
|
1079 | |
add feture. all model class ...
|
1080 |
You can get L<DBIx::Custom>. |
update pod
|
1081 | |
add feture. all model class ...
|
1082 |
my $dbi = $model->dbi; |
removed experimental base_ta...
|
1083 | |
add feture. all model class ...
|
1084 |
You can also call all methods of L<DBIx::Custom> and L<DBI>. |
added experimental DBIx::Cus...
|
1085 | |
update pod
|
1086 |
# DBIx::Custom method |
add feture. all model class ...
|
1087 |
$model->execute($sql); |
update pod
|
1088 |
|
1089 |
# DBI method |
|
add feture. all model class ...
|
1090 |
$model->begin_work; |
1091 |
$model->commit; |
|
added experimental DBIx::Cus...
|
1092 | |
add models() attribute
|
1093 |
If you want to get all models, you can get them by keys of C<models()>. |
1094 | ||
1095 |
my @models = keys %{$self->models}; |
|
1096 | ||
add DBIx::Custom::Model fore...
|
1097 |
You can set primary key to model. |
1098 | ||
1099 |
$model->primary_key(['id', 'number_id']); |
|
1100 | ||
update pod
|
1101 |
Primary key is used by C<insert_at>, C<update_at()>, C<delete_at()>, |
add DBIx::Custom::Model fore...
|
1102 |
C<select_at()>. |
1103 | ||
add experimental DBIx::Custo...
|
1104 |
by C<filter> you can define filters applied by C<apply_filter()> |
1105 | ||
1106 |
$model->filter({ |
|
1107 |
title => {out => ..., in => ..., end => ...}, |
|
1108 |
author => {out => ..., in => ..., end => ...} |
|
1109 |
}); |
|
1110 | ||
1111 |
This filters is applied when C<include_model()> is called. |
|
1112 | ||
add DBIx::Custom::Model colu...
|
1113 |
You can set column names |
1114 | ||
1115 |
$model->columns(['id', 'number_id']); |
|
1116 | ||
add experimental setup_model...
|
1117 |
Column names is automarically set by C<setup_model()>. |
1118 |
This method is needed to be call after C<include_model()>. |
|
1119 | ||
1120 |
$dbi->setup_model; |
|
1121 | ||
cleanup
|
1122 |
You can set C<relation> |
1123 | ||
1124 |
$model->relation({'book.company_id' => 'company.id'}); |
|
1125 | ||
1126 |
This relation is used by C<select()>, C<select_at()> |
|
1127 | ||
add experimental DBIx::Custo...
|
1128 |
=head2 Class name, Model name, Table name |
1129 | ||
1130 |
Class name, model name, and table name is a little different. |
|
1131 |
Generally Class name is model name, and table name is model name. |
|
1132 | ||
1133 |
CLASS MODEL TABLE |
|
1134 |
book (CLASS) -> book (MODEL) -> book |
|
1135 | ||
1136 |
You can change model name. |
|
1137 | ||
1138 |
package MyModel::book; |
|
1139 |
|
|
1140 |
__PACAKGE__->attr(name => 'book_model'); |
|
1141 | ||
1142 |
CLASS MODEL TABLE |
|
1143 |
book book_model (MODEL) -> book_model |
|
1144 | ||
1145 |
Model name is the name used by L<model()> of L<DBIx::Custom>. |
|
1146 | ||
1147 |
$dbi->model('book_model'); |
|
1148 | ||
1149 |
You can change table name. |
|
1150 | ||
1151 |
package MyModel::book; |
|
1152 |
|
|
1153 |
__PACAKGE__->attr(table => 'book_table'); |
|
1154 | ||
1155 |
CLASS MODEL TABLE |
|
1156 |
book (CLASS) -> book book_table |
|
1157 | ||
1158 |
Table name is the table really accessed. |
|
1159 | ||
1160 |
$dbi->model('book')->insert(...); # access to "book_table" |
|
1161 | ||
add experimental DBIx::Custo...
|
1162 |
=head2 Create column clause automatically : column_clause() |
1163 | ||
1164 |
To create column clause automatically, use C<column_clause()>. |
|
1165 |
Valude of C<table> and C<columns> is used. |
|
1166 | ||
1167 |
my $column_clause = $model->column_clause; |
|
1168 | ||
1169 |
If C<table> is 'book'�AC<column> is ['id', 'name'], |
|
1170 |
the following clause is created. |
|
1171 | ||
1172 |
book.id as id, book.name as name |
|
1173 | ||
1174 |
These column name is for removing column name ambiguities. |
|
1175 | ||
1176 |
If you remove some columns, use C<remove> option. |
|
1177 | ||
1178 |
my $column_clause = $model->column_clause(remove => ['id']); |
|
1179 | ||
1180 |
If you add some column, use C<add> option. |
|
1181 | ||
1182 |
my $column_clause = $model->column_clause(add => ['company.id as company__id']); |
|
1183 | ||
add feture. all model class ...
|
1184 |
=head2 Model Examples |
added DBIx::Custom::Guides
|
1185 | |
add feture. all model class ...
|
1186 |
Model examples |
added DBIx::Custom::Guides
|
1187 | |
update pod
|
1188 |
package MyDBI; |
1189 |
|
|
1190 |
use base 'DBIx::Custom'; |
|
added DBIx::Custom::Guides
|
1191 |
|
update pod
|
1192 |
sub connect { |
1193 |
my $self = shift->SUPER::connect(@_); |
|
1194 |
|
|
add feture. all model class ...
|
1195 |
$self->include_model( |
1196 |
MyModel => [ |
|
removed experimental base_ta...
|
1197 |
'book', |
1198 |
'company' |
|
1199 |
] |
|
update pod
|
1200 |
); |
1201 |
} |
|
removed experimental base_ta...
|
1202 |
|
add feture. all model class ...
|
1203 |
package MyModel::book; |
1204 |
use base 'DBIx::Custom::Model'; |
|
removed experimental base_ta...
|
1205 |
|
add experimental update_at()...
|
1206 |
__PACKAGE__->attr('primary_key' => sub { ['id'] }; |
1207 |
|
|
removed experimental base_ta...
|
1208 |
sub insert { ... } |
1209 |
sub list { ... } |
|
1210 |
|
|
add feture. all model class ...
|
1211 |
package MyModel::company; |
1212 |
use base 'DBIx::Custom::Model'; |
|
add experimental update_at()...
|
1213 | |
1214 |
__PACKAGE__->attr('primary_key' => sub { ['id'] }; |
|
removed experimental base_ta...
|
1215 |
|
1216 |
sub insert { ... } |
|
1217 |
sub list { ... } |
|
update pod
|
1218 | |
update pod
|
1219 |
=head2 8. Improve performance |
update pod
|
1220 | |
update pod
|
1221 |
=head3 Create query |
update pod
|
1222 | |
update pod
|
1223 |
If you can't get performance, create query by C<query> option. |
1224 |
For example, many insert is needed. |
|
update pod
|
1225 | |
update pod
|
1226 |
my $params = [ |
1227 |
{title => 'Perl', author => 'Ken'}, |
|
1228 |
{title => 'Good day', author => 'Tom'} |
|
1229 |
] |
|
1230 |
my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1); |
|
added DBIx::Custom::Guides
|
1231 | |
update pod
|
1232 |
Return value is L<DBIx::Custom::Query> object. |
1233 |
This query is executed by C<execute()>. |
|
added DBIx::Custom::Guides
|
1234 | |
update pod
|
1235 |
foreach my $param (@$params) { |
1236 |
$dbi->execute($query, $param); |
|
added DBIx::Custom::Guides
|
1237 |
} |
1238 | ||
update pod
|
1239 |
Performance is improved because statement handle is reused |
1240 |
C<query> option is used in C<insert()>, C<update()>, C<update_all()>, |
|
1241 |
C<delete()>, C<delete_all()>. |
|
1242 | ||
1243 |
Note that parameters count is same as method for creating query and C<execute()>. |
|
added DBIx::Custom::Guides
|
1244 | |
update pod
|
1245 |
You can create query from any SQL by C<create_query()>. |
added DBIx::Custom::Guides
|
1246 | |
update pod
|
1247 |
my $query = $dbi->create_query( |
1248 |
"insert into book {insert_param title author};"; |
|
1249 |
); |
|
added DBIx::Custom::Guides
|
1250 | |
update pod
|
1251 |
=head2 9. Other features |
added DBIx::Custom::Guides
|
1252 | |
update pod
|
1253 |
=head3 Add method |
added DBIx::Custom::Guides
|
1254 | |
update pod
|
1255 |
You can add method to L<DBIx::Custom> object. |
update pod
|
1256 |
use C<method()>. |
added DBIx::Custom::Guides
|
1257 | |
update pod
|
1258 |
$dbi->method( |
1259 |
update_or_insert => sub { |
|
1260 |
my $self = shift; |
|
1261 |
# something |
|
1262 |
}, |
|
1263 |
find_or_create => sub { |
|
1264 |
my $self = shift; |
|
1265 |
# something |
|
1266 |
} |
|
1267 |
); |
|
1268 | ||
update pod
|
1269 |
You can call these methods from L<DBIx::Custom> object. |
update pod
|
1270 | |
1271 |
$dbi->update_or_insert; |
|
1272 |
$dbi->find_or_create; |
|
1273 | ||
update pod
|
1274 |
=head3 Change result class |
added DBIx::Custom::Guides
|
1275 | |
update pod
|
1276 |
You can change result class. By default it is L<DBIx::Custom::Result>. |
added DBIx::Custom::Guides
|
1277 | |
update pod
|
1278 |
package MyResult; |
added DBIx::Custom::Guides
|
1279 |
use base 'DBIx::Custom::Result'; |
1280 |
|
|
1281 |
sub some_method { ... } |
|
1282 | ||
1283 |
1; |
|
1284 |
|
|
1285 |
package main; |
|
1286 |
|
|
update pod
|
1287 |
use MyResult; |
added DBIx::Custom::Guides
|
1288 |
|
1289 |
my $dbi = DBIx::Custom->connect(...); |
|
update pod
|
1290 |
$dbi->result_class('MyResult'); |
added DBIx::Custom::Guides
|
1291 | |
update pod
|
1292 |
=head3 Caching |
added DBIx::Custom::Guides
|
1293 | |
update pod
|
1294 |
SQL after parsing tag is cached for performance. |
1295 |
You can set C<cache()>. By default, chaching is true. |
|
added DBIx::Custom::Guides
|
1296 | |
update pod
|
1297 |
$dbi->cache(1); |
added DBIx::Custom::Guides
|
1298 | |
update pod
|
1299 |
The way to cache is changed by C<cache_method()>. |
1300 |
Default method is the following one. |
|
1301 |
Cache is saved to memory. |
|
update pod
|
1302 | |
1303 |
$dbi->cache_method(sub { |
|
1304 |
sub { |
|
added DBIx::Custom::Guides
|
1305 |
my $self = shift; |
update pod
|
1306 |
|
1307 |
$self->{_cached} ||= {}; |
|
1308 |
|
|
1309 |
if (@_ > 1) { |
|
update pod
|
1310 |
# Save cache |
update pod
|
1311 |
$self->{_cached}{$_[0]} = $_[1] |
1312 |
} |
|
1313 |
else { |
|
update pod
|
1314 |
# Get cache |
update pod
|
1315 |
return $self->{_cached}{$_[0]} |
1316 |
} |
|
added DBIx::Custom::Guides
|
1317 |
} |
update pod
|
1318 |
}); |
1319 |
|
|
update pod
|
1320 |
First argument is L<DBIx::Custom> object. |
1321 |
Second argument is SQL before parsing. |
|
1322 |
Third argument is SQL information after parsing. This is hash reference. |
|
added DBIx::Custom::Guides
|
1323 | |
update pod
|
1324 |
If third argument exists, you save cache, |
1325 |
and if third argument isn't exists, you get chace. |
|
added DBIx::Custom::Guides
|
1326 | |
pod fix
|
1327 |
=head1 EXAMPLES |
1328 | ||
update pod
|
1329 |
You can see exsamples in the following wiki. |
1330 | ||
pod fix
|
1331 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
add examples
|
1332 | |
added DBIx::Custom::Guides
|
1333 |
=cut |