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