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