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