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