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