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 { [] }, |
add experimental DBIx::Custo...
|
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 | ||
select method column option ...
|
96 |
sub method { |
DBIx::Custom::Model select()...
|
97 |
my $self = shift; |
select method column option ...
|
98 |
|
99 |
# Merge |
|
100 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
101 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
102 |
|
|
103 |
return $self; |
|
DBIx::Custom::Model select()...
|
104 |
} |
105 | ||
106 |
sub select { |
|
107 |
my $self = shift; |
|
108 |
$self->dbi->select( |
|
109 |
table => $self->table, |
|
110 |
relation => $self->relation, |
|
111 |
@_ |
|
112 |
); |
|
113 |
} |
|
add experimental DBIx::Custo...
|
114 | |
select method column option ...
|
115 |
sub select_at { |
add experimental DBIx::Custo...
|
116 |
my $self = shift; |
added insert, update, update...
|
117 |
|
select method column option ...
|
118 |
return $self->dbi->select_at( |
add experimental DBIx::Custo...
|
119 |
table => $self->table, |
120 |
primary_key => $self->primary_key, |
|
select method column option ...
|
121 |
relation => $self->relation, |
add experimental DBIx::Custo...
|
122 |
@_ |
123 |
); |
|
124 |
} |
|
125 | ||
select method column option ...
|
126 |
sub update { |
add experimental DBIx::Custo...
|
127 |
my $self = shift; |
select method column option ...
|
128 |
$self->dbi->update(table => $self->table, @_) |
add experimental DBIx::Custo...
|
129 |
} |
130 | ||
select method column option ...
|
131 |
sub update_all { |
132 |
my $self = shift; |
|
133 |
$self->dbi->update_all(table => $self->table, @_); |
|
134 |
} |
|
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 | ||
add DBIx::Custom::Model fore...
|
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 | ||
add experimental DBIx::Custo...
|
161 |
=head2 C<columns> |
add DBIx::Custom::Model colu...
|
162 | |
163 |
my $columns = $model->columns; |
|
164 |
$model = $model->columns(['id', 'number']); |
|
165 | ||
add DBIx::Custom::Model fore...
|
166 |
=head2 C<dbi> |
167 | ||
168 |
my $dbi = $model->dbi; |
|
169 |
$model = $model->dbi($dbi); |
|
170 | ||
171 |
L<DBIx::Custom> object. |
|
172 | ||
add experimental DBIx::Custo...
|
173 |
=head2 C<filter> |
174 | ||
175 |
my $dbi = $model->filter |
|
176 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
177 | ||
178 |
This filter is applied when L<DBIx::Custom> C<include_model()> is called. |
|
179 | ||
add experimental DBIx::Custo...
|
180 |
=head2 C<name> |
181 | ||
182 |
my $name = $model->name; |
|
183 |
$model = $model->name('book'); |
|
184 | ||
185 |
Model name. |
|
186 | ||
add DBIx::Custom::Model fore...
|
187 |
=head2 C<table> |
188 | ||
189 |
my $table = $model->table; |
|
190 |
$model = $model->table('book'); |
|
191 | ||
add experimental DBIx::Custo...
|
192 |
Table name. Model name and table name is different. |
193 |
Table name is real table name in database. |
|
194 | ||
add DBIx::Custom::Model fore...
|
195 |
=head2 C<primary_key> |
196 | ||
197 |
my $primary_key = $model->primary_key; |
|
198 |
$model = $model->primary_key(['id', 'number']); |
|
199 | ||
200 |
Foreign key. This is used by C<update_at()>, C<delete_at()>, |
|
201 |
C<select_at()>. |
|
202 | ||
added experimental DBIx::Cus...
|
203 |
=head1 METHODS |
204 | ||
table object call dbi object...
|
205 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
206 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
207 |
and implements the following new ones. |
208 | ||
add experimental DBIx::Custo...
|
209 |
=head2 C<column_clause()> |
210 | ||
211 |
To create column clause automatically, use C<column_clause()>. |
|
212 |
Valude of C<table> and C<columns> is used. |
|
213 | ||
214 |
my $column_clause = $model->column_clause; |
|
215 | ||
216 |
If C<table> is 'book'�AC<column> is ['id', 'name'], |
|
217 |
the following clause is created. |
|
218 | ||
219 |
book.id as id, book.name as name |
|
220 | ||
221 |
These column name is for removing column name ambiguities. |
|
222 | ||
223 |
If you remove some columns, use C<remove> option. |
|
224 | ||
225 |
my $column_clause = $model->column_clause(remove => ['id']); |
|
226 | ||
227 |
If you add some column, use C<add> option. |
|
228 | ||
229 |
my $column_clause = $model->column_clause(add => ['company.id as company__id']); |
|
230 | ||
added insert, update, update...
|
231 |
=head2 C<delete> |
232 | ||
table object call dbi object...
|
233 |
$table->delete(...); |
added insert, update, update...
|
234 |
|
235 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
236 |
you don't have to specify C<table> option. |
added insert, update, update...
|
237 | |
238 |
=head2 C<delete_all> |
|
239 | ||
table object call dbi object...
|
240 |
$table->delete_all(...); |
added insert, update, update...
|
241 |
|
242 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
243 |
you don't have to specify C<table> option. |
added insert, update, update...
|
244 | |
renamed experimental DBIx::C...
|
245 |
=head2 C<method> |
added experimental DBIx::Cus...
|
246 | |
table object call dbi object...
|
247 |
$table->method( |
248 |
count => sub { |
|
249 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
250 |
|
table object call dbi object...
|
251 |
return $self->select(column => 'count(*)', @_) |
252 |
->fetch_first->[0]; |
|
253 |
} |
|
254 |
); |
|
added experimental DBIx::Cus...
|
255 |
|
renamed experimental DBIx::C...
|
256 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
257 | |
added insert, update, update...
|
258 |
=head2 C<insert> |
259 | ||
table object call dbi object...
|
260 |
$table->insert(...); |
added insert, update, update...
|
261 |
|
262 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
263 |
you don't have to specify C<table> option. |
added insert, update, update...
|
264 | |
265 |
=head2 C<new> |
|
266 | ||
267 |
my $table = DBIx::Custom::Table->new; |
|
268 | ||
269 |
Create a L<DBIx::Custom::Table> object. |
|
270 | ||
271 |
=head2 C<select> |
|
272 | ||
table object call dbi object...
|
273 |
$table->select(...); |
added insert, update, update...
|
274 |
|
275 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
276 |
you don't have to specify C<table> option. |
added insert, update, update...
|
277 | |
278 |
=head2 C<update> |
|
279 | ||
table object call dbi object...
|
280 |
$table->update(...); |
added insert, update, update...
|
281 |
|
282 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
283 |
you don't have to specify C<table> option. |
added insert, update, update...
|
284 | |
285 |
=head2 C<update_all> |
|
286 | ||
287 |
$table->update_all(param => \%param); |
|
288 |
|
|
289 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
290 |
you don't have to specify table name. |