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