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