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 | ||
- "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 { |
|
96 |
warn "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 |
return $self->dbi->execute( |
|
102 |
shift, |
|
103 |
shift, |
|
104 |
table => $self->table, |
|
105 |
bind_type => $self->bind_type, |
|
106 |
primary_key => $self->primary_key, |
|
107 |
type => $self->type, |
|
108 |
@_ |
|
109 |
); |
|
110 |
} |
|
- removed placeholder count ...
|
111 |
} |
112 | ||
select method column option ...
|
113 |
sub DESTROY { } |
114 | ||
- method method of DBIx::Cus...
|
115 |
sub helper { |
cleanup
|
116 |
my $self = shift; |
117 |
|
|
118 |
# Merge |
|
119 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
120 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
121 |
|
|
122 |
return $self; |
|
adeed EXPERIMENTAL DBIx::Cus...
|
123 |
} |
124 | ||
cleanup
|
125 |
sub mycolumn { |
cleanup
|
126 |
my $self = shift; |
127 |
my $table = shift unless ref $_[0]; |
|
128 |
my $columns = shift; |
|
129 |
|
|
130 |
$table ||= $self->table || ''; |
|
131 |
$columns ||= $self->columns; |
|
132 |
|
|
133 |
return $self->dbi->mycolumn($table, $columns); |
|
cleanup
|
134 |
} |
135 | ||
removed EXPERIMETNAL flag fr...
|
136 |
sub new { |
cleanup
|
137 |
my $self = shift->SUPER::new(@_); |
138 |
|
|
139 |
# Check attribute names |
|
140 |
my @attrs = keys %$self; |
|
141 |
for my $attr (@attrs) { |
|
142 |
croak qq{"$attr" is invalid attribute name } . _subname |
|
143 |
unless $self->can($attr); |
|
144 |
} |
|
145 |
|
|
146 |
# Cache |
|
- "created_at" option is ren...
|
147 |
for my $attr (qw/dbi table created_at updated_at ctime mtime bind_type join primary_key/) { |
cleanup
|
148 |
$self->$attr; |
149 |
$self->{$attr} = undef unless exists $self->{$attr}; |
|
150 |
} |
|
151 |
$self->columns; |
|
152 |
|
|
153 |
return $self; |
|
removed EXPERIMETNAL flag fr...
|
154 |
} |
155 | ||
- DBIx::Custom Model filter ...
|
156 |
# DEPRECATED! |
micro optimization
|
157 |
has 'filter'; |
cleanup
|
158 |
has 'name'; |
added EXPERIMENTAL DBIx::Cus...
|
159 |
has 'type'; |
- "created_at" option is ren...
|
160 |
has 'created_at'; |
161 |
has 'updated_at'; |
|
- DBIx::Custom Model filter ...
|
162 | |
- method method of DBIx::Cus...
|
163 |
# DEPRECATED! |
164 |
sub method { |
|
cleanup
|
165 |
warn "method method is DEPRECATED! use helper instead"; |
166 |
return shift->helper(@_); |
|
- method method of DBIx::Cus...
|
167 |
} |
168 | ||
added experimental DBIx::Cus...
|
169 |
1; |
170 | ||
171 |
=head1 NAME |
|
172 | ||
- removed DEPRECATED DBIx::C...
|
173 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
174 | |
175 |
=head1 SYNOPSIS |
|
176 | ||
- removed placeholder count ...
|
177 |
use DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
178 | |
- removed placeholder count ...
|
179 |
my $model = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
180 | |
add DBIx::Custom::Model fore...
|
181 |
=head1 ATTRIBUTES |
182 | ||
183 |
=head2 C<dbi> |
|
184 | ||
cleanup
|
185 |
my $dbi = $model->dbi; |
186 |
$model = $model->dbi($dbi); |
|
add DBIx::Custom::Model fore...
|
187 | |
188 |
L<DBIx::Custom> object. |
|
189 | ||
- "created_at" option is ren...
|
190 |
=head2 C<ctime> |
update_or_insert method's re...
|
191 | |
- "created_at" option is ren...
|
192 |
my $ctime = $model->ctime; |
193 |
$model = $model->ctime('created_time'); |
|
update_or_insert method's re...
|
194 | |
195 |
Create timestamp column, this is passed to C<insert> or C<update> method. |
|
196 | ||
update pod
|
197 |
=head2 C<join> |
- added experimental DBIx::C...
|
198 | |
cleanup
|
199 |
my $join = $model->join; |
200 |
$model = $model->join( |
|
201 |
['left outer join company on book.company_id = company.id'] |
|
202 |
); |
|
203 |
|
|
DBIx::Custom::Model type att...
|
204 |
Join clause, this value is passed to C<select> method. |
- added experimental DBIx::C...
|
205 | |
added DBIx::Custom result_fi...
|
206 |
=head2 C<primary_key> |
207 | ||
cleanup
|
208 |
my $primary_key = $model->primary_key; |
209 |
$model = $model->primary_key(['id', 'number']); |
|
added DBIx::Custom result_fi...
|
210 | |
DBIx::Custom::Model type att...
|
211 |
Primary key,this is passed to C<insert>, C<update>, |
212 |
C<delete>, and C<select> method. |
|
added DBIx::Custom result_fi...
|
213 | |
add DBIx::Custom::Model fore...
|
214 |
=head2 C<table> |
215 | ||
cleanup
|
216 |
my $model = $model->table; |
217 |
$model = $model->table('book'); |
|
add DBIx::Custom::Model fore...
|
218 | |
DBIx::Custom::Model type att...
|
219 |
Table name, this is passed to C<select> method. |
add experimental DBIx::Custo...
|
220 | |
DBIx::Custom::Model type att...
|
221 |
=head2 C<bind_type> |
- added EXPERIMENTAL type() ...
|
222 | |
cleanup
|
223 |
my $type = $model->bind_type; |
224 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
225 |
|
|
DBIx::Custom::Model type att...
|
226 |
Database data type, this is used as type optioon of C<insert>, |
227 |
C<update>, C<update_all>, C<delete>, C<delete_all>, |
|
cleanup
|
228 |
and C<select> method |
- added EXPERIMENTAL type() ...
|
229 | |
- "created_at" option is ren...
|
230 |
=head2 C<mtime> |
update_or_insert method's re...
|
231 | |
- "created_at" option is ren...
|
232 |
my $mtime = $model->mtime; |
233 |
$model = $model->mtime('modified_time'); |
|
update_or_insert method's re...
|
234 | |
235 |
Updated timestamp column, this is passed to C<update> method. |
|
236 | ||
added experimental DBIx::Cus...
|
237 |
=head1 METHODS |
238 | ||
- DBIx::Custom Model filter ...
|
239 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
240 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
241 |
and implements the following new ones. |
242 | ||
- removed EXPERIMENTAL the f...
|
243 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
244 | |
cleanup
|
245 |
my $count = $model->count; |
- added EXPERIMENTAL DBIx::C...
|
246 | |
247 |
Get rows count. |
|
248 | ||
249 |
Options is same as C<select> method's ones. |
|
250 | ||
added insert, update, update...
|
251 |
=head2 C<delete> |
252 | ||
cleanup
|
253 |
$model->delete(...); |
254 |
|
|
DBIx::Custom::Model type att...
|
255 |
Same as C<delete> of L<DBIx::Custom> except that |
micro optimization
|
256 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
257 | |
258 |
=head2 C<delete_all> |
|
259 | ||
cleanup
|
260 |
$model->delete_all(...); |
261 |
|
|
DBIx::Custom::Model type att...
|
262 |
Same as C<delete_all> of L<DBIx::Custom> except that |
micro optimization
|
263 |
you don't have to specify options if you set attribute in model. |
- removed placeholder count ...
|
264 | |
added insert, update, update...
|
265 |
=head2 C<insert> |
266 | ||
cleanup
|
267 |
$model->insert(...); |
268 |
|
|
DBIx::Custom::Model type att...
|
269 |
Same as C<insert> of L<DBIx::Custom> except that |
micro optimization
|
270 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
271 | |
- method method of DBIx::Cus...
|
272 |
=head2 C<helper> |
adeed EXPERIMENTAL DBIx::Cus...
|
273 | |
cleanup
|
274 |
$model->helper( |
275 |
update_or_insert => sub { |
|
276 |
my $self = shift; |
|
277 |
|
|
278 |
# ... |
|
279 |
}, |
|
280 |
find_or_create => sub { |
|
281 |
my $self = shift; |
|
282 |
|
|
283 |
# ... |
|
284 |
} |
|
285 |
); |
|
adeed EXPERIMENTAL DBIx::Cus...
|
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 | |
cleanup
|
289 |
$model->update_or_insert; |
290 |
$model->find_or_create; |
|
adeed EXPERIMENTAL DBIx::Cus...
|
291 | |
- added EXPERIMENTAL type() ...
|
292 |
=head2 C<mycolumn> |
cleanup
|
293 | |
cleanup
|
294 |
my $column = $self->mycolumn; |
295 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
296 |
my $column = $self->mycolumn(['author', 'title']); |
|
cleanup
|
297 | |
298 |
Create column clause for myself. The follwoing column clause is created. |
|
299 | ||
cleanup
|
300 |
book.author as author, |
301 |
book.title as title |
|
cleanup
|
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 | ||
cleanup
|
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 | ||
cleanup
|
314 |
$model->select(...); |
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 | ||
cleanup
|
321 |
$model->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 | ||
cleanup
|
328 |
$model->update_all(param => \%param); |
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 | |
cleanup
|
335 |
$model->update_or_insert(...); |
336 |
|
|
micro optimization
|
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 |