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 |
|
|
56 |
my @args = (table => $self->table, type => $self->type); |
|
57 |
push @args, (primary_key => $self->primary_key) if $method =~ /_at$/; |
|
58 |
push @args, (join => $self->join) if $method =~ /^select/; |
|
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 |
|
145 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
146 | ||
- added EXPERIMENTAL DBIx::C...
|
147 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
148 | |
add experimental DBIx::Custo...
|
149 |
=head2 C<name> |
150 | ||
151 |
my $name = $model->name; |
|
152 |
$model = $model->name('book'); |
|
153 | ||
154 |
Model name. |
|
155 | ||
update pod
|
156 |
=head2 C<join> |
- added experimental DBIx::C...
|
157 | |
158 |
my $join = $model->join; |
|
159 |
$model = $model->join( |
|
160 |
['left outer join company on book.company_id = company.id'] |
|
161 |
); |
|
162 |
|
|
- added EXPERIMENTAL DBIx::C...
|
163 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
164 | |
add DBIx::Custom::Model fore...
|
165 |
=head2 C<table> |
166 | ||
167 |
my $table = $model->table; |
|
168 |
$model = $model->table('book'); |
|
169 | ||
- added EXPERIMENTAL DBIx::C...
|
170 |
Table name, this is used as C<select()> C<table> option. |
171 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
172 | |
cleanup
|
173 |
=head2 C<table_alias> EXPERIMENTAL |
174 | ||
175 |
my $table_alias = $model->table_alias; |
|
176 |
$model = $model->table_alias(user1 => 'user', user2 => 'user'); |
|
177 | ||
178 |
Table alias. If you define table alias, |
|
179 |
same filter as the table is avaliable |
|
180 |
, and can write $dbi->column('user1') to get all columns. |
|
181 | ||
add DBIx::Custom::Model fore...
|
182 |
=head2 C<primary_key> |
183 | ||
184 |
my $primary_key = $model->primary_key; |
|
185 |
$model = $model->primary_key(['id', 'number']); |
|
186 | ||
- added EXPERIMENTAL DBIx::C...
|
187 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
update pod
|
188 |
C<delete_at()>,C<select_at()>. |
add DBIx::Custom::Model fore...
|
189 | |
- added EXPERIMENTAL type() ...
|
190 |
=head2 C<type> |
191 | ||
192 |
my $type = $model->type; |
|
193 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
194 |
|
|
195 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
196 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
197 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
198 | |
- added EXPERIMENTAL DBIx::C...
|
199 |
=head2 C<view> |
200 | ||
201 |
my $view = $model->view; |
|
202 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
203 | ||
204 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
205 |
model is included by C<include_model>. |
|
206 | ||
added experimental DBIx::Cus...
|
207 |
=head1 METHODS |
208 | ||
table object call dbi object...
|
209 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
210 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
211 |
and implements the following new ones. |
212 | ||
cleanup
|
213 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
214 | |
cleanup
|
215 |
my $column = $self->column(book => ['author', 'title']); |
216 |
my $column = $self->column('book'); |
|
add experimental DBIx::Custo...
|
217 | |
cleanup
|
218 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
219 | |
cleanup
|
220 |
book.author as book__author, |
221 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
222 | |
cleanup
|
223 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
224 | |
added insert, update, update...
|
225 |
=head2 C<delete> |
226 | ||
table object call dbi object...
|
227 |
$table->delete(...); |
added insert, update, update...
|
228 |
|
229 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
230 |
you don't have to specify C<table> option. |
added insert, update, update...
|
231 | |
232 |
=head2 C<delete_all> |
|
233 | ||
table object call dbi object...
|
234 |
$table->delete_all(...); |
added insert, update, update...
|
235 |
|
236 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
237 |
you don't have to specify C<table> option. |
added insert, update, update...
|
238 | |
update pod
|
239 |
=head2 C<delete_at> |
240 | ||
241 |
$table->delete_at(...); |
|
242 |
|
|
243 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
244 |
you don't have to specify C<table> and C<primary_key> option. |
|
245 | ||
added insert, update, update...
|
246 |
=head2 C<insert> |
247 | ||
table object call dbi object...
|
248 |
$table->insert(...); |
added insert, update, update...
|
249 |
|
250 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
251 |
you don't have to specify C<table> option. |
added insert, update, update...
|
252 | |
update pod
|
253 |
=head2 C<insert> |
254 | ||
255 |
$table->insert_at(...); |
|
256 |
|
|
257 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
258 |
you don't have to specify C<table> and C<primary_key> option. |
|
259 | ||
- removed DEPRECATED DBIx::C...
|
260 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
261 | |
262 |
$model->method( |
|
263 |
update_or_insert => sub { |
|
264 |
my $self = shift; |
|
265 |
|
|
266 |
# ... |
|
267 |
}, |
|
268 |
find_or_create => sub { |
|
269 |
my $self = shift; |
|
270 |
|
|
271 |
# ... |
|
272 |
); |
|
273 | ||
274 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
275 | ||
276 |
$model->update_or_insert; |
|
277 |
$model->find_or_create; |
|
278 | ||
- added EXPERIMENTAL type() ...
|
279 |
=head2 C<mycolumn> |
cleanup
|
280 | |
281 |
my $column = $self->mycolumn; |
|
282 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
283 |
my $column = $self->mycolumn(['author', 'title']); |
|
284 | ||
285 |
Create column clause for myself. The follwoing column clause is created. |
|
286 | ||
287 |
book.author as author, |
|
288 |
book.title as title |
|
289 | ||
290 |
If table name is ommited, C<table> attribute of the model is used. |
|
291 |
If column names is omitted, C<columns> attribute of the model is used. |
|
292 | ||
added insert, update, update...
|
293 |
=head2 C<new> |
294 | ||
295 |
my $table = DBIx::Custom::Table->new; |
|
296 | ||
297 |
Create a L<DBIx::Custom::Table> object. |
|
298 | ||
299 |
=head2 C<select> |
|
300 | ||
table object call dbi object...
|
301 |
$table->select(...); |
added insert, update, update...
|
302 |
|
303 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
304 |
you don't have to specify C<table> option. |
added insert, update, update...
|
305 | |
update pod
|
306 |
=head2 C<select_at> |
307 | ||
308 |
$table->select_at(...); |
|
309 |
|
|
310 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
311 |
you don't have to specify C<table> and C<primary_key> option. |
|
312 | ||
added insert, update, update...
|
313 |
=head2 C<update> |
314 | ||
table object call dbi object...
|
315 |
$table->update(...); |
added insert, update, update...
|
316 |
|
317 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
318 |
you don't have to specify C<table> option. |
added insert, update, update...
|
319 | |
320 |
=head2 C<update_all> |
|
321 | ||
322 |
$table->update_all(param => \%param); |
|
323 |
|
|
324 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
325 |
you don't have to specify table name. |
|
update pod
|
326 | |
327 |
=head2 C<update_at> |
|
328 | ||
329 |
$table->update_at(...); |
|
330 |
|
|
331 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
332 |
you don't have to specify C<table> and C<primary_key> option. |