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'; |
|
9 | ||
added experimental DBIx::Cus...
|
10 |
# Carp trust relationship |
11 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
12 | ||
add DBIx::Custom::Model fore...
|
13 |
__PACKAGE__->attr( |
- added EXPERIMENTAL DBIx::C...
|
14 |
['dbi', 'name', 'table', 'view'], |
- added EXPERIMENTAL DBIx::C...
|
15 |
table_alias => sub { {} }, |
add DBIx::Custom::Model colu...
|
16 |
columns => sub { [] }, |
all filter can receive array...
|
17 |
filter => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
18 |
join => sub { [] }, |
- added EXPERIMENTAL type() ...
|
19 |
type => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
20 |
primary_key => sub { [] } |
add DBIx::Custom::Model fore...
|
21 |
); |
added experimental DBIx::Cus...
|
22 | |
- added EXPERIMENTAL DBIx::C...
|
23 |
our $AUTOLOAD; |
24 | ||
25 |
sub AUTOLOAD { |
|
26 |
my $self = shift; |
|
27 | ||
28 |
# Method name |
|
29 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
30 | ||
31 |
# Method |
|
adeed EXPERIMENTAL DBIx::Cus...
|
32 |
$self->{_methods} ||= {}; |
33 |
if (my $method = $self->{_methods}->{$mname}) { |
|
34 |
return $self->$method(@_) |
|
35 |
} |
|
36 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
- added EXPERIMENTAL DBIx::C...
|
37 |
$self->dbi->$dbi_method(@_); |
38 |
} |
|
- removed EXPERIMENTAL Prefo...
|
39 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
40 |
$self->dbi->dbh->$dbh_method(@_); |
41 |
} |
|
42 |
else { |
|
improved error messages
|
43 |
croak qq{Can't locate object method "$mname" via "$package"} |
44 |
. qq{ (DBIx::Custom::Model::AUTOLOAD) } |
|
- added EXPERIMENTAL DBIx::C...
|
45 |
} |
46 |
} |
|
47 | ||
- added EXPERIMENTAL type() ...
|
48 |
my @methods = qw/insert insert_at update update_at update_all |
49 |
delete delete_at delete_all select select_at/; |
|
50 |
foreach my $method (@methods) { |
|
51 | ||
52 |
my $code = sub { |
|
53 |
my $self = shift; |
|
54 |
|
|
55 |
my @args = (table => $self->table, type => $self->type); |
|
56 |
push @args, (primary_key => $self->primary_key) if $method =~ /_at$/; |
|
57 |
push @args, (join => $self->join) if $method =~ /^select/; |
|
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) { |
|
113 |
croak qq{"$attr" is invalid attribute name} |
|
improved error messages
|
114 |
. qq{ (DBIx::Custom::Model::new) } |
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 | |
add DBIx::Custom::Model fore...
|
173 |
=head2 C<primary_key> |
174 | ||
175 |
my $primary_key = $model->primary_key; |
|
176 |
$model = $model->primary_key(['id', 'number']); |
|
177 | ||
- added EXPERIMENTAL DBIx::C...
|
178 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
update pod
|
179 |
C<delete_at()>,C<select_at()>. |
add DBIx::Custom::Model fore...
|
180 | |
- added EXPERIMENTAL type() ...
|
181 |
=head2 C<type> |
182 | ||
183 |
my $type = $model->type; |
|
184 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
185 |
|
|
186 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
187 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
188 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
189 | |
- added EXPERIMENTAL DBIx::C...
|
190 |
=head2 C<view> |
191 | ||
192 |
my $view = $model->view; |
|
193 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
194 | ||
195 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
196 |
model is included by C<include_model>. |
|
197 | ||
added experimental DBIx::Cus...
|
198 |
=head1 METHODS |
199 | ||
table object call dbi object...
|
200 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
201 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
202 |
and implements the following new ones. |
203 | ||
cleanup
|
204 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
205 | |
cleanup
|
206 |
my $column = $self->column(book => ['author', 'title']); |
207 |
my $column = $self->column('book'); |
|
add experimental DBIx::Custo...
|
208 | |
cleanup
|
209 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
210 | |
cleanup
|
211 |
book.author as book__author, |
212 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
213 | |
cleanup
|
214 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
215 | |
added insert, update, update...
|
216 |
=head2 C<delete> |
217 | ||
table object call dbi object...
|
218 |
$table->delete(...); |
added insert, update, update...
|
219 |
|
220 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
221 |
you don't have to specify C<table> option. |
added insert, update, update...
|
222 | |
223 |
=head2 C<delete_all> |
|
224 | ||
table object call dbi object...
|
225 |
$table->delete_all(...); |
added insert, update, update...
|
226 |
|
227 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
228 |
you don't have to specify C<table> option. |
added insert, update, update...
|
229 | |
update pod
|
230 |
=head2 C<delete_at> |
231 | ||
232 |
$table->delete_at(...); |
|
233 |
|
|
234 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
235 |
you don't have to specify C<table> and C<primary_key> option. |
|
236 | ||
added insert, update, update...
|
237 |
=head2 C<insert> |
238 | ||
table object call dbi object...
|
239 |
$table->insert(...); |
added insert, update, update...
|
240 |
|
241 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
242 |
you don't have to specify C<table> option. |
added insert, update, update...
|
243 | |
update pod
|
244 |
=head2 C<insert> |
245 | ||
246 |
$table->insert_at(...); |
|
247 |
|
|
248 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
249 |
you don't have to specify C<table> and C<primary_key> option. |
|
250 | ||
- removed DEPRECATED DBIx::C...
|
251 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
252 | |
253 |
$model->method( |
|
254 |
update_or_insert => sub { |
|
255 |
my $self = shift; |
|
256 |
|
|
257 |
# ... |
|
258 |
}, |
|
259 |
find_or_create => sub { |
|
260 |
my $self = shift; |
|
261 |
|
|
262 |
# ... |
|
263 |
); |
|
264 | ||
265 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
266 | ||
267 |
$model->update_or_insert; |
|
268 |
$model->find_or_create; |
|
269 | ||
- added EXPERIMENTAL type() ...
|
270 |
=head2 C<mycolumn> |
cleanup
|
271 | |
272 |
my $column = $self->mycolumn; |
|
273 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
274 |
my $column = $self->mycolumn(['author', 'title']); |
|
275 | ||
276 |
Create column clause for myself. The follwoing column clause is created. |
|
277 | ||
278 |
book.author as author, |
|
279 |
book.title as title |
|
280 | ||
281 |
If table name is ommited, C<table> attribute of the model is used. |
|
282 |
If column names is omitted, C<columns> attribute of the model is used. |
|
283 | ||
added insert, update, update...
|
284 |
=head2 C<new> |
285 | ||
286 |
my $table = DBIx::Custom::Table->new; |
|
287 | ||
288 |
Create a L<DBIx::Custom::Table> object. |
|
289 | ||
290 |
=head2 C<select> |
|
291 | ||
table object call dbi object...
|
292 |
$table->select(...); |
added insert, update, update...
|
293 |
|
294 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
295 |
you don't have to specify C<table> option. |
added insert, update, update...
|
296 | |
update pod
|
297 |
=head2 C<select_at> |
298 | ||
299 |
$table->select_at(...); |
|
300 |
|
|
301 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
302 |
you don't have to specify C<table> and C<primary_key> option. |
|
303 | ||
added insert, update, update...
|
304 |
=head2 C<update> |
305 | ||
table object call dbi object...
|
306 |
$table->update(...); |
added insert, update, update...
|
307 |
|
308 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
309 |
you don't have to specify C<table> option. |
added insert, update, update...
|
310 | |
311 |
=head2 C<update_all> |
|
312 | ||
313 |
$table->update_all(param => \%param); |
|
314 |
|
|
315 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
316 |
you don't have to specify table name. |
|
update pod
|
317 | |
318 |
=head2 C<update_at> |
|
319 | ||
320 |
$table->update_at(...); |
|
321 |
|
|
322 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
323 |
you don't have to specify C<table> and C<primary_key> option. |