add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
updatedd pod
|
2 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
3 | |
4 |
use Carp 'croak'; |
|
cleanup
|
5 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
6 | |
added experimental DBIx::Cus...
|
7 |
# Carp trust relationship |
8 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
9 | ||
added EXPERIMENTAL DBIx::Cus...
|
10 |
has [qw/dbi table created_at updated_at bind_type join primary_key/], |
11 |
columns => sub { [] }; |
|
added experimental DBIx::Cus...
|
12 | |
- added EXPERIMENTAL DBIx::C...
|
13 |
our $AUTOLOAD; |
14 | ||
15 |
sub AUTOLOAD { |
|
16 |
my $self = shift; |
|
17 | ||
18 |
# Method name |
|
19 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
20 | ||
21 |
# Method |
|
adeed EXPERIMENTAL DBIx::Cus...
|
22 |
$self->{_methods} ||= {}; |
23 |
if (my $method = $self->{_methods}->{$mname}) { |
|
24 |
return $self->$method(@_) |
|
25 |
} |
|
26 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
- added EXPERIMENTAL DBIx::C...
|
27 |
$self->dbi->$dbi_method(@_); |
28 |
} |
|
- removed EXPERIMENTAL Prefo...
|
29 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
30 |
$self->dbi->dbh->$dbh_method(@_); |
31 |
} |
|
32 |
else { |
|
cleanup
|
33 |
croak qq{Can't locate object method "$mname" via "$package" } |
34 |
. _subname; |
|
- added EXPERIMENTAL DBIx::C...
|
35 |
} |
36 |
} |
|
37 | ||
- added EXPERIMENTAL type() ...
|
38 |
my @methods = qw/insert insert_at update update_at update_all |
- added EXPERIMENTAL DBIx::C...
|
39 |
delete delete_at delete_all select select_at count/; |
cleanup
|
40 |
for my $method (@methods) { |
- added EXPERIMENTAL type() ...
|
41 | |
42 |
my $code = sub { |
|
43 |
my $self = shift; |
|
added EXPERIMENTAL DBIx::Cus...
|
44 |
|
45 |
unless ($self->{_attribute_cache}{$self}) { |
|
46 |
$self->$_ for qw/table bind_type primary_key |
|
47 |
type join created_at updated_at/; |
|
48 |
$self->{_attribute_cache}{$self} = 1; |
|
49 |
} |
|
50 | ||
- removed EXPERIMENTAL call_...
|
51 |
$self->dbi->$method( |
52 |
@_ % 2 ? shift : (), |
|
added EXPERIMENTAL DBIx::Cus...
|
53 |
table => $self->{table}, |
54 |
exists $self->{type} ? (type => $self->{type}) : (), |
|
55 |
exists $self->{created_at} && $method eq 'insert' ? (created_at => $self->{created_at}) : (), |
|
56 |
exists $self->{updated_at} && ($method eq 'insert' || $method eq 'update') ? (updated_at => $self->{updated_at}) : (), |
|
57 |
exists $self->{bind_type} ? (bind_type=> $self->{bind_type}) : (), |
|
58 |
exists $self->{primary_key} ? (primary_key => $self->{primary_key}) : (), |
|
59 |
exists $self->{join} && index($method, 'select') != -1 ? (join => $self->{join}) : (), |
|
- removed EXPERIMENTAL call_...
|
60 |
@_ |
61 |
) |
|
- added EXPERIMENTAL type() ...
|
62 |
}; |
63 |
|
|
64 |
no strict 'refs'; |
|
65 |
my $class = __PACKAGE__; |
|
66 |
*{"${class}::$method"} = $code; |
|
67 |
} |
|
68 | ||
- removed placeholder count ...
|
69 |
sub execute { |
70 |
my $self = shift; |
|
- removed EXPERIMENTAL the f...
|
71 |
return $self->dbi->execute( |
72 |
shift, |
|
73 |
shift, |
|
74 |
table => $self->table, |
|
75 |
bind_type => $self->bind_type, |
|
76 |
primary_key => $self->primary_key, |
|
77 |
type => $self->type, |
|
78 |
@_ |
|
79 |
); |
|
- removed placeholder count ...
|
80 |
} |
81 | ||
select method column option ...
|
82 |
sub DESTROY { } |
83 | ||
- method method of DBIx::Cus...
|
84 |
sub helper { |
adeed EXPERIMENTAL DBIx::Cus...
|
85 |
my $self = shift; |
86 |
|
|
87 |
# Merge |
|
88 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
89 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
90 |
|
|
91 |
return $self; |
|
92 |
} |
|
93 | ||
cleanup
|
94 |
sub mycolumn { |
95 |
my $self = shift; |
|
96 |
my $table = shift unless ref $_[0]; |
|
97 |
my $columns = shift; |
|
98 |
|
|
99 |
$table ||= $self->table || ''; |
|
100 |
$columns ||= $self->columns; |
|
101 |
|
|
102 |
return $self->dbi->mycolumn($table, $columns); |
|
103 |
} |
|
104 | ||
removed EXPERIMETNAL flag fr...
|
105 |
sub new { |
106 |
my $self = shift->SUPER::new(@_); |
|
107 |
|
|
108 |
# Check attribute names |
|
109 |
my @attrs = keys %$self; |
|
cleanup
|
110 |
for my $attr (@attrs) { |
cleanup
|
111 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
112 |
unless $self->can($attr); |
113 |
} |
|
114 |
|
|
115 |
return $self; |
|
116 |
} |
|
117 | ||
- DBIx::Custom Model filter ...
|
118 |
# DEPRECATED! |
micro optimization
|
119 |
has 'filter'; |
cleanup
|
120 |
has 'name'; |
added EXPERIMENTAL DBIx::Cus...
|
121 |
has 'type'; |
- DBIx::Custom Model filter ...
|
122 | |
- method method of DBIx::Cus...
|
123 | |
124 |
# DEPRECATED! |
|
125 |
sub method { |
|
126 |
warn "method method is DEPRECATED! use helper instead"; |
|
127 |
return shift->helper(@_); |
|
128 |
} |
|
129 | ||
added experimental DBIx::Cus...
|
130 |
1; |
131 | ||
132 |
=head1 NAME |
|
133 | ||
- removed DEPRECATED DBIx::C...
|
134 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
135 | |
136 |
=head1 SYNOPSIS |
|
137 | ||
- removed placeholder count ...
|
138 |
use DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
139 | |
- removed placeholder count ...
|
140 |
my $model = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
141 | |
add DBIx::Custom::Model fore...
|
142 |
=head1 ATTRIBUTES |
143 | ||
144 |
=head2 C<dbi> |
|
145 | ||
146 |
my $dbi = $model->dbi; |
|
DBIx::Custom::Model type att...
|
147 |
$model = $model->dbi($dbi); |
add DBIx::Custom::Model fore...
|
148 | |
149 |
L<DBIx::Custom> object. |
|
150 | ||
update pod
|
151 |
=head2 C<join> |
- added experimental DBIx::C...
|
152 | |
153 |
my $join = $model->join; |
|
DBIx::Custom::Model type att...
|
154 |
$model = $model->join( |
- added experimental DBIx::C...
|
155 |
['left outer join company on book.company_id = company.id'] |
156 |
); |
|
157 |
|
|
DBIx::Custom::Model type att...
|
158 |
Join clause, this value is passed to C<select> method. |
- added experimental DBIx::C...
|
159 | |
added DBIx::Custom result_fi...
|
160 |
=head2 C<primary_key> |
161 | ||
162 |
my $primary_key = $model->primary_key; |
|
DBIx::Custom::Model type att...
|
163 |
$model = $model->primary_key(['id', 'number']); |
added DBIx::Custom result_fi...
|
164 | |
DBIx::Custom::Model type att...
|
165 |
Primary key,this is passed to C<insert>, C<update>, |
166 |
C<delete>, and C<select> method. |
|
added DBIx::Custom result_fi...
|
167 | |
add DBIx::Custom::Model fore...
|
168 |
=head2 C<table> |
169 | ||
- removed placeholder count ...
|
170 |
my $model = $model->table; |
DBIx::Custom::Model type att...
|
171 |
$model = $model->table('book'); |
add DBIx::Custom::Model fore...
|
172 | |
DBIx::Custom::Model type att...
|
173 |
Table name, this is passed to C<select> method. |
add experimental DBIx::Custo...
|
174 | |
DBIx::Custom::Model type att...
|
175 |
=head2 C<bind_type> |
- added EXPERIMENTAL type() ...
|
176 | |
DBIx::Custom::Model type att...
|
177 |
my $type = $model->bind_type; |
178 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
- added EXPERIMENTAL type() ...
|
179 |
|
DBIx::Custom::Model type att...
|
180 |
Database data type, this is used as type optioon of C<insert>, |
181 |
C<update>, C<update_all>, C<delete>, C<delete_all>, |
|
182 |
C<select>, and C<execute> method |
|
- added EXPERIMENTAL type() ...
|
183 | |
added experimental DBIx::Cus...
|
184 |
=head1 METHODS |
185 | ||
- DBIx::Custom Model filter ...
|
186 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
187 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
188 |
and implements the following new ones. |
189 | ||
- removed EXPERIMENTAL the f...
|
190 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
191 | |
192 |
my $count = $model->count; |
|
193 | ||
194 |
Get rows count. |
|
195 | ||
196 |
Options is same as C<select> method's ones. |
|
197 | ||
added insert, update, update...
|
198 |
=head2 C<delete> |
199 | ||
- removed placeholder count ...
|
200 |
$model->delete(...); |
added insert, update, update...
|
201 |
|
DBIx::Custom::Model type att...
|
202 |
Same as C<delete> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
203 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
204 | |
205 |
=head2 C<delete_all> |
|
206 | ||
- removed placeholder count ...
|
207 |
$model->delete_all(...); |
added insert, update, update...
|
208 |
|
DBIx::Custom::Model type att...
|
209 |
Same as C<delete_all> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
210 |
you don't have to specify C<table> and C<primary_key> option. |
211 | ||
- removed EXPERIMENTAL the f...
|
212 |
=head2 C<execute> |
- removed placeholder count ...
|
213 | |
214 |
$model->execute(...); |
|
215 | ||
216 |
Same as C<execute> of L<DBIx::Custom> except that |
|
217 |
you don't have to specify C<table> and C<primary_key> option. |
|
added insert, update, update...
|
218 | |
219 |
=head2 C<insert> |
|
220 | ||
- removed placeholder count ...
|
221 |
$model->insert(...); |
added insert, update, update...
|
222 |
|
DBIx::Custom::Model type att...
|
223 |
Same as C<insert> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
224 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
225 | |
- method method of DBIx::Cus...
|
226 |
=head2 C<helper> |
adeed EXPERIMENTAL DBIx::Cus...
|
227 | |
- method method of DBIx::Cus...
|
228 |
$model->helper( |
adeed EXPERIMENTAL DBIx::Cus...
|
229 |
update_or_insert => sub { |
230 |
my $self = shift; |
|
231 |
|
|
232 |
# ... |
|
233 |
}, |
|
234 |
find_or_create => sub { |
|
235 |
my $self = shift; |
|
236 |
|
|
237 |
# ... |
|
238 |
); |
|
239 | ||
- method method of DBIx::Cus...
|
240 |
Register helper. These helper is called directly from L<DBIx::Custom::Model> object. |
adeed EXPERIMENTAL DBIx::Cus...
|
241 | |
242 |
$model->update_or_insert; |
|
243 |
$model->find_or_create; |
|
244 | ||
- added EXPERIMENTAL type() ...
|
245 |
=head2 C<mycolumn> |
cleanup
|
246 | |
247 |
my $column = $self->mycolumn; |
|
248 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
249 |
my $column = $self->mycolumn(['author', 'title']); |
|
250 | ||
251 |
Create column clause for myself. The follwoing column clause is created. |
|
252 | ||
253 |
book.author as author, |
|
254 |
book.title as title |
|
255 | ||
256 |
If table name is ommited, C<table> attribute of the model is used. |
|
257 |
If column names is omitted, C<columns> attribute of the model is used. |
|
258 | ||
added insert, update, update...
|
259 |
=head2 C<new> |
260 | ||
- removed placeholder count ...
|
261 |
my $model = DBIx::Custom::Model->new; |
added insert, update, update...
|
262 | |
- removed placeholder count ...
|
263 |
Create a L<DBIx::Custom::Model> object. |
added insert, update, update...
|
264 | |
265 |
=head2 C<select> |
|
266 | ||
- removed placeholder count ...
|
267 |
$model->select(...); |
added insert, update, update...
|
268 |
|
DBIx::Custom::Model type att...
|
269 |
Same as C<select> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
270 |
you don't have to specify C<table>, C<primary_key> and C<jon> option. |
added insert, update, update...
|
271 | |
272 |
=head2 C<update> |
|
273 | ||
- removed placeholder count ...
|
274 |
$model->update(...); |
added insert, update, update...
|
275 |
|
DBIx::Custom::Model type att...
|
276 |
Same as C<update> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
277 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
278 | |
279 |
=head2 C<update_all> |
|
280 | ||
- removed placeholder count ...
|
281 |
$model->update_all(param => \%param); |
added insert, update, update...
|
282 |
|
DBIx::Custom::Model type att...
|
283 |
Same as C<update_all> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
284 |
you don't have to specify C<table> and C<primary_key> option. |
update pod
|
285 | |
update pod
|
286 |
=cut |