add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
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( |
add experimental DBIx::Custo...
|
14 |
['dbi', 'name', 'table'], |
add DBIx::Custom::Model colu...
|
15 |
columns => sub { [] }, |
all filter can receive array...
|
16 |
filter => sub { [] }, |
add experimental DBIx::Custo...
|
17 |
primary_key => sub { [] }, |
18 |
relation => sub { {} } |
|
add DBIx::Custom::Model fore...
|
19 |
); |
added experimental DBIx::Cus...
|
20 | |
21 |
our $AUTOLOAD; |
|
22 | ||
23 |
sub AUTOLOAD { |
|
24 |
my $self = shift; |
|
25 | ||
renamed experimental DBIx::C...
|
26 |
# Method name |
27 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added experimental DBIx::Cus...
|
28 | |
renamed experimental DBIx::C...
|
29 |
# Method |
30 |
$self->{_methods} ||= {}; |
|
table object call dbi object...
|
31 |
if (my $method = $self->{_methods}->{$mname}) { |
32 |
return $self->$method(@_) |
|
33 |
} |
|
add feture. all model class ...
|
34 |
elsif ($self->dbi->can($mname)) { |
35 |
$self->dbi->$mname(@_); |
|
36 |
} |
|
37 |
elsif ($self->dbi->dbh->can($mname)) { |
|
38 |
$self->dbi->dbh->$mname(@_); |
|
39 |
} |
|
40 |
else { |
|
41 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
42 |
} |
|
added experimental DBIx::Cus...
|
43 |
} |
44 | ||
select method column option ...
|
45 |
sub column_clause { |
added experimental DBIx::Cus...
|
46 |
my $self = shift; |
47 |
|
|
select method column option ...
|
48 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
added experimental DBIx::Cus...
|
49 |
|
select method column option ...
|
50 |
my $table = $self->table; |
51 |
my $columns = $self->columns; |
|
52 |
my $add = $args->{add} || []; |
|
53 |
my $remove = $args->{remove} || []; |
|
54 |
my %remove = map {$_ => 1} @$remove; |
|
55 |
|
|
56 |
my @column; |
|
57 |
foreach my $column (@$columns) { |
|
58 |
push @column, "$table.$column as $column" |
|
59 |
unless $remove{$column}; |
|
60 |
} |
|
61 |
|
|
62 |
foreach my $column (@$add) { |
|
63 |
push @column, $column; |
|
64 |
} |
|
65 |
|
|
66 |
return join (', ', @column); |
|
added experimental DBIx::Cus...
|
67 |
} |
68 | ||
select method column option ...
|
69 |
sub delete { |
DBIx::Custom::Model select()...
|
70 |
my $self = shift; |
select method column option ...
|
71 |
$self->dbi->delete(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
72 |
} |
73 | ||
select method column option ...
|
74 |
sub delete_all { |
DBIx::Custom::Model select()...
|
75 |
my $self = shift; |
select method column option ...
|
76 |
$self->dbi->delete_all(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
77 |
} |
78 | ||
select method column option ...
|
79 |
sub delete_at { |
DBIx::Custom::Model select()...
|
80 |
my $self = shift; |
select method column option ...
|
81 |
|
82 |
return $self->dbi->delete_at( |
|
83 |
table => $self->table, |
|
84 |
primary_key => $self->primary_key, |
|
85 |
@_ |
|
86 |
); |
|
DBIx::Custom::Model select()...
|
87 |
} |
88 | ||
select method column option ...
|
89 |
sub DESTROY { } |
90 | ||
91 |
sub insert { |
|
DBIx::Custom::Model select()...
|
92 |
my $self = shift; |
select method column option ...
|
93 |
$self->dbi->insert(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
94 |
} |
95 | ||
- added experimental DBIx::C...
|
96 |
sub insert_at { |
97 |
my $self = shift; |
|
98 |
|
|
99 |
return $self->dbi->insert_at( |
|
100 |
table => $self->table, |
|
101 |
primary_key => $self->primary_key, |
|
102 |
@_ |
|
103 |
); |
|
104 |
} |
|
105 | ||
select method column option ...
|
106 |
sub method { |
DBIx::Custom::Model select()...
|
107 |
my $self = shift; |
select method column option ...
|
108 |
|
109 |
# Merge |
|
110 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
111 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
112 |
|
|
113 |
return $self; |
|
DBIx::Custom::Model select()...
|
114 |
} |
115 | ||
116 |
sub select { |
|
117 |
my $self = shift; |
|
118 |
$self->dbi->select( |
|
119 |
table => $self->table, |
|
120 |
relation => $self->relation, |
|
121 |
@_ |
|
122 |
); |
|
123 |
} |
|
add experimental DBIx::Custo...
|
124 | |
select method column option ...
|
125 |
sub select_at { |
add experimental DBIx::Custo...
|
126 |
my $self = shift; |
added insert, update, update...
|
127 |
|
select method column option ...
|
128 |
return $self->dbi->select_at( |
add experimental DBIx::Custo...
|
129 |
table => $self->table, |
130 |
primary_key => $self->primary_key, |
|
select method column option ...
|
131 |
relation => $self->relation, |
add experimental DBIx::Custo...
|
132 |
@_ |
133 |
); |
|
134 |
} |
|
135 | ||
select method column option ...
|
136 |
sub update { |
add experimental DBIx::Custo...
|
137 |
my $self = shift; |
select method column option ...
|
138 |
$self->dbi->update(table => $self->table, @_) |
add experimental DBIx::Custo...
|
139 |
} |
140 | ||
select method column option ...
|
141 |
sub update_all { |
142 |
my $self = shift; |
|
143 |
$self->dbi->update_all(table => $self->table, @_); |
|
144 |
} |
|
145 | ||
146 | ||
147 |
sub update_at { |
|
add experimental DBIx::Custo...
|
148 |
my $self = shift; |
many changed
|
149 |
|
select method column option ...
|
150 |
return $self->dbi->update_at( |
add experimental DBIx::Custo...
|
151 |
table => $self->table, |
152 |
primary_key => $self->primary_key, |
|
153 |
@_ |
|
154 |
); |
|
added insert, update, update...
|
155 |
} |
156 | ||
added experimental DBIx::Cus...
|
157 |
1; |
158 | ||
159 |
=head1 NAME |
|
160 | ||
add DBIx::Custom::Model fore...
|
161 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
162 | |
163 |
=head1 SYNOPSIS |
|
164 | ||
165 |
use DBIx::Custom::Table; |
|
166 | ||
add feture. all model class ...
|
167 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
168 | |
add DBIx::Custom::Model fore...
|
169 |
=head1 ATTRIBUTES |
170 | ||
add experimental DBIx::Custo...
|
171 |
=head2 C<columns> |
add DBIx::Custom::Model colu...
|
172 | |
173 |
my $columns = $model->columns; |
|
174 |
$model = $model->columns(['id', 'number']); |
|
175 | ||
add DBIx::Custom::Model fore...
|
176 |
=head2 C<dbi> |
177 | ||
178 |
my $dbi = $model->dbi; |
|
179 |
$model = $model->dbi($dbi); |
|
180 | ||
181 |
L<DBIx::Custom> object. |
|
182 | ||
add experimental DBIx::Custo...
|
183 |
=head2 C<filter> |
184 | ||
185 |
my $dbi = $model->filter |
|
186 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
187 | ||
188 |
This filter is applied when L<DBIx::Custom> C<include_model()> is called. |
|
189 | ||
add experimental DBIx::Custo...
|
190 |
=head2 C<name> |
191 | ||
192 |
my $name = $model->name; |
|
193 |
$model = $model->name('book'); |
|
194 | ||
195 |
Model name. |
|
196 | ||
add DBIx::Custom::Model fore...
|
197 |
=head2 C<table> |
198 | ||
199 |
my $table = $model->table; |
|
200 |
$model = $model->table('book'); |
|
201 | ||
add experimental DBIx::Custo...
|
202 |
Table name. Model name and table name is different. |
203 |
Table name is real table name in database. |
|
204 | ||
add DBIx::Custom::Model fore...
|
205 |
=head2 C<primary_key> |
206 | ||
207 |
my $primary_key = $model->primary_key; |
|
208 |
$model = $model->primary_key(['id', 'number']); |
|
209 | ||
210 |
Foreign key. This is used by C<update_at()>, C<delete_at()>, |
|
211 |
C<select_at()>. |
|
212 | ||
added experimental DBIx::Cus...
|
213 |
=head1 METHODS |
214 | ||
table object call dbi object...
|
215 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
216 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
217 |
and implements the following new ones. |
218 | ||
add experimental DBIx::Custo...
|
219 |
=head2 C<column_clause()> |
220 | ||
221 |
To create column clause automatically, use C<column_clause()>. |
|
222 |
Valude of C<table> and C<columns> is used. |
|
223 | ||
224 |
my $column_clause = $model->column_clause; |
|
225 | ||
226 |
If C<table> is 'book'�AC<column> is ['id', 'name'], |
|
227 |
the following clause is created. |
|
228 | ||
229 |
book.id as id, book.name as name |
|
230 | ||
231 |
These column name is for removing column name ambiguities. |
|
232 | ||
233 |
If you remove some columns, use C<remove> option. |
|
234 | ||
235 |
my $column_clause = $model->column_clause(remove => ['id']); |
|
236 | ||
237 |
If you add some column, use C<add> option. |
|
238 | ||
239 |
my $column_clause = $model->column_clause(add => ['company.id as company__id']); |
|
240 | ||
added insert, update, update...
|
241 |
=head2 C<delete> |
242 | ||
table object call dbi object...
|
243 |
$table->delete(...); |
added insert, update, update...
|
244 |
|
245 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
246 |
you don't have to specify C<table> option. |
added insert, update, update...
|
247 | |
248 |
=head2 C<delete_all> |
|
249 | ||
table object call dbi object...
|
250 |
$table->delete_all(...); |
added insert, update, update...
|
251 |
|
252 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
253 |
you don't have to specify C<table> option. |
added insert, update, update...
|
254 | |
renamed experimental DBIx::C...
|
255 |
=head2 C<method> |
added experimental DBIx::Cus...
|
256 | |
table object call dbi object...
|
257 |
$table->method( |
258 |
count => sub { |
|
259 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
260 |
|
table object call dbi object...
|
261 |
return $self->select(column => 'count(*)', @_) |
262 |
->fetch_first->[0]; |
|
263 |
} |
|
264 |
); |
|
added experimental DBIx::Cus...
|
265 |
|
renamed experimental DBIx::C...
|
266 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
267 | |
added insert, update, update...
|
268 |
=head2 C<insert> |
269 | ||
table object call dbi object...
|
270 |
$table->insert(...); |
added insert, update, update...
|
271 |
|
272 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
273 |
you don't have to specify C<table> option. |
added insert, update, update...
|
274 | |
275 |
=head2 C<new> |
|
276 | ||
277 |
my $table = DBIx::Custom::Table->new; |
|
278 | ||
279 |
Create a L<DBIx::Custom::Table> object. |
|
280 | ||
281 |
=head2 C<select> |
|
282 | ||
table object call dbi object...
|
283 |
$table->select(...); |
added insert, update, update...
|
284 |
|
285 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
286 |
you don't have to specify C<table> option. |
added insert, update, update...
|
287 | |
288 |
=head2 C<update> |
|
289 | ||
table object call dbi object...
|
290 |
$table->update(...); |
added insert, update, update...
|
291 |
|
292 |
Same as C<update()> 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 | |
295 |
=head2 C<update_all> |
|
296 | ||
297 |
$table->update_all(param => \%param); |
|
298 |
|
|
299 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
300 |
you don't have to specify table name. |