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