add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
updatedd pod
|
3 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
4 | |
5 |
use Carp 'croak'; |
|
cleanup
|
6 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
7 | |
added experimental DBIx::Cus...
|
8 |
# Carp trust relationship |
9 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
10 | ||
updatedd pod
|
11 |
has [qw/dbi name table view/], |
- added EXPERIMENTAL DBIx::C...
|
12 |
table_alias => sub { {} }, |
add DBIx::Custom::Model colu...
|
13 |
columns => sub { [] }, |
all filter can receive array...
|
14 |
filter => sub { [] }, |
added DBIx::Custom result_fi...
|
15 |
result_filter => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
16 |
join => sub { [] }, |
- added EXPERIMENTAL type() ...
|
17 |
type => sub { [] }, |
updatedd pod
|
18 |
primary_key => sub { [] }; |
added experimental DBIx::Cus...
|
19 | |
- added EXPERIMENTAL DBIx::C...
|
20 |
our $AUTOLOAD; |
21 | ||
22 |
sub AUTOLOAD { |
|
23 |
my $self = shift; |
|
24 | ||
25 |
# Method name |
|
26 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
27 | ||
28 |
# Method |
|
adeed EXPERIMENTAL DBIx::Cus...
|
29 |
$self->{_methods} ||= {}; |
30 |
if (my $method = $self->{_methods}->{$mname}) { |
|
31 |
return $self->$method(@_) |
|
32 |
} |
|
33 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
- added EXPERIMENTAL DBIx::C...
|
34 |
$self->dbi->$dbi_method(@_); |
35 |
} |
|
- removed EXPERIMENTAL Prefo...
|
36 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
37 |
$self->dbi->dbh->$dbh_method(@_); |
38 |
} |
|
39 |
else { |
|
cleanup
|
40 |
croak qq{Can't locate object method "$mname" via "$package" } |
41 |
. _subname; |
|
- added EXPERIMENTAL DBIx::C...
|
42 |
} |
43 |
} |
|
44 | ||
- added EXPERIMENTAL type() ...
|
45 |
my @methods = qw/insert insert_at update update_at update_all |
46 |
delete delete_at delete_all select select_at/; |
|
47 |
foreach my $method (@methods) { |
|
48 | ||
49 |
my $code = sub { |
|
50 |
my $self = shift; |
|
- fixed bug that model inser...
|
51 | |
added tests
|
52 |
my @args = ( |
53 |
table => $self->table, |
|
54 |
type => $self->type, |
|
55 |
primary_key => $self->primary_key |
|
56 |
); |
|
- added EXPERIMENTAL type() ...
|
57 |
push @args, (join => $self->join) if $method =~ /^select/; |
- fixed bug that model inser...
|
58 |
unshift @args, shift if @_ % 2; |
- added EXPERIMENTAL type() ...
|
59 |
|
60 |
$self->dbi->$method(@args, @_); |
|
61 |
}; |
|
62 |
|
|
63 |
no strict 'refs'; |
|
64 |
my $class = __PACKAGE__; |
|
65 |
*{"${class}::$method"} = $code; |
|
66 |
} |
|
67 | ||
- added EXPERIMENTAL DBIx::C...
|
68 |
sub column { |
69 |
my ($self, $table, $columns) = @_; |
|
70 |
|
|
71 |
$self->{_table_alias} ||= {}; |
|
72 |
my $dist; |
|
73 |
$dist = $self->dbi->{_table_alias}{$table} |
|
74 |
? $self->dbi->{_table_alias}{$table} |
|
75 |
: $table; |
|
76 |
|
|
77 |
$self->dbi->{_model_from} ||= {}; |
|
78 |
my $model = $self->dbi->{_model_from}->{$dist}; |
|
79 |
|
|
80 |
$columns ||= $self->model($model)->columns; |
|
81 |
|
|
cleanup
|
82 |
return $self->dbi->column($table, $columns); |
- added EXPERIMENTAL DBIx::C...
|
83 |
} |
84 | ||
added EXPERIMENTAL col metho...
|
85 |
sub col { |
86 |
my ($self, $table, $columns) = @_; |
|
87 |
|
|
88 |
$self->{_table_alias} ||= {}; |
|
89 |
my $dist; |
|
90 |
$dist = $self->dbi->{_table_alias}{$table} |
|
91 |
? $self->dbi->{_table_alias}{$table} |
|
92 |
: $table; |
|
93 |
|
|
94 |
$self->dbi->{_model_from} ||= {}; |
|
95 |
my $model = $self->dbi->{_model_from}->{$dist}; |
|
96 |
|
|
97 |
$columns ||= $self->model($model)->columns; |
|
98 |
|
|
99 |
return $self->dbi->col($table, $columns); |
|
100 |
} |
|
101 | ||
select method column option ...
|
102 |
sub DESTROY { } |
103 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
104 |
sub method { |
105 |
my $self = shift; |
|
106 |
|
|
107 |
# Merge |
|
108 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
109 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
110 |
|
|
111 |
return $self; |
|
112 |
} |
|
113 | ||
cleanup
|
114 |
sub mycolumn { |
115 |
my $self = shift; |
|
116 |
my $table = shift unless ref $_[0]; |
|
117 |
my $columns = shift; |
|
118 |
|
|
119 |
$table ||= $self->table || ''; |
|
120 |
$columns ||= $self->columns; |
|
121 |
|
|
122 |
return $self->dbi->mycolumn($table, $columns); |
|
123 |
} |
|
124 | ||
removed EXPERIMETNAL flag fr...
|
125 |
sub new { |
126 |
my $self = shift->SUPER::new(@_); |
|
127 |
|
|
128 |
# Check attribute names |
|
129 |
my @attrs = keys %$self; |
|
130 |
foreach my $attr (@attrs) { |
|
cleanup
|
131 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
132 |
unless $self->can($attr); |
133 |
} |
|
134 |
|
|
135 |
return $self; |
|
136 |
} |
|
137 | ||
added experimental DBIx::Cus...
|
138 |
1; |
139 | ||
140 |
=head1 NAME |
|
141 | ||
- removed DEPRECATED DBIx::C...
|
142 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
143 | |
144 |
=head1 SYNOPSIS |
|
145 | ||
146 |
use DBIx::Custom::Table; |
|
147 | ||
add feture. all model class ...
|
148 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
149 | |
add DBIx::Custom::Model fore...
|
150 |
=head1 ATTRIBUTES |
151 | ||
152 |
=head2 C<dbi> |
|
153 | ||
154 |
my $dbi = $model->dbi; |
|
155 |
$model = $model->dbi($dbi); |
|
156 | ||
157 |
L<DBIx::Custom> object. |
|
158 | ||
add experimental DBIx::Custo...
|
159 |
=head2 C<filter> |
160 | ||
161 |
my $dbi = $model->filter |
|
added DBIx::Custom result_fi...
|
162 |
$model = $model->filter( |
163 |
title => {out => 'tp_to_date', in => 'date_to_tp'} |
|
164 |
author => {out => ..., in => ...}, |
|
165 |
); |
|
add experimental DBIx::Custo...
|
166 | |
- added EXPERIMENTAL DBIx::C...
|
167 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
168 | |
add experimental DBIx::Custo...
|
169 |
=head2 C<name> |
170 | ||
171 |
my $name = $model->name; |
|
172 |
$model = $model->name('book'); |
|
173 | ||
174 |
Model name. |
|
175 | ||
update pod
|
176 |
=head2 C<join> |
- added experimental DBIx::C...
|
177 | |
178 |
my $join = $model->join; |
|
179 |
$model = $model->join( |
|
180 |
['left outer join company on book.company_id = company.id'] |
|
181 |
); |
|
182 |
|
|
- added EXPERIMENTAL DBIx::C...
|
183 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
184 | |
added DBIx::Custom result_fi...
|
185 |
=head2 C<primary_key> |
186 | ||
187 |
my $primary_key = $model->primary_key; |
|
188 |
$model = $model->primary_key(['id', 'number']); |
|
189 | ||
190 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
|
191 |
C<delete_at()>,C<select_at()>. |
|
192 | ||
updated_pod
|
193 |
=head2 C<result_filter> EXPERIMENTAL |
added DBIx::Custom result_fi...
|
194 | |
195 |
my $dbi = $model->result_filter |
|
196 |
$model = $model->result_filter( |
|
197 |
title => sub { ... }, |
|
198 |
author => sub { ... } |
|
199 |
); |
|
200 | ||
201 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> or C<create_model> is called. |
|
202 | ||
add DBIx::Custom::Model fore...
|
203 |
=head2 C<table> |
204 | ||
205 |
my $table = $model->table; |
|
206 |
$model = $model->table('book'); |
|
207 | ||
- added EXPERIMENTAL DBIx::C...
|
208 |
Table name, this is used as C<select()> C<table> option. |
209 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
210 | |
cleanup
|
211 |
=head2 C<table_alias> EXPERIMENTAL |
212 | ||
213 |
my $table_alias = $model->table_alias; |
|
214 |
$model = $model->table_alias(user1 => 'user', user2 => 'user'); |
|
215 | ||
216 |
Table alias. If you define table alias, |
|
217 |
same filter as the table is avaliable |
|
218 |
, and can write $dbi->column('user1') to get all columns. |
|
219 | ||
- added EXPERIMENTAL type() ...
|
220 |
=head2 C<type> |
221 | ||
222 |
my $type = $model->type; |
|
223 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
224 |
|
|
225 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
226 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
227 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
228 | |
- added EXPERIMENTAL DBIx::C...
|
229 |
=head2 C<view> |
230 | ||
231 |
my $view = $model->view; |
|
232 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
233 | ||
234 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
235 |
model is included by C<include_model>. |
|
236 | ||
added experimental DBIx::Cus...
|
237 |
=head1 METHODS |
238 | ||
table object call dbi object...
|
239 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
240 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
241 |
and implements the following new ones. |
242 | ||
cleanup
|
243 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
244 | |
- select() EXPERIMETNAL colu...
|
245 |
my $column = $model->column(book => ['author', 'title']); |
246 |
my $column = $model->column('book'); |
|
add experimental DBIx::Custo...
|
247 | |
cleanup
|
248 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
249 | |
cleanup
|
250 |
book.author as book__author, |
251 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
252 | |
cleanup
|
253 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
254 | |
added EXPERIMENTAL col metho...
|
255 |
=head2 C<col> EXPERIMETNAL |
256 | ||
- select() EXPERIMETNAL colu...
|
257 |
my $column = $model->col(book => ['author', 'title']); |
258 |
my $column = $model->col('book'); |
|
added EXPERIMENTAL col metho...
|
259 | |
260 |
Create column clause. The follwoing column clause is created. |
|
261 | ||
262 |
book.author as "book.author", |
|
263 |
book.title as "book.title" |
|
264 | ||
265 |
If column names is omitted, C<columns> attribute of the model is used. |
|
266 | ||
added insert, update, update...
|
267 |
=head2 C<delete> |
268 | ||
table object call dbi object...
|
269 |
$table->delete(...); |
added insert, update, update...
|
270 |
|
271 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
272 |
you don't have to specify C<table> option. |
added insert, update, update...
|
273 | |
274 |
=head2 C<delete_all> |
|
275 | ||
table object call dbi object...
|
276 |
$table->delete_all(...); |
added insert, update, update...
|
277 |
|
278 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
279 |
you don't have to specify C<table> option. |
added insert, update, update...
|
280 | |
281 |
=head2 C<insert> |
|
282 | ||
table object call dbi object...
|
283 |
$table->insert(...); |
added insert, update, update...
|
284 |
|
285 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
286 |
you don't have to specify C<table> option. |
added insert, update, update...
|
287 | |
- removed DEPRECATED DBIx::C...
|
288 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
289 | |
290 |
$model->method( |
|
291 |
update_or_insert => sub { |
|
292 |
my $self = shift; |
|
293 |
|
|
294 |
# ... |
|
295 |
}, |
|
296 |
find_or_create => sub { |
|
297 |
my $self = shift; |
|
298 |
|
|
299 |
# ... |
|
300 |
); |
|
301 | ||
302 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
303 | ||
304 |
$model->update_or_insert; |
|
305 |
$model->find_or_create; |
|
306 | ||
- added EXPERIMENTAL type() ...
|
307 |
=head2 C<mycolumn> |
cleanup
|
308 | |
309 |
my $column = $self->mycolumn; |
|
310 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
311 |
my $column = $self->mycolumn(['author', 'title']); |
|
312 | ||
313 |
Create column clause for myself. The follwoing column clause is created. |
|
314 | ||
315 |
book.author as author, |
|
316 |
book.title as title |
|
317 | ||
318 |
If table name is ommited, C<table> attribute of the model is used. |
|
319 |
If column names is omitted, C<columns> attribute of the model is used. |
|
320 | ||
added insert, update, update...
|
321 |
=head2 C<new> |
322 | ||
323 |
my $table = DBIx::Custom::Table->new; |
|
324 | ||
325 |
Create a L<DBIx::Custom::Table> object. |
|
326 | ||
327 |
=head2 C<select> |
|
328 | ||
table object call dbi object...
|
329 |
$table->select(...); |
added insert, update, update...
|
330 |
|
331 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
332 |
you don't have to specify C<table> option. |
added insert, update, update...
|
333 | |
334 |
=head2 C<update> |
|
335 | ||
table object call dbi object...
|
336 |
$table->update(...); |
added insert, update, update...
|
337 |
|
338 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
339 |
you don't have to specify C<table> option. |
added insert, update, update...
|
340 | |
341 |
=head2 C<update_all> |
|
342 | ||
343 |
$table->update_all(param => \%param); |
|
344 |
|
|
345 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
346 |
you don't have to specify table name. |
|
update pod
|
347 | |
- select() EXPERIMETNAL colu...
|
348 |
=head2 C<update_at> DEPRECATED! |
update pod
|
349 | |
350 |
$table->update_at(...); |
|
351 |
|
|
352 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
353 |
you don't have to specify C<table> and C<primary_key> option. |
|
- select() EXPERIMETNAL colu...
|
354 | |
355 |
=head2 C<select_at> DEPRECATED! |
|
356 | ||
357 |
$table->select_at(...); |
|
358 |
|
|
359 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
360 |
you don't have to specify C<table> and C<primary_key> option. |
|
361 | ||
362 |
=head2 C<insert_at> DEPRECATED! |
|
363 | ||
364 |
$table->insert_at(...); |
|
365 |
|
|
366 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
367 |
you don't have to specify C<table> and C<primary_key> option. |
|
368 | ||
369 |
=head2 C<delete_at> DEPRECATED! |
|
370 | ||
371 |
$table->delete_at(...); |
|
372 |
|
|
373 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
374 |
you don't have to specify C<table> and C<primary_key> option. |