... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1744 |
|
2 |
+ - moved DBIx::Custom::Guide to wiki |
|
1 | 3 |
0.1733 |
2 | 4 |
- select method join option can receive string. |
3 | 5 |
- removed DEPRECATED status of select param option |
... | ... |
@@ -240,7 +240,7 @@ sub delete { |
240 | 240 |
|
241 | 241 |
# Where |
242 | 242 |
my $where = defined $opt{id} |
243 |
- ? $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table}) |
|
243 |
+ ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table}) |
|
244 | 244 |
: $opt{where}; |
245 | 245 |
my $w = $self->_where_clause_and_param($where, $opt{where_param}); |
246 | 246 |
|
... | ... |
@@ -348,67 +348,59 @@ sub each_table { |
348 | 348 |
sub execute { |
349 | 349 |
my $self = shift; |
350 | 350 |
my $sql = shift; |
351 |
+ |
|
352 |
+ # Options |
|
351 | 353 |
my $param; |
352 | 354 |
$param = shift if @_ % 2; |
353 | 355 |
my %opt = @_; |
354 |
- |
|
355 |
- # Options |
|
356 |
- my $p = $opt{param} || {}; |
|
357 |
- $param ||= $p; |
|
356 |
+ warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter}; |
|
357 |
+ $param ||= $opt{param} || {}; |
|
358 | 358 |
my $tables = $opt{table} || []; |
359 | 359 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
360 |
- my $filter = $opt{filter}; |
|
361 |
- $filter = _array_to_hash($filter); |
|
362 |
- my $bind_type = $opt{bind_type} || $opt{type}; |
|
363 |
- $bind_type = _array_to_hash($bind_type); |
|
364 |
- my $type_rule_off = $opt{type_rule_off}; |
|
365 |
- my $type_rule_off_parts = { |
|
366 |
- 1 => $opt{type_rule1_off}, |
|
367 |
- 2 => $opt{type_rule2_off} |
|
368 |
- }; |
|
369 |
- my $query_return = $opt{query}; |
|
370 |
- my $table_alias = $opt{table_alias} || {}; |
|
371 |
- my $after_build_sql = $opt{after_build_sql} || $opt{sqlfilter}; |
|
372 |
- warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter}; |
|
373 |
- my $id = $opt{id}; |
|
374 |
- my $primary_key = $opt{primary_key}; |
|
375 |
- croak "execute method primary_key option " . |
|
376 |
- "must be specified when id is specified " . _subname |
|
377 |
- if defined $id && !defined $primary_key; |
|
378 |
- $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
|
379 |
- my $append = $opt{append}; |
|
380 |
- $sql .= $append if defined $append && !ref $sql; |
|
360 |
+ my $filter = _array_to_hash($opt{filter}); |
|
361 |
+ |
|
362 |
+ # Append |
|
363 |
+ $sql .= $opt{append} if defined $opt{append} && !ref $sql; |
|
381 | 364 |
|
382 |
- my $query |
|
383 |
- = ref $sql ? $sql : $self->_create_query($sql, $after_build_sql); |
|
365 |
+ # Query |
|
366 |
+ my $query = ref $sql |
|
367 |
+ ? $sql |
|
368 |
+ : $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter}); |
|
384 | 369 |
|
385 | 370 |
# Save query |
386 | 371 |
$self->last_sql($query->sql); |
387 | 372 |
|
388 |
- return $query if $query_return; |
|
373 |
+ # Return query |
|
374 |
+ return $query if $opt{query}; |
|
389 | 375 |
|
390 |
- # DEPRECATED! Merge query filter |
|
376 |
+ # Merge query filter(DEPRECATED!) |
|
391 | 377 |
$filter ||= $query->{filter} || {}; |
392 | 378 |
|
393 | 379 |
# Tables |
394 | 380 |
unshift @$tables, @{$query->{tables} || []}; |
395 | 381 |
my $main_table = @{$tables}[-1]; |
396 |
- |
|
397 |
- if (defined $id) { |
|
398 |
- my $id_param = $self->_id_to_param($id, $primary_key, $main_table); |
|
382 |
+ |
|
383 |
+ # Convert id to parameter |
|
384 |
+ if (defined $opt{id}) { |
|
385 |
+ my $id_param = $self->_id_to_param($opt{id}, $opt{primary_key}, $main_table); |
|
399 | 386 |
$param = $self->merge_param($id_param, $param); |
400 | 387 |
} |
401 | 388 |
|
402 |
- # DEPRECATED! Cleanup tables |
|
389 |
+ # Cleanup tables(DEPRECATED!) |
|
403 | 390 |
$tables = $self->_remove_duplicate_table($tables, $main_table) |
404 | 391 |
if @$tables > 1; |
405 | 392 |
|
406 | 393 |
# Type rule |
407 | 394 |
my $type_filters = {}; |
408 |
- unless ($type_rule_off) { |
|
395 |
+ unless ($opt{type_rule_off}) { |
|
396 |
+ my $type_rule_off_parts = { |
|
397 |
+ 1 => $opt{type_rule1_off}, |
|
398 |
+ 2 => $opt{type_rule2_off} |
|
399 |
+ }; |
|
409 | 400 |
for my $i (1, 2) { |
410 | 401 |
unless ($type_rule_off_parts->{$i}) { |
411 | 402 |
$type_filters->{$i} = {}; |
403 |
+ my $table_alias = $opt{table_alias} || {}; |
|
412 | 404 |
for my $alias (keys %$table_alias) { |
413 | 405 |
my $table = $table_alias->{$alias}; |
414 | 406 |
|
... | ... |
@@ -422,7 +414,7 @@ sub execute { |
422 | 414 |
} |
423 | 415 |
} |
424 | 416 |
|
425 |
- # DEPRECATED! Applied filter |
|
417 |
+ # Applied filter(DEPRECATED!) |
|
426 | 418 |
if ($self->{filter}{on}) { |
427 | 419 |
my $applied_filter = {}; |
428 | 420 |
for my $table (@$tables) { |
... | ... |
@@ -448,13 +440,8 @@ sub execute { |
448 | 440 |
} |
449 | 441 |
|
450 | 442 |
# Create bind values |
451 |
- my $bind = $self->_create_bind_values( |
|
452 |
- $param, |
|
453 |
- $query->columns, |
|
454 |
- $filter, |
|
455 |
- $type_filters, |
|
456 |
- $bind_type |
|
457 |
- ); |
|
443 |
+ my $bind = $self->_create_bind_values($param, $query->columns, |
|
444 |
+ $filter, $type_filters, _array_to_hash($opt{bind_type} || $opt{type})); |
|
458 | 445 |
|
459 | 446 |
# Execute |
460 | 447 |
my $sth = $query->sth; |
... | ... |
@@ -462,11 +449,8 @@ sub execute { |
462 | 449 |
eval { |
463 | 450 |
for (my $i = 0; $i < @$bind; $i++) { |
464 | 451 |
my $bind_type = $bind->[$i]->{bind_type}; |
465 |
- $sth->bind_param( |
|
466 |
- $i + 1, |
|
467 |
- $bind->[$i]->{value}, |
|
468 |
- $bind_type ? $bind_type : () |
|
469 |
- ); |
|
452 |
+ $sth->bind_param($i + 1, $bind->[$i]->{value}, |
|
453 |
+ $bind_type ? $bind_type : ()); |
|
470 | 454 |
} |
471 | 455 |
$affected = $sth->execute; |
472 | 456 |
}; |
... | ... |
@@ -491,7 +475,7 @@ sub execute { |
491 | 475 |
# Select statement |
492 | 476 |
if ($sth->{NUM_OF_FIELDS}) { |
493 | 477 |
|
494 |
- # DEPRECATED! Filter |
|
478 |
+ # Filter(DEPRECATED!) |
|
495 | 479 |
my $filter = {}; |
496 | 480 |
if ($self->{filter}{on}) { |
497 | 481 |
$filter->{in} = {}; |
... | ... |
@@ -499,10 +483,8 @@ sub execute { |
499 | 483 |
push @$tables, $main_table if $main_table; |
500 | 484 |
for my $table (@$tables) { |
501 | 485 |
for my $way (qw/in end/) { |
502 |
- $filter->{$way} = { |
|
503 |
- %{$filter->{$way}}, |
|
504 |
- %{$self->{filter}{$way}{$table} || {}} |
|
505 |
- }; |
|
486 |
+ $filter->{$way} = {%{$filter->{$way}}, |
|
487 |
+ %{$self->{filter}{$way}{$table} || {}}}; |
|
506 | 488 |
} |
507 | 489 |
} |
508 | 490 |
} |
... | ... |
@@ -519,10 +501,8 @@ sub execute { |
519 | 501 |
from2 => $self->type_rule->{from2} |
520 | 502 |
}, |
521 | 503 |
); |
522 |
- |
|
523 | 504 |
return $result; |
524 | 505 |
} |
525 |
- |
|
526 | 506 |
# Not select statement |
527 | 507 |
else { return $affected } |
528 | 508 |
} |
... | ... |
@@ -589,7 +569,7 @@ sub insert { |
589 | 569 |
|
590 | 570 |
# Merge id to parameter |
591 | 571 |
$param = $self->merge_param( |
592 |
- $self->_id_to_param($opt{id}, $opt{primary_key}), $param) |
|
572 |
+ $self->_id_to_param(delete $opt{id}, $opt{primary_key}), $param) |
|
593 | 573 |
if defined $opt{id}; |
594 | 574 |
|
595 | 575 |
# Insert statement |
... | ... |
@@ -838,10 +818,7 @@ sub select { |
838 | 818 |
$found->{$table} = 1; |
839 | 819 |
} |
840 | 820 |
} |
841 |
- else { |
|
842 |
- my $main_table = $tables->[-1] || ''; |
|
843 |
- $sql .= $self->_q($main_table) . ' '; |
|
844 |
- } |
|
821 |
+ else { $sql .= $self->_q($tables->[-1] || '') . ' ' } |
|
845 | 822 |
$sql =~ s/, $/ /; |
846 | 823 |
croak "select method table option must be specified " . _subname |
847 | 824 |
unless $tables->[-1]; |
... | ... |
@@ -852,7 +829,7 @@ sub select { |
852 | 829 |
|
853 | 830 |
# Where |
854 | 831 |
my $where = defined $opt{id} |
855 |
- ? $self->_id_to_param($opt{id}, $opt{primary_key}, $tables->[-1]) |
|
832 |
+ ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $tables->[-1]) |
|
856 | 833 |
: $opt{where}; |
857 | 834 |
my $w = $self->_where_clause_and_param($where, $where_param); |
858 | 835 |
|
... | ... |
@@ -1024,7 +1001,7 @@ sub update { |
1024 | 1001 |
|
1025 | 1002 |
# Convert id to where parameter |
1026 | 1003 |
my $where = defined $opt{id} |
1027 |
- ? $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table}) |
|
1004 |
+ ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table}) |
|
1028 | 1005 |
: $opt{where}; |
1029 | 1006 |
|
1030 | 1007 |
# Where |
... | ... |
@@ -1893,7 +1870,7 @@ sub _add_relation_table { |
1893 | 1870 |
|
1894 | 1871 |
=head1 NAME |
1895 | 1872 |
|
1896 |
-DBIx::Custom - Execute insert, update, delete, and select statement easily |
|
1873 |
+DBIx::Custom - DBI extension to execute insert, update, delete, and select easily |
|
1897 | 1874 |
|
1898 | 1875 |
=head1 SYNOPSIS |
1899 | 1876 |
|
... | ... |
@@ -2799,10 +2776,10 @@ See L<DBIx::Custom::Model> to know model features. |
2799 | 2776 |
|
2800 | 2777 |
=head2 C<insert_timestamp> |
2801 | 2778 |
|
2802 |
-$dbi->insert_timestamp( |
|
2803 |
- [qw/created_at updated_at/] |
|
2804 |
- => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") } |
|
2805 |
-); |
|
2779 |
+ $dbi->insert_timestamp( |
|
2780 |
+ [qw/created_at updated_at/] |
|
2781 |
+ => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") } |
|
2782 |
+ ); |
|
2806 | 2783 |
|
2807 | 2784 |
Timestamp value when C<insert> method is executed |
2808 | 2785 |
with C<timestamp> option. |
... | ... |
@@ -2811,7 +2788,7 @@ If C<insert_timestamp> is set and C<insert> method is executed |
2811 | 2788 |
with C<timestamp> option, column C<created_at> and C<update_at> |
2812 | 2789 |
is automatically set to the value like "2010-10-11 10:12:54". |
2813 | 2790 |
|
2814 |
-$dbi->insert($param, table => 'book', timestamp => 1); |
|
2791 |
+ $dbi->insert($param, table => 'book', timestamp => 1); |
|
2815 | 2792 |
|
2816 | 2793 |
=head2 C<like_value> |
2817 | 2794 |
|
... | ... |
@@ -1,627 +1,9 @@ |
1 |
-=encoding utf8 |
|
2 |
- |
|
3 | 1 |
=head1 NAME |
4 | 2 |
|
5 | 3 |
DBIx::Custom::Guide - DBIx::Custom Guide |
6 | 4 |
|
7 |
-=head1 FEATURES |
|
8 |
- |
|
9 |
-L<DBIx::Custom> is the wrapper class of L<DBI> to execute SQL easily. |
|
10 |
-This module have the following features. |
|
11 |
- |
|
12 |
-=over 4 |
|
13 |
- |
|
14 |
-=item * |
|
15 |
- |
|
16 |
-Execute C<insert>, C<update>, C<delete>, or C<select> statement easily |
|
17 |
- |
|
18 |
-=item * |
|
19 |
- |
|
20 |
-Create C<where> clause flexibly |
|
21 |
- |
|
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 |
|
43 |
- |
|
44 |
-=item * |
|
45 |
- |
|
46 |
-Create C<order by> clause flexibly |
|
47 |
- |
|
48 |
-=back |
|
49 |
- |
|
50 |
-=head1 GUIDE |
|
51 |
- |
|
52 |
-=head2 Connect to database |
|
53 |
- |
|
54 |
- use DBIx::Custom; |
|
55 |
- my $dbi = DBIx::Custom->connect( |
|
56 |
- dsn => "dbi:mysql:database=bookshop", |
|
57 |
- user => 'ken', |
|
58 |
- password => '!LFKD%$&', |
|
59 |
- option => {mysql_enable_utf8 => 1} |
|
60 |
- ); |
|
61 |
- |
|
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. |
|
64 |
- |
|
65 |
-C<option> is L<DBI> option. |
|
66 |
-By default, the following option is set. |
|
67 |
-Exeption is thrown when fatal error occur and commit mode is auto commit. |
|
68 |
- |
|
69 |
- { |
|
70 |
- RaiseError => 1 |
|
71 |
- PrintError => 0 |
|
72 |
- AutoCommit => 1 |
|
73 |
- } |
|
74 |
- |
|
75 |
-=head2 Execute query |
|
76 |
- |
|
77 |
-=head3 Insert Statement : C<insert> |
|
78 |
- |
|
79 |
-If you want to execute insert statement, use C<insert> method. |
|
80 |
- |
|
81 |
- $dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
|
82 |
- |
|
83 |
-First argument is insert row data, C<table> is table name. |
|
84 |
- |
|
85 |
-=head3 Update Statement : C<update> |
|
86 |
- |
|
87 |
-If you want to execute update stateimuse, use C<update> method. |
|
88 |
- |
|
89 |
- $dbi->update( |
|
90 |
- {title => 'Perl', author => 'Ken'}, |
|
91 |
- table => 'book', |
|
92 |
- where => {id => 5} |
|
93 |
- ); |
|
94 |
- |
|
95 |
-First argument is update row data, C<table> is table name, C<where> is condition. |
|
96 |
- |
|
97 |
-Note that you can't execute C<update> method without C<where>. |
|
98 |
-If you want to update all rows, use update_all. |
|
99 |
- |
|
100 |
- $dbi->update_all({title => 'Perl', author => 'Ken'}, table => 'book'); |
|
101 |
- |
|
102 |
-=head3 Delete Statement : C<delete> |
|
103 |
- |
|
104 |
-If you want to execute delete statement, use C<delete> method. |
|
105 |
- |
|
106 |
- $dbi->delete(table => 'book', where => {author => 'Ken'}); |
|
107 |
- |
|
108 |
-C<table> is table name, C<where> is condition. |
|
109 |
- |
|
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. |
|
112 |
- |
|
113 |
- $dbi->delete_all(table => 'book'); |
|
114 |
- |
|
115 |
-=head3 Select Statement : C<select> |
|
116 |
- |
|
117 |
-If you want to execute select statement, use C<select> method. |
|
118 |
- |
|
119 |
- my $result = $dbi->select(table => 'book'); |
|
120 |
- |
|
121 |
-Return value is L<DBIx::Custom::Result> object. |
|
122 |
-You can fetch rows by C<fetch> method. |
|
123 |
- |
|
124 |
- while (my $row = $result->fetch) { |
|
125 |
- my $title = $row->[0]; |
|
126 |
- my $author = $row->[1]; |
|
127 |
- } |
|
128 |
- |
|
129 |
-See also L<Fetch row/"Fetch row"> about L<DBIx::Custom::Result>. |
|
130 |
- |
|
131 |
-You can specify column names by C<column> option |
|
132 |
-and condition by C<where> option. |
|
133 |
- |
|
134 |
- my $result = $dbi->select( |
|
135 |
- table => 'book', |
|
136 |
- column => ['author', 'title'], |
|
137 |
- where => {author => 'Ken'} |
|
138 |
- ); |
|
139 |
- |
|
140 |
-You can specify join clause by C<join> option. |
|
141 |
- |
|
142 |
- my $result = $dbi->select( |
|
143 |
- table => 'book', |
|
144 |
- column => ['company.name as company_name'] |
|
145 |
- where => {'book.name' => 'Perl'}, |
|
146 |
- join => ['left outer join company on book.company_id = company.id] |
|
147 |
- ); |
|
148 |
- |
|
149 |
-Note that join clause is joined only when C<where> or C<column> option contains table name, |
|
150 |
-such as book.name. |
|
151 |
- |
|
152 |
-You can append statement to the end of whole statement by C<append> option. |
|
153 |
- |
|
154 |
- my $result = $dbi->select( |
|
155 |
- table => 'book', |
|
156 |
- where => {author => 'Ken'}, |
|
157 |
- append => 'for update', |
|
158 |
- ); |
|
159 |
- |
|
160 |
-=head3 C<execute> |
|
161 |
- |
|
162 |
-If you want to execute SQL, use C<execute> method. |
|
163 |
- |
|
164 |
- $dbi->execute("select * from book;"); |
|
165 |
- |
|
166 |
-You can specify named placeholder. |
|
167 |
- |
|
168 |
- $dbi->execute( |
|
169 |
- "select * from book title = :title and author = :author;" |
|
170 |
- {title => 'Perl', author => 'Ken'} |
|
171 |
- ); |
|
172 |
- |
|
173 |
-:title and :author is named placeholder, which is replaced to placeholers. |
|
174 |
- |
|
175 |
- select * from book title = ? and author = ?; |
|
176 |
- |
|
177 |
-=head3 C<dbh> |
|
178 |
- |
|
179 |
- my $dbh = $dbi->dbh; |
|
180 |
- |
|
181 |
-Get get database handle object of L<DBI>. |
|
182 |
- |
|
183 |
-=head3 C<DBI> methods |
|
184 |
- |
|
185 |
- $dbi->do(...); |
|
186 |
- $dbi->begin_work; |
|
187 |
- |
|
188 |
-You can call all methods of L<DBI> from L<DBIx::Custom> object. |
|
189 |
- |
|
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> |
|
196 |
- |
|
197 |
- my $row = $result->fetch; |
|
198 |
- |
|
199 |
-C<fetch> method fetch a row and put it into array reference. |
|
200 |
-You can continue to fetch |
|
201 |
- |
|
202 |
- while (my $row = $result->fetch) { |
|
203 |
- my $title = $row->[0]; |
|
204 |
- my $author = $row->[1]; |
|
205 |
- } |
|
206 |
- |
|
207 |
-=head3 Fetch only first row (array) : C<fetch_first> |
|
208 |
- |
|
209 |
- my $row = $result->fetch_first; |
|
210 |
- |
|
211 |
-C<fetch_first> fetch a only first row and finish statment handle, |
|
212 |
-and put it into array refrence. |
|
213 |
- |
|
214 |
-=head3 Fetch all rows (array) : C<fetch_all> |
|
215 |
- |
|
216 |
- my $rows = $result->fetch_all; |
|
217 |
- |
|
218 |
-C<fetch_all> fetch all rows and put them into array of array reference. |
|
219 |
- |
|
220 |
-=head3 Fetch a row (hash) : C<fetch_hash> |
|
221 |
- |
|
222 |
- my $row = $result->fetch_hash; |
|
223 |
- |
|
224 |
-C<fetch_hash> fetch a row and put it into hash reference. |
|
225 |
-You can fetch a row while row exists. |
|
226 |
- |
|
227 |
- while (my $row = $result->fetch_hash) { |
|
228 |
- my $title = $row->{title}; |
|
229 |
- my $author = $row->{author}; |
|
230 |
- } |
|
231 |
- |
|
232 |
-=head3 Fetch only a first row (hash) : C<fetch_hash_first> |
|
233 |
- |
|
234 |
- my $row = $result->fetch_hash_first; |
|
235 |
- |
|
236 |
-C<fetch_hash_first> fetch only a first row and finish statement handle, |
|
237 |
-and put them into hash refrence. |
|
238 |
- |
|
239 |
-C<one> is C<fetch_hash_first> synonym to save word typing. |
|
240 |
- |
|
241 |
- my $row = $result->one; |
|
242 |
- |
|
243 |
-=head3 Fetch all rows (hash) : C<fetch_hash_all> |
|
244 |
- |
|
245 |
- my $rows = $result->fetch_hash_all; |
|
246 |
- |
|
247 |
-C<fetch_hash_all> fetch all rows and put them into array of hash reference. |
|
248 |
- |
|
249 |
-=head3 Statement Handle : C<sth> |
|
250 |
- |
|
251 |
- my $sth = $result->sth; |
|
252 |
- |
|
253 |
-If you want to get statment handle, use <sth> method. |
|
254 |
- |
|
255 |
-=head2 Named placeholder |
|
256 |
- |
|
257 |
-=head3 Basic of Parameter |
|
258 |
- |
|
259 |
-You can embedd named placeholder into SQL. |
|
260 |
- |
|
261 |
- select * from book where title = :title and author like :author; |
|
262 |
- |
|
263 |
-:title and :author is named placeholder |
|
264 |
- |
|
265 |
-Named placeholder is replaced by place holder. |
|
266 |
- |
|
267 |
- select * from book where title = ? and author like ?; |
|
268 |
- |
|
269 |
-use C<execute> to execute SQL. |
|
270 |
- |
|
271 |
- my $sql = "select * from book where title = :title and author like :author;" |
|
272 |
- $dbi->execute($sql, {title => 'Perl', author => '%Ken%'}); |
|
273 |
- |
|
274 |
-You can specify C<filter> at C<execute>. |
|
275 |
- |
|
276 |
- $dbi->execute($sql, {title => 'Perl', author => '%Ken%'} |
|
277 |
- filter => {title => 'to_something'); |
|
278 |
- |
|
279 |
-=head3 Manipulate same name's columns |
|
280 |
- |
|
281 |
-It is ok if there are same name's columns. |
|
282 |
-Let's think two date comparison. |
|
283 |
- |
|
284 |
- my $sql = "select * from table where date > :date and date < :date;"; |
|
285 |
- |
|
286 |
-In this case, You specify parameter values as array reference. |
|
287 |
- |
|
288 |
- my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']}); |
|
289 |
- |
|
290 |
-=head2 Create where clause |
|
291 |
- |
|
292 |
-=head3 Dinamically create where clause : where |
|
293 |
- |
|
294 |
-You want to search multiple conditions in many times. |
|
295 |
-Let's think the following three cases. |
|
296 |
- |
|
297 |
-Case1: Search only C<title> |
|
298 |
- |
|
299 |
- where title = :title |
|
300 |
- |
|
301 |
-Case2: Search only C<author> |
|
302 |
- |
|
303 |
- where author = :author |
|
304 |
- |
|
305 |
-Case3: Search C<title> and C<author> |
|
306 |
- |
|
307 |
- where title = :title and author = :author |
|
308 |
- |
|
309 |
-L<DBIx::Custom> support dinamic where clause creating. |
|
310 |
-At first, create L<DBIx::Custom::Where> object by C<where>. |
|
311 |
- |
|
312 |
- my $where = $dbi->where; |
|
313 |
- |
|
314 |
-Set clause by C<clause> |
|
315 |
- |
|
316 |
- $where->clause( |
|
317 |
- ['and', 'title = :title, 'author = :author'] |
|
318 |
- ); |
|
319 |
- |
|
320 |
-C<clause> is the following format. |
|
321 |
- |
|
322 |
- ['or' or 'and', PART1, PART1, PART1] |
|
323 |
- |
|
324 |
-First argument is 'or' or 'and'. |
|
325 |
-Later than first argument are part which contains named placeholder. |
|
326 |
- |
|
327 |
-You can write more complex format. |
|
328 |
- |
|
329 |
- ['and', |
|
330 |
- 'title = :title', |
|
331 |
- ['or', 'author = :author', 'date like :date'] |
|
332 |
- ] |
|
333 |
- |
|
334 |
-This mean "title = :title and ( author = :author or date like :date )". |
|
335 |
- |
|
336 |
-After setting C<clause>, set C<param>. |
|
337 |
- |
|
338 |
- $where->param({title => 'Perl'}); |
|
339 |
- |
|
340 |
-In this example, parameter contains only title. |
|
341 |
- |
|
342 |
-If you execute C<string_to>, you can get where clause |
|
343 |
-which contain only named placeholder. |
|
344 |
- |
|
345 |
- my $where_clause = $where->to_string; |
|
346 |
- |
|
347 |
-Parameter name is only title, the following where clause is created. |
|
348 |
- |
|
349 |
- where title = :title |
|
350 |
- |
|
351 |
-You can also create where clause by stringification. |
|
352 |
- |
|
353 |
- my $where_clause = "$where"; |
|
354 |
- |
|
355 |
-This is useful to embbed it into SQL. |
|
356 |
- |
|
357 |
-=head3 In case where clause contains same name columns |
|
358 |
- |
|
359 |
-Even if same name parameters exists, you can create where clause. |
|
360 |
-Let's think that there are starting date and ending date. |
|
361 |
- |
|
362 |
- my $param = {start_date => '2010-11-15', end_date => '2011-11-21'}; |
|
363 |
- |
|
364 |
-In this case, you set parameter value as array reference. |
|
365 |
- |
|
366 |
- my $p = {date => ['2010-11-15', '2011-11-21']}; |
|
367 |
- |
|
368 |
-You can embbed these values into same name parameters. |
|
369 |
- |
|
370 |
- $where->clause( |
|
371 |
- ['and', 'date > :date', 'date < :date'] |
|
372 |
- ); |
|
373 |
- $where->param($p); |
|
374 |
- |
|
375 |
-If starting date isn't exists, create the following parameter. |
|
376 |
- |
|
377 |
- my $p = {date => [$dbi->not_exists, '2011-11-21']}; |
|
378 |
- |
|
379 |
-You can get DBIx::Custom::NotExists object by C<not_exists> |
|
380 |
-This mean correnspondinf value isn't exists. |
|
381 |
- |
|
382 |
-If ending date isn't exists, create the following parameter. |
|
383 |
- |
|
384 |
- my $p = {date => ['2010-11-15']}; |
|
385 |
- |
|
386 |
-If both date isn't exists, create the following parameter. |
|
387 |
- |
|
388 |
- my $p = {date => []}; |
|
389 |
- |
|
390 |
-This logic is a little difficut. See the following ones. |
|
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 |
- |
|
398 |
-=head3 With C<select> |
|
399 |
- |
|
400 |
-You can pass L<DBIx::Custom::Where> object to C<where> of C<select>. |
|
401 |
- |
|
402 |
- my $where = $dbi->where; |
|
403 |
- $where->clause(['and', 'title = :title', 'author = :author']); |
|
404 |
- $where->param({title => 'Perl'}); |
|
405 |
- my $result = $dbi->select(table => 'book', where => $where); |
|
406 |
- |
|
407 |
-You can also pass it to C<where> of C<update>AC<delete> |
|
408 |
- |
|
409 |
-=head3 With C<execute> |
|
410 |
- |
|
411 |
-L<DBIx::Custom::Where> object is embedded into SQL. |
|
412 |
- |
|
413 |
- my $where = $dbi->where; |
|
414 |
- $where->clause(['and', 'title = :title', 'author = :author']); |
|
415 |
- $where->param({title => 'Perl'}); |
|
416 |
- |
|
417 |
- my $sql = <<"EOS"; |
|
418 |
- select * from book; |
|
419 |
- $where |
|
420 |
- EOS |
|
421 |
- |
|
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; |
|
469 |
- |
|
470 |
-=head2 7. Model |
|
471 |
- |
|
472 |
-=head3 Model |
|
473 |
- |
|
474 |
-you can define model extending L<DBIx::Custom::Model> |
|
475 |
-to improve source code view. |
|
476 |
- |
|
477 |
-At first, you create basic model class extending <DBIx::Custom::Model>. |
|
478 |
-Each L<DBIx::Custom> class inherit L<Object::Simple>. |
|
479 |
-so you can inherit the following way. |
|
480 |
- |
|
481 |
- package MyModel; |
|
482 |
- use DBIx::Custom::Model -base; |
|
483 |
- |
|
484 |
-Next, you create each model classes. |
|
485 |
- |
|
486 |
-MyModel::book |
|
487 |
- |
|
488 |
- package MyModel::book; |
|
489 |
- use MyModel -base; |
|
490 |
- |
|
491 |
- sub insert { ... } |
|
492 |
- sub list { ... } |
|
493 |
- |
|
494 |
-MyModel::company |
|
495 |
- |
|
496 |
- package MyModel::company; |
|
497 |
- use MyModel -base; |
|
498 |
- |
|
499 |
- sub insert { ... } |
|
500 |
- sub list { ... } |
|
501 |
- |
|
502 |
-The follwoing modules location is needed. |
|
503 |
- |
|
504 |
- MyModel.pm |
|
505 |
- MyModel / book.pm |
|
506 |
- / company.pm |
|
507 |
- |
|
508 |
-You can include these models by C<include_model> |
|
509 |
- |
|
510 |
- $dbi->include_model('MyModel'); |
|
511 |
- |
|
512 |
-First argument is name space of model. |
|
513 |
- |
|
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, |
|
519 |
-C<insert>, C<update>, C<update_all>, |
|
520 |
-C<delete>, C<delete_all>, C<select> |
|
521 |
-without C<table> option. |
|
522 |
- |
|
523 |
- $dbi->model('book')->insert($param); |
|
524 |
- |
|
525 |
-Model is L<DBIx::Custom::Model>. |
|
526 |
- |
|
527 |
-If you need table nameAyou can get it by C<table>. |
|
528 |
- |
|
529 |
- my $table = $model->table; |
|
530 |
- |
|
531 |
-You can get L<DBIx::Custom>. |
|
532 |
- |
|
533 |
- my $dbi = $model->dbi; |
|
534 |
- |
|
535 |
-You can also call all methods of L<DBIx::Custom> and L<DBI>. |
|
536 |
- |
|
537 |
- # DBIx::Custom method |
|
538 |
- $model->execute($sql); |
|
539 |
- |
|
540 |
- # DBI method |
|
541 |
- $model->begin_work; |
|
542 |
- $model->commit; |
|
543 |
- |
|
544 |
-If you want to get all models, you can get them by keys of C<models>. |
|
545 |
- |
|
546 |
- my @models = keys %{$self->models}; |
|
547 |
- |
|
548 |
-You can set primary key to model. |
|
549 |
- |
|
550 |
- $model->primary_key(['id', 'number_id']); |
|
551 |
- |
|
552 |
-Primary key is used by C<insert>, C<update>, C<delete>, |
|
553 |
-and C<select> methods. |
|
554 |
- |
|
555 |
-You can set column names |
|
556 |
- |
|
557 |
- $model->columns(['id', 'number_id']); |
|
558 |
- |
|
559 |
-Column names is automarically set by C<setup_model>. |
|
560 |
-This method is needed to be call after C<include_model>. |
|
561 |
- |
|
562 |
- $dbi->setup_model; |
|
563 |
- |
|
564 |
-You can set C<join> |
|
565 |
- |
|
566 |
- $model->join(['left outer join company on book.company_id = company.id']); |
|
567 |
- |
|
568 |
-C<join> is used by C<select> method. |
|
569 |
- |
|
570 |
-=head2 Create column clause automatically : mycolumn, column |
|
571 |
- |
|
572 |
-To create column clause automatically, use C<mycolumn>. |
|
573 |
-Valude of C<table> and C<columns> is used. |
|
574 |
- |
|
575 |
- my $mycolumns = $model->mycolumn; |
|
576 |
- |
|
577 |
-If C<table> is 'book'AC<column> is ['id', 'name'], |
|
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 |
- |
|
586 |
- my $columns = $model->column('company'); |
|
587 |
- |
|
588 |
-If C<table> is "company", C<column> return ['id', 'name'], |
|
589 |
-the following clause is created. |
|
590 |
- |
|
591 |
- company.id as "company.id", company.name as "company.name" |
|
592 |
- |
|
593 |
-=head2 Model Examples |
|
594 |
- |
|
595 |
-Model examples |
|
596 |
- |
|
597 |
- package MyDBI; |
|
598 |
- use DBIx::Custom -base; |
|
599 |
- |
|
600 |
- sub connect { |
|
601 |
- my $self = shift->SUPER::connect(@_); |
|
602 |
- |
|
603 |
- $self->include_model( |
|
604 |
- MyModel => [ |
|
605 |
- 'book', |
|
606 |
- 'company' |
|
607 |
- ] |
|
608 |
- ); |
|
609 |
- } |
|
610 |
- |
|
611 |
- package MyModel::book; |
|
612 |
- use DBIx::Custom::Model -base; |
|
613 |
- |
|
614 |
- has primary_key => sub { ['id'] }; |
|
615 |
- |
|
616 |
- sub insert { ... } |
|
617 |
- sub list { ... } |
|
618 |
- |
|
619 |
- package MyModel::company; |
|
620 |
- use DBIx::Custom::Model -base; |
|
5 |
+=head1 LINK |
|
621 | 6 |
|
622 |
- has primary_key => sub { ['id'] }; |
|
623 |
- |
|
624 |
- sub insert { ... } |
|
625 |
- sub list { ... } |
|
7 |
+Guide is moved to the following page. |
|
626 | 8 |
|
627 |
-=cut |
|
9 |
+L<https://github.com/yuki-kimoto/DBIx-Custom/wiki/DBIx%3A%3ACustom-Guide> |