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'; |
|
- added DBIX_CUSTOM_SUPPRESS...
|
5 |
use DBIx::Custom::Util qw/_subname _deprecate/; |
added experimental DBIx::Cus...
|
6 | |
added experimental DBIx::Cus...
|
7 |
# Carp trust relationship |
8 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
9 | ||
- "created_at" option is ren...
|
10 |
has [qw/dbi table ctime mtime bind_type join primary_key/], |
cleanup
|
11 |
columns => sub { [] }; |
added experimental DBIx::Cus...
|
12 | |
- added EXPERIMENTAL DBIx::C...
|
13 |
our $AUTOLOAD; |
14 | ||
15 |
sub AUTOLOAD { |
|
cleanup
|
16 |
my $self = shift; |
17 | ||
18 |
# Method name |
|
19 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
20 | ||
21 |
# Method |
|
22 |
$self->{_methods} ||= {}; |
|
23 |
if (my $method = $self->{_methods}->{$mname}) { |
|
24 |
return $self->$method(@_) |
|
25 |
} |
|
26 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
27 |
$self->dbi->$dbi_method(@_); |
|
28 |
} |
|
29 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
|
30 |
$self->dbi->dbh->$dbh_method(@_); |
|
31 |
} |
|
32 |
else { |
|
33 |
croak qq{Can't locate object method "$mname" via "$package" } |
|
34 |
. _subname; |
|
35 |
} |
|
- added EXPERIMENTAL DBIx::C...
|
36 |
} |
37 | ||
- added EXPERIMENTAL type() ...
|
38 |
my @methods = qw/insert insert_at update update_at update_all |
cleanup
|
39 |
delete delete_at delete_all select select_at count/; |
cleanup
|
40 |
for my $method (@methods) { |
cleanup
|
41 |
|
42 |
my $code = |
|
43 |
qq/sub {/ . |
|
44 |
qq/my \$self = shift;/ . |
|
45 |
qq/\$self->dbi->$method(/ . |
|
46 |
qq/\@_ % 2 ? shift : (),/; |
|
47 | ||
48 |
|
|
49 |
my @attrs = qw/table type primary_key bind_type/; |
|
- "created_at" option is ren...
|
50 |
my @insert_attrs = qw/created_at updated_at ctime mtime/; |
51 |
my @update_attrs = qw/updated_at mtime/; |
|
cleanup
|
52 |
my @select_attrs = qw/join/; |
53 |
if ($method eq 'insert') { push @attrs, @insert_attrs } |
|
54 |
elsif ($method eq 'update') { push @attrs, @update_attrs } |
|
55 |
elsif (index($method, 'select') != -1 || $method eq 'count') { |
|
56 |
push @attrs, @select_attrs |
|
57 |
} |
|
58 |
|
|
59 |
for my $attr (@attrs) { |
|
60 |
$code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),"; |
|
61 |
} |
|
62 |
|
|
63 |
$code .= qq/\@_);/ . |
|
64 |
qq/}/; |
|
65 |
|
|
66 |
no strict 'refs'; |
|
67 |
*{__PACKAGE__ . "::$method"} = eval $code; |
|
68 |
croak $code if $@; |
|
- added EXPERIMENTAL type() ...
|
69 |
} |
70 | ||
fixed update_or_insert metho...
|
71 |
sub update_or_insert { |
cleanup
|
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} || {}; |
|
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 { croak "selected row must be one " . _subname } |
|
fixed update_or_insert metho...
|
87 |
} |
88 | ||
- removed placeholder count ...
|
89 |
sub execute { |
cleanup
|
90 |
my $self = shift; |
91 |
|
|
92 |
if ($ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE}) { |
|
93 |
$self->dbi->execute(@_); |
|
94 |
} |
|
95 |
else { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
96 |
_deprecate('0.24', "DBIx::Custom::Model execute method is DEPRECATED! " . |
97 |
"use DBIx::Custom execute method. " . |
|
98 |
"If you want to call DBIx::Custom execute method directory from model, " . |
|
99 |
"set \$ENV{DBIX_CUSTOM_DISABLE_MODEL_EXECUTE} to 1 " . |
|
100 |
"until DBIx::Custom::Model execute method is removed in the future." ); |
|
101 |
|
|
cleanup
|
102 |
return $self->dbi->execute( |
103 |
shift, |
|
104 |
shift, |
|
105 |
table => $self->table, |
|
106 |
bind_type => $self->bind_type, |
|
107 |
primary_key => $self->primary_key, |
|
108 |
type => $self->type, |
|
109 |
@_ |
|
110 |
); |
|
111 |
} |
|
- removed placeholder count ...
|
112 |
} |
113 | ||
select method column option ...
|
114 |
sub DESTROY { } |
115 | ||
- method method of DBIx::Cus...
|
116 |
sub helper { |
cleanup
|
117 |
my $self = shift; |
118 |
|
|
119 |
# Merge |
|
120 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
121 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
122 |
|
|
123 |
return $self; |
|
adeed EXPERIMENTAL DBIx::Cus...
|
124 |
} |
125 | ||
cleanup
|
126 |
sub mycolumn { |
cleanup
|
127 |
my $self = shift; |
128 |
my $table = shift unless ref $_[0]; |
|
129 |
my $columns = shift; |
|
130 |
|
|
131 |
$table ||= $self->table || ''; |
|
132 |
$columns ||= $self->columns; |
|
133 |
|
|
134 |
return $self->dbi->mycolumn($table, $columns); |
|
cleanup
|
135 |
} |
136 | ||
removed EXPERIMETNAL flag fr...
|
137 |
sub new { |
cleanup
|
138 |
my $self = shift->SUPER::new(@_); |
139 |
|
|
140 |
# Check attribute names |
|
141 |
my @attrs = keys %$self; |
|
142 |
for my $attr (@attrs) { |
|
143 |
croak qq{"$attr" is invalid attribute name } . _subname |
|
144 |
unless $self->can($attr); |
|
145 |
} |
|
146 |
|
|
147 |
# Cache |
|
- "created_at" option is ren...
|
148 |
for my $attr (qw/dbi table created_at updated_at ctime mtime bind_type join primary_key/) { |
cleanup
|
149 |
$self->$attr; |
150 |
$self->{$attr} = undef unless exists $self->{$attr}; |
|
151 |
} |
|
152 |
$self->columns; |
|
153 |
|
|
154 |
return $self; |
|
removed EXPERIMETNAL flag fr...
|
155 |
} |
156 | ||
- DBIx::Custom Model filter ...
|
157 |
# DEPRECATED! |
micro optimization
|
158 |
has 'filter'; |
cleanup
|
159 |
has 'name'; |
added EXPERIMENTAL DBIx::Cus...
|
160 |
has 'type'; |
- "created_at" option is ren...
|
161 |
has 'created_at'; |
162 |
has 'updated_at'; |
|
- DBIx::Custom Model filter ...
|
163 | |
- method method of DBIx::Cus...
|
164 |
# DEPRECATED! |
165 |
sub method { |
|
- added DBIX_CUSTOM_SUPPRESS...
|
166 |
_deprecate('0.24', "method method is DEPRECATED! use helper instead"); |
cleanup
|
167 |
return shift->helper(@_); |
- method method of DBIx::Cus...
|
168 |
} |
169 | ||
added experimental DBIx::Cus...
|
170 |
1; |
171 | ||
172 |
=head1 NAME |
|
173 | ||
fixed documentation miss abo...
|
174 |
DBIx::Custom::Model - Model object |
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 | ||
cleanup and added deprecated...
|
184 |
=head2 dbi |
add DBIx::Custom::Model fore...
|
185 | |
cleanup
|
186 |
my $dbi = $model->dbi; |
187 |
$model = $model->dbi($dbi); |
|
add DBIx::Custom::Model fore...
|
188 | |
189 |
L<DBIx::Custom> object. |
|
190 | ||
cleanup and added deprecated...
|
191 |
=head2 ctime |
update_or_insert method's re...
|
192 | |
- "created_at" option is ren...
|
193 |
my $ctime = $model->ctime; |
194 |
$model = $model->ctime('created_time'); |
|
update_or_insert method's re...
|
195 | |
196 |
Create timestamp column, this is passed to C<insert> or C<update> method. |
|
197 | ||
cleanup and added deprecated...
|
198 |
=head2 join |
- added experimental DBIx::C...
|
199 | |
cleanup
|
200 |
my $join = $model->join; |
201 |
$model = $model->join( |
|
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 | |
cleanup and added deprecated...
|
207 |
=head2 primary_key |
added DBIx::Custom result_fi...
|
208 | |
cleanup
|
209 |
my $primary_key = $model->primary_key; |
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 | |
cleanup and added deprecated...
|
215 |
=head2 table |
add DBIx::Custom::Model fore...
|
216 | |
cleanup
|
217 |
my $model = $model->table; |
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 | |
cleanup and added deprecated...
|
222 |
=head2 bind_type |
- added EXPERIMENTAL type() ...
|
223 | |
cleanup
|
224 |
my $type = $model->bind_type; |
225 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
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 | |
cleanup and added deprecated...
|
231 |
=head2 mtime |
update_or_insert method's re...
|
232 | |
- "created_at" option is ren...
|
233 |
my $mtime = $model->mtime; |
234 |
$model = $model->mtime('modified_time'); |
|
update_or_insert method's re...
|
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 | ||
cleanup and added deprecated...
|
244 |
=head2 count |
- added EXPERIMENTAL DBIx::C...
|
245 | |
cleanup
|
246 |
my $count = $model->count; |
- added EXPERIMENTAL DBIx::C...
|
247 | |
248 |
Get rows count. |
|
249 | ||
250 |
Options is same as C<select> method's ones. |
|
251 | ||
cleanup and added deprecated...
|
252 |
=head2 delete |
added insert, update, update...
|
253 | |
cleanup
|
254 |
$model->delete(...); |
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 | |
cleanup and added deprecated...
|
259 |
=head2 delete_all |
added insert, update, update...
|
260 | |
cleanup
|
261 |
$model->delete_all(...); |
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 | |
cleanup and added deprecated...
|
266 |
=head2 insert |
added insert, update, update...
|
267 | |
cleanup
|
268 |
$model->insert(...); |
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 | |
cleanup and added deprecated...
|
273 |
=head2 helper |
adeed EXPERIMENTAL DBIx::Cus...
|
274 | |
cleanup
|
275 |
$model->helper( |
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 |
); |
|
adeed EXPERIMENTAL DBIx::Cus...
|
287 | |
- method method of DBIx::Cus...
|
288 |
Register helper. These helper is called directly from L<DBIx::Custom::Model> object. |
adeed EXPERIMENTAL DBIx::Cus...
|
289 | |
cleanup
|
290 |
$model->update_or_insert; |
291 |
$model->find_or_create; |
|
adeed EXPERIMENTAL DBIx::Cus...
|
292 | |
cleanup and added deprecated...
|
293 |
=head2 mycolumn |
cleanup
|
294 | |
cleanup
|
295 |
my $column = $self->mycolumn; |
296 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
297 |
my $column = $self->mycolumn(['author', 'title']); |
|
cleanup
|
298 | |
299 |
Create column clause for myself. The follwoing column clause is created. |
|
300 | ||
cleanup
|
301 |
book.author as author, |
302 |
book.title as title |
|
cleanup
|
303 | |
304 |
If table name is ommited, C<table> attribute of the model is used. |
|
305 |
If column names is omitted, C<columns> attribute of the model is used. |
|
306 | ||
cleanup and added deprecated...
|
307 |
=head2 new |
added insert, update, update...
|
308 | |
cleanup
|
309 |
my $model = DBIx::Custom::Model->new; |
added insert, update, update...
|
310 | |
- removed placeholder count ...
|
311 |
Create a L<DBIx::Custom::Model> object. |
added insert, update, update...
|
312 | |
cleanup and added deprecated...
|
313 |
=head2 select |
added insert, update, update...
|
314 | |
cleanup
|
315 |
$model->select(...); |
316 |
|
|
DBIx::Custom::Model type att...
|
317 |
Same as C<select> of L<DBIx::Custom> except that |
micro optimization
|
318 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
319 | |
cleanup and added deprecated...
|
320 |
=head2 update |
added insert, update, update...
|
321 | |
cleanup
|
322 |
$model->update(...); |
323 |
|
|
DBIx::Custom::Model type att...
|
324 |
Same as C<update> of L<DBIx::Custom> except that |
micro optimization
|
325 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
326 | |
cleanup and added deprecated...
|
327 |
=head2 update_all |
added insert, update, update...
|
328 | |
cleanup
|
329 |
$model->update_all(param => \%param); |
330 |
|
|
DBIx::Custom::Model type att...
|
331 |
Same as C<update_all> of L<DBIx::Custom> except that |
micro optimization
|
332 |
you don't have to specify options if you set attribute in model. |
333 | ||
cleanup and added deprecated...
|
334 |
=head2 update_or_insert |
micro optimization
|
335 | |
cleanup
|
336 |
$model->update_or_insert(...); |
337 |
|
|
micro optimization
|
338 |
Same as C<update> of L<DBIx::Custom> except that |
339 |
you don't have to specify options if you set attribute in model. |
|
update pod
|
340 | |
update pod
|
341 |
=cut |