add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
- added EXPERIMENTAL DBIx::C...
|
6 |
use base 'Object::Simple'; |
added experimental DBIx::Cus...
|
7 | |
8 |
use Carp 'croak'; |
|
9 | ||
added experimental DBIx::Cus...
|
10 |
# Carp trust relationship |
11 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
12 | ||
add DBIx::Custom::Model fore...
|
13 |
__PACKAGE__->attr( |
- added EXPERIMENTAL DBIx::C...
|
14 |
['dbi', 'name', 'table', 'view'], |
- added EXPERIMENTAL DBIx::C...
|
15 |
table_alias => sub { {} }, |
add DBIx::Custom::Model colu...
|
16 |
columns => sub { [] }, |
all filter can receive array...
|
17 |
filter => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
18 |
join => sub { [] }, |
19 |
primary_key => sub { [] } |
|
add DBIx::Custom::Model fore...
|
20 |
); |
added experimental DBIx::Cus...
|
21 | |
- added EXPERIMENTAL DBIx::C...
|
22 |
our $AUTOLOAD; |
23 | ||
24 |
sub AUTOLOAD { |
|
25 |
my $self = shift; |
|
26 | ||
27 |
# Method name |
|
28 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
29 | ||
30 |
# Method |
|
31 |
if (my $dbi_method = $self->dbi->can($mname)) { |
|
32 |
$self->dbi->$dbi_method(@_); |
|
33 |
} |
|
34 |
elsif (my $dbh_method = $self->dbi->dbh->can($mname)) { |
|
35 |
$self->dbi->dbh->$dbh_method(@_); |
|
36 |
} |
|
37 |
else { |
|
38 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
39 |
} |
|
40 |
} |
|
41 | ||
select method column option ...
|
42 |
sub column_clause { |
added experimental DBIx::Cus...
|
43 |
my $self = shift; |
44 |
|
|
select method column option ...
|
45 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
added experimental DBIx::Cus...
|
46 |
|
select method column option ...
|
47 |
my $table = $self->table; |
48 |
my $columns = $self->columns; |
|
49 |
my $add = $args->{add} || []; |
|
50 |
my $remove = $args->{remove} || []; |
|
51 |
my %remove = map {$_ => 1} @$remove; |
|
- added experimental DBIx::C...
|
52 |
my $prefix = $args->{prefix} || ''; |
select method column option ...
|
53 |
|
54 |
my @column; |
|
55 |
foreach my $column (@$columns) { |
|
- added experimental DBIx::C...
|
56 |
push @column, "$table.$column as $prefix$column" |
select method column option ...
|
57 |
unless $remove{$column}; |
58 |
} |
|
59 |
|
|
60 |
foreach my $column (@$add) { |
|
61 |
push @column, $column; |
|
62 |
} |
|
63 |
|
|
64 |
return join (', ', @column); |
|
added experimental DBIx::Cus...
|
65 |
} |
66 | ||
- added EXPERIMENTAL DBIx::C...
|
67 |
sub mycolumn { |
68 |
my ($self, $columns) = @_; |
|
69 |
|
|
70 |
my $table = $self->table || ''; |
|
71 |
$columns ||= $self->columns; |
|
72 |
|
|
73 |
my @column; |
|
74 |
push @column, "$table.$_ as $_" for @$columns; |
|
75 |
|
|
76 |
return join (', ', @column); |
|
77 |
} |
|
78 | ||
79 |
sub column { |
|
80 |
my ($self, $table, $columns) = @_; |
|
81 |
|
|
82 |
$self->{_table_alias} ||= {}; |
|
83 |
my $dist; |
|
84 |
$dist = $self->dbi->{_table_alias}{$table} |
|
85 |
? $self->dbi->{_table_alias}{$table} |
|
86 |
: $table; |
|
87 |
|
|
88 |
$self->dbi->{_model_from} ||= {}; |
|
89 |
my $model = $self->dbi->{_model_from}->{$dist}; |
|
90 |
|
|
91 |
$columns ||= $self->model($model)->columns; |
|
92 |
|
|
93 |
my @column; |
|
94 |
push @column, "$table.$_ as ${table}__$_" for @$columns; |
|
95 |
|
|
96 |
return join (', ', @column); |
|
97 |
} |
|
98 | ||
select method column option ...
|
99 |
sub delete { |
DBIx::Custom::Model select()...
|
100 |
my $self = shift; |
select method column option ...
|
101 |
$self->dbi->delete(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
102 |
} |
103 | ||
select method column option ...
|
104 |
sub delete_all { |
DBIx::Custom::Model select()...
|
105 |
my $self = shift; |
select method column option ...
|
106 |
$self->dbi->delete_all(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
107 |
} |
108 | ||
select method column option ...
|
109 |
sub delete_at { |
DBIx::Custom::Model select()...
|
110 |
my $self = shift; |
select method column option ...
|
111 |
|
112 |
return $self->dbi->delete_at( |
|
113 |
table => $self->table, |
|
114 |
primary_key => $self->primary_key, |
|
115 |
@_ |
|
116 |
); |
|
DBIx::Custom::Model select()...
|
117 |
} |
118 | ||
select method column option ...
|
119 |
sub DESTROY { } |
120 | ||
121 |
sub insert { |
|
DBIx::Custom::Model select()...
|
122 |
my $self = shift; |
select method column option ...
|
123 |
$self->dbi->insert(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
124 |
} |
125 | ||
- added experimental DBIx::C...
|
126 |
sub insert_at { |
127 |
my $self = shift; |
|
128 |
|
|
129 |
return $self->dbi->insert_at( |
|
130 |
table => $self->table, |
|
131 |
primary_key => $self->primary_key, |
|
132 |
@_ |
|
133 |
); |
|
134 |
} |
|
135 | ||
DBIx::Custom::Model select()...
|
136 |
sub select { |
137 |
my $self = shift; |
|
138 |
$self->dbi->select( |
|
139 |
table => $self->table, |
|
- added experimental DBIx::C...
|
140 |
join => $self->join, |
DBIx::Custom::Model select()...
|
141 |
@_ |
142 |
); |
|
143 |
} |
|
add experimental DBIx::Custo...
|
144 | |
select method column option ...
|
145 |
sub select_at { |
add experimental DBIx::Custo...
|
146 |
my $self = shift; |
added insert, update, update...
|
147 |
|
select method column option ...
|
148 |
return $self->dbi->select_at( |
add experimental DBIx::Custo...
|
149 |
table => $self->table, |
150 |
primary_key => $self->primary_key, |
|
- added experimental DBIx::C...
|
151 |
join => $self->join, |
add experimental DBIx::Custo...
|
152 |
@_ |
153 |
); |
|
154 |
} |
|
155 | ||
select method column option ...
|
156 |
sub update { |
add experimental DBIx::Custo...
|
157 |
my $self = shift; |
select method column option ...
|
158 |
$self->dbi->update(table => $self->table, @_) |
add experimental DBIx::Custo...
|
159 |
} |
160 | ||
select method column option ...
|
161 |
sub update_all { |
162 |
my $self = shift; |
|
163 |
$self->dbi->update_all(table => $self->table, @_); |
|
164 |
} |
|
165 | ||
166 |
sub update_at { |
|
add experimental DBIx::Custo...
|
167 |
my $self = shift; |
many changed
|
168 |
|
select method column option ...
|
169 |
return $self->dbi->update_at( |
add experimental DBIx::Custo...
|
170 |
table => $self->table, |
171 |
primary_key => $self->primary_key, |
|
172 |
@_ |
|
173 |
); |
|
added insert, update, update...
|
174 |
} |
175 | ||
added experimental DBIx::Cus...
|
176 |
1; |
177 | ||
178 |
=head1 NAME |
|
179 | ||
update pod
|
180 |
DBIx::Custom::Model - Model EXPERIMENTAL |
added experimental DBIx::Cus...
|
181 | |
182 |
=head1 SYNOPSIS |
|
183 | ||
184 |
use DBIx::Custom::Table; |
|
185 | ||
add feture. all model class ...
|
186 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
187 | |
add DBIx::Custom::Model fore...
|
188 |
=head1 ATTRIBUTES |
189 | ||
190 |
=head2 C<dbi> |
|
191 | ||
192 |
my $dbi = $model->dbi; |
|
193 |
$model = $model->dbi($dbi); |
|
194 | ||
195 |
L<DBIx::Custom> object. |
|
196 | ||
add experimental DBIx::Custo...
|
197 |
=head2 C<filter> |
198 | ||
199 |
my $dbi = $model->filter |
|
200 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
201 | ||
- added EXPERIMENTAL DBIx::C...
|
202 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
203 | |
add experimental DBIx::Custo...
|
204 |
=head2 C<name> |
205 | ||
206 |
my $name = $model->name; |
|
207 |
$model = $model->name('book'); |
|
208 | ||
209 |
Model name. |
|
210 | ||
update pod
|
211 |
=head2 C<join> |
- added experimental DBIx::C...
|
212 | |
213 |
my $join = $model->join; |
|
214 |
$model = $model->join( |
|
215 |
['left outer join company on book.company_id = company.id'] |
|
216 |
); |
|
217 |
|
|
- added EXPERIMENTAL DBIx::C...
|
218 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
219 | |
add DBIx::Custom::Model fore...
|
220 |
=head2 C<table> |
221 | ||
222 |
my $table = $model->table; |
|
223 |
$model = $model->table('book'); |
|
224 | ||
- added EXPERIMENTAL DBIx::C...
|
225 |
Table name, this is used as C<select()> C<table> option. |
226 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
227 | |
add DBIx::Custom::Model fore...
|
228 |
=head2 C<primary_key> |
229 | ||
230 |
my $primary_key = $model->primary_key; |
|
231 |
$model = $model->primary_key(['id', 'number']); |
|
232 | ||
- added EXPERIMENTAL DBIx::C...
|
233 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
update pod
|
234 |
C<delete_at()>,C<select_at()>. |
add DBIx::Custom::Model fore...
|
235 | |
- added EXPERIMENTAL DBIx::C...
|
236 |
=head2 C<view> |
237 | ||
238 |
my $view = $model->view; |
|
239 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
240 | ||
241 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
242 |
model is included by C<include_model>. |
|
243 | ||
added experimental DBIx::Cus...
|
244 |
=head1 METHODS |
245 | ||
table object call dbi object...
|
246 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
247 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
248 |
and implements the following new ones. |
249 | ||
add experimental DBIx::Custo...
|
250 |
=head2 C<column_clause()> |
251 | ||
252 |
To create column clause automatically, use C<column_clause()>. |
|
253 |
Valude of C<table> and C<columns> is used. |
|
254 | ||
255 |
my $column_clause = $model->column_clause; |
|
256 | ||
257 |
If C<table> is 'book'�AC<column> is ['id', 'name'], |
|
258 |
the following clause is created. |
|
259 | ||
260 |
book.id as id, book.name as name |
|
261 | ||
262 |
These column name is for removing column name ambiguities. |
|
263 | ||
264 |
If you remove some columns, use C<remove> option. |
|
265 | ||
266 |
my $column_clause = $model->column_clause(remove => ['id']); |
|
267 | ||
268 |
If you add some column, use C<add> option. |
|
269 | ||
270 |
my $column_clause = $model->column_clause(add => ['company.id as company__id']); |
|
271 | ||
- added experimental DBIx::C...
|
272 |
If you add column name prefix, use C<prefix> option |
273 | ||
274 |
my $column_clause = $model->column_clause(prefix => 'book__'); |
|
275 | ||
276 |
The following clause is created. |
|
277 | ||
278 |
book.id as book__id, book.name as book__name |
|
279 | ||
added insert, update, update...
|
280 |
=head2 C<delete> |
281 | ||
table object call dbi object...
|
282 |
$table->delete(...); |
added insert, update, update...
|
283 |
|
284 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
285 |
you don't have to specify C<table> option. |
added insert, update, update...
|
286 | |
287 |
=head2 C<delete_all> |
|
288 | ||
table object call dbi object...
|
289 |
$table->delete_all(...); |
added insert, update, update...
|
290 |
|
291 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
292 |
you don't have to specify C<table> option. |
added insert, update, update...
|
293 | |
update pod
|
294 |
=head2 C<delete_at> |
295 | ||
296 |
$table->delete_at(...); |
|
297 |
|
|
298 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
299 |
you don't have to specify C<table> and C<primary_key> option. |
|
300 | ||
added insert, update, update...
|
301 |
=head2 C<insert> |
302 | ||
table object call dbi object...
|
303 |
$table->insert(...); |
added insert, update, update...
|
304 |
|
305 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
306 |
you don't have to specify C<table> option. |
added insert, update, update...
|
307 | |
update pod
|
308 |
=head2 C<insert> |
309 | ||
310 |
$table->insert_at(...); |
|
311 |
|
|
312 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
313 |
you don't have to specify C<table> and C<primary_key> option. |
|
314 | ||
added insert, update, update...
|
315 |
=head2 C<new> |
316 | ||
317 |
my $table = DBIx::Custom::Table->new; |
|
318 | ||
319 |
Create a L<DBIx::Custom::Table> object. |
|
320 | ||
321 |
=head2 C<select> |
|
322 | ||
table object call dbi object...
|
323 |
$table->select(...); |
added insert, update, update...
|
324 |
|
325 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
326 |
you don't have to specify C<table> option. |
added insert, update, update...
|
327 | |
update pod
|
328 |
=head2 C<select_at> |
329 | ||
330 |
$table->select_at(...); |
|
331 |
|
|
332 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
333 |
you don't have to specify C<table> and C<primary_key> option. |
|
334 | ||
added insert, update, update...
|
335 |
=head2 C<update> |
336 | ||
table object call dbi object...
|
337 |
$table->update(...); |
added insert, update, update...
|
338 |
|
339 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
340 |
you don't have to specify C<table> option. |
added insert, update, update...
|
341 | |
342 |
=head2 C<update_all> |
|
343 | ||
344 |
$table->update_all(param => \%param); |
|
345 |
|
|
346 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
347 |
you don't have to specify table name. |
|
update pod
|
348 | |
349 |
=head2 C<update_at> |
|
350 | ||
351 |
$table->update_at(...); |
|
352 |
|
|
353 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
354 |
you don't have to specify C<table> and C<primary_key> option. |