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; |
92b858c9 |
8 | use IO::File; |
701da8c4 |
9 | use Carp::Clan qw/DBIx::Class/; |
8b445e33 |
10 | |
bd7efd39 |
11 | BEGIN { |
12 | |
cb5f2eea |
13 | package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :( |
bd7efd39 |
14 | |
15 | use base qw/SQL::Abstract::Limit/; |
16 | |
54540863 |
17 | sub select { |
18 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
19 | @rest = (-1) unless defined $rest[0]; |
20 | $self->SUPER::select($table, $self->_recurse_fields($fields), |
21 | $where, $order, @rest); |
22 | } |
23 | |
24 | sub _emulate_limit { |
25 | my $self = shift; |
26 | if ($_[3] == -1) { |
27 | return $_[1].$self->_order_by($_[2]); |
28 | } else { |
29 | return $self->SUPER::_emulate_limit(@_); |
30 | } |
31 | } |
32 | |
33 | sub _recurse_fields { |
34 | my ($self, $fields) = @_; |
35 | my $ref = ref $fields; |
36 | return $self->_quote($fields) unless $ref; |
37 | return $$fields if $ref eq 'SCALAR'; |
38 | |
39 | if ($ref eq 'ARRAY') { |
40 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
41 | } elsif ($ref eq 'HASH') { |
42 | foreach my $func (keys %$fields) { |
43 | return $self->_sqlcase($func) |
44 | .'( '.$self->_recurse_fields($fields->{$func}).' )'; |
45 | } |
46 | } |
47 | } |
48 | |
49 | sub _order_by { |
50 | my $self = shift; |
51 | my $ret = ''; |
52 | if (ref $_[0] eq 'HASH') { |
53 | if (defined $_[0]->{group_by}) { |
54 | $ret = $self->_sqlcase(' group by ') |
55 | .$self->_recurse_fields($_[0]->{group_by}); |
56 | } |
57 | if (defined $_[0]->{order_by}) { |
58 | $ret .= $self->SUPER::_order_by($_[0]->{order_by}); |
59 | } |
60 | } else { |
61 | $ret = $self->SUPER::_order_by(@_); |
62 | } |
63 | return $ret; |
64 | } |
65 | |
2a816814 |
66 | sub _table { |
bd7efd39 |
67 | my ($self, $from) = @_; |
68 | if (ref $from eq 'ARRAY') { |
69 | return $self->_recurse_from(@$from); |
70 | } elsif (ref $from eq 'HASH') { |
71 | return $self->_make_as($from); |
72 | } else { |
73 | return $from; |
74 | } |
75 | } |
76 | |
77 | sub _recurse_from { |
78 | my ($self, $from, @join) = @_; |
79 | my @sqlf; |
80 | push(@sqlf, $self->_make_as($from)); |
81 | foreach my $j (@join) { |
82 | my ($to, $on) = @$j; |
73856587 |
83 | |
54540863 |
84 | # check whether a join type exists |
85 | my $join_clause = ''; |
86 | if (ref($to) eq 'HASH' and exists($to->{-join_type})) { |
87 | $join_clause = ' '.uc($to->{-join_type}).' JOIN '; |
88 | } else { |
89 | $join_clause = ' JOIN '; |
90 | } |
73856587 |
91 | push(@sqlf, $join_clause); |
92 | |
bd7efd39 |
93 | if (ref $to eq 'ARRAY') { |
94 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
95 | } else { |
96cdbbab |
96 | push(@sqlf, $self->_make_as($to)); |
bd7efd39 |
97 | } |
98 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
99 | } |
100 | return join('', @sqlf); |
101 | } |
102 | |
103 | sub _make_as { |
104 | my ($self, $from) = @_; |
54540863 |
105 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) } |
2a816814 |
106 | reverse each %{$self->_skip_options($from)}); |
73856587 |
107 | } |
108 | |
109 | sub _skip_options { |
54540863 |
110 | my ($self, $hash) = @_; |
111 | my $clean_hash = {}; |
112 | $clean_hash->{$_} = $hash->{$_} |
113 | for grep {!/^-/} keys %$hash; |
114 | return $clean_hash; |
bd7efd39 |
115 | } |
116 | |
117 | sub _join_condition { |
118 | my ($self, $cond) = @_; |
5efe4c79 |
119 | if (ref $cond eq 'HASH') { |
120 | my %j; |
121 | for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; }; |
122 | return $self->_recurse_where(\%j); |
123 | } elsif (ref $cond eq 'ARRAY') { |
124 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
125 | } else { |
126 | die "Can't handle this yet!"; |
127 | } |
bd7efd39 |
128 | } |
129 | |
2a816814 |
130 | sub _quote { |
131 | my ($self, $label) = @_; |
132 | return '' unless defined $label; |
41728a6e |
133 | return $label unless $self->{quote_char}; |
2a816814 |
134 | return $self->SUPER::_quote($label); |
135 | } |
136 | |
f66596f9 |
137 | sub _RowNum { |
138 | my $self = shift; |
139 | my $c; |
140 | $_[0] =~ s/SELECT (.*?) FROM/ |
141 | 'SELECT '.join(', ', map { $_.' AS col'.++$c } split(', ', $1)).' FROM'/e; |
142 | $self->SUPER::_RowNum(@_); |
143 | } |
144 | |
7be93b07 |
145 | # Accessor for setting limit dialect. This is useful |
146 | # for JDBC-bridge among others where the remote SQL-dialect cannot |
147 | # be determined by the name of the driver alone. |
148 | # |
149 | sub limit_dialect { |
150 | my $self = shift; |
151 | $self->{limit_dialect} = shift if @_; |
152 | return $self->{limit_dialect}; |
153 | } |
154 | |
486ad69b |
155 | package DBIx::Class::Storage::DBI::DebugCallback; |
156 | |
157 | sub print { |
158 | my ($self, $string) = @_; |
159 | $string =~ m/^(\w+)/; |
160 | ${$self}->($1, $string); |
161 | } |
162 | |
bd7efd39 |
163 | } # End of BEGIN block |
164 | |
8b445e33 |
165 | use base qw/DBIx::Class/; |
166 | |
1f692767 |
167 | __PACKAGE__->load_components(qw/AccessorGroup/); |
8b445e33 |
168 | |
223b8fe3 |
169 | __PACKAGE__->mk_group_accessors('simple' => |
92b858c9 |
170 | qw/connect_info _dbh _sql_maker debug debugfh cursor on_connect_do transaction_depth/); |
8091aa91 |
171 | |
8b445e33 |
172 | sub new { |
223b8fe3 |
173 | my $new = bless({}, ref $_[0] || $_[0]); |
28927b50 |
174 | $new->cursor("DBIx::Class::Storage::DBI::Cursor"); |
d79f59b9 |
175 | $new->transaction_depth(0); |
5e65c358 |
176 | if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) && |
177 | ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) { |
701da8c4 |
178 | $new->debugfh(IO::File->new($1, 'w')||croak "Cannot open trace file $1"); |
92b858c9 |
179 | } else { |
180 | $new->debugfh(IO::File->new('>&STDERR')); |
181 | } |
28927b50 |
182 | $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; |
223b8fe3 |
183 | return $new; |
8b445e33 |
184 | } |
185 | |
8b445e33 |
186 | =head1 NAME |
187 | |
188 | DBIx::Class::Storage::DBI - DBI storage handler |
189 | |
190 | =head1 SYNOPSIS |
191 | |
192 | =head1 DESCRIPTION |
193 | |
194 | This class represents the connection to the database |
195 | |
196 | =head1 METHODS |
197 | |
8b445e33 |
198 | =cut |
199 | |
d7c4c15c |
200 | =head2 on_connect_do |
201 | |
202 | Executes the sql statements given as a listref on every db connect. |
203 | |
92b858c9 |
204 | =head2 debug |
205 | |
206 | Causes SQL trace information to be emitted on C<debugfh> filehandle |
207 | (or C<STDERR> if C<debugfh> has not specifically been set). |
208 | |
209 | =head2 debugfh |
210 | |
211 | Sets or retrieves the filehandle used for trace/debug output. This |
212 | should be an IO::Handle compatible object (only the C<print> method is |
213 | used). Initially set to be STDERR - although see information on the |
214 | L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable. |
215 | |
486ad69b |
216 | =head2 debugcb |
217 | |
218 | Sets a callback to be executed each time a statement is run; takes a sub |
219 | reference. Overrides debugfh. Callback is executed as $sub->($op, $info) |
220 | where $op is SELECT/INSERT/UPDATE/DELETE and $info is what would normally |
221 | be printed. |
222 | |
d7c4c15c |
223 | =cut |
224 | |
486ad69b |
225 | sub debugcb { |
226 | my ($self, $cb) = @_; |
227 | my $cb_obj = bless(\$cb, 'DBIx::Class::Storage::DBI::DebugCallback'); |
228 | $self->debugfh($cb_obj); |
229 | } |
230 | |
412db1f4 |
231 | sub disconnect { |
232 | my ($self) = @_; |
233 | |
92925617 |
234 | if( $self->connected ) { |
235 | $self->_dbh->rollback unless $self->_dbh->{AutoCommit}; |
236 | $self->_dbh->disconnect; |
237 | $self->_dbh(undef); |
238 | } |
412db1f4 |
239 | } |
240 | |
241 | sub connected { |
8b445e33 |
242 | my ($self) = @_; |
412db1f4 |
243 | |
8b445e33 |
244 | my $dbh; |
412db1f4 |
245 | (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) |
246 | } |
247 | |
248 | sub ensure_connected { |
249 | my ($self) = @_; |
250 | |
251 | unless ($self->connected) { |
8b445e33 |
252 | $self->_populate_dbh; |
253 | } |
412db1f4 |
254 | } |
255 | |
256 | sub dbh { |
257 | my ($self) = @_; |
258 | |
259 | $self->ensure_connected; |
8b445e33 |
260 | return $self->_dbh; |
261 | } |
262 | |
48c69e7c |
263 | sub sql_maker { |
264 | my ($self) = @_; |
fdc1c3d0 |
265 | unless ($self->_sql_maker) { |
bd7efd39 |
266 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
267 | } |
268 | return $self->_sql_maker; |
269 | } |
270 | |
8b445e33 |
271 | sub _populate_dbh { |
272 | my ($self) = @_; |
273 | my @info = @{$self->connect_info || []}; |
274 | $self->_dbh($self->_connect(@info)); |
843f8ecd |
275 | my $driver = $self->_dbh->{Driver}->{Name}; |
34470972 |
276 | eval "require DBIx::Class::Storage::DBI::${driver}"; |
277 | unless ($@) { |
843f8ecd |
278 | bless $self, "DBIx::Class::Storage::DBI::${driver}"; |
279 | } |
d7c4c15c |
280 | # if on-connect sql statements are given execute them |
281 | foreach my $sql_statement (@{$self->on_connect_do || []}) { |
282 | $self->_dbh->do($sql_statement); |
283 | } |
8b445e33 |
284 | } |
285 | |
286 | sub _connect { |
287 | my ($self, @info) = @_; |
288 | return DBI->connect(@info); |
289 | } |
290 | |
8091aa91 |
291 | =head2 txn_begin |
8b445e33 |
292 | |
8091aa91 |
293 | Calls begin_work on the current dbh. |
8b445e33 |
294 | |
295 | =cut |
296 | |
8091aa91 |
297 | sub txn_begin { |
d79f59b9 |
298 | my $self = shift; |
299 | $self->dbh->begin_work if $self->{transaction_depth}++ == 0 and $self->dbh->{AutoCommit}; |
8091aa91 |
300 | } |
8b445e33 |
301 | |
8091aa91 |
302 | =head2 txn_commit |
8b445e33 |
303 | |
8091aa91 |
304 | Issues a commit against the current dbh. |
8b445e33 |
305 | |
8091aa91 |
306 | =cut |
307 | |
308 | sub txn_commit { |
d79f59b9 |
309 | my $self = shift; |
310 | if ($self->{transaction_depth} == 0) { |
311 | $self->dbh->commit unless $self->dbh->{AutoCommit}; |
8091aa91 |
312 | } |
313 | else { |
d79f59b9 |
314 | $self->dbh->commit if --$self->{transaction_depth} == 0; |
8091aa91 |
315 | } |
316 | } |
317 | |
318 | =head2 txn_rollback |
319 | |
320 | Issues a rollback against the current dbh. |
8b445e33 |
321 | |
322 | =cut |
323 | |
8091aa91 |
324 | sub txn_rollback { |
d79f59b9 |
325 | my $self = shift; |
326 | if ($self->{transaction_depth} == 0) { |
327 | $self->dbh->rollback unless $self->dbh->{AutoCommit}; |
8091aa91 |
328 | } |
329 | else { |
d79f59b9 |
330 | --$self->{transaction_depth} == 0 ? $self->dbh->rollback : die $@; |
8091aa91 |
331 | } |
332 | } |
8b445e33 |
333 | |
223b8fe3 |
334 | sub _execute { |
335 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
336 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
337 | unshift(@bind, @$extra_bind) if $extra_bind; |
f59ffc79 |
338 | if ($self->debug) { |
339 | my @debug_bind = map { defined $_ ? $_ : 'NULL' } @bind; |
340 | $self->debugfh->print("$sql: @debug_bind\n"); |
341 | } |
2f5911b2 |
342 | my $sth = $self->sth($sql,$op); |
438adc0e |
343 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
701da8c4 |
344 | my $rv; |
345 | if ($sth) { |
346 | $rv = $sth->execute(@bind); |
347 | } else { |
348 | croak "'$sql' did not generate a statement."; |
349 | } |
223b8fe3 |
350 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
351 | } |
352 | |
8b445e33 |
353 | sub insert { |
354 | my ($self, $ident, $to_insert) = @_; |
701da8c4 |
355 | croak( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" ) |
a4b2f17b |
356 | unless ($self->_execute('insert' => [], $ident, $to_insert)); |
8b445e33 |
357 | return $to_insert; |
358 | } |
359 | |
360 | sub update { |
223b8fe3 |
361 | return shift->_execute('update' => [], @_); |
8b445e33 |
362 | } |
363 | |
364 | sub delete { |
223b8fe3 |
365 | return shift->_execute('delete' => [], @_); |
8b445e33 |
366 | } |
367 | |
de705b51 |
368 | sub _select { |
8b445e33 |
369 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
370 | my $order = $attrs->{order_by}; |
371 | if (ref $condition eq 'SCALAR') { |
372 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
373 | } |
54540863 |
374 | if (exists $attrs->{group_by}) { |
375 | $order = { group_by => $attrs->{group_by}, |
376 | ($order ? (order_by => $order) : ()) }; |
377 | } |
5c91499f |
378 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
379 | if ($attrs->{software_limit} || |
380 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
381 | $attrs->{software_limit} = 1; |
5c91499f |
382 | } else { |
383 | push @args, $attrs->{rows}, $attrs->{offset}; |
384 | } |
de705b51 |
385 | return $self->_execute(@args); |
386 | } |
387 | |
388 | sub select { |
389 | my $self = shift; |
390 | my ($ident, $select, $condition, $attrs) = @_; |
cb5f2eea |
391 | return $self->cursor->new($self, \@_, $attrs); |
8b445e33 |
392 | } |
393 | |
6157db4f |
394 | # Need to call finish() to work round broken DBDs |
395 | |
1a14aa3f |
396 | sub select_single { |
de705b51 |
397 | my $self = shift; |
398 | my ($rv, $sth, @bind) = $self->_select(@_); |
6157db4f |
399 | my @row = $sth->fetchrow_array; |
400 | $sth->finish(); |
401 | return @row; |
1a14aa3f |
402 | } |
403 | |
8b445e33 |
404 | sub sth { |
cb5f2eea |
405 | my ($self, $sql) = @_; |
91fa659e |
406 | # 3 is the if_active parameter which avoids active sth re-use |
407 | return $self->dbh->prepare_cached($sql, {}, 3); |
8b445e33 |
408 | } |
409 | |
a953d8d9 |
410 | =head2 columns_info_for |
411 | |
412 | Returns database type info for a given table columns. |
413 | |
414 | =cut |
415 | |
416 | sub columns_info_for { |
417 | my ($self, $table) = @_; |
a953d8d9 |
418 | my %result; |
103e3e03 |
419 | if ( $self->dbh->can( 'column_info' ) ){ |
420 | my $sth = $self->dbh->column_info( undef, undef, $table, '%' ); |
421 | $sth->execute(); |
422 | while ( my $info = $sth->fetchrow_hashref() ){ |
423 | my %column_info; |
a953d8d9 |
424 | $column_info{data_type} = $info->{TYPE_NAME}; |
425 | $column_info{size} = $info->{COLUMN_SIZE}; |
426 | $column_info{is_nullable} = $info->{NULLABLE}; |
103e3e03 |
427 | $result{$info->{COLUMN_NAME}} = \%column_info; |
428 | } |
429 | }else{ |
430 | my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); |
431 | $sth->execute; |
432 | my @columns = @{$sth->{NAME}}; |
433 | for my $i ( 0 .. $#columns ){ |
434 | $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i]; |
a953d8d9 |
435 | } |
a953d8d9 |
436 | } |
437 | return \%result; |
438 | } |
439 | |
843f8ecd |
440 | sub last_insert_id { |
441 | my ($self, $row) = @_; |
442 | |
443 | return $self->dbh->func('last_insert_rowid'); |
444 | |
445 | } |
446 | |
447 | |
448 | |
92925617 |
449 | sub DESTROY { shift->disconnect } |
450 | |
8b445e33 |
451 | 1; |
452 | |
92b858c9 |
453 | =head1 ENVIRONMENT VARIABLES |
454 | |
455 | =head2 DBIX_CLASS_STORAGE_DBI_DEBUG |
456 | |
457 | If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information |
458 | is produced (as when the L<debug> method is set). |
459 | |
460 | If the value is of the form C<1=/path/name> then the trace output is |
461 | written to the file C</path/name>. |
462 | |
8b445e33 |
463 | =head1 AUTHORS |
464 | |
daec44b8 |
465 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
466 | |
9f19b1d6 |
467 | Andy Grundman <andy@hybridized.org> |
468 | |
8b445e33 |
469 | =head1 LICENSE |
470 | |
471 | You may distribute this code under the same terms as Perl itself. |
472 | |
473 | =cut |
474 | |