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 | ||
added experimental DBIx::Cus...
|
92 |
1; |
93 | ||
94 |
=head1 NAME |
|
95 | ||
update pod
|
96 |
DBIx::Custom::Model - Model EXPERIMENTAL |
added experimental DBIx::Cus...
|
97 | |
98 |
=head1 SYNOPSIS |
|
99 | ||
100 |
use DBIx::Custom::Table; |
|
101 | ||
add feture. all model class ...
|
102 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
103 | |
add DBIx::Custom::Model fore...
|
104 |
=head1 ATTRIBUTES |
105 | ||
106 |
=head2 C<dbi> |
|
107 | ||
108 |
my $dbi = $model->dbi; |
|
109 |
$model = $model->dbi($dbi); |
|
110 | ||
111 |
L<DBIx::Custom> object. |
|
112 | ||
add experimental DBIx::Custo...
|
113 |
=head2 C<filter> |
114 | ||
115 |
my $dbi = $model->filter |
|
116 |
$model = $model->filter({out => 'tp_to_date', in => 'date_to_tp'}); |
|
117 | ||
- added EXPERIMENTAL DBIx::C...
|
118 |
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called. |
add experimental DBIx::Custo...
|
119 | |
add experimental DBIx::Custo...
|
120 |
=head2 C<name> |
121 | ||
122 |
my $name = $model->name; |
|
123 |
$model = $model->name('book'); |
|
124 | ||
125 |
Model name. |
|
126 | ||
update pod
|
127 |
=head2 C<join> |
- added experimental DBIx::C...
|
128 | |
129 |
my $join = $model->join; |
|
130 |
$model = $model->join( |
|
131 |
['left outer join company on book.company_id = company.id'] |
|
132 |
); |
|
133 |
|
|
- added EXPERIMENTAL DBIx::C...
|
134 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
135 | |
add DBIx::Custom::Model fore...
|
136 |
=head2 C<table> |
137 | ||
138 |
my $table = $model->table; |
|
139 |
$model = $model->table('book'); |
|
140 | ||
- added EXPERIMENTAL DBIx::C...
|
141 |
Table name, this is used as C<select()> C<table> option. |
142 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
143 | |
add DBIx::Custom::Model fore...
|
144 |
=head2 C<primary_key> |
145 | ||
146 |
my $primary_key = $model->primary_key; |
|
147 |
$model = $model->primary_key(['id', 'number']); |
|
148 | ||
- added EXPERIMENTAL DBIx::C...
|
149 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
update pod
|
150 |
C<delete_at()>,C<select_at()>. |
add DBIx::Custom::Model fore...
|
151 | |
- added EXPERIMENTAL type() ...
|
152 |
=head2 C<type> |
153 | ||
154 |
my $type = $model->type; |
|
155 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
156 |
|
|
157 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
158 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
159 |
C<select(), C<select_at()> |
|
160 | ||
- added EXPERIMENTAL DBIx::C...
|
161 |
=head2 C<view> |
162 | ||
163 |
my $view = $model->view; |
|
164 |
$model = $model->view('select id, DATE(issue_datetime) as date from book'); |
|
165 | ||
166 |
View. This view is registered by C<view()> of L<DBIx::Custom> when |
|
167 |
model is included by C<include_model>. |
|
168 | ||
added experimental DBIx::Cus...
|
169 |
=head1 METHODS |
170 | ||
table object call dbi object...
|
171 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
172 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
173 |
and implements the following new ones. |
174 | ||
cleanup
|
175 |
=head2 C<column> EXPERIMETNAL |
add experimental DBIx::Custo...
|
176 | |
cleanup
|
177 |
my $column = $self->column(book => ['author', 'title']); |
178 |
my $column = $self->column('book'); |
|
add experimental DBIx::Custo...
|
179 | |
cleanup
|
180 |
Create column clause. The follwoing column clause is created. |
add experimental DBIx::Custo...
|
181 | |
cleanup
|
182 |
book.author as book__author, |
183 |
book.title as book__title |
|
add experimental DBIx::Custo...
|
184 | |
cleanup
|
185 |
If column names is omitted, C<columns> attribute of the model is used. |
- added experimental DBIx::C...
|
186 | |
added insert, update, update...
|
187 |
=head2 C<delete> |
188 | ||
table object call dbi object...
|
189 |
$table->delete(...); |
added insert, update, update...
|
190 |
|
191 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
192 |
you don't have to specify C<table> option. |
added insert, update, update...
|
193 | |
194 |
=head2 C<delete_all> |
|
195 | ||
table object call dbi object...
|
196 |
$table->delete_all(...); |
added insert, update, update...
|
197 |
|
198 |
Same as C<delete_all()> 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 | |
update pod
|
201 |
=head2 C<delete_at> |
202 | ||
203 |
$table->delete_at(...); |
|
204 |
|
|
205 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
206 |
you don't have to specify C<table> and C<primary_key> option. |
|
207 | ||
added insert, update, update...
|
208 |
=head2 C<insert> |
209 | ||
table object call dbi object...
|
210 |
$table->insert(...); |
added insert, update, update...
|
211 |
|
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 | |
update pod
|
215 |
=head2 C<insert> |
216 | ||
217 |
$table->insert_at(...); |
|
218 |
|
|
219 |
Same as C<insert_at()> of L<DBIx::Custom> except that |
|
220 |
you don't have to specify C<table> and C<primary_key> option. |
|
221 | ||
- added EXPERIMENTAL type() ...
|
222 |
=head2 C<mycolumn> |
cleanup
|
223 | |
224 |
my $column = $self->mycolumn; |
|
225 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
226 |
my $column = $self->mycolumn(['author', 'title']); |
|
227 | ||
228 |
Create column clause for myself. The follwoing column clause is created. |
|
229 | ||
230 |
book.author as author, |
|
231 |
book.title as title |
|
232 | ||
233 |
If table name is ommited, C<table> attribute of the model is used. |
|
234 |
If column names is omitted, C<columns> attribute of the model is used. |
|
235 | ||
added insert, update, update...
|
236 |
=head2 C<new> |
237 | ||
238 |
my $table = DBIx::Custom::Table->new; |
|
239 | ||
240 |
Create a L<DBIx::Custom::Table> object. |
|
241 | ||
242 |
=head2 C<select> |
|
243 | ||
table object call dbi object...
|
244 |
$table->select(...); |
added insert, update, update...
|
245 |
|
246 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
247 |
you don't have to specify C<table> option. |
added insert, update, update...
|
248 | |
update pod
|
249 |
=head2 C<select_at> |
250 | ||
251 |
$table->select_at(...); |
|
252 |
|
|
253 |
Same as C<select_at()> of L<DBIx::Custom> except that |
|
254 |
you don't have to specify C<table> and C<primary_key> option. |
|
255 | ||
added insert, update, update...
|
256 |
=head2 C<update> |
257 | ||
table object call dbi object...
|
258 |
$table->update(...); |
added insert, update, update...
|
259 |
|
260 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
261 |
you don't have to specify C<table> option. |
added insert, update, update...
|
262 | |
263 |
=head2 C<update_all> |
|
264 | ||
265 |
$table->update_all(param => \%param); |
|
266 |
|
|
267 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
268 |
you don't have to specify table name. |
|
update pod
|
269 | |
270 |
=head2 C<update_at> |
|
271 | ||
272 |
$table->update_at(...); |
|
273 |
|
|
274 |
Same as C<update_at()> of L<DBIx::Custom> except that |
|
275 |
you don't have to specify C<table> and C<primary_key> option. |