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 } |
|
55 |
elsif (index($method, 'select') != -1) { push @attrs, @select_attrs } |
|
56 |
|
|
57 |
for my $attr (@attrs) { |
|
micro optimization
|
58 |
$code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),"; |
micro optimization
|
59 |
} |
60 |
|
|
61 |
$code .= qq/\@_);/ . |
|
62 |
qq/}/; |
|
- added EXPERIMENTAL type() ...
|
63 |
|
64 |
no strict 'refs'; |
|
micro optimization
|
65 |
*{__PACKAGE__ . "::$method"} = eval $code; |
66 |
croak $code if $@; |
|
- added EXPERIMENTAL type() ...
|
67 |
} |
68 | ||
fixed update_or_insert metho...
|
69 |
sub update_or_insert { |
70 |
my ($self, $param, %opt) = @_; |
|
71 |
|
|
72 |
croak "update_or_insert method need primary_key and id option " |
|
73 |
unless (defined $opt{id} || defined $self->{id}) |
|
74 |
&& (defined $opt{primary_key} || defined $self->{primary_key}); |
|
75 |
|
|
76 |
my $statement_opt = $opt{option} || {}; |
|
- id option work if id count...
|
77 |
my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all; |
78 |
if (@$rows == 0) { |
|
79 |
return $self->insert($param, %opt, %{$statement_opt->{insert} || {}}); |
|
80 |
} |
|
81 |
elsif (@$rows == 1) { |
|
82 |
return $self->update($param, %opt, %{$statement_opt->{update} || {}}); |
|
83 |
} |
|
84 |
else { |
|
85 |
croak "selected row must be one " . _subname; |
|
86 |
} |
|
fixed update_or_insert metho...
|
87 |
} |
88 | ||
- removed placeholder count ...
|
89 |
sub execute { |
90 |
my $self = shift; |
|
- DBIx::Custom::QueryBuilder...
|
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 { |
adeed EXPERIMENTAL DBIx::Cus...
|
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; |
|
123 |
} |
|
124 | ||
cleanup
|
125 |
sub mycolumn { |
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); |
|
134 |
} |
|
135 | ||
removed EXPERIMETNAL flag fr...
|
136 |
sub new { |
137 |
my $self = shift->SUPER::new(@_); |
|
138 |
|
|
139 |
# Check attribute names |
|
140 |
my @attrs = keys %$self; |
|
cleanup
|
141 |
for my $attr (@attrs) { |
cleanup
|
142 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
143 |
unless $self->can($attr); |
144 |
} |
|
145 |
|
|
micro optimization
|
146 |
# Cache |
147 |
for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) { |
|
148 |
$self->$attr; |
|
149 |
$self->{$attr} = undef unless exists $self->{$attr}; |
|
150 |
} |
|
151 |
$self->columns; |
|
152 |
|
|
removed EXPERIMETNAL flag fr...
|
153 |
return $self; |
154 |
} |
|
155 | ||
- DBIx::Custom Model filter ...
|
156 |
# DEPRECATED! |
micro optimization
|
157 |
has 'filter'; |
cleanup
|
158 |
has 'name'; |
added EXPERIMENTAL DBIx::Cus...
|
159 |
has 'type'; |
- DBIx::Custom Model filter ...
|
160 | |
- method method of DBIx::Cus...
|
161 | |
162 |
# DEPRECATED! |
|
163 |
sub method { |
|
164 |
warn "method method is DEPRECATED! use helper instead"; |
|
165 |
return shift->helper(@_); |
|
166 |
} |
|
167 | ||
added experimental DBIx::Cus...
|
168 |
1; |
169 | ||
170 |
=head1 NAME |
|
171 | ||
- removed DEPRECATED DBIx::C...
|
172 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
173 | |
174 |
=head1 SYNOPSIS |
|
175 | ||
- removed placeholder count ...
|
176 |
use DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
177 | |
- removed placeholder count ...
|
178 |
my $model = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
179 | |
add DBIx::Custom::Model fore...
|
180 |
=head1 ATTRIBUTES |
181 | ||
182 |
=head2 C<dbi> |
|
183 | ||
184 |
my $dbi = $model->dbi; |
|
DBIx::Custom::Model type att...
|
185 |
$model = $model->dbi($dbi); |
add DBIx::Custom::Model fore...
|
186 | |
187 |
L<DBIx::Custom> object. |
|
188 | ||
update_or_insert method's re...
|
189 |
=head2 C<created_at EXPERIMENTAL> |
190 | ||
191 |
my $created_at = $model->created_at; |
|
192 |
$model = $model->created_at('created_datatime'); |
|
193 | ||
194 |
Create timestamp column, this is passed to C<insert> or C<update> method. |
|
195 | ||
update pod
|
196 |
=head2 C<join> |
- added experimental DBIx::C...
|
197 | |
198 |
my $join = $model->join; |
|
DBIx::Custom::Model type att...
|
199 |
$model = $model->join( |
- added experimental DBIx::C...
|
200 |
['left outer join company on book.company_id = company.id'] |
201 |
); |
|
202 |
|
|
DBIx::Custom::Model type att...
|
203 |
Join clause, this value is passed to C<select> method. |
- added experimental DBIx::C...
|
204 | |
added DBIx::Custom result_fi...
|
205 |
=head2 C<primary_key> |
206 | ||
207 |
my $primary_key = $model->primary_key; |
|
DBIx::Custom::Model type att...
|
208 |
$model = $model->primary_key(['id', 'number']); |
added DBIx::Custom result_fi...
|
209 | |
DBIx::Custom::Model type att...
|
210 |
Primary key,this is passed to C<insert>, C<update>, |
211 |
C<delete>, and C<select> method. |
|
added DBIx::Custom result_fi...
|
212 | |
add DBIx::Custom::Model fore...
|
213 |
=head2 C<table> |
214 | ||
- removed placeholder count ...
|
215 |
my $model = $model->table; |
DBIx::Custom::Model type att...
|
216 |
$model = $model->table('book'); |
add DBIx::Custom::Model fore...
|
217 | |
DBIx::Custom::Model type att...
|
218 |
Table name, this is passed to C<select> method. |
add experimental DBIx::Custo...
|
219 | |
DBIx::Custom::Model type att...
|
220 |
=head2 C<bind_type> |
- added EXPERIMENTAL type() ...
|
221 | |
DBIx::Custom::Model type att...
|
222 |
my $type = $model->bind_type; |
223 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
- added EXPERIMENTAL type() ...
|
224 |
|
DBIx::Custom::Model type att...
|
225 |
Database data type, this is used as type optioon of C<insert>, |
226 |
C<update>, C<update_all>, C<delete>, C<delete_all>, |
|
cleanup
|
227 |
and C<select> method |
- added EXPERIMENTAL type() ...
|
228 | |
update_or_insert method's re...
|
229 |
=head2 C<updated_at EXPERIMENTAL> |
230 | ||
231 |
my $updated_at = $model->updated_at; |
|
232 |
$model = $model->updated_at('updated_datatime'); |
|
233 | ||
234 |
Updated timestamp column, this is passed to C<update> method. |
|
235 | ||
added experimental DBIx::Cus...
|
236 |
=head1 METHODS |
237 | ||
- DBIx::Custom Model filter ...
|
238 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
239 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
240 |
and implements the following new ones. |
241 | ||
- removed EXPERIMENTAL the f...
|
242 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
243 | |
244 |
my $count = $model->count; |
|
245 | ||
246 |
Get rows count. |
|
247 | ||
248 |
Options is same as C<select> method's ones. |
|
249 | ||
added insert, update, update...
|
250 |
=head2 C<delete> |
251 | ||
- removed placeholder count ...
|
252 |
$model->delete(...); |
added insert, update, update...
|
253 |
|
DBIx::Custom::Model type att...
|
254 |
Same as C<delete> of L<DBIx::Custom> except that |
micro optimization
|
255 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
256 | |
257 |
=head2 C<delete_all> |
|
258 | ||
- removed placeholder count ...
|
259 |
$model->delete_all(...); |
added insert, update, update...
|
260 |
|
DBIx::Custom::Model type att...
|
261 |
Same as C<delete_all> of L<DBIx::Custom> except that |
micro optimization
|
262 |
you don't have to specify options if you set attribute in model. |
- removed placeholder count ...
|
263 | |
added insert, update, update...
|
264 |
=head2 C<insert> |
265 | ||
- removed placeholder count ...
|
266 |
$model->insert(...); |
added insert, update, update...
|
267 |
|
DBIx::Custom::Model type att...
|
268 |
Same as C<insert> of L<DBIx::Custom> except that |
micro optimization
|
269 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
270 | |
- method method of DBIx::Cus...
|
271 |
=head2 C<helper> |
adeed EXPERIMENTAL DBIx::Cus...
|
272 | |
- method method of DBIx::Cus...
|
273 |
$model->helper( |
adeed EXPERIMENTAL DBIx::Cus...
|
274 |
update_or_insert => sub { |
275 |
my $self = shift; |
|
276 |
|
|
277 |
# ... |
|
278 |
}, |
|
279 |
find_or_create => sub { |
|
280 |
my $self = shift; |
|
281 |
|
|
282 |
# ... |
|
283 |
); |
|
284 | ||
- method method of DBIx::Cus...
|
285 |
Register helper. These helper is called directly from L<DBIx::Custom::Model> object. |
adeed EXPERIMENTAL DBIx::Cus...
|
286 | |
287 |
$model->update_or_insert; |
|
288 |
$model->find_or_create; |
|
289 | ||
- added EXPERIMENTAL type() ...
|
290 |
=head2 C<mycolumn> |
cleanup
|
291 | |
292 |
my $column = $self->mycolumn; |
|
293 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
294 |
my $column = $self->mycolumn(['author', 'title']); |
|
295 | ||
296 |
Create column clause for myself. The follwoing column clause is created. |
|
297 | ||
298 |
book.author as author, |
|
299 |
book.title as title |
|
300 | ||
301 |
If table name is ommited, C<table> attribute of the model is used. |
|
302 |
If column names is omitted, C<columns> attribute of the model is used. |
|
303 | ||
added insert, update, update...
|
304 |
=head2 C<new> |
305 | ||
- removed placeholder count ...
|
306 |
my $model = DBIx::Custom::Model->new; |
added insert, update, update...
|
307 | |
- removed placeholder count ...
|
308 |
Create a L<DBIx::Custom::Model> object. |
added insert, update, update...
|
309 | |
310 |
=head2 C<select> |
|
311 | ||
- removed placeholder count ...
|
312 |
$model->select(...); |
added insert, update, update...
|
313 |
|
DBIx::Custom::Model type att...
|
314 |
Same as C<select> of L<DBIx::Custom> except that |
micro optimization
|
315 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
316 | |
317 |
=head2 C<update> |
|
318 | ||
- removed placeholder count ...
|
319 |
$model->update(...); |
added insert, update, update...
|
320 |
|
DBIx::Custom::Model type att...
|
321 |
Same as C<update> of L<DBIx::Custom> except that |
micro optimization
|
322 |
you don't have to specify options if you set attribute in model. |
added insert, update, update...
|
323 | |
324 |
=head2 C<update_all> |
|
325 | ||
- removed placeholder count ...
|
326 |
$model->update_all(param => \%param); |
added insert, update, update...
|
327 |
|
DBIx::Custom::Model type att...
|
328 |
Same as C<update_all> of L<DBIx::Custom> except that |
micro optimization
|
329 |
you don't have to specify options if you set attribute in model. |
330 | ||
- id option work if id count...
|
331 |
=head2 C<update_or_insert> |
micro optimization
|
332 | |
333 |
$model->update_or_insert(...); |
|
334 |
|
|
335 |
Same as C<update> of L<DBIx::Custom> except that |
|
336 |
you don't have to specify options if you set attribute in model. |
|
update pod
|
337 | |
update pod
|
338 |
=cut |