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 | ||
renamed experimental DBIx::C...
|
44 |
sub method { |
added experimental DBIx::Cus...
|
45 |
my $self = shift; |
46 |
|
|
47 |
# Merge |
|
renamed experimental DBIx::C...
|
48 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
49 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added experimental DBIx::Cus...
|
50 |
|
51 |
return $self; |
|
52 |
} |
|
53 | ||
DBIx::Custom::Model select()...
|
54 |
sub insert { |
55 |
my $self = shift; |
|
56 |
$self->dbi->insert(table => $self->table, @_); |
|
57 |
} |
|
58 | ||
59 |
sub update { |
|
60 |
my $self = shift; |
|
61 |
$self->dbi->update(table => $self->table, @_) |
|
62 |
} |
|
63 | ||
64 |
sub update_all { |
|
65 |
my $self = shift; |
|
66 |
$self->dbi->update_all(table => $self->table, @_); |
|
67 |
} |
|
68 | ||
69 |
sub delete { |
|
70 |
my $self = shift; |
|
71 |
$self->dbi->delete(table => $self->table, @_); |
|
72 |
} |
|
73 | ||
74 |
sub delete_all { |
|
75 |
my $self = shift; |
|
76 |
$self->dbi->delete_all(table => $self->table, @_); |
|
77 |
} |
|
78 | ||
79 |
sub select { |
|
80 |
my $self = shift; |
|
81 |
$self->dbi->select( |
|
82 |
table => $self->table, |
|
83 |
relation => $self->relation, |
|
84 |
@_ |
|
85 |
); |
|
86 |
} |
|
add experimental DBIx::Custo...
|
87 | |
88 |
sub update_at { |
|
89 |
my $self = shift; |
|
added insert, update, update...
|
90 |
|
add experimental DBIx::Custo...
|
91 |
return $self->dbi->update_at( |
92 |
table => $self->table, |
|
93 |
primary_key => $self->primary_key, |
|
94 |
@_ |
|
95 |
); |
|
96 |
} |
|
97 | ||
98 |
sub delete_at { |
|
99 |
my $self = shift; |
|
100 |
|
|
101 |
return $self->dbi->delete_at( |
|
102 |
table => $self->table, |
|
103 |
primary_key => $self->primary_key, |
|
104 |
@_ |
|
105 |
); |
|
106 |
} |
|
107 | ||
108 |
sub select_at { |
|
109 |
my $self = shift; |
|
many changed
|
110 |
|
add experimental DBIx::Custo...
|
111 |
return $self->dbi->select_at( |
112 |
table => $self->table, |
|
113 |
primary_key => $self->primary_key, |
|
DBIx::Custom::Model select()...
|
114 |
relation => $self->relation, |
add experimental DBIx::Custo...
|
115 |
@_ |
116 |
); |
|
added insert, update, update...
|
117 |
} |
118 | ||
added experimental DBIx::Cus...
|
119 |
sub DESTROY { } |
120 | ||
121 |
1; |
|
122 | ||
123 |
=head1 NAME |
|
124 | ||
add DBIx::Custom::Model fore...
|
125 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
126 | |
127 |
=head1 SYNOPSIS |
|
128 | ||
129 |
use DBIx::Custom::Table; |
|
130 | ||
add feture. all model class ...
|
131 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
132 | |
add DBIx::Custom::Model fore...
|
133 |
=head1 ATTRIBUTES |
134 | ||
add DBIx::Custom::Model colu...
|
135 |
=head2 C<(experimental) columns> |
136 | ||
137 |
my $columns = $model->columns; |
|
138 |
$model = $model->columns(['id', 'number']); |
|
139 | ||
add DBIx::Custom::Model fore...
|
140 |
=head2 C<dbi> |
141 | ||
142 |
my $dbi = $model->dbi; |
|
143 |
$model = $model->dbi($dbi); |
|
144 | ||
145 |
L<DBIx::Custom> object. |
|
146 | ||
147 |
=head2 C<table> |
|
148 | ||
149 |
my $table = $model->table; |
|
150 |
$model = $model->table('book'); |
|
151 | ||
152 |
Table name. |
|
153 |
|
|
154 |
=head2 C<primary_key> |
|
155 | ||
156 |
my $primary_key = $model->primary_key; |
|
157 |
$model = $model->primary_key(['id', 'number']); |
|
158 | ||
159 |
Foreign key. This is used by C<update_at()>, C<delete_at()>, |
|
160 |
C<select_at()>. |
|
161 | ||
added experimental DBIx::Cus...
|
162 |
=head1 METHODS |
163 | ||
table object call dbi object...
|
164 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
165 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
166 |
and implements the following new ones. |
167 | ||
added insert, update, update...
|
168 |
=head2 C<delete> |
169 | ||
table object call dbi object...
|
170 |
$table->delete(...); |
added insert, update, update...
|
171 |
|
172 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
173 |
you don't have to specify C<table> option. |
added insert, update, update...
|
174 | |
175 |
=head2 C<delete_all> |
|
176 | ||
table object call dbi object...
|
177 |
$table->delete_all(...); |
added insert, update, update...
|
178 |
|
179 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
180 |
you don't have to specify C<table> option. |
added insert, update, update...
|
181 | |
renamed experimental DBIx::C...
|
182 |
=head2 C<method> |
added experimental DBIx::Cus...
|
183 | |
table object call dbi object...
|
184 |
$table->method( |
185 |
count => sub { |
|
186 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
187 |
|
table object call dbi object...
|
188 |
return $self->select(column => 'count(*)', @_) |
189 |
->fetch_first->[0]; |
|
190 |
} |
|
191 |
); |
|
added experimental DBIx::Cus...
|
192 |
|
renamed experimental DBIx::C...
|
193 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
194 | |
added insert, update, update...
|
195 |
=head2 C<insert> |
196 | ||
table object call dbi object...
|
197 |
$table->insert(...); |
added insert, update, update...
|
198 |
|
199 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
200 |
you don't have to specify C<table> option. |
added insert, update, update...
|
201 | |
202 |
=head2 C<new> |
|
203 | ||
204 |
my $table = DBIx::Custom::Table->new; |
|
205 | ||
206 |
Create a L<DBIx::Custom::Table> object. |
|
207 | ||
208 |
=head2 C<select> |
|
209 | ||
table object call dbi object...
|
210 |
$table->select(...); |
added insert, update, update...
|
211 |
|
212 |
Same as C<select()> 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 | |
215 |
=head2 C<update> |
|
216 | ||
table object call dbi object...
|
217 |
$table->update(...); |
added insert, update, update...
|
218 |
|
219 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
220 |
you don't have to specify C<table> option. |
added insert, update, update...
|
221 | |
222 |
=head2 C<update_all> |
|
223 | ||
224 |
$table->update_all(param => \%param); |
|
225 |
|
|
226 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
227 |
you don't have to specify table name. |