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