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