add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
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( |
14 |
['dbi', 'table'], |
|
add DBIx::Custom::Model colu...
|
15 |
columns => sub { [] }, |
add experimental DBIx::Custo...
|
16 |
primary_key => sub { [] }, |
17 |
relation => sub { {} } |
|
add DBIx::Custom::Model fore...
|
18 |
); |
added experimental DBIx::Cus...
|
19 | |
20 |
our $AUTOLOAD; |
|
21 | ||
22 |
sub AUTOLOAD { |
|
23 |
my $self = shift; |
|
24 | ||
renamed experimental DBIx::C...
|
25 |
# Method name |
26 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added experimental DBIx::Cus...
|
27 | |
renamed experimental DBIx::C...
|
28 |
# Method |
29 |
$self->{_methods} ||= {}; |
|
table object call dbi object...
|
30 |
if (my $method = $self->{_methods}->{$mname}) { |
31 |
return $self->$method(@_) |
|
32 |
} |
|
add feture. all model class ...
|
33 |
elsif ($self->dbi->can($mname)) { |
34 |
$self->dbi->$mname(@_); |
|
35 |
} |
|
36 |
elsif ($self->dbi->dbh->can($mname)) { |
|
37 |
$self->dbi->dbh->$mname(@_); |
|
38 |
} |
|
39 |
else { |
|
40 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
41 |
} |
|
added experimental DBIx::Cus...
|
42 |
} |
43 | ||
select method column option ...
|
44 |
sub column_clause { |
added experimental DBIx::Cus...
|
45 |
my $self = shift; |
46 |
|
|
select method column option ...
|
47 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
added experimental DBIx::Cus...
|
48 |
|
select method column option ...
|
49 |
my $table = $self->table; |
50 |
my $columns = $self->columns; |
|
51 |
my $add = $args->{add} || []; |
|
52 |
my $remove = $args->{remove} || []; |
|
53 |
my %remove = map {$_ => 1} @$remove; |
|
54 |
|
|
55 |
my @column; |
|
56 |
foreach my $column (@$columns) { |
|
57 |
push @column, "$table.$column as $column" |
|
58 |
unless $remove{$column}; |
|
59 |
} |
|
60 |
|
|
61 |
foreach my $column (@$add) { |
|
62 |
push @column, $column; |
|
63 |
} |
|
64 |
|
|
65 |
return join (', ', @column); |
|
added experimental DBIx::Cus...
|
66 |
} |
67 | ||
select method column option ...
|
68 |
sub delete { |
DBIx::Custom::Model select()...
|
69 |
my $self = shift; |
select method column option ...
|
70 |
$self->dbi->delete(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
71 |
} |
72 | ||
select method column option ...
|
73 |
sub delete_all { |
DBIx::Custom::Model select()...
|
74 |
my $self = shift; |
select method column option ...
|
75 |
$self->dbi->delete_all(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
76 |
} |
77 | ||
select method column option ...
|
78 |
sub delete_at { |
DBIx::Custom::Model select()...
|
79 |
my $self = shift; |
select method column option ...
|
80 |
|
81 |
return $self->dbi->delete_at( |
|
82 |
table => $self->table, |
|
83 |
primary_key => $self->primary_key, |
|
84 |
@_ |
|
85 |
); |
|
DBIx::Custom::Model select()...
|
86 |
} |
87 | ||
select method column option ...
|
88 |
sub DESTROY { } |
89 | ||
90 |
sub insert { |
|
DBIx::Custom::Model select()...
|
91 |
my $self = shift; |
select method column option ...
|
92 |
$self->dbi->insert(table => $self->table, @_); |
DBIx::Custom::Model select()...
|
93 |
} |
94 | ||
select method column option ...
|
95 |
sub method { |
DBIx::Custom::Model select()...
|
96 |
my $self = shift; |
select method column option ...
|
97 |
|
98 |
# Merge |
|
99 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
100 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
101 |
|
|
102 |
return $self; |
|
DBIx::Custom::Model select()...
|
103 |
} |
104 | ||
105 |
sub select { |
|
106 |
my $self = shift; |
|
107 |
$self->dbi->select( |
|
108 |
table => $self->table, |
|
109 |
relation => $self->relation, |
|
110 |
@_ |
|
111 |
); |
|
112 |
} |
|
add experimental DBIx::Custo...
|
113 | |
select method column option ...
|
114 |
sub select_at { |
add experimental DBIx::Custo...
|
115 |
my $self = shift; |
added insert, update, update...
|
116 |
|
select method column option ...
|
117 |
return $self->dbi->select_at( |
add experimental DBIx::Custo...
|
118 |
table => $self->table, |
119 |
primary_key => $self->primary_key, |
|
select method column option ...
|
120 |
relation => $self->relation, |
add experimental DBIx::Custo...
|
121 |
@_ |
122 |
); |
|
123 |
} |
|
124 | ||
select method column option ...
|
125 |
sub update { |
add experimental DBIx::Custo...
|
126 |
my $self = shift; |
select method column option ...
|
127 |
$self->dbi->update(table => $self->table, @_) |
add experimental DBIx::Custo...
|
128 |
} |
129 | ||
select method column option ...
|
130 |
sub update_all { |
131 |
my $self = shift; |
|
132 |
$self->dbi->update_all(table => $self->table, @_); |
|
133 |
} |
|
134 | ||
135 | ||
136 |
sub update_at { |
|
add experimental DBIx::Custo...
|
137 |
my $self = shift; |
many changed
|
138 |
|
select method column option ...
|
139 |
return $self->dbi->update_at( |
add experimental DBIx::Custo...
|
140 |
table => $self->table, |
141 |
primary_key => $self->primary_key, |
|
142 |
@_ |
|
143 |
); |
|
added insert, update, update...
|
144 |
} |
145 | ||
added experimental DBIx::Cus...
|
146 |
1; |
147 | ||
148 |
=head1 NAME |
|
149 | ||
add DBIx::Custom::Model fore...
|
150 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
151 | |
152 |
=head1 SYNOPSIS |
|
153 | ||
154 |
use DBIx::Custom::Table; |
|
155 | ||
add feture. all model class ...
|
156 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
157 | |
add DBIx::Custom::Model fore...
|
158 |
=head1 ATTRIBUTES |
159 | ||
add DBIx::Custom::Model colu...
|
160 |
=head2 C<(experimental) columns> |
161 | ||
162 |
my $columns = $model->columns; |
|
163 |
$model = $model->columns(['id', 'number']); |
|
164 | ||
add DBIx::Custom::Model fore...
|
165 |
=head2 C<dbi> |
166 | ||
167 |
my $dbi = $model->dbi; |
|
168 |
$model = $model->dbi($dbi); |
|
169 | ||
170 |
L<DBIx::Custom> object. |
|
171 | ||
172 |
=head2 C<table> |
|
173 | ||
174 |
my $table = $model->table; |
|
175 |
$model = $model->table('book'); |
|
176 | ||
177 |
Table name. |
|
178 |
|
|
179 |
=head2 C<primary_key> |
|
180 | ||
181 |
my $primary_key = $model->primary_key; |
|
182 |
$model = $model->primary_key(['id', 'number']); |
|
183 | ||
184 |
Foreign key. This is used by C<update_at()>, C<delete_at()>, |
|
185 |
C<select_at()>. |
|
186 | ||
added experimental DBIx::Cus...
|
187 |
=head1 METHODS |
188 | ||
table object call dbi object...
|
189 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
190 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
191 |
and implements the following new ones. |
192 | ||
added insert, update, update...
|
193 |
=head2 C<delete> |
194 | ||
table object call dbi object...
|
195 |
$table->delete(...); |
added insert, update, update...
|
196 |
|
197 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
198 |
you don't have to specify C<table> option. |
added insert, update, update...
|
199 | |
200 |
=head2 C<delete_all> |
|
201 | ||
table object call dbi object...
|
202 |
$table->delete_all(...); |
added insert, update, update...
|
203 |
|
204 |
Same as C<delete_all()> 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 | |
renamed experimental DBIx::C...
|
207 |
=head2 C<method> |
added experimental DBIx::Cus...
|
208 | |
table object call dbi object...
|
209 |
$table->method( |
210 |
count => sub { |
|
211 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
212 |
|
table object call dbi object...
|
213 |
return $self->select(column => 'count(*)', @_) |
214 |
->fetch_first->[0]; |
|
215 |
} |
|
216 |
); |
|
added experimental DBIx::Cus...
|
217 |
|
renamed experimental DBIx::C...
|
218 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
219 | |
added insert, update, update...
|
220 |
=head2 C<insert> |
221 | ||
table object call dbi object...
|
222 |
$table->insert(...); |
added insert, update, update...
|
223 |
|
224 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
225 |
you don't have to specify C<table> option. |
added insert, update, update...
|
226 | |
227 |
=head2 C<new> |
|
228 | ||
229 |
my $table = DBIx::Custom::Table->new; |
|
230 | ||
231 |
Create a L<DBIx::Custom::Table> object. |
|
232 | ||
233 |
=head2 C<select> |
|
234 | ||
table object call dbi object...
|
235 |
$table->select(...); |
added insert, update, update...
|
236 |
|
237 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
238 |
you don't have to specify C<table> option. |
added insert, update, update...
|
239 | |
240 |
=head2 C<update> |
|
241 | ||
table object call dbi object...
|
242 |
$table->update(...); |
added insert, update, update...
|
243 |
|
244 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
245 |
you don't have to specify C<table> option. |
added insert, update, update...
|
246 | |
247 |
=head2 C<update_all> |
|
248 | ||
249 |
$table->update_all(param => \%param); |
|
250 |
|
|
251 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
252 |
you don't have to specify table name. |