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 'DBIx::Custom'; |
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'], |
add DBIx::Custom::Model colu...
|
15 |
columns => sub { [] }, |
all filter can receive array...
|
16 |
filter => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
17 |
join => sub { [] }, |
18 |
primary_key => sub { [] } |
|
add DBIx::Custom::Model fore...
|
19 |
); |
added experimental DBIx::Cus...
|
20 | |
select method column option ...
|
21 |
sub column_clause { |
added experimental DBIx::Cus...
|
22 |
my $self = shift; |
23 |
|
|
select method column option ...
|
24 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
added experimental DBIx::Cus...
|
25 |
|
select method column option ...
|
26 |
my $table = $self->table; |
27 |
my $columns = $self->columns; |
|
28 |
my $add = $args->{add} || []; |
|
29 |
my $remove = $args->{remove} || []; |
|
30 |
my %remove = map {$_ => 1} @$remove; |
|
- added experimental DBIx::C...
|
31 |
my $prefix = $args->{prefix} || ''; |
select method column option ...
|
32 |
|
33 |
my @column; |
|
34 |
foreach my $column (@$columns) { |
|
- added experimental DBIx::C...
|
35 |
push @column, "$table.$column as $prefix$column" |
select method column option ...
|
36 |
unless $remove{$column}; |
37 |
} |
|
38 |
|
|
39 |
foreach my $column (@$add) { |
|
40 |
push @column, $column; |
|
41 |
} |
|
42 |
|
|
43 |
return join (', ', @column); |
|
added experimental DBIx::Cus...
|
44 |
} |
45 | ||
select method column option ...
|
46 |
sub delete { |
DBIx::Custom::Model select()...
|
47 |
my $self = shift; |
select method column option ...
|
48 |
$self->dbi->delete(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
49 |
} |
50 | ||
select method column option ...
|
51 |
sub delete_all { |
DBIx::Custom::Model select()...
|
52 |
my $self = shift; |
select method column option ...
|
53 |
$self->dbi->delete_all(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
54 |
} |
55 | ||
select method column option ...
|
56 |
sub delete_at { |
DBIx::Custom::Model select()...
|
57 |
my $self = shift; |
select method column option ...
|
58 |
|
59 |
return $self->dbi->delete_at( |
|
60 |
table => $self->table, |
|
61 |
primary_key => $self->primary_key, |
|
62 |
@_ |
|
63 |
); |
|
DBIx::Custom::Model select()...
|
64 |
} |
65 | ||
select method column option ...
|
66 |
sub DESTROY { } |
67 | ||
68 |
sub insert { |
|
DBIx::Custom::Model select()...
|
69 |
my $self = shift; |
select method column option ...
|
70 |
$self->dbi->insert(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
71 |
} |
72 | ||
- added experimental DBIx::C...
|
73 |
sub insert_at { |
74 |
my $self = shift; |
|
75 |
|
|
76 |
return $self->dbi->insert_at( |
|
77 |
table => $self->table, |
|
78 |
primary_key => $self->primary_key, |
|
79 |
@_ |
|
80 |
); |
|
81 |
} |
|
82 | ||
DBIx::Custom::Model select()...
|
83 |
sub select { |
84 |
my $self = shift; |
|
85 |
$self->dbi->select( |
|
86 |
table => $self->table, |
|
- added experimental DBIx::C...
|
87 |
join => $self->join, |
DBIx::Custom::Model select()...
|
88 |
@_ |
89 |
); |
|
90 |
} |
|
add experimental DBIx::Custo...
|
91 | |
select method column option ...
|
92 |
sub select_at { |
add experimental DBIx::Custo...
|
93 |
my $self = shift; |
added insert, update, update...
|
94 |
|
select method column option ...
|
95 |
return $self->dbi->select_at( |
add experimental DBIx::Custo...
|
96 |
table => $self->table, |
97 |
primary_key => $self->primary_key, |
|
- added experimental DBIx::C...
|
98 |
join => $self->join, |
add experimental DBIx::Custo...
|
99 |
@_ |
100 |
); |
|
101 |
} |
|
102 | ||
select method column option ...
|
103 |
sub update { |
add experimental DBIx::Custo...
|
104 |
my $self = shift; |
select method column option ...
|
105 |
$self->dbi->update(table => $self->table, @_) |
add experimental DBIx::Custo...
|
106 |
} |
107 | ||
select method column option ...
|
108 |
sub update_all { |
109 |
my $self = shift; |
|
110 |
$self->dbi->update_all(table => $self->table, @_); |
|
111 |
} |
|
112 | ||
113 | ||
114 |
sub update_at { |
|
add experimental DBIx::Custo...
|
115 |
my $self = shift; |
many changed
|
116 |
|
select method column option ...
|
117 |
return $self->dbi->update_at( |
add experimental DBIx::Custo...
|
118 |
table => $self->table, |
119 |
primary_key => $self->primary_key, |
|
120 |
@_ |
|
121 |
); |
|
added insert, update, update...
|
122 |
} |
123 | ||
added experimental DBIx::Cus...
|
124 |
1; |
125 | ||
126 |
=head1 NAME |
|
127 | ||
update pod
|
128 |
DBIx::Custom::Model - Model EXPERIMENTAL |
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 | ||
150 |
This filter is applied when L<DBIx::Custom> C<include_model()> is called. |
|
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 |
|
|
166 |
Default join clause. This is used by C<select()>. |
|
167 | ||
add DBIx::Custom::Model fore...
|
168 |
=head2 C<table> |
169 | ||
170 |
my $table = $model->table; |
|
171 |
$model = $model->table('book'); |
|
172 | ||
add experimental DBIx::Custo...
|
173 |
Table name. Model name and table name is different. |
174 |
Table name is real table name in database. |
|
175 | ||
add DBIx::Custom::Model fore...
|
176 |
=head2 C<primary_key> |
177 | ||
178 |
my $primary_key = $model->primary_key; |
|
179 |
$model = $model->primary_key(['id', 'number']); |
|
180 | ||
update pod
|
181 |
Foreign key. This is used by C<insert_at>,C<update_at()>, |
182 |
C<delete_at()>,C<select_at()>. |
|
add DBIx::Custom::Model fore...
|
183 | |
- added EXPERIMENTAL DBIx::C...
|
184 |
=head2 C<view> |
185 | ||
186 |
my $view = $model->view; |
|
187 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
188 | ||
189 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
190 |
model is included by C<include_model>. |
|
191 | ||
added experimental DBIx::Cus...
|
192 |
=head1 METHODS |
193 | ||
table object call dbi object...
|
194 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
195 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
196 |
and implements the following new ones. |
197 | ||
add experimental DBIx::Custo...
|
198 |
=head2 C<column_clause()> |
199 | ||
200 |
To create column clause automatically, use C<column_clause()>. |
|
201 |
Valude of C<table> and C<columns> is used. |
|
202 | ||
203 |
my $column_clause = $model->column_clause; |
|
204 | ||
205 |
If C<table> is 'book'�AC<column> is ['id', 'name'], |
|
206 |
the following clause is created. |
|
207 | ||
208 |
book.id as id, book.name as name |
|
209 | ||
210 |
These column name is for removing column name ambiguities. |
|
211 | ||
212 |
If you remove some columns, use C<remove> option. |
|
213 | ||
214 |
my $column_clause = $model->column_clause(remove => ['id']); |
|
215 | ||
216 |
If you add some column, use C<add> option. |
|
217 | ||
218 |
my $column_clause = $model->column_clause(add => ['company.id as company__id']); |
|
219 | ||
- added experimental DBIx::C...
|
220 |
If you add column name prefix, use C<prefix> option |
221 | ||
222 |
my $column_clause = $model->column_clause(prefix => 'book__'); |
|
223 | ||
224 |
The following clause is created. |
|
225 | ||
226 |
book.id as book__id, book.name as book__name |
|
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 | ||
added insert, update, update...
|
263 |
=head2 C<new> |
264 | ||
265 |
my $table = DBIx::Custom::Table->new; |
|
266 | ||
267 |
Create a L<DBIx::Custom::Table> object. |
|
268 | ||
269 |
=head2 C<select> |
|
270 | ||
table object call dbi object...
|
271 |
$table->select(...); |
added insert, update, update...
|
272 |
|
273 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
274 |
you don't have to specify C<table> option. |
added insert, update, update...
|
275 | |
update pod
|
276 |
=head2 C<select_at> |
277 | ||
278 |
$table->select_at(...); |
|
279 |
|
|
280 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
281 |
you don't have to specify C<table> and C<primary_key> option. |
|
282 | ||
added insert, update, update...
|
283 |
=head2 C<update> |
284 | ||
table object call dbi object...
|
285 |
$table->update(...); |
added insert, update, update...
|
286 |
|
287 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
288 |
you don't have to specify C<table> option. |
added insert, update, update...
|
289 | |
290 |
=head2 C<update_all> |
|
291 | ||
292 |
$table->update_all(param => \%param); |
|
293 |
|
|
294 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
295 |
you don't have to specify table name. |
|
update pod
|
296 | |
297 |
=head2 C<update_at> |
|
298 | ||
299 |
$table->update_at(...); |
|
300 |
|
|
301 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
302 |
you don't have to specify C<table> and C<primary_key> option. |