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