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 { |
|
43 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
44 |
} |
|
45 |
} |
|
46 | ||
- added EXPERIMENTAL type() ...
|
47 |
my @methods = qw/insert insert_at update update_at update_all |
48 |
delete delete_at delete_all select select_at/; |
|
49 |
foreach my $method (@methods) { |
|
50 | ||
51 |
my $code = sub { |
|
52 |
my $self = shift; |
|
53 |
|
|
54 |
my @args = (table => $self->table, type => $self->type); |
|
55 |
push @args, (primary_key => $self->primary_key) if $method =~ /_at$/; |
|
56 |
push @args, (join => $self->join) if $method =~ /^select/; |
|
57 |
|
|
58 |
$self->dbi->$method(@args, @_); |
|
59 |
}; |
|
60 |
|
|
61 |
no strict 'refs'; |
|
62 |
my $class = __PACKAGE__; |
|
63 |
*{"${class}::$method"} = $code; |
|
64 |
} |
|
65 | ||
- added EXPERIMENTAL DBIx::C...
|
66 |
sub column { |
67 |
my ($self, $table, $columns) = @_; |
|
68 |
|
|
69 |
$self->{_table_alias} ||= {}; |
|
70 |
my $dist; |
|
71 |
$dist = $self->dbi->{_table_alias}{$table} |
|
72 |
? $self->dbi->{_table_alias}{$table} |
|
73 |
: $table; |
|
74 |
|
|
75 |
$self->dbi->{_model_from} ||= {}; |
|
76 |
my $model = $self->dbi->{_model_from}->{$dist}; |
|
77 |
|
|
78 |
$columns ||= $self->model($model)->columns; |
|
79 |
|
|
cleanup
|
80 |
return $self->dbi->column($table, $columns); |
- added EXPERIMENTAL DBIx::C...
|
81 |
} |
82 | ||
select method column option ...
|
83 |
sub DESTROY { } |
84 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
85 |
sub method { |
86 |
my $self = shift; |
|
87 |
|
|
88 |
# Merge |
|
89 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
90 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
91 |
|
|
92 |
return $self; |
|
93 |
} |
|
94 | ||
cleanup
|
95 |
sub mycolumn { |
96 |
my $self = shift; |
|
97 |
my $table = shift unless ref $_[0]; |
|
98 |
my $columns = shift; |
|
99 |
|
|
100 |
$table ||= $self->table || ''; |
|
101 |
$columns ||= $self->columns; |
|
102 |
|
|
103 |
return $self->dbi->mycolumn($table, $columns); |
|
104 |
} |
|
105 | ||
removed EXPERIMETNAL flag fr...
|
106 |
sub new { |
107 |
my $self = shift->SUPER::new(@_); |
|
108 |
|
|
109 |
# Check attribute names |
|
110 |
my @attrs = keys %$self; |
|
111 |
foreach my $attr (@attrs) { |
|
112 |
croak qq{"$attr" is invalid attribute name} |
|
113 |
unless $self->can($attr); |
|
114 |
} |
|
115 |
|
|
116 |
return $self; |
|
117 |
} |
|
118 | ||
added experimental DBIx::Cus...
|
119 |
1; |
120 | ||
121 |
=head1 NAME |
|
122 | ||
- removed DEPRECATED DBIx::C...
|
123 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
124 | |
125 |
=head1 SYNOPSIS |
|
126 | ||
127 |
use DBIx::Custom::Table; |
|
128 | ||
add feture. all model class ...
|
129 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
130 | |
add DBIx::Custom::Model fore...
|
131 |
=head1 ATTRIBUTES |
132 | ||
133 |
=head2 C<dbi> |
|
134 | ||
135 |
my $dbi = $model->dbi; |
|
136 |
$model = $model->dbi($dbi); |
|
137 | ||
138 |
L<DBIx::Custom> object. |
|
139 | ||
add experimental DBIx::Custo...
|
140 |
=head2 C<filter> |
141 | ||
142 |
my $dbi = $model->filter |
|
143 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
144 | ||
- added EXPERIMENTAL DBIx::C...
|
145 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
146 | |
add experimental DBIx::Custo...
|
147 |
=head2 C<name> |
148 | ||
149 |
my $name = $model->name; |
|
150 |
$model = $model->name('book'); |
|
151 | ||
152 |
Model name. |
|
153 | ||
update pod
|
154 |
=head2 C<join> |
- added experimental DBIx::C...
|
155 | |
156 |
my $join = $model->join; |
|
157 |
$model = $model->join( |
|
158 |
['left outer join company on book.company_id = company.id'] |
|
159 |
); |
|
160 |
|
|
- added EXPERIMENTAL DBIx::C...
|
161 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
162 | |
add DBIx::Custom::Model fore...
|
163 |
=head2 C<table> |
164 | ||
165 |
my $table = $model->table; |
|
166 |
$model = $model->table('book'); |
|
167 | ||
- added EXPERIMENTAL DBIx::C...
|
168 |
Table name, this is used as C<select()> C<table> option. |
169 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
170 | |
add DBIx::Custom::Model fore...
|
171 |
=head2 C<primary_key> |
172 | ||
173 |
my $primary_key = $model->primary_key; |
|
174 |
$model = $model->primary_key(['id', 'number']); |
|
175 | ||
- added EXPERIMENTAL DBIx::C...
|
176 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
update pod
|
177 |
C<delete_at()>,C<select_at()>. |
add DBIx::Custom::Model fore...
|
178 | |
- added EXPERIMENTAL type() ...
|
179 |
=head2 C<type> |
180 | ||
181 |
my $type = $model->type; |
|
182 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
183 |
|
|
184 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
185 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
186 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
187 | |
- added EXPERIMENTAL DBIx::C...
|
188 |
=head2 C<view> |
189 | ||
190 |
my $view = $model->view; |
|
191 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
192 | ||
193 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
194 |
model is included by C<include_model>. |
|
195 | ||
added experimental DBIx::Cus...
|
196 |
=head1 METHODS |
197 | ||
table object call dbi object...
|
198 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
199 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
200 |
and implements the following new ones. |
201 | ||
cleanup
|
202 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
203 | |
cleanup
|
204 |
my $column = $self->column(book => ['author', 'title']); |
205 |
my $column = $self->column('book'); |
|
add experimental DBIx::Custo...
|
206 | |
cleanup
|
207 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
208 | |
cleanup
|
209 |
book.author as book__author, |
210 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
211 | |
cleanup
|
212 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
213 | |
added insert, update, update...
|
214 |
=head2 C<delete> |
215 | ||
table object call dbi object...
|
216 |
$table->delete(...); |
added insert, update, update...
|
217 |
|
218 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
219 |
you don't have to specify C<table> option. |
added insert, update, update...
|
220 | |
221 |
=head2 C<delete_all> |
|
222 | ||
table object call dbi object...
|
223 |
$table->delete_all(...); |
added insert, update, update...
|
224 |
|
225 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
226 |
you don't have to specify C<table> option. |
added insert, update, update...
|
227 | |
update pod
|
228 |
=head2 C<delete_at> |
229 | ||
230 |
$table->delete_at(...); |
|
231 |
|
|
232 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
233 |
you don't have to specify C<table> and C<primary_key> option. |
|
234 | ||
added insert, update, update...
|
235 |
=head2 C<insert> |
236 | ||
table object call dbi object...
|
237 |
$table->insert(...); |
added insert, update, update...
|
238 |
|
239 |
Same as C<insert()> 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<insert> |
243 | ||
244 |
$table->insert_at(...); |
|
245 |
|
|
246 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
247 |
you don't have to specify C<table> and C<primary_key> option. |
|
248 | ||
- removed DEPRECATED DBIx::C...
|
249 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
250 | |
251 |
$model->method( |
|
252 |
update_or_insert => sub { |
|
253 |
my $self = shift; |
|
254 |
|
|
255 |
# ... |
|
256 |
}, |
|
257 |
find_or_create => sub { |
|
258 |
my $self = shift; |
|
259 |
|
|
260 |
# ... |
|
261 |
); |
|
262 | ||
263 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
264 | ||
265 |
$model->update_or_insert; |
|
266 |
$model->find_or_create; |
|
267 | ||
- added EXPERIMENTAL type() ...
|
268 |
=head2 C<mycolumn> |
cleanup
|
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. |