added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Table; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use Carp 'croak'; |
|
9 | ||
10 |
__PACKAGE__->attr(['dbi', 'name']); |
|
11 | ||
12 |
sub new { |
|
13 |
my $self = shift->SUPER::new(@_); |
|
14 |
|
|
15 |
my $insert = sub { |
|
16 |
my $self = shift; |
|
17 |
|
|
18 |
return $self->dbi->insert(table => $self->name, param => shift); |
|
19 |
}; |
|
20 |
|
|
21 |
my $update = sub { |
|
22 |
my $self = shift; |
|
23 |
|
|
24 |
return $self->dbi->update(table => $self->name, param => shift, |
|
25 |
where => shift); |
|
26 |
}; |
|
27 |
|
|
28 |
my $update_all = sub { |
|
29 |
my $self = shift; |
|
30 |
|
|
31 |
return $self->dbi->update_all(table => $self->name, param => shift); |
|
32 |
}; |
|
33 |
|
|
34 |
my $delete = sub { |
|
35 |
my $self = shift; |
|
36 |
|
|
37 |
return $self->dbi->delete(table => $self->name, where => shift); |
|
38 |
}; |
|
39 |
|
|
40 |
my $delete_all = sub { |
|
41 |
my $self = shift; |
|
42 |
|
|
43 |
return $self->dbi->delete_all(table => $self->name); |
|
44 |
}; |
|
45 |
|
|
46 |
my $select = sub { |
|
47 |
my $self = shift; |
|
48 |
|
|
49 |
my $where = {}; |
|
50 |
my $column = ['*']; |
|
51 |
my $append = ''; |
|
52 |
|
|
53 |
foreach my $arg (@_) { |
|
54 |
my $type = ref $arg; |
|
55 |
|
|
56 |
if ($type eq 'ARRAY') { |
|
57 |
$column = $arg; |
|
58 |
} |
|
59 |
elsif ($type eq 'HASH') { |
|
60 |
$where = $arg; |
|
61 |
} |
|
62 |
else { |
|
63 |
$append = $arg; |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
return $self->dbi->select( |
|
68 |
table => $self->name, |
|
69 |
where => $where, |
|
70 |
column => $column, |
|
71 |
append => $append |
|
72 |
); |
|
73 |
}; |
|
74 |
|
|
75 |
$self->helper( |
|
76 |
insert => $insert, |
|
77 |
insert_simple => $insert, |
|
78 |
update => $update, |
|
79 |
update_simple => $update, |
|
80 |
update_all => $update_all, |
|
81 |
update_all_simple => $update_all, |
|
82 |
delete => $delete, |
|
83 |
delete_simple => $delete, |
|
84 |
delete_all => $delete_all, |
|
85 |
delete_all_simple => $delete_all, |
|
86 |
select => $select, |
|
87 |
select_simple => $select |
|
88 |
); |
|
89 |
|
|
90 |
return $self; |
|
91 |
} |
|
92 | ||
93 |
our $AUTOLOAD; |
|
94 | ||
95 |
sub AUTOLOAD { |
|
96 |
my $self = shift; |
|
97 | ||
98 |
# Method |
|
99 |
my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
100 | ||
101 |
# Helper |
|
102 |
$self->{_helpers} ||= {}; |
|
103 |
croak qq/Can't locate object method "$method" via "$package"/ |
|
104 |
unless my $helper = $self->{_helpers}->{$method}; |
|
105 | ||
106 |
# Run |
|
107 |
return $self->$helper(@_); |
|
108 |
} |
|
109 | ||
110 |
sub helper { |
|
111 |
my $self = shift; |
|
112 |
|
|
113 |
# Merge |
|
114 |
my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
115 |
$self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers}; |
|
116 |
|
|
117 |
return $self; |
|
118 |
} |
|
119 | ||
120 |
sub DESTROY { } |
|
121 | ||
122 |
1; |
|
123 | ||
124 |
=head1 NAME |
|
125 | ||
126 |
DBIx::Custom::Model - Modele base class(experimental) |
|
127 | ||
128 |
=head1 SYNOPSIS |
|
129 | ||
130 |
use DBIx::Custom::Table; |
|
131 | ||
132 |
my $table = DBIx::Custom::Table->new(name => 'books'); |
|
133 | ||
134 |
=head1 METHODS |
|
135 | ||
136 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
|
137 |
and implements the following new ones. |
|
138 | ||
139 |
=head2 C<helper> |
|
140 | ||
141 |
$table->helper(insert => sub { |
|
142 |
# ... |
|
143 |
}); |
|
144 | ||
145 |
=head2 C<new> |
|
146 | ||
147 |
my $table = DBIx::Custom->new; |
|
148 |
|
|
149 |
=head2 C<insert> |
|
150 | ||
151 |
$table->insert(\%param); |
|
152 | ||
153 |
Insert. |
|
154 | ||
155 |
=head2 C<insert_simple> |
|
156 | ||
157 |
Same as C<insert()>. |
|
158 | ||
159 |
=head2 C<update> |
|
160 | ||
161 |
$table->update(\%param, \%where); |
|
162 | ||
163 |
Update. |
|
164 | ||
165 |
=head2 C<update_simple> |
|
166 | ||
167 |
Same as C<update()>. |
|
168 | ||
169 |
=head2 C<update_all> |
|
170 | ||
171 |
$table->update_all(\%param); |
|
172 | ||
173 |
Update all. |
|
174 | ||
175 |
=head2 C<update_all_simple> |
|
176 | ||
177 |
Same as C<update_all>. |
|
178 | ||
179 |
=head2 C<delete> |
|
180 | ||
181 |
$table->delete(\%where); |
|
182 | ||
183 |
=head2 C<delete_simple()> |
|
184 | ||
185 |
Same as C<delete_all()>. |
|
186 | ||
187 |
=head2 C<delete_all> |
|
188 | ||
189 |
$table->delete_all(\%where); |
|
190 | ||
191 |
=head2 C<delete_all_simple()> |
|
192 | ||
193 |
Same as C<delete_all()>. |
|
194 | ||
195 |
=head2 C<select> |
|
196 | ||
197 |
$table->select(\%where); |
|
198 |
$table->select(\@column); |
|
199 |
$table->select($append); |
|
200 |
|
|
201 |
# And any combination |
|
202 |
$table->select(\%where, \@column, $append); |
|
203 | ||
204 |
=head2 C<select_simple> |
|
205 | ||
206 |
Same as C<select()>. |
|
207 |