update pod
|
1 |
=encoding utf8 |
2 | ||
added DBIx::Custom::Guides
|
3 |
=head1 NAME |
4 | ||
update pod
|
5 |
DBIx::Custom::Guide - DBIx::Custom Guide |
added DBIx::Custom::Guides
|
6 | |
pod fix
|
7 |
=head1 GUIDE |
added DBIx::Custom::Guides
|
8 | |
update pod
|
9 |
B<This guide is now writing.> |
update pod
|
10 | |
update pod
|
11 |
L<DBIx::Custom> is the class to make easy to execute SQL. |
12 |
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>. |
|
13 |
You can do thing more easy than L<DBIx::Class>, more flexible |
|
14 |
than L<DBIx::Simple>. |
|
added DBIx::Custom::Guides
|
15 | |
update pod
|
16 |
L<DBIx::Custom> is B<not< O/R mapper, O/R mapper is usefule, but |
17 |
you must learn many things. Created SQL is sometimes inefficient, |
|
18 |
and in many cases you create raw SQL because |
|
19 |
O/R mapper can't make complex SQL |
|
added DBIx::Custom::Guides
|
20 | |
update pod
|
21 |
L<DBIx::Custom> is opposit of O/R mapper. |
22 |
The main purpose is that we respect SQL |
|
23 |
and make easy difficult works if you use only L<DBI>. |
|
24 |
If you already learn SQL, it is easy to use L<DBIx::Custom>. |
|
deprecated DBIx::Custom::MyS...
|
25 | |
update pod
|
26 |
I explain L<DBIx::Custom> a little in this section. |
27 |
In L<DBIx::Custom>, you embbed tag in SQL. |
|
deprecated DBIx::Custom::MyS...
|
28 | |
update pod
|
29 |
select * from book where {= title} and {=author}; |
deprecated DBIx::Custom::MyS...
|
30 | |
update pod
|
31 |
The part arround {} is tag. |
32 |
This SQL is converted to the one which contains place holder. |
|
33 | ||
34 |
select * from book where title = ? and author = ?; |
|
35 | ||
36 |
Maybe you ask me that this conversion is meaningful. |
|
37 |
On the top of this, usuful features is implemented. |
|
38 |
See the following descriptions. |
|
39 | ||
40 |
=over 4 |
|
41 | ||
42 |
=item 1. Specify place holder binding value as hash refernce |
|
43 | ||
44 |
If you use L<DBI>, you must specify place holder binding value |
|
45 |
as array. |
|
46 | ||
47 |
$sth->execute(@bind); |
|
48 | ||
49 |
If you use L<DBIx::Custom>, you specify it as hash reference. |
|
50 |
|
|
51 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
52 |
$dbi->execute($sql, $param); |
|
53 | ||
54 |
=item 2. Filtering |
|
55 | ||
56 |
L<DBIx::Custom> provides filtering system. |
|
57 |
For example, You think that about date value you want to |
|
58 |
manipulate it as date object like L<Time::Piece> in Perl, |
|
59 |
and want to convert it to database DATE format. |
|
60 |
and want to do reverse. |
|
61 | ||
62 |
You can use filtering system. |
|
63 | ||
64 |
At first, register filter. |
|
65 | ||
66 |
$dbi->register_filter( |
|
67 |
tp_to_date => sub { |
|
68 |
... |
|
69 |
}, |
|
70 |
date_to_tp => sub { |
|
71 |
... |
|
72 |
} |
|
73 |
); |
|
74 | ||
75 |
next, apply this filter to each column. |
|
76 | ||
77 |
$dbi->apply_filter('book', |
|
78 |
'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'} |
|
79 |
); |
|
80 | ||
81 |
C<out> is perl-to-database way. C<in> is perl-from-database way. |
|
82 | ||
83 |
This filter is automatically enabled in many method. |
|
84 | ||
85 |
$dbi->insert(table => 'book', param => {issue_date => $tp}); |
|
86 | ||
87 | ||
88 |
=item 3. Selective search condition |
|
89 | ||
90 |
It is difficult to create selective where clause in L<DBI>. |
|
91 |
For example, If C<title> and C<author> is specified, we create |
|
92 |
the following SQL. |
|
93 | ||
94 |
select * from book where title = ? and author = ?; |
|
95 | ||
96 |
If only C<title> is specified, the following one |
|
97 | ||
98 |
select * from book where title = ?; |
|
99 | ||
100 |
If only C<author> is specified, the following one, |
|
101 | ||
102 |
select * from book where author = ?; |
|
103 | ||
104 |
This is hard work. Generally we use modules like L<SQL::Abstract>. |
|
105 |
L<DBIx::Custom> prepare the way to make it easy. |
|
106 | ||
107 |
# Where object |
|
108 |
my $where = $dbi->where; |
|
109 |
|
|
110 |
# Search condition |
|
111 |
$where->clause( |
|
112 |
['and', '{= title}', {'= author'}] |
|
113 |
); |
|
114 |
|
|
115 |
# Setting to automatically select needed column |
|
116 |
$where->param({title => 'Perl'}); |
|
117 | ||
118 |
# Embbed where clause to SQL |
|
119 |
my $sql = "select * from book $where"; |
|
120 | ||
121 |
You can create where clause which has selected search condition. |
|
122 |
You can write nesting of where clause and C<or> condition |
|
123 | ||
update pod
|
124 |
=item 4. Methods for insert, update, delete, select |
update pod
|
125 | |
update pod
|
126 |
L<DBIx::Custom> provides methods for insert, update, delete, select |
127 |
There are C<insert()>, C<update()>, C<delete()>,C<select()>. |
|
update pod
|
128 | |
129 |
my $param = {title => 'Perl', author => 'Ken'}; |
|
130 |
$dbi->insert(table => 'book', param => $param); |
|
131 | ||
update pod
|
132 |
=item 5. Register method for table. |
deprecated DBIx::Custom::MyS...
|
133 | |
update pod
|
134 |
You can register method for table. |
update pod
|
135 | |
update pod
|
136 |
$dbi->table('book')->method( |
update pod
|
137 |
list => sub { |
138 |
... |
|
139 |
}, |
|
update pod
|
140 |
something => sub { |
141 |
... |
|
update pod
|
142 |
} |
143 |
); |
|
144 | ||
update pod
|
145 |
use the mehtod. |
update pod
|
146 | |
147 |
$dbi->table('book')->list; |
|
148 | ||
update pod
|
149 |
Many O/R mapper must create class for table, |
150 |
but L<DBIx::Custom> make it easy. |
|
update pod
|
151 | |
152 |
=back |
|
153 | ||
update pod
|
154 |
L<DBIx::Custom> is very useful. |
155 |
See the following if you are interested in it. |
|
update pod
|
156 | |
update pod
|
157 |
=head2 1. Connect to database |
update pod
|
158 | |
update pod
|
159 |
Load L<DBIx::Custom>. |
update pod
|
160 | |
161 |
use DBIx::Custom; |
|
162 | ||
update pod
|
163 |
use C<connect()> to connect to database. |
164 |
Return value is L<DBIx::Custom> object. |
|
update pod
|
165 | |
166 |
my $dbi = DBIx::Custom->connect( |
|
update pod
|
167 |
data_source => "dbi:mysql:database=bookstore", |
update pod
|
168 |
user => 'ken', |
169 |
password => '!LFKD%$&', |
|
170 |
dbi_options => {mysql_enable_utf8 => 1} |
|
171 |
); |
|
172 | ||
update pod
|
173 |
C<data_source> must be one corresponding to the database system. |
174 |
The following ones are data source example. |
|
update pod
|
175 | |
176 |
B<MySQL> |
|
remove DBIx::Custom::Model
|
177 | |
178 |
"dbi:mysql:database=$database" |
|
179 |
"dbi:mysql:database=$database;host=$hostname;port=$port" |
|
180 | ||
update pod
|
181 |
B<SQLite> |
182 | ||
183 |
"dbi:SQLite:dbname=$database" |
|
184 |
"dbi:SQLite:dbname=:memory:" |
|
185 | ||
186 |
B<PostgreSQL> |
|
deprecated DBIx::Custom::MyS...
|
187 | |
188 |
"dbi:Pg:dbname=$dbname" |
|
189 | ||
update pod
|
190 |
B<Oracle> |
deprecated DBIx::Custom::MyS...
|
191 | |
192 |
"dbi:Oracle:$dbname" |
|
193 |
"dbi:Oracle:host=$host;sid=$sid" |
|
194 | ||
update pod
|
195 |
B<ODBC(Microsoft Access)> |
deprecated DBIx::Custom::MyS...
|
196 | |
197 |
"dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb" |
|
198 | ||
update pod
|
199 |
B<ODBC(SQL Server)> |
deprecated DBIx::Custom::MyS...
|
200 | |
201 |
"dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;" |
|
added DBIx::Custom::Guides
|
202 | |
update pod
|
203 |
If authentication is needed, you can specify C<user> and C<password> |
204 | ||
205 |
L<DBIx::Custom> is wrapper class of L<DBI>. |
|
206 |
You can use all methods of L<DBI> from L<DBIx::Custom> object. |
|
207 | ||
208 |
$dbi->do(...); |
|
209 |
$dbi->begin_work; |
|
update pod
|
210 | |
update pod
|
211 |
use C<dhb()> to get database handle of L<DBI> |
added DBIx::Custom::Guides
|
212 | |
update pod
|
213 |
my $dbh = $dbi->dbh; |
added DBIx::Custom::Guides
|
214 | |
update pod
|
215 |
By default, the following ones is set to database handle attributes. |
216 | ||
217 |
RaiseError -> 1 |
|
218 |
PrintError -> 0 |
|
219 |
AutoCommit -> 1 |
|
added DBIx::Custom::Guides
|
220 | |
update pod
|
221 |
If fatal error occuer, program terminate. |
222 |
If SQL is executed, commit is executed automatically. |
|
update pod
|
223 | |
update pod
|
224 |
=head2 2. Methods for insert, update, delete, or insert |
update pod
|
225 | |
update pod
|
226 |
There are following methods. |
update pod
|
227 | |
update pod
|
228 |
=head3 C<insert()> |
update pod
|
229 | |
update pod
|
230 |
use C<insert()> to insert row into database |
added DBIx::Custom::Guides
|
231 | |
remove DBIx::Custom::Model
|
232 |
$dbi->insert(table => 'book', |
added DBIx::Custom::Guides
|
233 |
param => {title => 'Perl', author => 'Ken'}); |
234 | ||
update pod
|
235 |
C<table> is table name, C<param> is insert data. |
added DBIx::Custom::Guides
|
236 | |
update pod
|
237 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
238 | |
update pod
|
239 |
insert into (title, author) values (?, ?); |
added DBIx::Custom::Guides
|
240 | |
update pod
|
241 |
=head3 C<update()> |
added DBIx::Custom::Guides
|
242 | |
update pod
|
243 |
use C<update()> to update row in database. |
added DBIx::Custom::Guides
|
244 | |
remove DBIx::Custom::Model
|
245 |
$dbi->update(table => 'book', |
added DBIx::Custom::Guides
|
246 |
param => {title => 'Perl', author => 'Ken'}, |
247 |
where => {id => 5}); |
|
248 | ||
update pod
|
249 |
C<table> is table name, C<param> is update data, C<where> is condition. |
added DBIx::Custom::Guides
|
250 | |
update pod
|
251 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
252 | |
update pod
|
253 |
update book set title = ?, author = ?; |
added DBIx::Custom::Guides
|
254 | |
update pod
|
255 |
You can't execute C<update()> without C<where> for safety. |
256 |
use C<update_all()> if you want to update all rows. |
|
added DBIx::Custom::Guides
|
257 | |
update pod
|
258 |
$dbi->update_all(table => 'book', |
259 |
param => {title => 'Perl', author => 'Ken'}); |
|
added DBIx::Custom::Guides
|
260 | |
update pod
|
261 |
=head3 C<delete()> |
added DBIx::Custom::Guides
|
262 | |
update pod
|
263 |
use C<delete()> to delete rows from database. |
added DBIx::Custom::Guides
|
264 | |
remove DBIx::Custom::Model
|
265 |
$dbi->delete(table => 'book', |
added DBIx::Custom::Guides
|
266 |
where => {author => 'Ken'}); |
267 | ||
update pod
|
268 |
C<table> is table name, C<where> is condition. |
added DBIx::Custom::Guides
|
269 | |
update pod
|
270 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
271 | |
update pod
|
272 |
delete from book where id = ?; |
added DBIx::Custom::Guides
|
273 | |
update pod
|
274 |
You can't execute C<delete()> without C<where> for safety. |
275 |
use C<delete_all()> if you want to delete all rows. |
|
added DBIx::Custom::Guides
|
276 | |
update pod
|
277 |
$dbi->delete_all(table => 'book'); |
added DBIx::Custom::Guides
|
278 | |
update pod
|
279 |
=head3 C<select()> |
added DBIx::Custom::Guides
|
280 | |
update pod
|
281 |
use C<select()> to select rows from database |
added DBIx::Custom::Guides
|
282 | |
remove DBIx::Custom::Model
|
283 |
my $result = $dbi->select(table => 'book'); |
added DBIx::Custom::Guides
|
284 | |
update pod
|
285 |
Following SQL is executed. |
added DBIx::Custom::Guides
|
286 | |
remove DBIx::Custom::Model
|
287 |
select * from book; |
added DBIx::Custom::Guides
|
288 | |
update pod
|
289 |
Return value is L<DBIx::Custom::Result> object. |
290 |
use C<fetch()> to fetch row. |
|
added DBIx::Custom::Guides
|
291 | |
292 |
while (my $row = $result->fetch) { |
|
293 |
my $title = $row->[0]; |
|
294 |
my $author = $row->[1]; |
|
295 |
} |
|
296 | ||
update pod
|
297 |
See L<3. Fetch row/"3. Fetch row"> about L<DBIx::Custom::Result>. |
added DBIx::Custom::Guides
|
298 | |
update pod
|
299 |
Continue more examples. |
added DBIx::Custom::Guides
|
300 | |
301 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
302 |
table => 'book', |
update pod
|
303 |
column => ['author', 'title'], |
added DBIx::Custom::Guides
|
304 |
where => {author => 'Ken'} |
305 |
); |
|
306 | ||
update pod
|
307 |
C<column> is column names, C<where> is condition. |
308 | ||
309 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
310 | |
remove DBIx::Custom::Model
|
311 |
select author, title from book where author = ?; |
added DBIx::Custom::Guides
|
312 | |
update pod
|
313 |
Next example. |
added DBIx::Custom::Guides
|
314 | |
315 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
316 |
table => ['book', 'rental'], |
update pod
|
317 |
where => {'book.name' => 'Perl'}, |
remove DBIx::Custom::Model
|
318 |
relation => {'book.id' => 'rental.book_id'} |
added DBIx::Custom::Guides
|
319 |
); |
320 | ||
update pod
|
321 |
C<relation> is relation of tables. This is inner join. |
322 | ||
323 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
324 | |
update pod
|
325 |
select * from book, rental where book.name = ? and book.id = rental.book_id; |
added DBIx::Custom::Guides
|
326 | |
update pod
|
327 |
Next example. |
added DBIx::Custom::Guides
|
328 | |
329 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
330 |
table => 'book', |
added DBIx::Custom::Guides
|
331 |
where => {author => 'Ken'}, |
update pod
|
332 |
append => 'for update', |
added DBIx::Custom::Guides
|
333 |
); |
334 | ||
update pod
|
335 |
C<append> is string appending to end of SQL. |
336 | ||
337 |
Following SQL is executed. |
|
added DBIx::Custom::Guides
|
338 | |
update pod
|
339 |
select * book where author = ? for update; |
added DBIx::Custom::Guides
|
340 | |
update pod
|
341 |
C<appned> is also used at C<insert()>, C<update()>, C<update_all()> |
342 |
C<delete()>, C<delete_all()>, and C<select()>. |
|
added DBIx::Custom::Guides
|
343 | |
update pod
|
344 |
=head3 C<execute()> SQL |
added DBIx::Custom::Guides
|
345 | |
update pod
|
346 |
use C<execute()> to execute SQL |
update pod
|
347 | |
348 |
$dbi->execute("select * from book;"); |
|
349 | ||
update pod
|
350 |
Process tag and execute SQL. |
update pod
|
351 | |
352 |
$dbi->execute( |
|
353 |
"select * from book {= title} and {= author};" |
|
354 |
param => {title => 'Perl', author => 'Ken'} |
|
355 |
); |
|
356 | ||
update pod
|
357 |
Following SQL is executed. |
update pod
|
358 | |
359 |
select * from book title = ? and author = ?; |
|
360 | ||
update pod
|
361 |
Values of title and author is embbdeded into placeholder. |
update pod
|
362 | |
update pod
|
363 |
See L<5. Tag/"5. Tag"> about tag. |
update pod
|
364 | |
update pod
|
365 |
You don't have to wirte last semicolon in C<execute()>. |
update pod
|
366 | |
367 |
$dbi->execute('select * from book'); |
|
368 | ||
update pod
|
369 |
=head2 3. Fetch row |
update pod
|
370 | |
update pod
|
371 |
Return value of C<select()> is L<DBIx::Custom::Result> object. |
372 |
There are many methods to fetch row. |
|
update pod
|
373 | |
update pod
|
374 |
=head3 Fetch a row (array) C<fetch()> |
update pod
|
375 | |
update pod
|
376 |
use C<fetch()> to fetch a row and assign it into array reference. |
377 | ||
378 |
my $row = $result->fetch; |
|
379 | ||
380 |
You can get all rows. |
|
added DBIx::Custom::Guides
|
381 | |
382 |
while (my $row = $result->fetch) { |
|
update pod
|
383 |
my $title = $row->[0]; |
384 |
my $author = $row->[1]; |
|
added DBIx::Custom::Guides
|
385 |
} |
386 | ||
update pod
|
387 |
=head3 Fetch only first row (array) C<fetch_first()> |
update pod
|
388 | |
update pod
|
389 |
use C<fetch_first()> to fetch only first row. |
added DBIx::Custom::Guides
|
390 | |
391 |
my $row = $result->fetch_first; |
|
392 | ||
update pod
|
393 |
You can't fetch rest rows |
394 |
because statement handle C<finish()> is executed. |
|
added DBIx::Custom::Guides
|
395 | |
update pod
|
396 |
=head3 Fetch rows (array) C<fetch_multi()> |
update pod
|
397 | |
update pod
|
398 |
use C<fetch_multi()> to fetch rows and assign it into |
399 |
array reference which has array references as element. |
|
update pod
|
400 | |
401 |
while (my $rows = $result->fetch_multi(2)) { |
|
402 |
my $title0 = $rows->[0][0]; |
|
403 |
my $author0 = $rows->[0][1]; |
|
404 |
|
|
405 |
my $title1 = $rows->[1][0]; |
|
406 |
my $author1 = $rows->[1][1]; |
|
added DBIx::Custom::Guides
|
407 |
} |
update pod
|
408 | |
update pod
|
409 |
Specify row count as argument. |
update pod
|
410 | |
update pod
|
411 |
You can get the following data. |
update pod
|
412 | |
413 |
[ |
|
414 |
['Perl', 'Ken'], |
|
415 |
['Ruby', 'Mark'] |
|
416 |
] |
|
417 | ||
update pod
|
418 |
=head3 Fetch all rows (array) C<fetch_all> |
update pod
|
419 | |
update pod
|
420 |
use C<fetch_all()> to fetch all rows and assign it into |
421 |
array reference which has array reference as element. |
|
added DBIx::Custom::Guides
|
422 | |
423 |
my $rows = $result->fetch_all; |
|
424 | ||
update pod
|
425 |
You can get the following data. |
update pod
|
426 | |
427 |
[ |
|
428 |
['Perl', 'Ken'], |
|
429 |
['Ruby', 'Mark'] |
|
430 |
] |
|
431 | ||
update pod
|
432 |
=head3 Fetch a row (hash) C<fetch_hash()> |
update pod
|
433 | |
update pod
|
434 |
use C<fetch_hash()> to fetch a row and assign it into hash reference. |
added DBIx::Custom::Guides
|
435 | |
436 |
while (my $row = $result->fetch_hash) { |
|
437 |
my $title = $row->{title}; |
|
438 |
my $author = $row->{author}; |
|
439 |
} |
|
440 | ||
update pod
|
441 |
=head3 Fetch only first row (hash) C<fetch_hash_first()> |
update pod
|
442 | |
update pod
|
443 |
use C<fetch_hash_first()> to fetch only first row |
444 |
and assign it into hash reference. |
|
added DBIx::Custom::Guides
|
445 | |
446 |
my $row = $result->fetch_hash_first; |
|
update pod
|
447 | |
update pod
|
448 |
You can't fetch rest rows |
449 |
because statement handle C<finish()> is executed. |
|
update pod
|
450 | |
update pod
|
451 |
=head3 Fetch rows (hash) C<fetch_hash_multi()> |
update pod
|
452 | |
update pod
|
453 |
use C<fetch_hash_multi()> to fetch rows and |
454 |
assign it into array reference which has hash references as element. |
|
added DBIx::Custom::Guides
|
455 | |
456 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
update pod
|
457 |
my $title0 = $rows->[0]{title}; |
458 |
my $author0 = $rows->[0]{author}; |
|
459 |
my $title1 = $rows->[1]{title}; |
|
460 |
my $author1 = $rows->[1]{author}; |
|
added DBIx::Custom::Guides
|
461 |
} |
update pod
|
462 | |
update pod
|
463 |
Specify row count as argument. |
update pod
|
464 | |
update pod
|
465 |
You can get the following data. |
update pod
|
466 | |
467 |
[ |
|
468 |
{title => 'Perl', author => 'Ken'}, |
|
469 |
{title => 'Ruby', author => 'Mark'} |
|
470 |
] |
|
471 | ||
update pod
|
472 |
=head3 Fetch all rows (hash) C<fetch_hash_all()> |
update pod
|
473 | |
update pod
|
474 |
use C<fetch_hash_all()> to fetch all rows and |
475 |
assign it into array reference which has hash |
|
476 |
references as element. |
|
added DBIx::Custom::Guides
|
477 | |
478 |
my $rows = $result->fetch_hash_all; |
|
479 | ||
update pod
|
480 |
You can get the following data. |
update pod
|
481 | |
482 |
[ |
|
483 |
{title => 'Perl', author => 'Ken'}, |
|
484 |
{title => 'Ruby', author => 'Mark'} |
|
485 |
] |
|
486 | ||
update pod
|
487 |
=head3 Statement handle C<sth()> |
update pod
|
488 | |
update pod
|
489 |
use <sth()> to get statement handle. |
added DBIx::Custom::Guides
|
490 | |
491 |
my $sth = $result->sth; |
|
492 | ||
update pod
|
493 |
=head2 4. Filtering |
update pod
|
494 | |
update pod
|
495 |
L<DBIx::Custom> provide value filtering. |
496 |
For example, You maybe want to convert L<Time::Piece> object to |
|
497 |
database date format when register data into database. |
|
498 |
and convert database date fromat to L<Time::Piece> object |
|
499 |
when get data from database. |
|
update pod
|
500 | |
update pod
|
501 |
=head3 Register filter C<register_filter()> |
update pod
|
502 | |
update pod
|
503 |
use C<register_filter() to register filter. |
update pod
|
504 | |
505 |
$dbi->register_filter( |
|
506 |
# Time::Piece object to DATE format |
|
507 |
tp_to_date => sub { |
|
508 |
my $date = shift; |
|
509 | ||
510 |
return '0000-00-00' unless $tp; |
|
511 |
return $tp->strftime('%Y-%m-%d'); |
|
512 |
}, |
|
513 |
|
|
514 |
# DATE to Time::Piece object |
|
515 |
date_to_tp => sub { |
|
516 |
my $date = shift; |
|
517 | ||
518 |
return if $date eq '0000-00-00'; |
|
519 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
520 |
}, |
|
521 |
); |
|
added DBIx::Custom::Guides
|
522 | |
update pod
|
523 |
Registered filter is used by C<apply_filter()> or etc. |
added DBIx::Custom::Guides
|
524 | |
update pod
|
525 |
=head3 Apply filter C<apply_filter()> |
added DBIx::Custom::Guides
|
526 | |
update pod
|
527 |
use C<apply_filter()> to apply registered filter. |
update pod
|
528 | |
529 |
$dbi->apply_filter('book', |
|
530 |
issue_date => {out => 'tp_to_date', in => 'date_to_tp'}, |
|
531 |
first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'} |
|
added DBIx::Custom::Guides
|
532 |
); |
533 | ||
update pod
|
534 |
First argument is table name. Arguments after first argument are pairs of column |
535 |
name and fitering rule. C<out> of filtering rule is filter which is used when data |
|
536 |
is send to database. C<in> of filtering rule is filter which is used when data |
|
537 |
is got from database. |
|
538 | ||
539 |
You can specify code reference as filter. |
|
update pod
|
540 | |
541 |
issue_date => {out => sub { ... }, in => sub { ... }} |
|
542 | ||
update pod
|
543 |
Applied filter become effective at insert()>, C<update()>, C<update_all()>, |
544 |
C<delete()>, C<delete_all()>, C<select()>. |
|
update pod
|
545 | |
546 |
my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d'); |
|
update pod
|
547 |
my $result = $dbi->select(table => 'book', where => {issue_date => $tp}); |
update pod
|
548 | |
update pod
|
549 |
When data is send to database, L<Time::Piece> object is converted |
550 |
to database date format "2010-10-14" |
|
update pod
|
551 | |
update pod
|
552 |
When data is fetched, database date format is |
553 |
converted to L<Time::Piece> object. |
|
update pod
|
554 | |
555 |
my $row = $resutl->fetch_hash_first; |
|
556 |
my $tp = $row->{issue_date}; |
|
557 | ||
update pod
|
558 |
You can also use column name which contains table name. |
update pod
|
559 | |
560 |
$dbi->select( |
|
561 |
table => 'book', |
|
update pod
|
562 |
where => {'book.issue_date' => $tp} |
update pod
|
563 |
); |
564 | ||
update pod
|
565 |
=head3 Individual filter C<filter> |
update pod
|
566 | |
update pod
|
567 |
You can apply individual filter . |
568 |
This filter overwrite the filter by C<apply_filter()> |
|
update pod
|
569 | |
update pod
|
570 |
use C<filter> option to apply individual filter |
571 |
when data is send to database. |
|
572 |
This option is used at C<insert()>, C<update()>, |
|
573 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
574 |
C<execute()>. |
|
update pod
|
575 | |
update pod
|
576 |
C<insert()> example: |
update pod
|
577 | |
578 |
$dbi->insert( |
|
579 |
table => 'book', |
|
580 |
param => {issue_date => $tp, first_issue_date => $tp}, |
|
581 |
filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'} |
|
582 |
); |
|
added DBIx::Custom::Guides
|
583 | |
update pod
|
584 |
C<execute()> example: |
update pod
|
585 | |
586 |
my $sql = <<"EOS"; |
|
587 |
select YEAR(issue_date) as issue_year |
|
588 |
from book |
|
589 |
where YEAR(issue_date) = {? issue_year} |
|
590 |
EOS |
|
591 |
|
|
added DBIx::Custom::Guides
|
592 |
my $result = $dbi->execute( |
update pod
|
593 |
$sql, |
594 |
param => {issue_year => '2010'}, |
|
595 |
filter => {issue_year => 'tp_to_year'} |
|
added DBIx::Custom::Guides
|
596 |
); |
597 | ||
update pod
|
598 |
You can also apply indivisual filter when you fetch row. |
599 |
use C<DBIx::Custom::Result>'s C<filter()>. |
|
added DBIx::Custom::Guides
|
600 | |
update pod
|
601 |
$result->filter(issue_year => 'year_to_tp'); |
added DBIx::Custom::Guides
|
602 | |
update pod
|
603 |
=head3 End filtering : C<end_filter()> |
update pod
|
604 | |
update pod
|
605 |
You can add filter at end. |
606 |
It is useful to create last output. |
|
607 |
use C<end_filter()> to add end filter. |
|
update pod
|
608 | |
609 |
$result->end_filter(issue_date => sub { |
|
610 |
my $tp = shift; |
|
611 |
|
|
612 |
return '' unless $tp; |
|
613 |
return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)'); |
|
614 |
}); |
|
615 | ||
update pod
|
616 |
In this example, L<Time::Piece> object is converted to readable format. |
update pod
|
617 | |
update pod
|
618 |
=head3 Automate applying filter C<each_column()> |
update pod
|
619 | |
update pod
|
620 |
It is useful to apply filter automatically at date type columns. |
621 |
You can use C<each_column()> to process all column infos. |
|
update pod
|
622 | |
623 |
$dbi->each_column( |
|
624 |
sub { |
|
625 |
my ($self, $table, $column, $info) = @_; |
|
626 |
|
|
627 |
my $type = $info->{TYPE_NAME}; |
|
628 |
|
|
629 |
my $filter = $type eq 'DATE' ? {out => 'tp_to_date', in => 'date_to_tp'} |
|
630 |
: $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'} |
|
631 |
: undef; |
|
632 |
|
|
633 |
$self->apply_filter($table, $column, $filter) |
|
634 |
if $filter; |
|
635 |
} |
|
636 |
); |
|
637 | ||
update pod
|
638 |
C<each_column() receive callback. |
639 |
callback arguments are L<DBIx::Custom> object, table name, column name, column information. |
|
640 |
Filter is applied automatically by column type. |
|
update pod
|
641 | |
update pod
|
642 |
=head2 5. Tag |
update pod
|
643 | |
update pod
|
644 |
=head3 Basic of Tag |
update pod
|
645 | |
update pod
|
646 |
You can embedd tag into SQL. |
update pod
|
647 | |
648 |
select * from book where {= title} and {like author}; |
|
649 | ||
update pod
|
650 |
{= title} and {like author} are tag. Tag has the folloring format. |
update pod
|
651 | |
update pod
|
652 |
{TAG_NAME ARG1 ARG2 ...} |
update pod
|
653 | |
update pod
|
654 |
Tag start C<{> and end C<}>. |
655 |
Don't insert space between C<{} and tag name. |
|
656 | ||
657 |
C<{> and C<}> are reserved word. |
|
658 |
If you want to use these, escape it by '\'; |
|
update pod
|
659 | |
660 |
select from book \\{ ... \\} |
|
661 | ||
update pod
|
662 |
\ is perl's escape character, you need two \. |
663 | ||
update pod
|
664 |
C<\>���̂�Perl�̃G�X�P�[�v�����ł��̂ŁA |
update pod
|
665 |
C<\>�͓�K�v�ɂȂ�܂��B |
update pod
|
666 | |
update pod
|
667 |
Tag is expanded before executing SQL. |
update pod
|
668 | |
669 |
select * from book where title = ? and author like ?; |
|
670 | ||
update pod
|
671 |
use C<execute()> to execute SQL which contains tag |
update pod
|
672 | |
673 |
my $sql = "select * from book where {= author} and {like title};" |
|
674 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}); |
|
675 | ||
update pod
|
676 |
You can specify values embedded into place holder as hash reference using |
677 |
C<param> option. |
|
update pod
|
678 | |
update pod
|
679 |
You can specify C<filter()> at C<execute()>. |
update pod
|
680 | |
681 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
682 |
filter => {title => 'to_something'); |
|
683 | ||
update pod
|
684 |
Note that at C<execute()> the filter applied by C<apply_filter()> |
685 |
don't has effective to columns. |
|
686 |
You need specify C<table> to have effective. |
|
update pod
|
687 | |
688 |
$dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'} |
|
689 |
table => ['book']); |
|
690 | ||
update pod
|
691 |
=head3 Tag list |
update pod
|
692 | |
693 |
L<DBIx::Custom>�ł͎��̃^�O���g�p�\�ł��B |
|
694 | ||
update pod
|
695 |
The following tag is available. |
update pod
|
696 | |
update pod
|
697 |
=head4 C<?> |
update pod
|
698 | |
699 |
{? NAME} -> ? |
|
700 | ||
update pod
|
701 |
=head4 C<=> |
update pod
|
702 | |
703 |
{= NAME} -> NAME = ? |
|
704 | ||
update pod
|
705 |
=head4 C<E<lt>E<gt>> |
update pod
|
706 | |
707 |
{<> NAME} -> NAME <> ? |
|
708 | ||
update pod
|
709 |
=head4 C<E<lt>> |
update pod
|
710 | |
711 |
{< NAME} -> NAME < ? |
|
712 | ||
update pod
|
713 |
=head4 C<E<gt>> |
update pod
|
714 | |
715 |
{> NAME} -> NAME > ? |
|
716 | ||
update pod
|
717 |
=head4 C<E<gt>=> |
update pod
|
718 | |
719 |
{>= NAME} -> NAME >= ? |
|
720 | ||
update pod
|
721 |
=head4 C<E<lt>=> |
update pod
|
722 | |
723 |
{<= NAME} -> NAME <= ? |
|
724 | ||
update pod
|
725 |
=head4 C<like> |
update pod
|
726 | |
727 |
{like NAME} -> NAME like ? |
|
728 | ||
update pod
|
729 |
=head4 C<in> |
update pod
|
730 | |
731 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
732 | ||
update pod
|
733 |
=head4 C<insert_param> |
update pod
|
734 | |
added DBIx::Custom::Guides
|
735 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
update pod
|
736 | |
update pod
|
737 |
=head4 C<update_param> |
update pod
|
738 | |
added DBIx::Custom::Guides
|
739 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
740 | ||
update pod
|
741 |
=head3 Manipulate same name's columns |
added DBIx::Custom::Guides
|
742 | |
update pod
|
743 |
It is ok if there are same name's columns. |
744 |
Let's think two date comparison. |
|
added DBIx::Custom::Guides
|
745 | |
update pod
|
746 |
my $sql = "select * from table where {> date} and {< date};"; |
added DBIx::Custom::Guides
|
747 | |
update pod
|
748 |
In this case, You specify paramter values as array reference. |
added DBIx::Custom::Guides
|
749 | |
update pod
|
750 |
my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']}); |
added DBIx::Custom::Guides
|
751 | |
update pod
|
752 |
=head3 Register Tag C<register_tag()> |
update pod
|
753 | |
update pod
|
754 |
You can register your tag. |
755 |
use C<register_tag()> to register tag. |
|
update pod
|
756 | |
757 |
$dbi->register_tag( |
|
758 |
'=' => sub { |
|
759 |
my $column = shift; |
|
760 |
|
|
761 |
return ["$column = ?", [$column]]; |
|
added DBIx::Custom::Guides
|
762 |
} |
763 |
); |
|
764 | ||
update pod
|
765 |
�����ł̓f�t�H���g��C<=>�^�O���ǂ̂悤�Ɏ������Ă��邩����Ă��܂��B |
766 |
�^�O��o�^������̈�̓^�O�̒��ɏ����ꂽ��ɂȂ�܂��B |
|
added experimental DBIx::Cus...
|
767 | |
update pod
|
768 |
{�^�O�� ��1 ��2} |
added experimental DBIx::Cus...
|
769 | |
update pod
|
770 |
C<=>�^�O�̏ꍇ�� |
cleanup
|
771 | |
update pod
|
772 |
{= title} |
added experimental DBIx::Cus...
|
773 | |
update pod
|
774 |
�Ƃ����`���ł�����A�T�u���[�`���ɂ�title�Ƃ����ЂƂ̗��킽��Ă��܂��B |
added experimental DBIx::Cus...
|
775 | |
update pod
|
776 |
�T�u���[�`���̖߂�l�ɂ͎��̌`���̔z��̃��t�@�����X��Ԃ��K�v������܂��B |
777 | ||
778 |
[ |
|
779 |
�W�J��̕�����, |
|
780 |
[�v���[�X�z���_�ɖ��ߍ��݂ɗ��p�����1, ��2, ...] |
|
781 |
] |
|
782 | ||
783 |
��ڂ̗v�f�͓W�J��̕�����ł��B���̗�ł� |
|
784 | ||
785 |
'title = ?' |
|
786 | ||
787 |
��Ԃ��K�v������܂��B |
|
788 | ||
789 |
��ڂ̗v�f�̓v���[�X�z���_�ɖ��ߍ��݂ɗ��p�����܂ޔz��� |
|
790 |
���t�@�����X�ł��B����̗�ł� |
|
791 | ||
792 |
['title'] |
|
793 | ||
794 |
��Ԃ��K�v������܂��B�����̃v���[�X�z���_��܂ޏꍇ�́A���̕����� |
|
795 |
�����ɂȂ�܂��BC<insert_param>�^�O��C<update_param>�^�O�� |
|
796 |
���̕�������ە����ɂȂ�Ă��܂��B |
|
797 | ||
798 |
��L��킹��� |
|
799 | ||
800 |
['title = ?', ['title']] |
|
added experimental DBIx::Cus...
|
801 |
|
update pod
|
802 |
��Ԃ��K�v������Ƃ������Ƃł��B |
added DBIx::Custom::Guides
|
803 | |
update pod
|
804 |
�^�O�̎���̑��̃T���v����L<DBIx::Custom::Tag>�̃\�[�X�R�[�h |
805 |
����ɂȂ�Ă݂Ă��������B |
|
added DBIx::Custom::Guides
|
806 | |
update pod
|
807 |
=head2 6. Where��̓��I�Ȑ��� |
808 | ||
809 |
=head3 Where��̓��I�Ȑ��� where() |
|
810 | ||
811 |
�����̌�����w�肵�āA�����s�������ꍇ������܂��B |
|
812 |
����3�̃P�[�X��where���l���Ă݂܂��傤�B |
|
813 |
���L�̂悤��where�傪�K�v�ɂȂ�܂��B |
|
814 | ||
815 |
title�̒l�����Ō�������ꍇ |
|
816 | ||
817 |
where {= title} |
|
818 | ||
819 |
author�̒l�����Ō�������ꍇ |
|
820 | ||
821 |
where {= author} |
|
822 | ||
823 |
title��author�̗���̒l�Ō�������ꍇ |
|
824 | ||
825 |
where {= title} and {=author} |
|
826 | ||
827 |
L<DBIx::Custom>�ł͓��I��Where��̐�����T�|�[�g���Ă��܂��B |
|
828 |
�܂�C<where()>��L<DBIx::Custom::Where>�I�u�W�F�N�g�����܂��B |
|
829 | ||
830 |
my $where = $dbi->where; |
|
831 | ||
832 |
����C<clause()>��g�p����where���L�q���܂��B |
|
833 | ||
834 |
$where->clause( |
|
835 |
['and', '{= title'}, '{= author}'] |
|
added DBIx::Custom::Guides
|
836 |
); |
837 | ||
update pod
|
838 |
clause�̎w���@�͎��̂悤�ɂȂ�܂��B |
839 | ||
840 |
['or' ���邢�� 'and', �^�O1, �^�O2, �^�O3] |
|
added DBIx::Custom::Guides
|
841 | |
update pod
|
842 |
����ɂ�or���邢��and��w�肵�܂��B����ȍ~�ɂ� |
843 |
������^�O��g��ċL�q���܂��B |
|
844 | ||
845 |
C<clause>�̎w��͓��q�ɂ��邱�Ƃ�ł��A����ɕ��G�ȏ� |
|
846 |
��L�q���邱�Ƃ�ł��܂��B |
|
847 | ||
848 |
['and', |
|
849 |
'{= title}', |
|
850 |
['or', '{= author}', '{like date}'] |
|
851 |
] |
|
852 | ||
853 |
���̂悤��C<clause>��ݒ肵�����C<param>�Ƀp�����[�^��w�肵�܂��B |
|
added DBIx::Custom::Guides
|
854 |
|
update pod
|
855 |
my $param => {title => 'Perl'}; |
856 |
$where->param($param); |
|
added DBIx::Custom::Guides
|
857 | |
update pod
|
858 |
���̗�ł�title�������p�����[�^�Ɋ܂܂�Ă��܂��B |
added DBIx::Custom::Guides
|
859 | |
update pod
|
860 |
���̌�C<to_string()>���s�����$param�Ɋ܂܂��p�����[�^���� |
861 |
where������邱�Ƃ��ł��܂��B |
|
862 | ||
863 |
my $where_clause = $where->to_string; |
|
864 | ||
865 |
�p�����[�^��title�����ł��̂ŁA���̂悤��where�傪��������܂��B |
|
866 | ||
867 |
where {= title} |
|
868 | ||
869 |
�܂�L<DBIx::Custom>�͕�����̕]����I�[�o�[���[�h���āAC<to_string()> |
|
870 |
��Ăяo���悤�ɂ��Ă��܂��̂ŁA���̂悤�ɂ���where������邱�Ƃ� |
|
871 |
�ł��܂��B |
|
872 | ||
873 |
my $where_clause = "$where"; |
|
874 | ||
875 |
�����SQL�̒���where��ߍ��ނƂ��ɂƂĂ�𗧂@�\�ł��B |
|
added DBIx::Custom::Guides
|
876 | |
update pod
|
877 |
=head3 ����̗�܂ޏꍇ |
check arguments of connect m...
|
878 | |
update pod
|
879 |
�^�O�̒��ɓ���̖��O���̂����݂����ꍇ�ł��I�� |
880 |
where���쐬���邱�Ƃ��ł��܂��B |
|
added experimental DBIx::Cus...
|
881 | |
update pod
|
882 |
���Ƃ��A�p�����[�^�Ƃ��ĊJ�n��t�ƏI����t��������Ƃ� |
883 |
�l���Ă݂Ă��������B |
|
added experimental DBIx::Cus...
|
884 | |
update pod
|
885 |
my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
added experimental DBIx::Cus...
|
886 | |
update pod
|
887 |
�܂��J�n��t�ƏI����t�̕Е���A�ǂ������Ȃ��ꍇ���邩����܂���B |
added experimental DBIx::Cus...
|
888 | |
update pod
|
889 |
���̏ꍇ�͎��̂悤�ȃp�����[�^�ɕϊ����邱�ƂőΉ����邱�Ƃ��ł��܂��B |
890 | ||
891 |
my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
892 | ||
893 |
�l���z��̃��t�@�����X�ɂȂ�Ă��邱�Ƃɒ��ڂ��Ă��������B���̂悤�ɂ���� |
|
894 |
�����̗��܂ރ^�O�ɏ��Ԃɖ��ߍ��ނ��Ƃ��ł��܂��B |
|
895 | ||
896 |
$where->clause( |
|
897 |
['and', '{> date}', '{< date}'] |
|
added experimental DBIx::Cus...
|
898 |
); |
update pod
|
899 |
$where->param($p); |
900 | ||
901 |
�܂��J�n��t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
902 | ||
903 |
my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
904 | ||
905 |
L<DBIx::Custom>��C<not_exists>��DBIx::Custom::NotExists�I�u�W�F�N�g�� |
|
906 |
�擾�ł��܂��B����͑Ή�����l�����݂��Ȃ����Ƃ�����߂̂�̂ł��B |
|
907 | ||
908 |
�܂��I����t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
909 | ||
910 |
my $p = {date => ['2010-11-15']}; |
|
911 | ||
912 |
�ǂ�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B |
|
913 | ||
914 |
my $p = {date => []}; |
|
915 | ||
916 |
��������̂ň�ԊȒP�ɍ쐬�ł��郍�W�b�N����Ă����܂��B |
|
917 | ||
918 |
my @date; |
|
919 |
push @date, exists $param->{start_date} ? $param->{start_date} |
|
920 |
: $dbi->not_exists; |
|
921 |
push @date, $param->{end_date} if exists $param->{end_date}; |
|
922 |
my $p = {date => \@date}; |
|
923 | ||
924 |
=head3 C<select()>�Ƃ̘A�g |
|
925 | ||
926 |
L<DBIx::Custom::Where>�I�u�W�F�N�g�� |
|
927 |
C<select()>��C<where>�ɒ��ړn�����Ƃ� |
|
928 |
�ł��܂��B |
|
929 |
|
|
930 |
my $where = $dbi->where; |
|
931 |
$where->clause(...); |
|
932 |
$where->param($param); |
|
933 |
my $result = $dbi->select(table => 'book', where => $where); |
|
934 | ||
935 |
���邢��C<update()>�AC<delete()>��where�Ɏw�肷�邱�Ƃ�\�ł��B |
|
936 | ||
937 |
=head3 C<execute()>�Ƃ̘A�g |
|
938 | ||
939 |
C<execute()>�Ƃ̘A�g�ł��BSQL��쐬����Ƃ��ɖ��ߍ��ނ��Ƃ��ł��܂��B |
|
added experimental DBIx::Cus...
|
940 | |
941 | ||
update pod
|
942 |
my $where = $dbi->where; |
943 |
$where->clause(...); |
|
944 |
$where->param($param); |
|
945 | ||
946 |
my $sql = <<"EOS" |
|
947 |
select * from book; |
|
948 |
$where |
|
949 |
EOS |
|
950 | ||
951 |
$dbi->execute($sql, param => $param); |
|
952 | ||
953 |
=head2 7. �e�[�u�����f�� |
|
954 | ||
955 |
=head3 �e�[�u���I�u�W�F�N�g�̍쐬 C<table()> |
|
956 | ||
957 |
L<DBIx::Custom>�̓��W�b�N�Ƃ��ăe�[�u���𒆐S�ɂ����� |
|
958 |
���f���̍쐬��x�����܂��B |
|
959 | ||
960 |
�A�v���P�[�V�����̃��W�b�N��L�q����Ƃ��ɁA���̃��W�b�N�� |
|
961 |
�f�[�^�x�[�X�̃e�[�u���ɂ����邱�Ƃ́ARDBMS�𗘗p���� |
|
962 |
���f���ł���A�R�[�h�̏d�����Ȃ� |
|
963 |
�킩��₷����̂ɂȂ�܂��B |
|
964 | ||
965 |
�e�[�u���I�u�W�F�N�g������ɂ�C<table()>��g�p���܂��B |
|
966 | ||
967 |
my $table = $dbi->table('book'); |
|
968 | ||
969 |
��ۂɃf�[�^�x�[�X�Ƀe�[�u���͑��݂��Ă���K�v�͂���܂���B |
|
970 |
����͉��z�I�ȃe�[�u���I�u�W�F�N�g�ł��B����� |
|
971 |
L<DBIx::Customm::Table>�I�u�W�F�N�g�ɂȂ�܂��B |
|
972 | ||
973 |
�e�[�u���I�u�W�F�N�g�����C<insert()>�AC<update()>�AC<update_all()>�A |
|
974 |
C<delete()>�AC<delete_all()>�AC<select()>�Ȃǂ̃��\�b�h�Ăяo�����Ƃ��ł��܂��B |
|
975 |
L<DBIx::Custom>�ƈقȂ�Ƃ���́AC<table>��K������w�肷��K�v�� |
|
976 |
�Ȃ��Ƃ������Ƃł��B |
|
977 | ||
978 |
$table->insert(param => $param); |
|
979 | ||
980 |
C<table���̒l�͎����I��book�ɐݒ肳��܂��B |
|
981 | ||
982 |
�܂��e�[�u���I�u�W�F�N�g�ɂ͓Ǝ��̃��\�b�h��lj���邱�Ƃ��ł��܂��B |
|
983 | ||
984 |
$table->method( |
|
985 |
register => sub { |
|
added experimental DBIx::Cus...
|
986 |
my $self = shift; |
update pod
|
987 |
my $table_name = $self->name; |
988 |
# something |
|
added experimental DBIx::Cus...
|
989 |
}, |
update pod
|
990 |
list => sub { |
991 |
my $self = shift; |
|
992 |
my $table_name = $self->name; |
|
993 |
# something |
|
994 |
} |
|
995 |
); |
|
996 | ||
997 |
���\�b�h�ɓn���������L<DBIx::Custom::Table>�I�u�W�F�N�g�ł��B |
|
998 |
C<name()>��g�p���āA�e�[�u������擾���邱�Ƃ��ł��܂��B |
|
999 | ||
1000 |
���̂悤�ɂ��ēo�^�������\�b�h�͒��ڌĂяo�����Ƃ��ł��܂��B |
|
1001 | ||
1002 |
$table->register(...); |
|
1003 |
$table->list(...); |
|
1004 | ||
1005 |
�܂��e�[�u����p�̃��\�b�h��I�[�o�[���C�h���č쐬���邱�Ƃ�ł��܂��B |
|
1006 | ||
1007 |
$table->method( |
|
1008 |
insert => sub { |
|
added experimental DBIx::Cus...
|
1009 |
my $self = shift; |
1010 |
|
|
update pod
|
1011 |
$self->base_insert(...); |
1012 |
|
|
1013 |
# something |
|
added experimental DBIx::Cus...
|
1014 |
} |
1015 |
); |
|
1016 | ||
update pod
|
1017 |
��Ƃ�Ƒ��݂��Ă���C<insert()>��ĂԂɂ�C<base_$method>�Ƃ��܂��BL<DBIx::Custom::Table> |
1018 |
�̃I�[�o�[���C�h�̋@�\�͊ȈՓI�Ȃ�̂ł����A�ƂĂ�֗��ł��B |
|
added experimental DBIx::Cus...
|
1019 | |
update pod
|
1020 |
=head2 �e�[�u���ŋ��L�̃��\�b�h�̓o�^ |
added experimental DBIx::Cus...
|
1021 | |
update pod
|
1022 |
���ׂẴe�[�u���Ń��\�b�h��L����ɂ�C<table>���\�b�h�Ńe�[�u����쐬����O�ɁA |
1023 |
C<base_table>�Ƀ��\�b�h��o�^���Ă����܂��B |
|
added experimental DBIx::Cus...
|
1024 | |
update pod
|
1025 |
$dbi->base_table->method( |
1026 |
count => sub { |
|
1027 |
my $self = shift; |
|
1028 |
return $self->select(column => ['count(*)']); |
|
1029 |
} |
|
1030 |
); |
|
added DBIx::Custom::Guides
|
1031 | |
update pod
|
1032 |
�܂��e�[�u�������L<DBIx::Custom>��L<DBI>�̂��ׂẴ��\�b�h��Ăяo�����Ƃ��ł��܂��B |
added DBIx::Custom::Guides
|
1033 | |
update pod
|
1034 |
# DBIx::Custom method |
1035 |
$table->execute($sql); |
|
1036 |
|
|
1037 |
# DBI method |
|
1038 |
$table->begin_work; |
|
1039 |
$table->commit; |
|
1040 | ||
1041 |
=head2 ��ʓI�ȃ��f���̍\�� |
|
added DBIx::Custom::Guides
|
1042 | |
update pod
|
1043 |
��ʓI�ɂ́AL<DBIx::Custom>��p�����ăR���X�g���N�^�̒��ɁA���f����쐬 |
1044 |
����̂��悢�ł��傤�B |
|
added DBIx::Custom::Guides
|
1045 | |
update pod
|
1046 |
package MyDBI; |
1047 |
|
|
1048 |
use base 'DBIx::Custom'; |
|
added DBIx::Custom::Guides
|
1049 |
|
update pod
|
1050 |
sub connect { |
1051 |
my $self = shift->SUPER::connect(@_); |
|
1052 |
|
|
1053 |
$self->base_table->method( |
|
1054 |
... => sub { ... } |
|
1055 |
); |
|
1056 |
|
|
1057 |
$self->table('book')->method( |
|
1058 |
insert_multi => sub { ... }, |
|
1059 |
... => sub { ... } |
|
1060 |
); |
|
1061 |
|
|
1062 |
$self->table('company')->method( |
|
1063 |
... => sub { ... }, |
|
1064 |
); |
|
1065 |
} |
|
1066 | ||
1067 |
���̂悤�ɂ��Ē�`���Ă����A���̂悤�ɗ��p���邱�Ƃ��ł��܂��B |
|
1068 | ||
1069 |
my $dbi = MyDBI->connect(...); |
|
1070 |
$dbi->table('book')->insert_multi(...); |
|
1071 | ||
1072 |
=head2 8. �p�t�H�[�}���X�̉�P |
|
1073 | ||
1074 |
=head3 �N�G���̍쐬 |
|
1075 | ||
1076 |
��C<insert()>���\�b�h��g�p���ăC���T�[�g���s�����ꍇ�A |
|
1077 |
�K�v�ȃp�t�H�[�}���X���Ȃ��ꍇ�����邩����܂���B |
|
1078 |
C<insert()>���\�b�h�́ASQL���ƃX�e�[�g�����g�n���h���� |
|
1079 |
����쐬���邽�߂ł��B |
|
1080 | ||
1081 |
���̂悤�ȏꍇ�́AC<query>�I�v�V������w�肷�邱�ƂŁA |
|
1082 |
�N�G����擾���邱�Ƃ��ł��܂��B |
|
1083 | ||
1084 |
my $query = $dbi->insert(table => 'book', param => $param, query => 1); |
|
1085 | ||
1086 |
�܂�C<create_query()>���\�b�h��g��ĔC�ӂ�SQL�̃N�G����쐬 |
|
1087 |
���邱�Ƃ�ł��܂��B |
|
1088 | ||
added DBIx::Custom::Guides
|
1089 |
my $query = $dbi->create_query( |
update pod
|
1090 |
"insert into book {insert_param title author};"; |
added DBIx::Custom::Guides
|
1091 |
); |
1092 | ||
update pod
|
1093 |
�߂�l��L<DBIx::Custom::Query>�I�u�W�F�N�g�ł��B |
1094 |
���̃I�u�W�F�N�g��SQL���ƃp�����[�^�o�C���h���̗� |
|
1095 |
�ێ����Ă��܂��B�܂��X�e�[�g�����g�n���h����ێ����Ă��܂��B |
|
added DBIx::Custom::Guides
|
1096 | |
1097 |
{ |
|
remove DBIx::Custom::Model
|
1098 |
sql => 'insert into book (title, author) values (?, ?);', |
update pod
|
1099 |
columns => ['title', 'author'], |
1100 |
sth => $sth |
|
added DBIx::Custom::Guides
|
1101 |
} |
1102 | ||
update pod
|
1103 |
�N�G���I�u�W�F�N�g��g��ČJ��Ԃ���s����ɂ�C<execute()>��g�p���܂��B |
added DBIx::Custom::Guides
|
1104 |
|
remove DBIx::Custom::Model
|
1105 |
my $params = [ |
added DBIx::Custom::Guides
|
1106 |
{title => 'Perl', author => 'Ken'}, |
1107 |
{title => 'Good days', author => 'Mike'} |
|
1108 |
]; |
|
1109 |
|
|
update pod
|
1110 |
foreach my $param (@$paramss) { |
1111 |
$dbi->execute($query, table => 'book', param => $input); |
|
added DBIx::Custom::Guides
|
1112 |
} |
1113 | ||
update pod
|
1114 |
C<execute>���\�b�h�̑���ɃN�G���I�u�W�F�g��n�����Ƃ��ł��܂��B |
1115 |
C<insert()>���\�b�h�������ł��B |
|
added DBIx::Custom::Guides
|
1116 | |
update pod
|
1117 |
���ӓ_������������܂��B����̓p�����[�^�̐��͕K�������łȂ��Ă͂Ȃ�Ȃ� |
1118 |
�Ƃ������Ƃł��B�ŏ���3�̃p�����[�^������n�����̂ɁA���̎�s�ł� |
|
1119 |
��̃p�����[�^��n���Ɨ\��Ȃ����ʂɂȂ�܂��B����� |
|
1120 |
���I�ɐ������ꂽSQL�Ɋ܂܂��v���[�X�z���_�̐����قȂ邩��ł��B |
|
1121 |
�܂�C<execute()>�ɂ��Ă͎����I�ɂ̓t�B���^���L��ɂȂ�Ȃ��̂ŁA |
|
1122 |
C<table>��w�肷��K�v�̂��邱�Ƃɒ��ӂ��Ă��������B |
|
1123 |
�{���ɕK�v�ȏꍇ�������p���Ă��������B |
|
added DBIx::Custom::Guides
|
1124 | |
update pod
|
1125 |
=head2 9. ���̑��̋@�\ |
added DBIx::Custom::Guides
|
1126 | |
update pod
|
1127 |
=head3 ���\�b�h�̓o�^ |
added DBIx::Custom::Guides
|
1128 | |
update pod
|
1129 |
���\�b�h��o�^����ɂ�C<method()>��g�p���܂��B |
added DBIx::Custom::Guides
|
1130 | |
update pod
|
1131 |
$dbi->method( |
1132 |
update_or_insert => sub { |
|
1133 |
my $self = shift; |
|
1134 |
# something |
|
1135 |
}, |
|
1136 |
find_or_create => sub { |
|
1137 |
my $self = shift; |
|
1138 |
# something |
|
1139 |
} |
|
1140 |
); |
|
1141 | ||
1142 |
<method()>�œo�^�������\�b�h�� |
|
1143 |
L<DBIx::Custom>�I�u�W�F�N�g���璼�ڌĂяo�����Ƃ��ł��܂��B |
|
1144 | ||
1145 |
$dbi->update_or_insert; |
|
1146 |
$dbi->find_or_create; |
|
1147 | ||
1148 |
=head3 ���ʃN���X�̕ύX |
|
added DBIx::Custom::Guides
|
1149 | |
update pod
|
1150 |
�K�v�Ȃ�Ό��ʃN���X��ύX���邱�Ƃ��ł��܂��B |
added DBIx::Custom::Guides
|
1151 | |
update pod
|
1152 |
package MyResult; |
added DBIx::Custom::Guides
|
1153 |
use base 'DBIx::Custom::Result'; |
1154 |
|
|
1155 |
sub some_method { ... } |
|
1156 | ||
1157 |
1; |
|
1158 |
|
|
1159 |
package main; |
|
1160 |
|
|
update pod
|
1161 |
use MyResult; |
added DBIx::Custom::Guides
|
1162 |
|
1163 |
my $dbi = DBIx::Custom->connect(...); |
|
update pod
|
1164 |
$dbi->result_class('MyResult'); |
added DBIx::Custom::Guides
|
1165 | |
update pod
|
1166 |
=head3 �L���b�V���O |
added DBIx::Custom::Guides
|
1167 | |
update pod
|
1168 |
�^�O�̓W�J���SQL�̓p�t�H�[�}���X�̗��R�̂��߂ɃL���b�V������܂��B |
1169 |
�����C<chace>�Őݒ�ł��A�f�t�H���g�ł̓L���b�V����s���ݒ�ł��B |
|
added DBIx::Custom::Guides
|
1170 | |
update pod
|
1171 |
$dbi->cache(1); |
added DBIx::Custom::Guides
|
1172 | |
update pod
|
1173 |
�L���b�V����@��C<cache_method>�Ƀ��\�b�h��w�肷�邱�Ƃ� |
1174 |
�ύX���邱�Ƃ��ł��܂��B |
|
1175 |
�f�[�^�̕ۑ��Ǝ擾�̂��߂̃��\�b�h���`���܂��B |
|
added DBIx::Custom::Guides
|
1176 | |
update pod
|
1177 |
�f�t�H���g�ł͎��̂悤�Ƀ�������ɃL���b�V����s����̂ɂȂ�Ă��܂��B |
1178 | ||
1179 |
$dbi->cache_method(sub { |
|
1180 |
sub { |
|
added DBIx::Custom::Guides
|
1181 |
my $self = shift; |
update pod
|
1182 |
|
1183 |
$self->{_cached} ||= {}; |
|
1184 |
|
|
1185 |
if (@_ > 1) { |
|
1186 |
# Set |
|
1187 |
$self->{_cached}{$_[0]} = $_[1] |
|
1188 |
} |
|
1189 |
else { |
|
1190 |
# Get |
|
1191 |
return $self->{_cached}{$_[0]} |
|
1192 |
} |
|
added DBIx::Custom::Guides
|
1193 |
} |
update pod
|
1194 |
}); |
1195 |
|
|
1196 |
����L<DBIx::Custom>�I�u�W�F�N�g�ł��B |
|
1197 |
����̓^�O�̓W�J�����O��SQL�ł��B |
|
1198 |
��O��̓^�O�̓W�J���SQL�ł��B |
|
added DBIx::Custom::Guides
|
1199 | |
update pod
|
1200 |
�����ō쐬����ꍇ�͑�O�����݂����ꍇ�̓L���b�V����ݒ肵�A |
1201 |
���݂��Ȃ�����ꍇ�̓L���b�V����擾�������� |
|
1202 |
�����������B |
|
added DBIx::Custom::Guides
|
1203 | |
update pod
|
1204 |
=cut |
added DBIx::Custom::Guides
|
1205 | |
pod fix
|
1206 |
=head1 EXAMPLES |
1207 | ||
1208 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
add examples
|
1209 | |
added DBIx::Custom::Guides
|
1210 |
=cut |