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