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