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 | ||
select method column option ...
|
85 |
sub DESTROY { } |
86 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
87 |
sub method { |
88 |
my $self = shift; |
|
89 |
|
|
90 |
# Merge |
|
91 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
92 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
93 |
|
|
94 |
return $self; |
|
95 |
} |
|
96 | ||
cleanup
|
97 |
sub mycolumn { |
98 |
my $self = shift; |
|
99 |
my $table = shift unless ref $_[0]; |
|
100 |
my $columns = shift; |
|
101 |
|
|
102 |
$table ||= $self->table || ''; |
|
103 |
$columns ||= $self->columns; |
|
104 |
|
|
105 |
return $self->dbi->mycolumn($table, $columns); |
|
106 |
} |
|
107 | ||
removed EXPERIMETNAL flag fr...
|
108 |
sub new { |
109 |
my $self = shift->SUPER::new(@_); |
|
110 |
|
|
111 |
# Check attribute names |
|
112 |
my @attrs = keys %$self; |
|
113 |
foreach my $attr (@attrs) { |
|
cleanup
|
114 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
115 |
unless $self->can($attr); |
116 |
} |
|
117 |
|
|
118 |
return $self; |
|
119 |
} |
|
120 | ||
added experimental DBIx::Cus...
|
121 |
1; |
122 | ||
123 |
=head1 NAME |
|
124 | ||
- removed DEPRECATED DBIx::C...
|
125 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
126 | |
127 |
=head1 SYNOPSIS |
|
128 | ||
129 |
use DBIx::Custom::Table; |
|
130 | ||
add feture. all model class ...
|
131 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
132 | |
add DBIx::Custom::Model fore...
|
133 |
=head1 ATTRIBUTES |
134 | ||
135 |
=head2 C<dbi> |
|
136 | ||
137 |
my $dbi = $model->dbi; |
|
138 |
$model = $model->dbi($dbi); |
|
139 | ||
140 |
L<DBIx::Custom> object. |
|
141 | ||
add experimental DBIx::Custo...
|
142 |
=head2 C<filter> |
143 | ||
144 |
my $dbi = $model->filter |
|
added DBIx::Custom result_fi...
|
145 |
$model = $model->filter( |
146 |
title => {out => 'tp_to_date', in => 'date_to_tp'} |
|
147 |
author => {out => ..., in => ...}, |
|
148 |
); |
|
add experimental DBIx::Custo...
|
149 | |
- added EXPERIMENTAL DBIx::C...
|
150 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
151 | |
add experimental DBIx::Custo...
|
152 |
=head2 C<name> |
153 | ||
154 |
my $name = $model->name; |
|
155 |
$model = $model->name('book'); |
|
156 | ||
157 |
Model name. |
|
158 | ||
update pod
|
159 |
=head2 C<join> |
- added experimental DBIx::C...
|
160 | |
161 |
my $join = $model->join; |
|
162 |
$model = $model->join( |
|
163 |
['left outer join company on book.company_id = company.id'] |
|
164 |
); |
|
165 |
|
|
- added EXPERIMENTAL DBIx::C...
|
166 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
167 | |
added DBIx::Custom result_fi...
|
168 |
=head2 C<primary_key> |
169 | ||
170 |
my $primary_key = $model->primary_key; |
|
171 |
$model = $model->primary_key(['id', 'number']); |
|
172 | ||
173 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
|
174 |
C<delete_at()>,C<select_at()>. |
|
175 | ||
updated_pod
|
176 |
=head2 C<result_filter> EXPERIMENTAL |
added DBIx::Custom result_fi...
|
177 | |
178 |
my $dbi = $model->result_filter |
|
179 |
$model = $model->result_filter( |
|
180 |
title => sub { ... }, |
|
181 |
author => sub { ... } |
|
182 |
); |
|
183 | ||
184 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> or C<create_model> is called. |
|
185 | ||
add DBIx::Custom::Model fore...
|
186 |
=head2 C<table> |
187 | ||
188 |
my $table = $model->table; |
|
189 |
$model = $model->table('book'); |
|
190 | ||
- added EXPERIMENTAL DBIx::C...
|
191 |
Table name, this is used as C<select()> C<table> option. |
192 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
193 | |
cleanup
|
194 |
=head2 C<table_alias> EXPERIMENTAL |
195 | ||
196 |
my $table_alias = $model->table_alias; |
|
197 |
$model = $model->table_alias(user1 => 'user', user2 => 'user'); |
|
198 | ||
199 |
Table alias. If you define table alias, |
|
200 |
same filter as the table is avaliable |
|
201 |
, and can write $dbi->column('user1') to get all columns. |
|
202 | ||
- added EXPERIMENTAL type() ...
|
203 |
=head2 C<type> |
204 | ||
205 |
my $type = $model->type; |
|
206 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
207 |
|
|
208 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
209 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
210 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
211 | |
- added EXPERIMENTAL DBIx::C...
|
212 |
=head2 C<view> |
213 | ||
214 |
my $view = $model->view; |
|
215 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
216 | ||
217 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
218 |
model is included by C<include_model>. |
|
219 | ||
added experimental DBIx::Cus...
|
220 |
=head1 METHODS |
221 | ||
table object call dbi object...
|
222 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
223 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
224 |
and implements the following new ones. |
225 | ||
cleanup
|
226 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
227 | |
- select() EXPERIMETNAL colu...
|
228 |
my $column = $model->column(book => ['author', 'title']); |
229 |
my $column = $model->column('book'); |
|
add experimental DBIx::Custo...
|
230 | |
cleanup
|
231 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
232 | |
cleanup
|
233 |
book.author as book__author, |
234 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
235 | |
cleanup
|
236 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
237 | |
added EXPERIMENTAL col metho...
|
238 |
=head2 C<col> EXPERIMETNAL |
239 | ||
- select() EXPERIMETNAL colu...
|
240 |
my $column = $model->col(book => ['author', 'title']); |
241 |
my $column = $model->col('book'); |
|
added EXPERIMENTAL col metho...
|
242 | |
243 |
Create column clause. The follwoing column clause is created. |
|
244 | ||
245 |
book.author as "book.author", |
|
246 |
book.title as "book.title" |
|
247 | ||
248 |
If column names is omitted, C<columns> attribute of the model is used. |
|
249 | ||
added insert, update, update...
|
250 |
=head2 C<delete> |
251 | ||
table object call dbi object...
|
252 |
$table->delete(...); |
added insert, update, update...
|
253 |
|
254 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
255 |
you don't have to specify C<table> option. |
added insert, update, update...
|
256 | |
257 |
=head2 C<delete_all> |
|
258 | ||
table object call dbi object...
|
259 |
$table->delete_all(...); |
added insert, update, update...
|
260 |
|
261 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
262 |
you don't have to specify C<table> option. |
added insert, update, update...
|
263 | |
264 |
=head2 C<insert> |
|
265 | ||
table object call dbi object...
|
266 |
$table->insert(...); |
added insert, update, update...
|
267 |
|
268 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
269 |
you don't have to specify C<table> option. |
added insert, update, update...
|
270 | |
- removed DEPRECATED DBIx::C...
|
271 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
272 | |
273 |
$model->method( |
|
274 |
update_or_insert => sub { |
|
275 |
my $self = shift; |
|
276 |
|
|
277 |
# ... |
|
278 |
}, |
|
279 |
find_or_create => sub { |
|
280 |
my $self = shift; |
|
281 |
|
|
282 |
# ... |
|
283 |
); |
|
284 | ||
285 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
286 | ||
287 |
$model->update_or_insert; |
|
288 |
$model->find_or_create; |
|
289 | ||
- added EXPERIMENTAL type() ...
|
290 |
=head2 C<mycolumn> |
cleanup
|
291 | |
292 |
my $column = $self->mycolumn; |
|
293 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
294 |
my $column = $self->mycolumn(['author', 'title']); |
|
295 | ||
296 |
Create column clause for myself. The follwoing column clause is created. |
|
297 | ||
298 |
book.author as author, |
|
299 |
book.title as title |
|
300 | ||
301 |
If table name is ommited, C<table> attribute of the model is used. |
|
302 |
If column names is omitted, C<columns> attribute of the model is used. |
|
303 | ||
added insert, update, update...
|
304 |
=head2 C<new> |
305 | ||
306 |
my $table = DBIx::Custom::Table->new; |
|
307 | ||
308 |
Create a L<DBIx::Custom::Table> object. |
|
309 | ||
310 |
=head2 C<select> |
|
311 | ||
table object call dbi object...
|
312 |
$table->select(...); |
added insert, update, update...
|
313 |
|
314 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
315 |
you don't have to specify C<table> option. |
added insert, update, update...
|
316 | |
317 |
=head2 C<update> |
|
318 | ||
table object call dbi object...
|
319 |
$table->update(...); |
added insert, update, update...
|
320 |
|
321 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
322 |
you don't have to specify C<table> option. |
added insert, update, update...
|
323 | |
324 |
=head2 C<update_all> |
|
325 | ||
326 |
$table->update_all(param => \%param); |
|
327 |
|
|
328 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
329 |
you don't have to specify table name. |
|
update pod
|
330 | |
- select() EXPERIMETNAL colu...
|
331 |
=head2 C<update_at> DEPRECATED! |
update pod
|
332 | |
333 |
$table->update_at(...); |
|
334 |
|
|
335 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
336 |
you don't have to specify C<table> and C<primary_key> option. |
|
- select() EXPERIMETNAL colu...
|
337 | |
338 |
=head2 C<select_at> DEPRECATED! |
|
339 | ||
340 |
$table->select_at(...); |
|
341 |
|
|
342 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
343 |
you don't have to specify C<table> and C<primary_key> option. |
|
344 | ||
345 |
=head2 C<insert_at> DEPRECATED! |
|
346 | ||
347 |
$table->insert_at(...); |
|
348 |
|
|
349 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
350 |
you don't have to specify C<table> and C<primary_key> option. |
|
351 | ||
352 |
=head2 C<delete_at> DEPRECATED! |
|
353 | ||
354 |
$table->delete_at(...); |
|
355 |
|
|
356 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
357 |
you don't have to specify C<table> and C<primary_key> option. |