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; |
|
- removed EXPERIMENTAL the f...
|
65 |
return $self->dbi->execute( |
66 |
shift, |
|
67 |
shift, |
|
68 |
table => $self->table, |
|
69 |
bind_type => $self->bind_type, |
|
70 |
primary_key => $self->primary_key, |
|
71 |
type => $self->type, |
|
72 |
@_ |
|
73 |
); |
|
- removed placeholder count ...
|
74 |
} |
75 | ||
select method column option ...
|
76 |
sub DESTROY { } |
77 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
78 |
sub method { |
79 |
my $self = shift; |
|
80 |
|
|
81 |
# Merge |
|
82 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
83 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
84 |
|
|
85 |
return $self; |
|
86 |
} |
|
87 | ||
cleanup
|
88 |
sub mycolumn { |
89 |
my $self = shift; |
|
90 |
my $table = shift unless ref $_[0]; |
|
91 |
my $columns = shift; |
|
92 |
|
|
93 |
$table ||= $self->table || ''; |
|
94 |
$columns ||= $self->columns; |
|
95 |
|
|
96 |
return $self->dbi->mycolumn($table, $columns); |
|
97 |
} |
|
98 | ||
removed EXPERIMETNAL flag fr...
|
99 |
sub new { |
100 |
my $self = shift->SUPER::new(@_); |
|
101 |
|
|
102 |
# Check attribute names |
|
103 |
my @attrs = keys %$self; |
|
104 |
foreach my $attr (@attrs) { |
|
cleanup
|
105 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
106 |
unless $self->can($attr); |
107 |
} |
|
108 |
|
|
109 |
return $self; |
|
110 |
} |
|
111 | ||
- DBIx::Custom Model filter ...
|
112 |
# DEPRECATED! |
micro optimization
|
113 |
has 'filter'; |
cleanup
|
114 |
has 'name'; |
DBIx::Custom::Model type att...
|
115 |
has type => sub { [] }; |
- DBIx::Custom Model filter ...
|
116 | |
added experimental DBIx::Cus...
|
117 |
1; |
118 | ||
119 |
=head1 NAME |
|
120 | ||
- removed DEPRECATED DBIx::C...
|
121 |
DBIx::Custom::Model - Model |
added experimental DBIx::Cus...
|
122 | |
123 |
=head1 SYNOPSIS |
|
124 | ||
- removed placeholder count ...
|
125 |
use DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
126 | |
- removed placeholder count ...
|
127 |
my $model = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
128 | |
add DBIx::Custom::Model fore...
|
129 |
=head1 ATTRIBUTES |
130 | ||
131 |
=head2 C<dbi> |
|
132 | ||
133 |
my $dbi = $model->dbi; |
|
DBIx::Custom::Model type att...
|
134 |
$model = $model->dbi($dbi); |
add DBIx::Custom::Model fore...
|
135 | |
136 |
L<DBIx::Custom> object. |
|
137 | ||
update pod
|
138 |
=head2 C<join> |
- added experimental DBIx::C...
|
139 | |
140 |
my $join = $model->join; |
|
DBIx::Custom::Model type att...
|
141 |
$model = $model->join( |
- added experimental DBIx::C...
|
142 |
['left outer join company on book.company_id = company.id'] |
143 |
); |
|
144 |
|
|
DBIx::Custom::Model type att...
|
145 |
Join clause, this value is passed to C<select> method. |
- added experimental DBIx::C...
|
146 | |
added DBIx::Custom result_fi...
|
147 |
=head2 C<primary_key> |
148 | ||
149 |
my $primary_key = $model->primary_key; |
|
DBIx::Custom::Model type att...
|
150 |
$model = $model->primary_key(['id', 'number']); |
added DBIx::Custom result_fi...
|
151 | |
DBIx::Custom::Model type att...
|
152 |
Primary key,this is passed to C<insert>, C<update>, |
153 |
C<delete>, and C<select> method. |
|
added DBIx::Custom result_fi...
|
154 | |
add DBIx::Custom::Model fore...
|
155 |
=head2 C<table> |
156 | ||
- removed placeholder count ...
|
157 |
my $model = $model->table; |
DBIx::Custom::Model type att...
|
158 |
$model = $model->table('book'); |
add DBIx::Custom::Model fore...
|
159 | |
DBIx::Custom::Model type att...
|
160 |
Table name, this is passed to C<select> method. |
add experimental DBIx::Custo...
|
161 | |
DBIx::Custom::Model type att...
|
162 |
=head2 C<bind_type> |
- added EXPERIMENTAL type() ...
|
163 | |
DBIx::Custom::Model type att...
|
164 |
my $type = $model->bind_type; |
165 |
$model = $model->bind_type(['image' => DBI::SQL_BLOB]); |
|
- added EXPERIMENTAL type() ...
|
166 |
|
DBIx::Custom::Model type att...
|
167 |
Database data type, this is used as type optioon of C<insert>, |
168 |
C<update>, C<update_all>, C<delete>, C<delete_all>, |
|
169 |
C<select>, and C<execute> method |
|
- added EXPERIMENTAL type() ...
|
170 | |
added experimental DBIx::Cus...
|
171 |
=head1 METHODS |
172 | ||
- DBIx::Custom Model filter ...
|
173 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
174 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
175 |
and implements the following new ones. |
176 | ||
- removed EXPERIMENTAL the f...
|
177 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
178 | |
179 |
my $count = $model->count; |
|
180 | ||
181 |
Get rows count. |
|
182 | ||
183 |
Options is same as C<select> method's ones. |
|
184 | ||
added insert, update, update...
|
185 |
=head2 C<delete> |
186 | ||
- removed placeholder count ...
|
187 |
$model->delete(...); |
added insert, update, update...
|
188 |
|
DBIx::Custom::Model type att...
|
189 |
Same as C<delete> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
190 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
191 | |
192 |
=head2 C<delete_all> |
|
193 | ||
- removed placeholder count ...
|
194 |
$model->delete_all(...); |
added insert, update, update...
|
195 |
|
DBIx::Custom::Model type att...
|
196 |
Same as C<delete_all> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
197 |
you don't have to specify C<table> and C<primary_key> option. |
198 | ||
- removed EXPERIMENTAL the f...
|
199 |
=head2 C<execute> |
- removed placeholder count ...
|
200 | |
201 |
$model->execute(...); |
|
202 | ||
203 |
Same as C<execute> of L<DBIx::Custom> except that |
|
204 |
you don't have to specify C<table> and C<primary_key> option. |
|
added insert, update, update...
|
205 | |
206 |
=head2 C<insert> |
|
207 | ||
- removed placeholder count ...
|
208 |
$model->insert(...); |
added insert, update, update...
|
209 |
|
DBIx::Custom::Model type att...
|
210 |
Same as C<insert> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
211 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
212 | |
- removed DEPRECATED DBIx::C...
|
213 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
214 | |
215 |
$model->method( |
|
216 |
update_or_insert => sub { |
|
217 |
my $self = shift; |
|
218 |
|
|
219 |
# ... |
|
220 |
}, |
|
221 |
find_or_create => sub { |
|
222 |
my $self = shift; |
|
223 |
|
|
224 |
# ... |
|
225 |
); |
|
226 | ||
227 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
228 | ||
229 |
$model->update_or_insert; |
|
230 |
$model->find_or_create; |
|
231 | ||
- added EXPERIMENTAL type() ...
|
232 |
=head2 C<mycolumn> |
cleanup
|
233 | |
234 |
my $column = $self->mycolumn; |
|
235 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
236 |
my $column = $self->mycolumn(['author', 'title']); |
|
237 | ||
238 |
Create column clause for myself. The follwoing column clause is created. |
|
239 | ||
240 |
book.author as author, |
|
241 |
book.title as title |
|
242 | ||
243 |
If table name is ommited, C<table> attribute of the model is used. |
|
244 |
If column names is omitted, C<columns> attribute of the model is used. |
|
245 | ||
added insert, update, update...
|
246 |
=head2 C<new> |
247 | ||
- removed placeholder count ...
|
248 |
my $model = DBIx::Custom::Model->new; |
added insert, update, update...
|
249 | |
- removed placeholder count ...
|
250 |
Create a L<DBIx::Custom::Model> object. |
added insert, update, update...
|
251 | |
252 |
=head2 C<select> |
|
253 | ||
- removed placeholder count ...
|
254 |
$model->select(...); |
added insert, update, update...
|
255 |
|
DBIx::Custom::Model type att...
|
256 |
Same as C<select> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
257 |
you don't have to specify C<table>, C<primary_key> and C<jon> option. |
added insert, update, update...
|
258 | |
259 |
=head2 C<update> |
|
260 | ||
- removed placeholder count ...
|
261 |
$model->update(...); |
added insert, update, update...
|
262 |
|
DBIx::Custom::Model type att...
|
263 |
Same as C<update> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
264 |
you don't have to specify C<table> and C<primary_key> option. |
added insert, update, update...
|
265 | |
266 |
=head2 C<update_all> |
|
267 | ||
- removed placeholder count ...
|
268 |
$model->update_all(param => \%param); |
added insert, update, update...
|
269 |
|
DBIx::Custom::Model type att...
|
270 |
Same as C<update_all> of L<DBIx::Custom> except that |
- removed placeholder count ...
|
271 |
you don't have to specify C<table> and C<primary_key> option. |
update pod
|
272 | |
update pod
|
273 |
=cut |