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