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