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