add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
updatedd pod
|
3 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
4 | |
5 |
use Carp 'croak'; |
|
cleanup
|
6 |
use DBIx::Custom::Util '_subname'; |
added experimental DBIx::Cus...
|
7 | |
added experimental DBIx::Cus...
|
8 |
# Carp trust relationship |
9 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
10 | ||
cleanup
|
11 |
has [qw/dbi table/], |
add DBIx::Custom::Model colu...
|
12 |
columns => sub { [] }, |
- added EXPERIMENTAL DBIx::C...
|
13 |
join => sub { [] }, |
- added EXPERIMENTAL type() ...
|
14 |
type => sub { [] }, |
updatedd pod
|
15 |
primary_key => sub { [] }; |
added experimental DBIx::Cus...
|
16 | |
- added EXPERIMENTAL DBIx::C...
|
17 |
our $AUTOLOAD; |
18 | ||
19 |
sub AUTOLOAD { |
|
20 |
my $self = shift; |
|
21 | ||
22 |
# Method name |
|
23 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
24 | ||
25 |
# Method |
|
adeed EXPERIMENTAL DBIx::Cus...
|
26 |
$self->{_methods} ||= {}; |
27 |
if (my $method = $self->{_methods}->{$mname}) { |
|
28 |
return $self->$method(@_) |
|
29 |
} |
|
30 |
elsif (my $dbi_method = $self->dbi->can($mname)) { |
|
- added EXPERIMENTAL DBIx::C...
|
31 |
$self->dbi->$dbi_method(@_); |
32 |
} |
|
- removed EXPERIMENTAL Prefo...
|
33 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
34 |
$self->dbi->dbh->$dbh_method(@_); |
35 |
} |
|
36 |
else { |
|
cleanup
|
37 |
croak qq{Can't locate object method "$mname" via "$package" } |
38 |
. _subname; |
|
- added EXPERIMENTAL DBIx::C...
|
39 |
} |
40 |
} |
|
41 | ||
- added EXPERIMENTAL type() ...
|
42 |
my @methods = qw/insert insert_at update update_at update_all |
43 |
delete delete_at delete_all select select_at/; |
|
44 |
foreach my $method (@methods) { |
|
45 | ||
46 |
my $code = sub { |
|
47 |
my $self = shift; |
|
- fixed bug that model inser...
|
48 | |
added tests
|
49 |
my @args = ( |
50 |
table => $self->table, |
|
51 |
type => $self->type, |
|
52 |
primary_key => $self->primary_key |
|
53 |
); |
|
- added EXPERIMENTAL type() ...
|
54 |
push @args, (join => $self->join) if $method =~ /^select/; |
- fixed bug that model inser...
|
55 |
unshift @args, shift if @_ % 2; |
- added EXPERIMENTAL type() ...
|
56 |
|
57 |
$self->dbi->$method(@args, @_); |
|
58 |
}; |
|
59 |
|
|
60 |
no strict 'refs'; |
|
61 |
my $class = __PACKAGE__; |
|
62 |
*{"${class}::$method"} = $code; |
|
63 |
} |
|
64 | ||
select method column option ...
|
65 |
sub DESTROY { } |
66 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
67 |
sub method { |
68 |
my $self = shift; |
|
69 |
|
|
70 |
# Merge |
|
71 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
72 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
73 |
|
|
74 |
return $self; |
|
75 |
} |
|
76 | ||
cleanup
|
77 |
sub mycolumn { |
78 |
my $self = shift; |
|
79 |
my $table = shift unless ref $_[0]; |
|
80 |
my $columns = shift; |
|
81 |
|
|
82 |
$table ||= $self->table || ''; |
|
83 |
$columns ||= $self->columns; |
|
84 |
|
|
85 |
return $self->dbi->mycolumn($table, $columns); |
|
86 |
} |
|
87 | ||
removed EXPERIMETNAL flag fr...
|
88 |
sub new { |
89 |
my $self = shift->SUPER::new(@_); |
|
90 |
|
|
91 |
# Check attribute names |
|
92 |
my @attrs = keys %$self; |
|
93 |
foreach my $attr (@attrs) { |
|
cleanup
|
94 |
croak qq{"$attr" is invalid attribute name } . _subname |
removed EXPERIMETNAL flag fr...
|
95 |
unless $self->can($attr); |
96 |
} |
|
97 |
|
|
98 |
return $self; |
|
99 |
} |
|
100 | ||
- DBIx::Custom Model filter ...
|
101 |
# DEPRECATED! |
102 |
has filter => sub { [] }; |
|
cleanup
|
103 |
has 'name'; |
- DBIx::Custom Model filter ...
|
104 | |
added experimental DBIx::Cus...
|
105 |
1; |
106 | ||
107 |
=head1 NAME |
|
108 | ||
- removed DEPRECATED DBIx::C...
|
109 |
DBIx::Custom::Model - Model |
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 | ||
update pod
|
126 |
=head2 C<join> |
- added experimental DBIx::C...
|
127 | |
128 |
my $join = $model->join; |
|
129 |
$model = $model->join( |
|
130 |
['left outer join company on book.company_id = company.id'] |
|
131 |
); |
|
132 |
|
|
- added EXPERIMENTAL DBIx::C...
|
133 |
Join clause, this is used as C<select()>'s C<join> option. |
- added experimental DBIx::C...
|
134 | |
added DBIx::Custom result_fi...
|
135 |
=head2 C<primary_key> |
136 | ||
137 |
my $primary_key = $model->primary_key; |
|
138 |
$model = $model->primary_key(['id', 'number']); |
|
139 | ||
140 |
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>, |
|
141 |
C<delete_at()>,C<select_at()>. |
|
142 | ||
add DBIx::Custom::Model fore...
|
143 |
=head2 C<table> |
144 | ||
145 |
my $table = $model->table; |
|
146 |
$model = $model->table('book'); |
|
147 | ||
- added EXPERIMENTAL DBIx::C...
|
148 |
Table name, this is used as C<select()> C<table> option. |
149 |
Generally, this is automatically set from class name. |
|
add experimental DBIx::Custo...
|
150 | |
- added EXPERIMENTAL type() ...
|
151 |
=head2 C<type> |
152 | ||
153 |
my $type = $model->type; |
|
154 |
$model = $model->type(['image' => DBI::SQL_BLOB]); |
|
155 |
|
|
156 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
|
157 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
|
where can recieve array refr...
|
158 |
C<select()>, C<select_at()> |
- added EXPERIMENTAL type() ...
|
159 | |
added experimental DBIx::Cus...
|
160 |
=head1 METHODS |
161 | ||
- DBIx::Custom Model filter ...
|
162 |
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>, |
163 |
and you can use all methods of L<DBIx::Custom> and L<DBI> |
|
added experimental DBIx::Cus...
|
164 |
and implements the following new ones. |
165 | ||
added insert, update, update...
|
166 |
=head2 C<delete> |
167 | ||
table object call dbi object...
|
168 |
$table->delete(...); |
added insert, update, update...
|
169 |
|
170 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
171 |
you don't have to specify C<table> option. |
added insert, update, update...
|
172 | |
173 |
=head2 C<delete_all> |
|
174 | ||
table object call dbi object...
|
175 |
$table->delete_all(...); |
added insert, update, update...
|
176 |
|
177 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
178 |
you don't have to specify C<table> option. |
added insert, update, update...
|
179 | |
180 |
=head2 C<insert> |
|
181 | ||
table object call dbi object...
|
182 |
$table->insert(...); |
added insert, update, update...
|
183 |
|
184 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
185 |
you don't have to specify C<table> option. |
added insert, update, update...
|
186 | |
- removed DEPRECATED DBIx::C...
|
187 |
=head2 C<method> |
adeed EXPERIMENTAL DBIx::Cus...
|
188 | |
189 |
$model->method( |
|
190 |
update_or_insert => sub { |
|
191 |
my $self = shift; |
|
192 |
|
|
193 |
# ... |
|
194 |
}, |
|
195 |
find_or_create => sub { |
|
196 |
my $self = shift; |
|
197 |
|
|
198 |
# ... |
|
199 |
); |
|
200 | ||
201 |
Register method. These method is called directly from L<DBIx::Custom::Model> object. |
|
202 | ||
203 |
$model->update_or_insert; |
|
204 |
$model->find_or_create; |
|
205 | ||
- added EXPERIMENTAL type() ...
|
206 |
=head2 C<mycolumn> |
cleanup
|
207 | |
208 |
my $column = $self->mycolumn; |
|
209 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
210 |
my $column = $self->mycolumn(['author', 'title']); |
|
211 | ||
212 |
Create column clause for myself. The follwoing column clause is created. |
|
213 | ||
214 |
book.author as author, |
|
215 |
book.title as title |
|
216 | ||
217 |
If table name is ommited, C<table> attribute of the model is used. |
|
218 |
If column names is omitted, C<columns> attribute of the model is used. |
|
219 | ||
added insert, update, update...
|
220 |
=head2 C<new> |
221 | ||
222 |
my $table = DBIx::Custom::Table->new; |
|
223 | ||
224 |
Create a L<DBIx::Custom::Table> object. |
|
225 | ||
226 |
=head2 C<select> |
|
227 | ||
table object call dbi object...
|
228 |
$table->select(...); |
added insert, update, update...
|
229 |
|
230 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
231 |
you don't have to specify C<table> option. |
added insert, update, update...
|
232 | |
233 |
=head2 C<update> |
|
234 | ||
table object call dbi object...
|
235 |
$table->update(...); |
added insert, update, update...
|
236 |
|
237 |
Same as C<update()> 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_all> |
|
241 | ||
242 |
$table->update_all(param => \%param); |
|
243 |
|
|
244 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
245 |
you don't have to specify table name. |
|
update pod
|
246 | |
update pod
|
247 |
=cut |