... | ... |
@@ -46,6 +46,7 @@ sub clone { |
46 | 46 |
$new->filters(%{$self->filters || {}}); |
47 | 47 |
$new->bind_filter($self->bind_filter); |
48 | 48 |
$new->fetch_filter($self->fetch_filter); |
49 |
+ $new->result_class($self->result_class); |
|
49 | 50 |
} |
50 | 51 |
|
51 | 52 |
# Attribute |
... | ... |
@@ -56,12 +57,15 @@ sub fetch_filter : Attr {} |
56 | 57 |
|
57 | 58 |
sub filters : Attr { type => 'hash', deref => 1, auto_build => sub { shift->filters({}) } } |
58 | 59 |
sub add_filter { shift->filters(@_) } |
60 |
+sub result_class : Attr { default => 'DBI::Custom::Result' } |
|
59 | 61 |
|
60 | 62 |
sub dbh : Attr { auto_build => sub { shift->connect } } |
61 | 63 |
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQLTemplate->new) } } |
62 | 64 |
|
65 |
+ |
|
63 | 66 |
our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/; |
64 | 67 |
|
68 |
+# Connect |
|
65 | 69 |
sub connect { |
66 | 70 |
my $self = shift; |
67 | 71 |
my $connect_info = $self->connect_info; |
... | ... |
@@ -83,9 +87,33 @@ sub connect { |
83 | 87 |
} |
84 | 88 |
); |
85 | 89 |
|
90 |
+ $self->auto_commit($self->dbh->{AutoCommit}); |
|
86 | 91 |
$self->dbh($dbh); |
87 | 92 |
} |
88 | 93 |
|
94 |
+# Is connected? |
|
95 |
+sub connected { |
|
96 |
+ my $self = shift; |
|
97 |
+ return exists $sefl->{dbh}; |
|
98 |
+} |
|
99 |
+ |
|
100 |
+# Disconnect |
|
101 |
+sub disconnect { |
|
102 |
+ my $self = shift; |
|
103 |
+ if ($self->conneced) { |
|
104 |
+ $self->dbh->disconnect; |
|
105 |
+ delete $self->{dbh}; |
|
106 |
+ } |
|
107 |
+} |
|
108 |
+ |
|
109 |
+# Reconnect |
|
110 |
+sub reconnect { |
|
111 |
+ my $self = shift; |
|
112 |
+ $sefl->disconnect if $self->connected; |
|
113 |
+ $self->connect; |
|
114 |
+} |
|
115 |
+ |
|
116 |
+ |
|
89 | 117 |
sub create_sql { |
90 | 118 |
my $self = shift; |
91 | 119 |
|
... | ... |
@@ -99,19 +127,55 @@ sub query { |
99 | 127 |
|
100 | 128 |
$filter ||= $self->bind_filter; |
101 | 129 |
|
102 |
- my ($sql, @bind) = $self->creqte_sql($template, $values, $filter); |
|
103 |
- $self->prepare($sql); |
|
104 |
- $self->execute(@bind); |
|
130 |
+ my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
|
131 |
+ my ( |
|
132 |
+ my $sth = $self->dbh->prepare($sql); |
|
133 |
+ $sth->execute(@bind); |
|
134 |
+ |
|
135 |
+ # Select |
|
136 |
+ if ($sth->{NUM_OF_FIELDS}) { |
|
137 |
+ my $result_class = $self->result_class; |
|
138 |
+ my $result = $result_class->new({sth => $sth}); |
|
139 |
+ return $result; |
|
140 |
+ } |
|
141 |
+ return; |
|
105 | 142 |
} |
106 | 143 |
|
107 | 144 |
sub query_raw_sql { |
108 | 145 |
my ($self, $sql, @bind) = @_; |
109 |
- $self->prepare($sql); |
|
110 |
- $self->execute(@bind); |
|
146 |
+ my $sth = $self->dbh->prepare($sql); |
|
147 |
+ $sth->execute(@bind); |
|
148 |
+ return $sth; |
|
111 | 149 |
} |
112 | 150 |
|
151 |
+sub auto_commit : Attr { |
|
152 |
+ |
|
113 | 153 |
Object::Simple->build_class; |
114 | 154 |
|
155 |
+package DBI::Custom::Result; |
|
156 |
+use Object::Simple; |
|
157 |
+ |
|
158 |
+sub sth : Attr {}; |
|
159 |
+ |
|
160 |
+sub fetchrow_arrayref { |
|
161 |
+ my $self = shift; |
|
162 |
+ $self->sth; |
|
163 |
+ |
|
164 |
+ |
|
165 |
+} |
|
166 |
+ |
|
167 |
+ |
|
168 |
+*fetch = \&fetchrow_arrayref; |
|
169 |
+ |
|
170 |
+sub err { shift->sth->err } |
|
171 |
+sub errstr { shift->sth->errstr } |
|
172 |
+sub finish { shift->sth->finish } |
|
173 |
+sub rows { shift->sth->rows } |
|
174 |
+sub state { shift->sth->state } |
|
175 |
+ |
|
176 |
+Object::Simple->build_class; |
|
177 |
+ |
|
178 |
+ |
|
115 | 179 |
package DBI::Custom::SQLTemplate; |
116 | 180 |
use Object::Simple; |
117 | 181 |
use Carp 'croak'; |
... | ... |
@@ -285,6 +349,9 @@ sub build_sql { |
285 | 349 |
|
286 | 350 |
Object::Simple->build_class; |
287 | 351 |
|
352 |
+package DBI::Custom; |
|
353 |
+1; |
|
354 |
+ |
|
288 | 355 |
=head1 NAME |
289 | 356 |
|
290 | 357 |
DBI::Custom - Customizable simple DBI |