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 | |
updatedd pod
|
7 |
=head1 FEATURES |
deprecated DBIx::Custom::MyS...
|
8 | |
updatedd pod
|
9 |
L<DBIx::Custom> is the wrapper class of L<DBI> to execute SQL easily. |
10 |
This module have the following features. |
|
update pod
|
11 | |
12 |
=over 4 |
|
13 | ||
updatedd pod
|
14 |
=item * Execute INSERT, UPDATE, DELETE, SELECT statement easily |
update pod
|
15 | |
updatedd pod
|
16 |
=item * You can specify bind values by hash reference |
update pod
|
17 | |
updatedd pod
|
18 |
=item * Filtering by data type. and you can set filter to any column |
update pod
|
19 | |
updatedd pod
|
20 |
=item * Creating where clause flexibly |
update pod
|
21 | |
updatedd pod
|
22 |
=imte * Support model |
update pod
|
23 | |
24 |
=back |
|
25 | ||
updatedd pod
|
26 |
=head1 GUIDE |
update pod
|
27 | |
updatedd pod
|
28 |
=head2 Connect To Database |
update pod
|
29 | |
30 |
use DBIx::Custom; |
|
31 |
my $dbi = DBIx::Custom->connect( |
|
- update_param_tag is DEPREC...
|
32 |
dsn => "dbi:mysql:database=bookshop", |
update pod
|
33 |
user => 'ken', |
34 |
password => '!LFKD%$&', |
|
updatedd pod
|
35 |
dbi_option => {mysql_enable_utf8 => 1} |
update pod
|
36 |
); |
37 | ||
updatedd pod
|
38 |
You can connect to database by C<connect> method. |
39 |
C<dsn> is data source name, C<user> is user name, C<password> is password. |
|
deprecated DBIx::Custom::MyS...
|
40 | |
updatedd pod
|
41 |
C<dbi_option> is L<DBI> option. |
42 |
By default, the following option is set. |
|
43 |
Fatal error throw exeption and commit mode is auto commit. |
|
deprecated DBIx::Custom::MyS...
|
44 | |
updatedd pod
|
45 |
{ |
46 |
RaiseError => 1 |
|
47 |
PrintError => 0 |
|
48 |
AutoCommit => 1 |
|
49 |
} |
|
added DBIx::Custom::Guides
|
50 | |
updatedd pod
|
51 |
=head2 Execute Query |
added DBIx::Custom::Guides
|
52 | |
updatedd pod
|
53 |
=head3 Insert Statement : C<insert> |
added DBIx::Custom::Guides
|
54 | |
updatedd pod
|
55 |
If you want to execute insert statement, use C<insert> method. |
added DBIx::Custom::Guides
|
56 | |
updatedd pod
|
57 |
$dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
added DBIx::Custom::Guides
|
58 | |
updatedd pod
|
59 |
First argument is insert row data, C<table> is table name. |
added DBIx::Custom::Guides
|
60 | |
updatedd pod
|
61 |
=head3 Update Statement : C<update> |
added DBIx::Custom::Guides
|
62 | |
updatedd pod
|
63 |
If you want to execute update stateimuse, use C<update> method. |
added DBIx::Custom::Guides
|
64 | |
updatedd pod
|
65 |
$dbi->update( |
66 |
{title => 'Perl', author => 'Ken'}, |
|
67 |
table => 'book', |
|
68 |
where => {id => 5} |
|
69 |
); |
|
added DBIx::Custom::Guides
|
70 | |
updatedd pod
|
71 |
First argument is update row data, C<table> is table name, C<where> is condition. |
added DBIx::Custom::Guides
|
72 | |
updatedd pod
|
73 |
Note that you can't execute C<update> method without C<where>. |
74 |
If you want to update all rows, use update_all. |
|
added DBIx::Custom::Guides
|
75 | |
updatedd pod
|
76 |
$dbi->update_all({title => 'Perl', author => 'Ken'}, table => 'book'); |
added DBIx::Custom::Guides
|
77 | |
updatedd pod
|
78 |
=head3 Delete Statement : C<delete> |
added DBIx::Custom::Guides
|
79 | |
updatedd pod
|
80 |
If you want to execute delete statement, use C<delete> method. |
added DBIx::Custom::Guides
|
81 | |
updatedd pod
|
82 |
$dbi->delete(table => 'book', where => {author => 'Ken'}); |
added DBIx::Custom::Guides
|
83 | |
update pod
|
84 |
C<table> is table name, C<where> is condition. |
added DBIx::Custom::Guides
|
85 | |
updatedd pod
|
86 |
Note that you can't execute C<delete> method without C<where>. |
87 |
If you want to delete all rows, use C<delete_all> method. |
|
added DBIx::Custom::Guides
|
88 | |
update pod
|
89 |
$dbi->delete_all(table => 'book'); |
added DBIx::Custom::Guides
|
90 | |
updatedd pod
|
91 |
=head3 Select Statement : C<select> |
added DBIx::Custom::Guides
|
92 | |
updatedd pod
|
93 |
If you want to execute select statement, use C<select> method. |
added DBIx::Custom::Guides
|
94 | |
remove DBIx::Custom::Model
|
95 |
my $result = $dbi->select(table => 'book'); |
added DBIx::Custom::Guides
|
96 | |
update pod
|
97 |
Return value is L<DBIx::Custom::Result> object. |
updatedd pod
|
98 |
You can fetch rows by C<fetch> method. |
added DBIx::Custom::Guides
|
99 | |
100 |
while (my $row = $result->fetch) { |
|
101 |
my $title = $row->[0]; |
|
102 |
my $author = $row->[1]; |
|
103 |
} |
|
104 | ||
updatedd pod
|
105 |
See also L<Fetch row/"Fetch row"> about L<DBIx::Custom::Result>. |
added DBIx::Custom::Guides
|
106 | |
updatedd pod
|
107 |
You can specify column names by C<column> option |
108 |
and condition by C<where> option. |
|
added DBIx::Custom::Guides
|
109 | |
110 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
111 |
table => 'book', |
update pod
|
112 |
column => ['author', 'title'], |
added DBIx::Custom::Guides
|
113 |
where => {author => 'Ken'} |
114 |
); |
|
115 | ||
updatedd pod
|
116 |
You can specify join clause by C<join> option. |
added DBIx::Custom::Guides
|
117 | |
118 |
my $result = $dbi->select( |
|
cleanup
|
119 |
table => 'book', |
updatedd pod
|
120 |
column => ['company.name as company_name'] |
cleanup
|
121 |
where => {'book.name' => 'Perl'}, |
122 |
join => ['left outer join company on book.company_id = company.id] |
|
added DBIx::Custom::Guides
|
123 |
); |
124 | ||
updatedd pod
|
125 |
Note that join clause is joined only when C<where> or C<column> option contains table name, |
126 |
such as book.name. |
|
cleanup
|
127 | |
updatedd pod
|
128 |
You can append statement to the end of whole statement by C<append> option. |
added DBIx::Custom::Guides
|
129 | |
130 |
my $result = $dbi->select( |
|
remove DBIx::Custom::Model
|
131 |
table => 'book', |
added DBIx::Custom::Guides
|
132 |
where => {author => 'Ken'}, |
update pod
|
133 |
append => 'for update', |
added DBIx::Custom::Guides
|
134 |
); |
135 | ||
updatedd pod
|
136 |
=head3 C<execute> |
added DBIx::Custom::Guides
|
137 | |
updatedd pod
|
138 |
If you want to execute SQL, use C<execute> method. |
update pod
|
139 | |
140 |
$dbi->execute("select * from book;"); |
|
141 | ||
updatedd pod
|
142 |
You can specify parameters. |
update pod
|
143 | |
144 |
$dbi->execute( |
|
updated pod
|
145 |
"select * from book title = :title and author = :author;" |
updatedd pod
|
146 |
{title => 'Perl', author => 'Ken'} |
update pod
|
147 |
); |
148 | ||
updatedd pod
|
149 |
:title and :author is parameters, which is replaced to placeholers. |
update pod
|
150 | |
151 |
select * from book title = ? and author = ?; |
|
152 | ||
updatedd pod
|
153 |
See also L<Parameter/"Parameter"> about parameter. |
update pod
|
154 | |
updatedd pod
|
155 |
=head3 C<dbh> |
update pod
|
156 | |
updatedd pod
|
157 |
my $dbh = $dbi->dbh; |
update pod
|
158 | |
updatedd pod
|
159 |
Get get database handle object of L<DBI>. |
update pod
|
160 | |
updatedd pod
|
161 |
=head3 C<DBI> methods |
update pod
|
162 | |
updatedd pod
|
163 |
$dbi->do(...); |
164 |
$dbi->begin_work; |
|
update pod
|
165 | |
updatedd pod
|
166 |
You can call all methods of L<DBI> from L<DBIx::Custom> object. |
update pod
|
167 | |
updatedd pod
|
168 |
=head2 Fetch Rows |
169 | ||
170 |
C<select> method return value is L<DBIx::Custom::Result> object. |
|
171 |
You can fetch a row or rows by various methods. |
|
172 | ||
173 |
=head3 Fetch a row (array) : C<fetch> |
|
update pod
|
174 | |
175 |
my $row = $result->fetch; |
|
176 | ||
updatedd pod
|
177 |
C<fetch> method fetch a row and put it into array reference. |
178 |
You can continue to fetch |
|
added DBIx::Custom::Guides
|
179 | |
180 |
while (my $row = $result->fetch) { |
|
update pod
|
181 |
my $title = $row->[0]; |
182 |
my $author = $row->[1]; |
|
added DBIx::Custom::Guides
|
183 |
} |
184 | ||
updatedd pod
|
185 |
=head3 Fetch only first row (array) : C<fetch_first> |
added DBIx::Custom::Guides
|
186 | |
187 |
my $row = $result->fetch_first; |
|
188 | ||
updatedd pod
|
189 |
C<fetch_first> fetch a only first row and finish statment handle, |
190 |
and put it into array refrence. |
|
update pod
|
191 | |
update pod
|
192 |
=head3 Fetch all rows (array) : C<fetch_all> |
update pod
|
193 | |
added DBIx::Custom::Guides
|
194 |
my $rows = $result->fetch_all; |
195 | ||
updatedd pod
|
196 |
C<fetch_all> fetch all rows and put them into array of array reference. |
update pod
|
197 | |
updatedd pod
|
198 |
=head3 Fetch a row (hash) : C<fetch_hash> |
update pod
|
199 | |
updatedd pod
|
200 |
my $row = $result->fetch_hash; |
update pod
|
201 | |
updatedd pod
|
202 |
C<fetch_hash> fetch a row and put it into hash reference. |
203 |
You can fetch a row while row exists. |
|
added DBIx::Custom::Guides
|
204 | |
205 |
while (my $row = $result->fetch_hash) { |
|
206 |
my $title = $row->{title}; |
|
207 |
my $author = $row->{author}; |
|
208 |
} |
|
209 | ||
updatedd pod
|
210 |
=head3 Fetch only a first row (hash) : C<fetch_hash_first> |
added DBIx::Custom::Guides
|
211 | |
212 |
my $row = $result->fetch_hash_first; |
|
update pod
|
213 | |
updatedd pod
|
214 |
C<fetch_hash_first> fetch only a first row and finish statement handle, |
215 |
and put them into hash refrence. |
|
update pod
|
216 | |
updatedd pod
|
217 |
C<one> is C<fetch_hash_first> synonym to save word typing. |
update pod
|
218 | |
updatedd pod
|
219 |
my $row = $result->one; |
update pod
|
220 | |
updatedd pod
|
221 |
=head3 Fetch all rows (hash) : C<fetch_hash_all> |
added DBIx::Custom::Guides
|
222 | |
223 |
my $rows = $result->fetch_hash_all; |
|
224 | ||
updatedd pod
|
225 |
C<fetch_hash_all> fetch all rows and put them into array of hash reference. |
update pod
|
226 | |
updatedd pod
|
227 |
=head3 Statement Handle : C<sth> |
added DBIx::Custom::Guides
|
228 | |
229 |
my $sth = $result->sth; |
|
230 | ||
updatedd pod
|
231 |
If you want to get statment handle, use <sth> method. |
update pod
|
232 | |
updatedd pod
|
233 |
=head2 Parameter |
update pod
|
234 | |
updated pod
|
235 |
=head3 Basic of Parameter |
update pod
|
236 | |
updated pod
|
237 |
You can embedd parameter into SQL. |
update pod
|
238 | |
updated pod
|
239 |
select * from book where title = :title and author like :author; |
update pod
|
240 | |
updated pod
|
241 |
:title and :author is parameter |
update pod
|
242 | |
updated pod
|
243 |
Parameter is converted to place holder. |
update pod
|
244 | |
245 |
select * from book where title = ? and author like ?; |
|
246 | ||
updatedd pod
|
247 |
use C<execute> to execute SQL. |
update pod
|
248 | |
updated pod
|
249 |
my $sql = "select * from book where title = :title and author like :author;" |
updatedd pod
|
250 |
$dbi->execute($sql, {title => 'Perl', author => '%Ken%'}); |
update pod
|
251 | |
update pod
|
252 |
You can specify values embedded into place holder as hash reference using |
253 |
C<param> option. |
|
update pod
|
254 | |
updatedd pod
|
255 |
You can specify C<filter> at C<execute>. |
update pod
|
256 | |
updatedd pod
|
257 |
$dbi->execute($sql, {title => 'Perl', author => '%Ken%'} |
update pod
|
258 |
filter => {title => 'to_something'); |
259 | ||
updatedd pod
|
260 |
Note that at C<execute> the filter applied by C<apply_filter> |
update pod
|
261 |
don't has effective to columns. |
improved table search in col...
|
262 |
You have to use C<table> option |
update pod
|
263 | |
improved table search in col...
|
264 |
$dbi->execute($sql, table => ['author', 'book']); |
update pod
|
265 | |
update pod
|
266 |
=head3 Manipulate same name's columns |
added DBIx::Custom::Guides
|
267 | |
update pod
|
268 |
It is ok if there are same name's columns. |
269 |
Let's think two date comparison. |
|
added DBIx::Custom::Guides
|
270 | |
updated pod
|
271 |
my $sql = "select * from table where date > :date and date < :date;"; |
added DBIx::Custom::Guides
|
272 | |
updatedd pod
|
273 |
In this case, You specify parameter values as array reference. |
added DBIx::Custom::Guides
|
274 | |
updatedd pod
|
275 |
my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']}); |
added DBIx::Custom::Guides
|
276 | |
updatedd pod
|
277 |
=head2 Create where clause |
update pod
|
278 | |
updatedd pod
|
279 |
=head3 Dinamically create where clause : where |
update pod
|
280 | |
update pod
|
281 |
You want to search multiple conditions in many times. |
282 |
Let's think the following three cases. |
|
update pod
|
283 | |
update pod
|
284 |
Case1: Search only C<title> |
update pod
|
285 | |
updated pod
|
286 |
where title = :title |
update pod
|
287 | |
update pod
|
288 |
Case2: Search only C<author> |
update pod
|
289 | |
updated pod
|
290 |
where author = :author |
update pod
|
291 | |
update pod
|
292 |
Case3: Search C<title> and C<author> |
update pod
|
293 | |
updated pod
|
294 |
where title = :title and author = :author |
update pod
|
295 | |
update pod
|
296 |
L<DBIx::Custom> support dinamic where clause creating. |
updatedd pod
|
297 |
At first, create L<DBIx::Custom::Where> object by C<where>. |
update pod
|
298 | |
299 |
my $where = $dbi->where; |
|
300 | ||
updatedd pod
|
301 |
Set clause by C<clause> |
update pod
|
302 | |
303 |
$where->clause( |
|
updated pod
|
304 |
['and', 'title = :title, 'author = :author'] |
added DBIx::Custom::Guides
|
305 |
); |
306 | ||
update pod
|
307 |
C<clause> is the following format. |
update pod
|
308 | |
updated pod
|
309 |
['or' or 'and', PART1, PART1, PART1] |
added DBIx::Custom::Guides
|
310 | |
update pod
|
311 |
First argument is 'or' or 'and'. |
updated pod
|
312 |
Later than first argument are part which contains parameter. |
update pod
|
313 | |
update pod
|
314 |
You can write more complex format. |
update pod
|
315 | |
316 |
['and', |
|
updated pod
|
317 |
'title = :title', |
318 |
['or', 'author = :author', 'date like :date'] |
|
update pod
|
319 |
] |
320 | ||
updated pod
|
321 |
This mean "title = :title and ( author = :author or date like :date )". |
update pod
|
322 | |
323 |
After setting C<clause>, set C<param>. |
|
added DBIx::Custom::Guides
|
324 |
|
update pod
|
325 |
$where->param({title => 'Perl'}); |
added DBIx::Custom::Guides
|
326 | |
update pod
|
327 |
In this example, parameter contains only title. |
added DBIx::Custom::Guides
|
328 | |
updatedd pod
|
329 |
If you execute C<string_to>, you can get where clause |
update pod
|
330 |
which contain only parameter name. |
update pod
|
331 | |
332 |
my $where_clause = $where->to_string; |
|
333 | ||
update pod
|
334 |
Parameter name is only title, the following where clause is created. |
update pod
|
335 | |
updated pod
|
336 |
where title = :title |
update pod
|
337 | |
update pod
|
338 |
You can also create where clause by stringification. |
update pod
|
339 | |
340 |
my $where_clause = "$where"; |
|
341 | ||
update pod
|
342 |
This is useful to embbed it into SQL. |
added DBIx::Custom::Guides
|
343 | |
update pod
|
344 |
=head3 In case where clause contains same name columns |
check arguments of connect m...
|
345 | |
updated pod
|
346 |
Even if same name parameters exists, you can create where clause. |
update pod
|
347 |
Let's think that there are starting date and ending date. |
added experimental DBIx::Cus...
|
348 | |
update pod
|
349 |
my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
added experimental DBIx::Cus...
|
350 | |
update pod
|
351 |
In this case, you set parameter value as array reference. |
update pod
|
352 | |
353 |
my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
354 | ||
updated pod
|
355 |
You can embbed these values into same name parameters. |
update pod
|
356 | |
357 |
$where->clause( |
|
updated pod
|
358 |
['and', 'date > :date', 'date < :date'] |
added experimental DBIx::Cus...
|
359 |
); |
update pod
|
360 |
$where->param($p); |
361 | ||
update pod
|
362 |
If starting date isn't exists, create the following parameter. |
update pod
|
363 | |
364 |
my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
365 | ||
updatedd pod
|
366 |
You can get DBIx::Custom::NotExists object by C<not_exists> |
update pod
|
367 |
This mean correnspondinf value isn't exists. |
update pod
|
368 | |
update pod
|
369 |
If ending date isn't exists, create the following parameter. |
update pod
|
370 | |
371 |
my $p = {date => ['2010-11-15']}; |
|
372 | ||
update pod
|
373 |
If both date isn't exists, create the following parameter. |
update pod
|
374 | |
375 |
my $p = {date => []}; |
|
376 | ||
update pod
|
377 |
This logic is a little difficut. See the following ones. |
update pod
|
378 | |
379 |
my @date; |
|
380 |
push @date, exists $param->{start_date} ? $param->{start_date} |
|
381 |
: $dbi->not_exists; |
|
382 |
push @date, $param->{end_date} if exists $param->{end_date}; |
|
383 |
my $p = {date => \@date}; |
|
384 | ||
updatedd pod
|
385 |
=head3 With C<select> |
update pod
|
386 | |
updatedd pod
|
387 |
You can pass L<DBIx::Custom::Where> object to C<where> of C<select>. |
update pod
|
388 |
|
389 |
my $where = $dbi->where; |
|
updated pod
|
390 |
$where->clause(['and', 'title = :title', 'author = :author']); |
update pod
|
391 |
$where->param({title => 'Perl'}); |
update pod
|
392 |
my $result = $dbi->select(table => 'book', where => $where); |
393 | ||
updatedd pod
|
394 |
You can also pass it to C<where> of C<update>AC<delete> |
update pod
|
395 | |
updatedd pod
|
396 |
=head3 With C<execute> |
added experimental DBIx::Cus...
|
397 | |
update pod
|
398 |
L<DBIx::Custom::Where> object is embedded into SQL. |
added experimental DBIx::Cus...
|
399 | |
update pod
|
400 |
my $where = $dbi->where; |
updated pod
|
401 |
$where->clause(['and', 'title = :title', 'author = :author']); |
update pod
|
402 |
$where->param({title => 'Perl'}); |
update pod
|
403 | |
update pod
|
404 |
my $sql = <<"EOS"; |
updated pod
|
405 |
select * from book; |
update pod
|
406 |
$where |
407 |
EOS |
|
408 | ||
updatedd pod
|
409 |
$dbi->execute($sql, $param, table => 'book'); |
410 | ||
411 |
=head2 Filtering |
|
412 | ||
413 |
=head3 Register filter : C<register_filter> |
|
414 | ||
415 |
If you want to register filter, use C<register_filter>. |
|
416 | ||
417 |
$dbi->register_filter( |
|
418 |
# Time::Piece object to DATE format |
|
419 |
tp_to_date => sub { |
|
420 |
my $date = shift; |
|
421 |
return $tp->strftime('%Y-%m-%d'); |
|
422 |
}, |
|
423 |
|
|
424 |
# DATE to Time::Piece object |
|
425 |
date_to_tp => sub { |
|
426 |
my $date = shift; |
|
427 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
428 |
}, |
|
429 |
); |
|
430 | ||
431 |
=head3 Filter before sending data into database : C<filter> option |
|
432 | ||
433 |
If you filter sending data, use C<filter> option. |
|
434 | ||
435 |
$dbi->execute( |
|
436 |
'insert into book (date) values (:date)', |
|
437 |
{date => $tp}, |
|
438 |
filter => {date => 'tp_to_date'} |
|
439 |
); |
|
440 | ||
441 |
You can use C<filter> option in C<insert>, C<update>, C<delete>, C<select> method. |
|
442 | ||
443 |
$dbi->insert( |
|
444 |
{date => $tp}, |
|
445 |
table => 'book', |
|
446 |
filter => {date => 'tp_to_date'} |
|
447 |
); |
|
448 | ||
449 |
=head3 Filter after fetching data from database. |
|
450 | ||
451 |
If you filter fetch data, use L<DBIx::Custom::Result>'s C<filter> method. |
|
452 | ||
453 |
my $result = $dbi->select(column => 'date', table => 'book'); |
|
454 |
$result->filter(date => 'date_to_tp'); |
|
455 |
my $row = $result->one; |
|
update pod
|
456 | |
add feture. all model class ...
|
457 |
=head2 7. Model |
update pod
|
458 | |
add feture. all model class ...
|
459 |
=head3 Model |
update pod
|
460 | |
add feture. all model class ...
|
461 |
you can define model extending L<DBIx::Custom::Model> |
removed experimental base_ta...
|
462 |
to improve source code view. |
update pod
|
463 | |
add experimental DBIx::Custo...
|
464 |
At first, you create basic model class extending <DBIx::Custom::Model>. |
updatedd pod
|
465 |
Each L<DBIx::Custom> class inherit L<Object::Simple>. |
466 |
so you can inherit the following way. |
|
update pod
|
467 | |
add experimental DBIx::Custo...
|
468 |
package MyModel; |
updatedd pod
|
469 |
use DBIx::Custom::Model -base; |
update pod
|
470 | |
add experimental DBIx::Custo...
|
471 |
Next, you create each model classes. |
update pod
|
472 | |
add experimental DBIx::Custo...
|
473 |
MyModel::book |
removed experimental base_ta...
|
474 | |
add experimental DBIx::Custo...
|
475 |
package MyModel::book; |
updatedd pod
|
476 |
use MyModel -base; |
add experimental DBIx::Custo...
|
477 |
|
478 |
sub insert { ... } |
|
479 |
sub list { ... } |
|
removed experimental base_ta...
|
480 | |
add experimental DBIx::Custo...
|
481 |
MyModel::company |
removed experimental base_ta...
|
482 | |
add experimental DBIx::Custo...
|
483 |
package MyModel::company; |
updatedd pod
|
484 |
use MyModel -base; |
add experimental DBIx::Custo...
|
485 |
|
486 |
sub insert { ... } |
|
487 |
sub list { ... } |
|
add feture. all model class ...
|
488 | |
489 |
The follwoing modules location is needed. |
|
490 | ||
491 |
MyModel.pm |
|
492 |
MyModel / book.pm |
|
493 |
/ company.pm |
|
add experimental DBIx::Custo...
|
494 | |
updatedd pod
|
495 |
You can include these models by C<include_model> |
add experimental DBIx::Custo...
|
496 | |
497 |
$dbi->include_model('MyModel'); |
|
498 | ||
499 |
First argument is name space of model. |
|
500 | ||
add feture. all model class ...
|
501 |
You can use model like this. |
502 | ||
503 |
my $result = $dbi->model('book')->list; |
|
504 | ||
505 |
In mode, You can use such as methods, |
|
updatedd pod
|
506 |
C<insert>, C<update>, C<update_all>, |
507 |
C<delete>, C<delete_all>, C<select> |
|
add feture. all model class ...
|
508 |
without C<table> option. |
509 | ||
updatedd pod
|
510 |
$dbi->model('book')->insert($param); |
update pod
|
511 | |
add feture. all model class ...
|
512 |
Model is L<DBIx::Custom::Model>. |
update pod
|
513 | |
updatedd pod
|
514 |
If you need table nameAyou can get it by C<table>. |
update pod
|
515 | |
add feture. all model class ...
|
516 |
my $table = $model->table; |
update pod
|
517 | |
add feture. all model class ...
|
518 |
You can get L<DBIx::Custom>. |
update pod
|
519 | |
add feture. all model class ...
|
520 |
my $dbi = $model->dbi; |
removed experimental base_ta...
|
521 | |
add feture. all model class ...
|
522 |
You can also call all methods of L<DBIx::Custom> and L<DBI>. |
added experimental DBIx::Cus...
|
523 | |
update pod
|
524 |
# DBIx::Custom method |
add feture. all model class ...
|
525 |
$model->execute($sql); |
update pod
|
526 |
|
527 |
# DBI method |
|
add feture. all model class ...
|
528 |
$model->begin_work; |
529 |
$model->commit; |
|
added experimental DBIx::Cus...
|
530 | |
updatedd pod
|
531 |
If you want to get all models, you can get them by keys of C<models>. |
add models() attribute
|
532 | |
533 |
my @models = keys %{$self->models}; |
|
534 | ||
add DBIx::Custom::Model fore...
|
535 |
You can set primary key to model. |
536 | ||
537 |
$model->primary_key(['id', 'number_id']); |
|
538 | ||
updatedd pod
|
539 |
Primary key is used by C<insert>, C<update>, C<delete>, |
540 |
and C<select> methods. |
|
add DBIx::Custom::Model fore...
|
541 | |
updatedd pod
|
542 |
by C<filter> you can define filters applied by C<apply_filter> |
add experimental DBIx::Custo...
|
543 | |
544 |
$model->filter({ |
|
545 |
title => {out => ..., in => ..., end => ...}, |
|
546 |
author => {out => ..., in => ..., end => ...} |
|
547 |
}); |
|
548 | ||
updatedd pod
|
549 |
This filters is applied when C<include_model> is called. |
add experimental DBIx::Custo...
|
550 | |
add DBIx::Custom::Model colu...
|
551 |
You can set column names |
552 | ||
553 |
$model->columns(['id', 'number_id']); |
|
554 | ||
updatedd pod
|
555 |
Column names is automarically set by C<setup_model>. |
556 |
This method is needed to be call after C<include_model>. |
|
add experimental setup_model...
|
557 | |
558 |
$dbi->setup_model; |
|
559 | ||
cleanup
|
560 |
You can set C<join> |
cleanup
|
561 | |
cleanup
|
562 |
$model->join(['left outer join company on book.company_id = company.id']); |
cleanup
|
563 | |
updatedd pod
|
564 |
C<join> is used by C<select> method. |
cleanup
|
565 | |
add experimental DBIx::Custo...
|
566 |
=head2 Class name, Model name, Table name |
567 | ||
568 |
Class name, model name, and table name is a little different. |
|
569 |
Generally Class name is model name, and table name is model name. |
|
570 | ||
571 |
CLASS MODEL TABLE |
|
572 |
book (CLASS) -> book (MODEL) -> book |
|
573 | ||
574 |
You can change model name. |
|
575 | ||
576 |
package MyModel::book; |
|
cleanup
|
577 |
use MyModel -base; |
add experimental DBIx::Custo...
|
578 |
|
cleanup
|
579 |
has name => 'book_model'; |
add experimental DBIx::Custo...
|
580 | |
581 |
CLASS MODEL TABLE |
|
582 |
book book_model (MODEL) -> book_model |
|
583 | ||
updatedd pod
|
584 |
Model name is the name used by L<model> of L<DBIx::Custom>. |
add experimental DBIx::Custo...
|
585 | |
586 |
$dbi->model('book_model'); |
|
587 | ||
588 |
You can change table name. |
|
589 | ||
590 |
package MyModel::book; |
|
cleanup
|
591 |
use MyModel -base; |
add experimental DBIx::Custo...
|
592 |
|
cleanup
|
593 |
has table => 'book_table'; |
add experimental DBIx::Custo...
|
594 | |
595 |
CLASS MODEL TABLE |
|
596 |
book (CLASS) -> book book_table |
|
597 | ||
598 |
Table name is the table really accessed. |
|
599 | ||
600 |
$dbi->model('book')->insert(...); # access to "book_table" |
|
601 | ||
updatedd pod
|
602 |
=head2 Create column clause automatically : mycolumn, column |
cleanup
|
603 | |
updatedd pod
|
604 |
To create column clause automatically, use C<mycolumn>. |
cleanup
|
605 |
Valude of C<table> and C<columns> is used. |
606 | ||
cleanup
|
607 |
my $mycolumns = $model->mycolumn; |
cleanup
|
608 | |
data_source is DEPRECATED! I...
|
609 |
If C<table> is 'book'AC<column> is ['id', 'name'], |
cleanup
|
610 |
the following clause is created. |
611 | ||
612 |
book.id as id, book.name as name |
|
613 | ||
614 |
These column name is for removing column name ambiguities. |
|
615 | ||
616 |
You can create column clause from columns of other table. |
|
617 | ||
cleanup
|
618 |
my $columns = $model->column('company'); |
cleanup
|
619 | |
cleanup
|
620 |
If C<table> is "company", C<column> return ['id', 'name'], |
cleanup
|
621 |
the following clause is created. |
622 | ||
cleanup
|
623 |
company.id as "company.id", company.name as "company.name" |
add experimental DBIx::Custo...
|
624 | |
add feture. all model class ...
|
625 |
=head2 Model Examples |
added DBIx::Custom::Guides
|
626 | |
add feture. all model class ...
|
627 |
Model examples |
added DBIx::Custom::Guides
|
628 | |
update pod
|
629 |
package MyDBI; |
updatedd pod
|
630 |
use DBIx::Custom -base; |
added DBIx::Custom::Guides
|
631 |
|
update pod
|
632 |
sub connect { |
633 |
my $self = shift->SUPER::connect(@_); |
|
634 |
|
|
add feture. all model class ...
|
635 |
$self->include_model( |
636 |
MyModel => [ |
|
removed experimental base_ta...
|
637 |
'book', |
638 |
'company' |
|
639 |
] |
|
update pod
|
640 |
); |
641 |
} |
|
removed experimental base_ta...
|
642 |
|
add feture. all model class ...
|
643 |
package MyModel::book; |
updatedd pod
|
644 |
use DBIx::Custom::Model -base; |
removed experimental base_ta...
|
645 |
|
updatedd pod
|
646 |
has primary_key => sub { ['id'] }; |
add experimental update_at()...
|
647 |
|
removed experimental base_ta...
|
648 |
sub insert { ... } |
649 |
sub list { ... } |
|
650 |
|
|
add feture. all model class ...
|
651 |
package MyModel::company; |
updatedd pod
|
652 |
use DBIx::Custom::Model -base; |
add experimental update_at()...
|
653 | |
updatedd pod
|
654 |
has primary_key => sub { ['id'] }; |
removed experimental base_ta...
|
655 |
|
656 |
sub insert { ... } |
|
657 |
sub list { ... } |
|
update pod
|
658 | |
added DBIx::Custom::Guides
|
659 |
=cut |