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