first commit
|
1 |
package DBI::Custom; |
2 |
use Object::Simple; |
|
add test
|
3 | |
4 |
our $VERSION = '0.0101'; |
|
5 | ||
6 |
use Carp 'croak'; |
|
add some method
|
7 |
use DBI; |
add tests
|
8 |
use DBI::Custom::SQL::Template; |
add tests
|
9 |
use DBI::Custom::Result; |
add tests
|
10 | |
11 |
### Class-Object Accessors |
|
catch up Object::Simple upda...
|
12 |
sub connect_info : ClassObjectAttr { |
13 |
type => 'hash', |
|
14 |
initialize => { |
|
add tests
|
15 |
clone => sub { |
add tests
|
16 |
my $value = shift; |
17 |
my $new_value = \%{$value || {}}; |
|
18 |
$new_value->{options} = $value->{options} if $value->{options}; |
|
19 |
return $new_value; |
|
add tests
|
20 |
}, |
21 |
default => sub { {} }, |
|
catch up Object::Simple upda...
|
22 |
} |
23 |
} |
|
24 | ||
25 |
sub bind_filter : ClassObjectAttr { |
|
26 |
initialize => {clone => 'scalar'} |
|
27 |
} |
|
28 | ||
29 |
sub fetch_filter : ClassObjectAttr { |
|
30 |
initialize => {clone => 'scalar'} |
|
31 |
} |
|
32 | ||
33 |
sub filters : ClassObjectAttr { |
|
34 |
type => 'hash', |
|
35 |
deref => 1, |
|
36 |
initialize => { |
|
add tests
|
37 |
clone => 'hash', |
38 |
default => sub { {} } |
|
catch up Object::Simple upda...
|
39 |
} |
40 |
} |
|
first commit
|
41 | |
catch up Object::Simple upda...
|
42 |
sub result_class : ClassObjectAttr { |
43 |
initialize => { |
|
add tests
|
44 |
clone => 'scalar', |
45 |
default => 'DBI::Custom::Result' |
|
catch up Object::Simple upda...
|
46 |
} |
47 |
} |
|
cleanup
|
48 | |
catch up Object::Simple upda...
|
49 |
sub sql_template : ClassObjectAttr { |
50 |
initialize => { |
|
add tests
|
51 |
clone => sub {my $value = shift; $value ? $value->clone : undef}, |
catch up Object::Simple upda...
|
52 |
default => sub {DBI::Custom::SQL::Template->new} |
53 |
} |
|
54 |
} |
|
cleanup
|
55 | |
catch up Object::Simple upda...
|
56 |
sub valid_connect_info : ClassObjectAttr { |
57 |
type => 'hash', |
|
58 |
deref => 1, |
|
59 |
initialize => { |
|
add tests
|
60 |
clone => 'hash', |
add tests
|
61 |
default => sub { return {map {$_ => 1} qw/data_source user password options/} }, |
catch up Object::Simple upda...
|
62 |
} |
63 |
} |
|
cleanup
|
64 | |
add tests
|
65 |
### Object Accessor |
add tests
|
66 |
sub dbh : Attr {} |
add tests
|
67 | |
68 | ||
69 |
### Methods |
|
70 |
# Add filter |
|
71 |
sub add_filter { |
|
72 |
my $invocant = shift; |
|
73 |
|
|
74 |
my %old_filters = $invocant->filters; |
|
75 |
my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; |
|
76 |
$invocant->filters(%old_filters, %new_filters); |
|
77 |
} |
|
add various
|
78 | |
79 |
# Auto commit |
|
80 |
sub auto_commit { |
|
81 |
my $self = shift; |
|
82 |
|
|
83 |
croak("Cannot change AutoCommit becouse of not connected") |
|
84 |
unless $self->dbh; |
|
85 |
|
|
86 |
if (@_) { |
|
87 |
$self->dbh->{AutoCommit} = $_[0]; |
|
88 |
return $self; |
|
89 |
} |
|
90 |
return $self->dbh->{AutoCommit}; |
|
91 |
} |
|
add test
|
92 | |
add various things
|
93 |
# Connect |
add some method
|
94 |
sub connect { |
95 |
my $self = shift; |
|
96 |
my $connect_info = $self->connect_info; |
|
97 |
|
|
add test
|
98 |
foreach my $key (keys %{$self->connect_info}) { |
add test module
|
99 |
croak("connect_info '$key' is wrong name") |
try various
|
100 |
unless $self->valid_connect_info->{$key}; |
add test
|
101 |
} |
102 |
|
|
add some method
|
103 |
my $dbh = DBI->connect( |
add test
|
104 |
$connect_info->{data_source}, |
add some method
|
105 |
$connect_info->{user}, |
106 |
$connect_info->{password}, |
|
107 |
{ |
|
108 |
RaiseError => 1, |
|
109 |
PrintError => 0, |
|
110 |
AutoCommit => 1, |
|
111 |
%{$connect_info->{options} || {} } |
|
112 |
} |
|
113 |
); |
|
114 |
|
|
115 |
$self->dbh($dbh); |
|
add various
|
116 |
return $self; |
add some method
|
117 |
} |
first commit
|
118 | |
add tests
|
119 |
# DESTROY |
add tests
|
120 |
sub DESTROY { |
121 |
my $self = shift; |
|
add tests
|
122 |
$self->disconnect if $self->connected; |
add tests
|
123 |
} |
124 | ||
add various things
|
125 |
# Is connected? |
126 |
sub connected { |
|
127 |
my $self = shift; |
|
add tests
|
128 |
return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')}; |
add various things
|
129 |
} |
130 | ||
131 |
# Disconnect |
|
132 |
sub disconnect { |
|
133 |
my $self = shift; |
|
add tests
|
134 |
if ($self->connected) { |
add various things
|
135 |
$self->dbh->disconnect; |
136 |
delete $self->{dbh}; |
|
137 |
} |
|
138 |
} |
|
139 | ||
140 |
# Reconnect |
|
141 |
sub reconnect { |
|
142 |
my $self = shift; |
|
add tests
|
143 |
$self->disconnect if $self->connected; |
add various things
|
144 |
$self->connect; |
145 |
} |
|
146 | ||
try various
|
147 |
# Run tranzaction |
148 |
sub run_tranzaction { |
|
149 |
my ($self, $tranzaction) = @_; |
|
150 |
|
|
151 |
$self->auto_commit(0); |
|
152 |
|
|
153 |
eval { |
|
154 |
$tranzaction->(); |
|
155 |
$self->dbh->commit; |
|
156 |
}; |
|
157 |
|
|
158 |
if ($@) { |
|
159 |
my $tranzaction_error = $@; |
|
160 |
|
|
161 |
$self->dbh->rollback or croak("$@ and rollback also failed"); |
|
162 |
croak("$tranzaction_error"); |
|
163 |
} |
|
164 |
$self->auto_commit(1); |
|
add tests
|
165 |
} |
166 | ||
cleanup
|
167 |
# Create SQL from SQL template |
add test
|
168 |
sub create_sql { |
169 |
my $self = shift; |
|
170 |
|
|
171 |
my ($sql, @bind) = $self->sql_template->create_sql(@_); |
|
172 |
|
|
173 |
return ($sql, @bind); |
|
174 |
} |
|
175 | ||
cleanup
|
176 |
# Prepare and execute SQL |
add some method
|
177 |
sub query { |
try varioud way
|
178 |
my ($self, $template, $values, $filter) = @_; |
179 |
|
|
add tests
|
180 |
my $sth_options; |
181 |
|
|
182 |
# Rearrange when argumets is hash referecne |
|
183 |
if (ref $template eq 'HASH') { |
|
184 |
my $args = $template; |
|
185 |
($template, $values, $filter, $sth_options) |
|
186 |
= @{$args}{qw/template values filter sth_options/}; |
|
187 |
} |
|
188 |
|
|
try varioud way
|
189 |
$filter ||= $self->bind_filter; |
190 |
|
|
add various things
|
191 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
add tests
|
192 |
|
193 |
$self->connect unless $self->connected; |
|
194 |
|
|
add various things
|
195 |
my $sth = $self->dbh->prepare($sql); |
add tests
|
196 |
|
197 |
if ($sth_options) { |
|
198 |
foreach my $key (keys %$sth_options) { |
|
199 |
$sth->{$key} = $sth_options->{$key}; |
|
200 |
} |
|
201 |
} |
|
202 |
|
|
cleanup
|
203 |
# Execute |
add tests
|
204 |
my $ret_val = $sth->execute(@bind); |
add various things
|
205 |
|
cleanup
|
206 |
# Return resultset if select statement is executed |
add various things
|
207 |
if ($sth->{NUM_OF_FIELDS}) { |
208 |
my $result_class = $self->result_class; |
|
add various
|
209 |
my $result = $result_class->new({ |
210 |
sth => $sth, |
|
211 |
fetch_filter => $self->fetch_filter |
|
212 |
}); |
|
add various things
|
213 |
return $result; |
214 |
} |
|
add tests
|
215 |
return $ret_val; |
add test
|
216 |
} |
217 | ||
cleanup
|
218 |
# Prepare and execute raw SQL |
add test
|
219 |
sub query_raw_sql { |
cleanup
|
220 |
my ($self, $sql, @bind_values) = @_; |
add tests
|
221 |
|
cleanup
|
222 |
# Connect |
add various
|
223 |
$self->connect unless $self->connected; |
cleanup
|
224 |
|
225 |
# Add semicolon if not exist; |
|
add tests
|
226 |
$sql .= ';' unless $sql =~ /;$/; |
cleanup
|
227 |
|
228 |
# Prepare |
|
add various things
|
229 |
my $sth = $self->dbh->prepare($sql); |
cleanup
|
230 |
|
231 |
# Execute |
|
232 |
$sth->execute(@bind_values); |
|
233 |
|
|
add various things
|
234 |
return $sth; |
add test
|
235 |
} |
236 | ||
237 |
Object::Simple->build_class; |
|
238 | ||
first commit
|
239 |
=head1 NAME |
240 | ||
add test
|
241 |
DBI::Custom - Customizable simple DBI |
first commit
|
242 | |
243 |
=head1 VERSION |
|
244 | ||
add test
|
245 |
Version 0.0101 |
first commit
|
246 | |
247 |
=cut |
|
248 | ||
249 |
=head1 SYNOPSIS |
|
250 | ||
add test
|
251 |
my $dbi = DBI::Custom->new; |
first commit
|
252 | |
add test
|
253 |
=head1 METHODS |
first commit
|
254 | |
add test
|
255 |
=head2 add_filter |
first commit
|
256 | |
add test
|
257 |
=head2 bind_filter |
first commit
|
258 | |
add test
|
259 |
=head2 connect |
first commit
|
260 | |
add test
|
261 |
=head2 connect_info |
first commit
|
262 | |
add test
|
263 |
=head2 dbh |
first commit
|
264 | |
add test
|
265 |
=head2 fetch_filter |
first commit
|
266 | |
add test
|
267 |
=head2 filters |
first commit
|
268 | |
add test
|
269 |
=head2 new |
270 | ||
271 |
=head2 query |
|
first commit
|
272 | |
add test
|
273 |
=head2 create_sql |
274 | ||
275 |
=head2 query_raw_sql |
|
276 | ||
277 |
=head2 sql_template |
|
278 | ||
add tests
|
279 |
=head2 auto_commit |
280 | ||
281 |
=head2 connected |
|
282 | ||
283 |
=head2 disconnect |
|
284 | ||
285 |
=head2 reconnect |
|
286 | ||
287 |
=head2 result_class |
|
288 | ||
cleanup
|
289 |
=head2 run_tranzaction |
first commit
|
290 | |
cleanup
|
291 |
=head2 valid_connect_info |
first commit
|
292 | |
293 | ||
add various
|
294 |
=head1 AUTHOR |
first commit
|
295 | |
add various
|
296 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
297 | |
298 |
=head1 COPYRIGHT & LICENSE |
|
299 | ||
300 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
301 | ||
302 |
This program is free software; you can redistribute it and/or modify it |
|
303 |
under the same terms as Perl itself. |
|
304 | ||
305 | ||
306 |
=cut |
|
307 | ||
308 |
1; # End of DBI::Custom |