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