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