Commit | Line | Data |
8b445e33 |
1 | package DBIx::Class::Storage::DBI; |
2 | |
20a2c954 |
3 | use strict; |
4 | use warnings; |
8b445e33 |
5 | use DBI; |
aeaf3ce2 |
6 | use SQL::Abstract::Limit; |
28927b50 |
7 | use DBIx::Class::Storage::DBI::Cursor; |
8b445e33 |
8 | |
bd7efd39 |
9 | BEGIN { |
10 | |
cb5f2eea |
11 | package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :( |
bd7efd39 |
12 | |
13 | use base qw/SQL::Abstract::Limit/; |
14 | |
54540863 |
15 | sub select { |
16 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
17 | @rest = (-1) unless defined $rest[0]; |
18 | $self->SUPER::select($table, $self->_recurse_fields($fields), |
19 | $where, $order, @rest); |
20 | } |
21 | |
22 | sub _emulate_limit { |
23 | my $self = shift; |
24 | if ($_[3] == -1) { |
25 | return $_[1].$self->_order_by($_[2]); |
26 | } else { |
27 | return $self->SUPER::_emulate_limit(@_); |
28 | } |
29 | } |
30 | |
31 | sub _recurse_fields { |
32 | my ($self, $fields) = @_; |
33 | my $ref = ref $fields; |
34 | return $self->_quote($fields) unless $ref; |
35 | return $$fields if $ref eq 'SCALAR'; |
36 | |
37 | if ($ref eq 'ARRAY') { |
38 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
39 | } elsif ($ref eq 'HASH') { |
40 | foreach my $func (keys %$fields) { |
41 | return $self->_sqlcase($func) |
42 | .'( '.$self->_recurse_fields($fields->{$func}).' )'; |
43 | } |
44 | } |
45 | } |
46 | |
47 | sub _order_by { |
48 | my $self = shift; |
49 | my $ret = ''; |
50 | if (ref $_[0] eq 'HASH') { |
51 | if (defined $_[0]->{group_by}) { |
52 | $ret = $self->_sqlcase(' group by ') |
53 | .$self->_recurse_fields($_[0]->{group_by}); |
54 | } |
55 | if (defined $_[0]->{order_by}) { |
56 | $ret .= $self->SUPER::_order_by($_[0]->{order_by}); |
57 | } |
58 | } else { |
59 | $ret = $self->SUPER::_order_by(@_); |
60 | } |
61 | return $ret; |
62 | } |
63 | |
2a816814 |
64 | sub _table { |
bd7efd39 |
65 | my ($self, $from) = @_; |
66 | if (ref $from eq 'ARRAY') { |
67 | return $self->_recurse_from(@$from); |
68 | } elsif (ref $from eq 'HASH') { |
69 | return $self->_make_as($from); |
70 | } else { |
71 | return $from; |
72 | } |
73 | } |
74 | |
75 | sub _recurse_from { |
76 | my ($self, $from, @join) = @_; |
77 | my @sqlf; |
78 | push(@sqlf, $self->_make_as($from)); |
79 | foreach my $j (@join) { |
80 | my ($to, $on) = @$j; |
73856587 |
81 | |
54540863 |
82 | # check whether a join type exists |
83 | my $join_clause = ''; |
84 | if (ref($to) eq 'HASH' and exists($to->{-join_type})) { |
85 | $join_clause = ' '.uc($to->{-join_type}).' JOIN '; |
86 | } else { |
87 | $join_clause = ' JOIN '; |
88 | } |
73856587 |
89 | push(@sqlf, $join_clause); |
90 | |
bd7efd39 |
91 | if (ref $to eq 'ARRAY') { |
92 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
93 | } else { |
96cdbbab |
94 | push(@sqlf, $self->_make_as($to)); |
bd7efd39 |
95 | } |
96 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
97 | } |
98 | return join('', @sqlf); |
99 | } |
100 | |
101 | sub _make_as { |
102 | my ($self, $from) = @_; |
54540863 |
103 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) } |
2a816814 |
104 | reverse each %{$self->_skip_options($from)}); |
73856587 |
105 | } |
106 | |
107 | sub _skip_options { |
54540863 |
108 | my ($self, $hash) = @_; |
109 | my $clean_hash = {}; |
110 | $clean_hash->{$_} = $hash->{$_} |
111 | for grep {!/^-/} keys %$hash; |
112 | return $clean_hash; |
bd7efd39 |
113 | } |
114 | |
115 | sub _join_condition { |
116 | my ($self, $cond) = @_; |
5efe4c79 |
117 | if (ref $cond eq 'HASH') { |
118 | my %j; |
119 | for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; }; |
120 | return $self->_recurse_where(\%j); |
121 | } elsif (ref $cond eq 'ARRAY') { |
122 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
123 | } else { |
124 | die "Can't handle this yet!"; |
125 | } |
bd7efd39 |
126 | } |
127 | |
2a816814 |
128 | sub _quote { |
129 | my ($self, $label) = @_; |
130 | return '' unless defined $label; |
131 | return $self->SUPER::_quote($label); |
132 | } |
133 | |
bd7efd39 |
134 | } # End of BEGIN block |
135 | |
8b445e33 |
136 | use base qw/DBIx::Class/; |
137 | |
438adc0e |
138 | __PACKAGE__->load_components(qw/Exception AccessorGroup/); |
8b445e33 |
139 | |
223b8fe3 |
140 | __PACKAGE__->mk_group_accessors('simple' => |
d79f59b9 |
141 | qw/connect_info _dbh _sql_maker debug cursor on_connect_do transaction_depth/); |
8091aa91 |
142 | |
8b445e33 |
143 | sub new { |
223b8fe3 |
144 | my $new = bless({}, ref $_[0] || $_[0]); |
28927b50 |
145 | $new->cursor("DBIx::Class::Storage::DBI::Cursor"); |
d79f59b9 |
146 | $new->transaction_depth(0); |
28927b50 |
147 | $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; |
223b8fe3 |
148 | return $new; |
8b445e33 |
149 | } |
150 | |
8b445e33 |
151 | =head1 NAME |
152 | |
153 | DBIx::Class::Storage::DBI - DBI storage handler |
154 | |
155 | =head1 SYNOPSIS |
156 | |
157 | =head1 DESCRIPTION |
158 | |
159 | This class represents the connection to the database |
160 | |
161 | =head1 METHODS |
162 | |
8b445e33 |
163 | =cut |
164 | |
d7c4c15c |
165 | =head2 on_connect_do |
166 | |
167 | Executes the sql statements given as a listref on every db connect. |
168 | |
169 | =cut |
170 | |
8b445e33 |
171 | sub dbh { |
172 | my ($self) = @_; |
173 | my $dbh; |
174 | unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) { |
175 | $self->_populate_dbh; |
176 | } |
177 | return $self->_dbh; |
178 | } |
179 | |
48c69e7c |
180 | sub sql_maker { |
181 | my ($self) = @_; |
fdc1c3d0 |
182 | unless ($self->_sql_maker) { |
bd7efd39 |
183 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
184 | } |
185 | return $self->_sql_maker; |
186 | } |
187 | |
8b445e33 |
188 | sub _populate_dbh { |
189 | my ($self) = @_; |
190 | my @info = @{$self->connect_info || []}; |
191 | $self->_dbh($self->_connect(@info)); |
d7c4c15c |
192 | |
193 | # if on-connect sql statements are given execute them |
194 | foreach my $sql_statement (@{$self->on_connect_do || []}) { |
195 | $self->_dbh->do($sql_statement); |
196 | } |
8b445e33 |
197 | } |
198 | |
199 | sub _connect { |
200 | my ($self, @info) = @_; |
201 | return DBI->connect(@info); |
202 | } |
203 | |
8091aa91 |
204 | =head2 txn_begin |
8b445e33 |
205 | |
8091aa91 |
206 | Calls begin_work on the current dbh. |
8b445e33 |
207 | |
208 | =cut |
209 | |
8091aa91 |
210 | sub txn_begin { |
d79f59b9 |
211 | my $self = shift; |
212 | $self->dbh->begin_work if $self->{transaction_depth}++ == 0 and $self->dbh->{AutoCommit}; |
8091aa91 |
213 | } |
8b445e33 |
214 | |
8091aa91 |
215 | =head2 txn_commit |
8b445e33 |
216 | |
8091aa91 |
217 | Issues a commit against the current dbh. |
8b445e33 |
218 | |
8091aa91 |
219 | =cut |
220 | |
221 | sub txn_commit { |
d79f59b9 |
222 | my $self = shift; |
223 | if ($self->{transaction_depth} == 0) { |
224 | $self->dbh->commit unless $self->dbh->{AutoCommit}; |
8091aa91 |
225 | } |
226 | else { |
d79f59b9 |
227 | $self->dbh->commit if --$self->{transaction_depth} == 0; |
8091aa91 |
228 | } |
229 | } |
230 | |
231 | =head2 txn_rollback |
232 | |
233 | Issues a rollback against the current dbh. |
8b445e33 |
234 | |
235 | =cut |
236 | |
8091aa91 |
237 | sub txn_rollback { |
d79f59b9 |
238 | my $self = shift; |
239 | if ($self->{transaction_depth} == 0) { |
240 | $self->dbh->rollback unless $self->dbh->{AutoCommit}; |
8091aa91 |
241 | } |
242 | else { |
d79f59b9 |
243 | --$self->{transaction_depth} == 0 ? $self->dbh->rollback : die $@; |
8091aa91 |
244 | } |
245 | } |
8b445e33 |
246 | |
223b8fe3 |
247 | sub _execute { |
248 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
249 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
250 | unshift(@bind, @$extra_bind) if $extra_bind; |
223b8fe3 |
251 | warn "$sql: @bind" if $self->debug; |
2f5911b2 |
252 | my $sth = $self->sth($sql,$op); |
438adc0e |
253 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
254 | my $rv = $sth->execute(@bind); |
223b8fe3 |
255 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
256 | } |
257 | |
8b445e33 |
258 | sub insert { |
259 | my ($self, $ident, $to_insert) = @_; |
20a2c954 |
260 | $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" ) |
a4b2f17b |
261 | unless ($self->_execute('insert' => [], $ident, $to_insert)); |
8b445e33 |
262 | return $to_insert; |
263 | } |
264 | |
265 | sub update { |
223b8fe3 |
266 | return shift->_execute('update' => [], @_); |
8b445e33 |
267 | } |
268 | |
269 | sub delete { |
223b8fe3 |
270 | return shift->_execute('delete' => [], @_); |
8b445e33 |
271 | } |
272 | |
de705b51 |
273 | sub _select { |
8b445e33 |
274 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
275 | my $order = $attrs->{order_by}; |
276 | if (ref $condition eq 'SCALAR') { |
277 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
278 | } |
54540863 |
279 | if (exists $attrs->{group_by}) { |
280 | $order = { group_by => $attrs->{group_by}, |
281 | ($order ? (order_by => $order) : ()) }; |
282 | } |
5c91499f |
283 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
284 | if ($attrs->{software_limit} || |
285 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
286 | $attrs->{software_limit} = 1; |
5c91499f |
287 | } else { |
288 | push @args, $attrs->{rows}, $attrs->{offset}; |
289 | } |
de705b51 |
290 | return $self->_execute(@args); |
291 | } |
292 | |
293 | sub select { |
294 | my $self = shift; |
295 | my ($ident, $select, $condition, $attrs) = @_; |
cb5f2eea |
296 | return $self->cursor->new($self, \@_, $attrs); |
8b445e33 |
297 | } |
298 | |
1a14aa3f |
299 | sub select_single { |
de705b51 |
300 | my $self = shift; |
301 | my ($rv, $sth, @bind) = $self->_select(@_); |
1a14aa3f |
302 | return $sth->fetchrow_array; |
303 | } |
304 | |
8b445e33 |
305 | sub sth { |
cb5f2eea |
306 | my ($self, $sql) = @_; |
91fa659e |
307 | # 3 is the if_active parameter which avoids active sth re-use |
308 | return $self->dbh->prepare_cached($sql, {}, 3); |
8b445e33 |
309 | } |
310 | |
a953d8d9 |
311 | =head2 columns_info_for |
312 | |
313 | Returns database type info for a given table columns. |
314 | |
315 | =cut |
316 | |
317 | sub columns_info_for { |
318 | my ($self, $table) = @_; |
a953d8d9 |
319 | my %result; |
103e3e03 |
320 | if ( $self->dbh->can( 'column_info' ) ){ |
321 | my $sth = $self->dbh->column_info( undef, undef, $table, '%' ); |
322 | $sth->execute(); |
323 | while ( my $info = $sth->fetchrow_hashref() ){ |
324 | my %column_info; |
a953d8d9 |
325 | $column_info{data_type} = $info->{TYPE_NAME}; |
326 | $column_info{size} = $info->{COLUMN_SIZE}; |
327 | $column_info{is_nullable} = $info->{NULLABLE}; |
103e3e03 |
328 | $result{$info->{COLUMN_NAME}} = \%column_info; |
329 | } |
330 | }else{ |
331 | my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); |
332 | $sth->execute; |
333 | my @columns = @{$sth->{NAME}}; |
334 | for my $i ( 0 .. $#columns ){ |
335 | $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i]; |
a953d8d9 |
336 | } |
a953d8d9 |
337 | } |
338 | return \%result; |
339 | } |
340 | |
8b445e33 |
341 | 1; |
342 | |
8b445e33 |
343 | =head1 AUTHORS |
344 | |
daec44b8 |
345 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
346 | |
9f19b1d6 |
347 | Andy Grundman <andy@hybridized.org> |
348 | |
8b445e33 |
349 | =head1 LICENSE |
350 | |
351 | You may distribute this code under the same terms as Perl itself. |
352 | |
353 | =cut |
354 | |