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