add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
updatedd pod
|
2 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
3 | |
4 |
use Carp 'croak'; |
|
cleanup
|
5 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
6 | |
added experimental DBIx::Cus...
|
7 |
# Carp trust relationship |
8 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
9 | ||
added EXPERIMENTAL DBIx::Cus...
|
10 |
has [qw/dbi table created_at updated_at bind_type join primary_key/], |
11 |
columns => sub { [] }; |
|
added experimental DBIx::Cus...
|
12 | |
- added EXPERIMENTAL DBIx::C...
|
13 |
our $AUTOLOAD; |
14 | ||
15 |
sub AUTOLOAD { |
|
16 |
my $self = shift; |
|
17 | ||
18 |
# Method name |
|
19 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
20 | ||
21 |
# Method |
|
adeed EXPERIMENTAL DBIx::Cus...
|
22 |
$self->{_methods} ||= {}; |
23 |
if (my $method = $self->{_methods}->{$mname}) { |
|
24 |
return $self->$method(@_) |
|
25 |
} |
|
26 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
- added EXPERIMENTAL DBIx::C...
|
27 |
$self->dbi->$dbi_method(@_); |
28 |
} |
|
- removed EXPERIMENTAL Prefo...
|
29 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
30 |
$self->dbi->dbh->$dbh_method(@_); |
31 |
} |
|
32 |
else { |
|
cleanup
|
33 |
croak qq{Can't locate object method "$mname" via "$package" } |
34 |
. _subname; |
|
- added EXPERIMENTAL DBIx::C...
|
35 |
} |
36 |
} |
|
37 | ||
- added EXPERIMENTAL type() ...
|
38 |
my @methods = qw/insert insert_at update update_at update_all |
fixed update_or_insert metho...
|
39 |
delete delete_at delete_all select select_at count/; |
cleanup
|
40 |
for my $method (@methods) { |
micro optimization
|
41 |
|
42 |
my $code = |
|
43 |
qq/sub {/ . |
|
44 |
qq/my \$self = shift;/ . |
|
45 |
qq/\$self->dbi->$method(/ . |
|
46 |
qq/\@_ % 2 ? shift : (),/; |
|
- added EXPERIMENTAL type() ...
|
47 | |
micro optimization
|
48 |
|
49 |
my @attrs = qw/table type primary_key bind_type/; |
|
50 |
my @insert_attrs = qw/created_at updated_at/; |
|
51 |
my @update_attrs = qw/updated_at/; |
|
52 |
my @select_attrs = qw/join/; |
|
53 |
if ($method eq 'insert') { push @attrs, @insert_attrs } |
|
54 |
elsif ($method eq 'update') { push @attrs, @update_attrs } |
|
- fixed bug that DBIx::Custo...
|
55 |
elsif (index($method, 'select') != -1 || $method eq 'count') { |
56 |
push @attrs, @select_attrs |
|
57 |
} |
|
micro optimization
|
58 |
|
59 |
for my $attr (@attrs) { |
|
micro optimization
|
60 |
$code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),"; |
micro optimization
|
61 |
} |
62 |
|
|
63 |
$code .= qq/\@_);/ . |
|
64 |
qq/}/; |
|
- added EXPERIMENTAL type() ...
|
65 |
|
66 |
no strict 'refs'; |
|
micro optimization
|
67 |
*{__PACKAGE__ . "::$method"} = eval $code; |
68 |
croak $code if $@; |
|
- added EXPERIMENTAL type() ...
|
69 |
} |
70 | ||
fixed update_or_insert metho...
|
71 |
sub update_or_insert { |
72 |
my ($self, $param, %opt) = @_; |
|
73 |
|
|
74 |
croak "update_or_insert method need primary_key and id option " |
|
75 |
unless (defined $opt{id} || defined $self->{id}) |
|
76 |
&& (defined $opt{primary_key} || defined $self->{primary_key}); |
|
77 |
|
|
78 |
my $statement_opt = $opt{option} || {}; |
|
- id option work if id count...
|
79 |
my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all; |
80 |
if (@$rows == 0) { |
|
81 |
return $self->insert($param, %opt, %{$statement_opt->{insert} || {}}); |
|
82 |
} |
|
83 |
elsif (@$rows == 1) { |
|
84 |
return $self->update($param, %opt, %{$statement_opt->{update} || {}}); |
|
85 |
} |
|
86 |
else { |
|
87 |
croak "selected row must be one " . _subname; |
|
88 |
} |
|
fixed update_or_insert metho...
|
89 |
} |
90 | ||
- removed placeholder count ...
|
91 |
sub execute { |
92 |
my $self = shift; |
|
- DBIx::Custom::QueryBuilder...
|
93 |
|
94 |
if ($ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE}) { |
|
95 |
$self->dbi->execute(@_); |
|
96 |
} |
|
97 |
else { |
|
98 |
warn "DBIx::Custom::Model execute method is DEPRECATED! " . |
|
99 |
"use DBIx::Custom execute method. " . |
|
100 |
"If you want to call DBIx::Custom execute method directory from model, " . |
|
101 |
"set \$ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE} to 1 " . |
|
102 |
"until DBIx::Custom::Model execute method is removed in the future." ; |
|
103 |
return $self->dbi->execute( |
|
104 |
shift, |
|
105 |
shift, |
|
106 |
table => $self->table, |
|
107 |
bind_type => $self->bind_type, |
|
108 |
primary_key => $self->primary_key, |
|
109 |
type => $self->type, |
|
110 |
@_ |
|
111 |
); |
|
112 |
} |
|
- removed placeholder count ...
|
113 |
} |
114 | ||
select method column option ...
|
115 |
sub DESTROY { } |
116 | ||
- method method of DBIx::Cus...
|
117 |
sub helper { |
adeed EXPERIMENTAL DBIx::Cus...
|
118 |
my $self = shift; |
119 |
|
|
120 |
# Merge |
|
121 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
122 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
123 |
|
|
124 |
return $self; |
|
125 |
} |
|
126 | ||
cleanup
|
127 |
sub mycolumn { |
128 |
my $self = shift; |
|
129 |
my $table = shift unless ref $_[0]; |
|
130 |
my $columns = shift; |
|
131 |
|
|
132 |
$table ||= $self->table || ''; |
|
133 |
$columns ||= $self->columns; |
|
134 |
|
|
135 |
return $self->dbi->mycolumn($table, $columns); |
|
136 |
} |
|
137 | ||
removed EXPERIMETNAL flag fr...
|
138 |
sub new { |
139 |
my $self = shift->SUPER::new(@_); |
|
140 |
|
|
141 |
# Check attribute names |
|
142 |
my @attrs = keys %$self; |
|
cleanup
|
143 |
for my $attr (@attrs) { |
cleanup
|
144 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
145 |
unless $self->can($attr); |
146 |
} |
|
147 |
|
|
micro optimization
|
148 |
# Cache |
149 |
for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) { |
|
150 |
$self->$attr; |
|
151 |
$self->{$attr} = undef unless exists $self->{$attr}; |
|
152 |
} |
|
153 |
$self->columns; |
|
154 |
|
|
removed EXPERIMETNAL flag fr...
|
155 |
return $self; |
156 |
} |
|
157 | ||
- DBIx::Custom Model filter ...
|
158 |
# DEPRECATED! |
micro optimization
|
159 |
has 'filter'; |
cleanup
|
160 |
has 'name'; |
added EXPERIMENTAL DBIx::Cus...
|
161 |
has 'type'; |
- DBIx::Custom Model filter ...
|
162 | |
- method method of DBIx::Cus...
|
163 | |
164 |
# DEPRECATED! |
|
165 |
sub method { |
|
166 |
warn "method method is DEPRECATED! use helper instead"; |
|
167 |
return shift->helper(@_); |
|
168 |
} |
|
169 | ||
added experimental DBIx::Cus...
|
170 |
1; |
171 | ||
172 |
=head1 NAME |
|
173 | ||
- removed DEPRECATED DBIx::C...
|
174 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
175 | |
176 |
=head1 SYNOPSIS |
|
177 | ||
- removed placeholder count ...
|
178 |
use DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
179 | |
- removed placeholder count ...
|
180 |
my $model = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
181 | |
add DBIx::Custom::Model fore...
|
182 |
=head1 ATTRIBUTES |
183 | ||
184 |
=head2 C<dbi> |
|
185 | ||
186 |
my $dbi = $model->dbi; |
|
DBIx::Custom::Model type att...
|
187 |
$model = $model->dbi($dbi); |
add DBIx::Custom::Model fore...
|
188 | |
189 |
L<DBIx::Custom> object. |
|
190 | ||
update_or_insert method's re...
|
191 |
=head2 C<created_at EXPERIMENTAL> |
192 | ||
193 |
my $created_at = $model->created_at; |
|
194 |
$model = $model->created_at('created_datatime'); |
|
195 | ||
196 |
Create timestamp column, this is passed to C<insert> or C<update> method. |
|
197 | ||
update pod
|
198 |
=head2 C<join> |
- added experimental DBIx::C...
|
199 | |
200 |
my $join = $model->join; |
|
DBIx::Custom::Model type att...
|
201 |
$model = $model->join( |
- added experimental DBIx::C...
|
202 |
['left outer join company on book.company_id = company.id'] |
203 |
); |
|
204 |
|
|
DBIx::Custom::Model type att...
|
205 |
Join clause, this value is passed to C<select> method. |
- added experimental DBIx::C...
|
206 | |
added DBIx::Custom result_fi...
|
207 |
=head2 C<primary_key> |
208 | ||
209 |
my $primary_key = $model->primary_key; |
|
DBIx::Custom::Model type att...
|
210 |
$model = $model->primary_key(['id', 'number']); |
added DBIx::Custom result_fi...
|
211 | |
DBIx::Custom::Model type att...
|
212 |
Primary key,this is passed to C<insert>, C<update>, |
213 |
C<delete>, and C<select> method. |
|
added DBIx::Custom result_fi...
|
214 | |
add DBIx::Custom::Model fore...
|
215 |
=head2 C<table> |
216 | ||
- removed placeholder count ...
|
217 |
my $model = $model->table; |
DBIx::Custom::Model type att...
|
218 |
$model = $model->table('book'); |
add DBIx::Custom::Model fore...
|
219 | |
DBIx::Custom::Model type att...
|
220 |
Table name, this is passed to C<select> method. |
add experimental DBIx::Custo...
|
221 | |
DBIx::Custom::Model type att...
|
222 |
=head2 C<bind_type> |
- added EXPERIMENTAL type() ...
|
223 | |
DBIx::Custom::Model type att...
|
224 |
my $type = $model->bind_type; |
225 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
- added EXPERIMENTAL type() ...
|
226 |
|
DBIx::Custom::Model type att...
|
227 |
Database data type, this is used as type optioon of C<insert>, |
228 |
C<update>, C<update_all>, C<delete>, C<delete_all>, |
|
cleanup
|
229 |
and C<select> method |
- added EXPERIMENTAL type() ...
|
230 | |
update_or_insert method's re...
|
231 |
=head2 C<updated_at EXPERIMENTAL> |
232 | ||
233 |
my $updated_at = $model->updated_at; |
|
234 |
$model = $model->updated_at('updated_datatime'); |
|
235 | ||
236 |
Updated timestamp column, this is passed to C<update> method. |
|
237 | ||
added experimental DBIx::Cus...
|
238 |
=head1 METHODS |
239 | ||
- DBIx::Custom Model filter ...
|
240 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
241 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
242 |
and implements the following new ones. |
243 | ||
- removed EXPERIMENTAL the f...
|
244 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
245 | |
246 |
my $count = $model->count; |
|
247 | ||
248 |
Get rows count. |
|
249 | ||
250 |
Options is same as C<select> method's ones. |
|
251 | ||
added insert, update, update...
|
252 |
=head2 C<delete> |
253 | ||
- removed placeholder count ...
|
254 |
$model->delete(...); |
added insert, update, update...
|
255 |
|
DBIx::Custom::Model type att...
|
256 |
Same as C<delete> of L<DBIx::Custom> except that |
micro optimization
|
257 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
258 | |
259 |
=head2 C<delete_all> |
|
260 | ||
- removed placeholder count ...
|
261 |
$model->delete_all(...); |
added insert, update, update...
|
262 |
|
DBIx::Custom::Model type att...
|
263 |
Same as C<delete_all> of L<DBIx::Custom> except that |
micro optimization
|
264 |
you don't have to specify options if you set attribute in model. |
- removed placeholder count ...
|
265 | |
added insert, update, update...
|
266 |
=head2 C<insert> |
267 | ||
- removed placeholder count ...
|
268 |
$model->insert(...); |
added insert, update, update...
|
269 |
|
DBIx::Custom::Model type att...
|
270 |
Same as C<insert> of L<DBIx::Custom> except that |
micro optimization
|
271 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
272 | |
- method method of DBIx::Cus...
|
273 |
=head2 C<helper> |
adeed EXPERIMENTAL DBIx::Cus...
|
274 | |
- method method of DBIx::Cus...
|
275 |
$model->helper( |
adeed EXPERIMENTAL DBIx::Cus...
|
276 |
update_or_insert => sub { |
277 |
my $self = shift; |
|
278 |
|
|
279 |
# ... |
|
280 |
}, |
|
281 |
find_or_create => sub { |
|
282 |
my $self = shift; |
|
283 |
|
|
284 |
# ... |
|
285 |
); |
|
286 | ||
- method method of DBIx::Cus...
|
287 |
Register helper. These helper is called directly from L<DBIx::Custom::Model> object. |
adeed EXPERIMENTAL DBIx::Cus...
|
288 | |
289 |
$model->update_or_insert; |
|
290 |
$model->find_or_create; |
|
291 | ||
- added EXPERIMENTAL type() ...
|
292 |
=head2 C<mycolumn> |
cleanup
|
293 | |
294 |
my $column = $self->mycolumn; |
|
295 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
296 |
my $column = $self->mycolumn(['author', 'title']); |
|
297 | ||
298 |
Create column clause for myself. The follwoing column clause is created. |
|
299 | ||
300 |
book.author as author, |
|
301 |
book.title as title |
|
302 | ||
303 |
If table name is ommited, C<table> attribute of the model is used. |
|
304 |
If column names is omitted, C<columns> attribute of the model is used. |
|
305 | ||
added insert, update, update...
|
306 |
=head2 C<new> |
307 | ||
- removed placeholder count ...
|
308 |
my $model = DBIx::Custom::Model->new; |
added insert, update, update...
|
309 | |
- removed placeholder count ...
|
310 |
Create a L<DBIx::Custom::Model> object. |
added insert, update, update...
|
311 | |
312 |
=head2 C<select> |
|
313 | ||
- removed placeholder count ...
|
314 |
$model->select(...); |
added insert, update, update...
|
315 |
|
DBIx::Custom::Model type att...
|
316 |
Same as C<select> of L<DBIx::Custom> except that |
micro optimization
|
317 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
318 | |
319 |
=head2 C<update> |
|
320 | ||
- removed placeholder count ...
|
321 |
$model->update(...); |
added insert, update, update...
|
322 |
|
DBIx::Custom::Model type att...
|
323 |
Same as C<update> of L<DBIx::Custom> except that |
micro optimization
|
324 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
325 | |
326 |
=head2 C<update_all> |
|
327 | ||
- removed placeholder count ...
|
328 |
$model->update_all(param => \%param); |
added insert, update, update...
|
329 |
|
DBIx::Custom::Model type att...
|
330 |
Same as C<update_all> of L<DBIx::Custom> except that |
micro optimization
|
331 |
you don't have to specify options if you set attribute in model. |
332 | ||
- id option work if id count...
|
333 |
=head2 C<update_or_insert> |
micro optimization
|
334 | |
335 |
$model->update_or_insert(...); |
|
336 |
|
|
337 |
Same as C<update> of L<DBIx::Custom> except that |
|
338 |
you don't have to specify options if you set attribute in model. |
|
update pod
|
339 | |
update pod
|
340 |
=cut |