Commit | Line | Data |
8b445e33 |
1 | package DBIx::Class::Storage::DBI; |
2 | |
a62cf8d4 |
3 | use base 'DBIx::Class::Storage'; |
4 | |
20a2c954 |
5 | use strict; |
6 | use warnings; |
8b445e33 |
7 | use DBI; |
aeaf3ce2 |
8 | use SQL::Abstract::Limit; |
28927b50 |
9 | use DBIx::Class::Storage::DBI::Cursor; |
92b858c9 |
10 | use IO::File; |
701da8c4 |
11 | use Carp::Clan qw/DBIx::Class/; |
8b445e33 |
12 | |
bd7efd39 |
13 | BEGIN { |
14 | |
cb5f2eea |
15 | package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :( |
bd7efd39 |
16 | |
17 | use base qw/SQL::Abstract::Limit/; |
18 | |
54540863 |
19 | sub select { |
20 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
21 | @rest = (-1) unless defined $rest[0]; |
8839560b |
22 | local $self->{having_bind} = []; |
bc0c9800 |
23 | my ($sql, @ret) = $self->SUPER::select( |
24 | $table, $self->_recurse_fields($fields), $where, $order, @rest |
25 | ); |
8839560b |
26 | return wantarray ? ($sql, @ret, @{$self->{having_bind}}) : $sql; |
54540863 |
27 | } |
28 | |
29 | sub _emulate_limit { |
30 | my $self = shift; |
31 | if ($_[3] == -1) { |
32 | return $_[1].$self->_order_by($_[2]); |
33 | } else { |
34 | return $self->SUPER::_emulate_limit(@_); |
35 | } |
36 | } |
37 | |
38 | sub _recurse_fields { |
39 | my ($self, $fields) = @_; |
40 | my $ref = ref $fields; |
41 | return $self->_quote($fields) unless $ref; |
42 | return $$fields if $ref eq 'SCALAR'; |
43 | |
44 | if ($ref eq 'ARRAY') { |
45 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
46 | } elsif ($ref eq 'HASH') { |
47 | foreach my $func (keys %$fields) { |
48 | return $self->_sqlcase($func) |
49 | .'( '.$self->_recurse_fields($fields->{$func}).' )'; |
50 | } |
51 | } |
52 | } |
53 | |
54 | sub _order_by { |
55 | my $self = shift; |
56 | my $ret = ''; |
8839560b |
57 | my @extra; |
54540863 |
58 | if (ref $_[0] eq 'HASH') { |
59 | if (defined $_[0]->{group_by}) { |
60 | $ret = $self->_sqlcase(' group by ') |
61 | .$self->_recurse_fields($_[0]->{group_by}); |
62 | } |
8839560b |
63 | if (defined $_[0]->{having}) { |
64 | my $frag; |
65 | ($frag, @extra) = $self->_recurse_where($_[0]->{having}); |
66 | push(@{$self->{having_bind}}, @extra); |
67 | $ret .= $self->_sqlcase(' having ').$frag; |
68 | } |
54540863 |
69 | if (defined $_[0]->{order_by}) { |
70 | $ret .= $self->SUPER::_order_by($_[0]->{order_by}); |
71 | } |
e535069e |
72 | } elsif(ref $_[0] eq 'SCALAR') { |
73 | $ret = $self->_sqlcase(' order by ').${ $_[0] }; |
54540863 |
74 | } else { |
75 | $ret = $self->SUPER::_order_by(@_); |
76 | } |
77 | return $ret; |
78 | } |
79 | |
f48dd03f |
80 | sub _order_directions { |
81 | my ($self, $order) = @_; |
82 | $order = $order->{order_by} if ref $order eq 'HASH'; |
83 | return $self->SUPER::_order_directions($order); |
84 | } |
85 | |
2a816814 |
86 | sub _table { |
bd7efd39 |
87 | my ($self, $from) = @_; |
88 | if (ref $from eq 'ARRAY') { |
89 | return $self->_recurse_from(@$from); |
90 | } elsif (ref $from eq 'HASH') { |
91 | return $self->_make_as($from); |
92 | } else { |
93 | return $from; |
94 | } |
95 | } |
96 | |
97 | sub _recurse_from { |
98 | my ($self, $from, @join) = @_; |
99 | my @sqlf; |
100 | push(@sqlf, $self->_make_as($from)); |
101 | foreach my $j (@join) { |
102 | my ($to, $on) = @$j; |
73856587 |
103 | |
54540863 |
104 | # check whether a join type exists |
105 | my $join_clause = ''; |
106 | if (ref($to) eq 'HASH' and exists($to->{-join_type})) { |
107 | $join_clause = ' '.uc($to->{-join_type}).' JOIN '; |
108 | } else { |
109 | $join_clause = ' JOIN '; |
110 | } |
73856587 |
111 | push(@sqlf, $join_clause); |
112 | |
bd7efd39 |
113 | if (ref $to eq 'ARRAY') { |
114 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
115 | } else { |
96cdbbab |
116 | push(@sqlf, $self->_make_as($to)); |
bd7efd39 |
117 | } |
118 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
119 | } |
120 | return join('', @sqlf); |
121 | } |
122 | |
123 | sub _make_as { |
124 | my ($self, $from) = @_; |
54540863 |
125 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) } |
bc0c9800 |
126 | reverse each %{$self->_skip_options($from)}); |
73856587 |
127 | } |
128 | |
129 | sub _skip_options { |
54540863 |
130 | my ($self, $hash) = @_; |
131 | my $clean_hash = {}; |
132 | $clean_hash->{$_} = $hash->{$_} |
133 | for grep {!/^-/} keys %$hash; |
134 | return $clean_hash; |
bd7efd39 |
135 | } |
136 | |
137 | sub _join_condition { |
138 | my ($self, $cond) = @_; |
5efe4c79 |
139 | if (ref $cond eq 'HASH') { |
140 | my %j; |
bc0c9800 |
141 | for (keys %$cond) { |
142 | my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; |
143 | }; |
5efe4c79 |
144 | return $self->_recurse_where(\%j); |
145 | } elsif (ref $cond eq 'ARRAY') { |
146 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
147 | } else { |
148 | die "Can't handle this yet!"; |
149 | } |
bd7efd39 |
150 | } |
151 | |
2a816814 |
152 | sub _quote { |
153 | my ($self, $label) = @_; |
154 | return '' unless defined $label; |
3b24f6ea |
155 | return "*" if $label eq '*'; |
41728a6e |
156 | return $label unless $self->{quote_char}; |
3b24f6ea |
157 | if(ref $self->{quote_char} eq "ARRAY"){ |
158 | return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1] |
159 | if !defined $self->{name_sep}; |
160 | my $sep = $self->{name_sep}; |
161 | return join($self->{name_sep}, |
162 | map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } |
163 | split(/\Q$sep\E/,$label)); |
164 | } |
2a816814 |
165 | return $self->SUPER::_quote($label); |
166 | } |
167 | |
f66596f9 |
168 | sub _RowNum { |
169 | my $self = shift; |
170 | my $c; |
171 | $_[0] =~ s/SELECT (.*?) FROM/ |
172 | 'SELECT '.join(', ', map { $_.' AS col'.++$c } split(', ', $1)).' FROM'/e; |
173 | $self->SUPER::_RowNum(@_); |
174 | } |
175 | |
7be93b07 |
176 | # Accessor for setting limit dialect. This is useful |
177 | # for JDBC-bridge among others where the remote SQL-dialect cannot |
178 | # be determined by the name of the driver alone. |
179 | # |
180 | sub limit_dialect { |
181 | my $self = shift; |
182 | $self->{limit_dialect} = shift if @_; |
183 | return $self->{limit_dialect}; |
184 | } |
185 | |
2437a1e3 |
186 | sub quote_char { |
187 | my $self = shift; |
188 | $self->{quote_char} = shift if @_; |
189 | return $self->{quote_char}; |
190 | } |
191 | |
192 | sub name_sep { |
193 | my $self = shift; |
194 | $self->{name_sep} = shift if @_; |
195 | return $self->{name_sep}; |
196 | } |
197 | |
198 | |
199 | |
200 | |
486ad69b |
201 | package DBIx::Class::Storage::DBI::DebugCallback; |
202 | |
203 | sub print { |
204 | my ($self, $string) = @_; |
205 | $string =~ m/^(\w+)/; |
206 | ${$self}->($1, $string); |
207 | } |
208 | |
bd7efd39 |
209 | } # End of BEGIN block |
210 | |
8b445e33 |
211 | use base qw/DBIx::Class/; |
212 | |
1f692767 |
213 | __PACKAGE__->load_components(qw/AccessorGroup/); |
8b445e33 |
214 | |
223b8fe3 |
215 | __PACKAGE__->mk_group_accessors('simple' => |
1b45b01e |
216 | qw/_connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugfh |
1346e22d |
217 | cursor on_connect_do transaction_depth/); |
8091aa91 |
218 | |
8b445e33 |
219 | sub new { |
223b8fe3 |
220 | my $new = bless({}, ref $_[0] || $_[0]); |
28927b50 |
221 | $new->cursor("DBIx::Class::Storage::DBI::Cursor"); |
d79f59b9 |
222 | $new->transaction_depth(0); |
5e65c358 |
223 | if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) && |
224 | ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) { |
bc0c9800 |
225 | $new->debugfh(IO::File->new($1, 'w')) |
226 | or $new->throw_exception("Cannot open trace file $1"); |
92b858c9 |
227 | } else { |
228 | $new->debugfh(IO::File->new('>&STDERR')); |
229 | } |
28927b50 |
230 | $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; |
223b8fe3 |
231 | return $new; |
8b445e33 |
232 | } |
233 | |
1c339d71 |
234 | sub throw_exception { |
235 | my ($self, $msg) = @_; |
3b042bcb |
236 | croak($msg); |
1c339d71 |
237 | } |
238 | |
75d07914 |
239 | =head1 NAME |
8b445e33 |
240 | |
241 | DBIx::Class::Storage::DBI - DBI storage handler |
242 | |
243 | =head1 SYNOPSIS |
244 | |
245 | =head1 DESCRIPTION |
246 | |
247 | This class represents the connection to the database |
248 | |
249 | =head1 METHODS |
250 | |
8b445e33 |
251 | =cut |
252 | |
1b45b01e |
253 | =head2 connect_info |
254 | |
255 | Connection information arrayref. Can either be the same arguments |
256 | one would pass to DBI->connect, or a code-reference which returns |
257 | a connected database handle. In either case, there is an optional |
258 | final element in the arrayref, which can hold a hashref of |
259 | connection-specific Storage::DBI options. These include |
260 | C<on_connect_do>, and the sql_maker options C<limit_dialect>, |
261 | C<quote_char>, and C<name_sep>. Examples: |
262 | |
263 | ->connect_info([ 'dbi:SQLite:./foo.db' ]); |
264 | ->connect_info(sub { DBI->connect(...) }); |
265 | ->connect_info([ 'dbi:Pg:dbname=foo', |
266 | 'postgres', |
267 | '', |
268 | { AutoCommit => 0 }, |
269 | { quote_char => q{`}, name_sep => q{@} }, |
270 | ]); |
271 | |
d7c4c15c |
272 | =head2 on_connect_do |
273 | |
274 | Executes the sql statements given as a listref on every db connect. |
275 | |
92b858c9 |
276 | =head2 debug |
277 | |
278 | Causes SQL trace information to be emitted on C<debugfh> filehandle |
279 | (or C<STDERR> if C<debugfh> has not specifically been set). |
280 | |
281 | =head2 debugfh |
282 | |
283 | Sets or retrieves the filehandle used for trace/debug output. This |
284 | should be an IO::Handle compatible object (only the C<print> method is |
285 | used). Initially set to be STDERR - although see information on the |
286 | L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable. |
287 | |
486ad69b |
288 | =head2 debugcb |
289 | |
290 | Sets a callback to be executed each time a statement is run; takes a sub |
291 | reference. Overrides debugfh. Callback is executed as $sub->($op, $info) |
292 | where $op is SELECT/INSERT/UPDATE/DELETE and $info is what would normally |
293 | be printed. |
294 | |
d7c4c15c |
295 | =cut |
296 | |
486ad69b |
297 | sub debugcb { |
298 | my ($self, $cb) = @_; |
299 | my $cb_obj = bless(\$cb, 'DBIx::Class::Storage::DBI::DebugCallback'); |
300 | $self->debugfh($cb_obj); |
301 | } |
302 | |
412db1f4 |
303 | sub disconnect { |
304 | my ($self) = @_; |
305 | |
92925617 |
306 | if( $self->connected ) { |
307 | $self->_dbh->rollback unless $self->_dbh->{AutoCommit}; |
308 | $self->_dbh->disconnect; |
309 | $self->_dbh(undef); |
310 | } |
412db1f4 |
311 | } |
312 | |
313 | sub connected { |
8b445e33 |
314 | my ($self) = @_; |
412db1f4 |
315 | |
1346e22d |
316 | if(my $dbh = $self->_dbh) { |
317 | if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) { |
318 | $self->_sql_maker(undef); |
319 | return $self->_dbh(undef); |
320 | } |
321 | elsif($self->_conn_pid != $$) { |
322 | $self->_dbh->{InactiveDestroy} = 1; |
323 | $self->_sql_maker(undef); |
324 | return $self->_dbh(undef) |
325 | } |
326 | return ($dbh->FETCH('Active') && $dbh->ping); |
327 | } |
328 | |
329 | return 0; |
412db1f4 |
330 | } |
331 | |
332 | sub ensure_connected { |
333 | my ($self) = @_; |
334 | |
335 | unless ($self->connected) { |
8b445e33 |
336 | $self->_populate_dbh; |
337 | } |
412db1f4 |
338 | } |
339 | |
340 | sub dbh { |
341 | my ($self) = @_; |
342 | |
343 | $self->ensure_connected; |
8b445e33 |
344 | return $self->_dbh; |
345 | } |
346 | |
48c69e7c |
347 | sub sql_maker { |
348 | my ($self) = @_; |
fdc1c3d0 |
349 | unless ($self->_sql_maker) { |
bd7efd39 |
350 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
351 | } |
352 | return $self->_sql_maker; |
353 | } |
354 | |
1b45b01e |
355 | sub connect_info { |
356 | my ($self, $info_arg) = @_; |
357 | |
358 | if($info_arg) { |
359 | my $info = [ @$info_arg ]; # copy because we can alter it |
360 | my $last_info = $info->[-1]; |
361 | if(ref $last_info eq 'HASH') { |
362 | my $used; |
363 | if(my $on_connect_do = $last_info->{on_connect_do}) { |
364 | $used = 1; |
365 | $self->on_connect_do($self->{on_connect_do}); |
366 | } |
367 | foreach my $sql_maker_opt (qw/limit_dialect quote_char name_sep/) { |
368 | if(my $opt_val = $last_info->{$sql_maker_opt}) { |
369 | $used = 1; |
370 | $self->sql_maker->$sql_maker_opt($opt_val); |
371 | } |
372 | } |
373 | |
374 | # remove our options hashref if it was there, to avoid confusing |
375 | # DBI in the case the user didn't use all 4 DBI options, as in: |
376 | # [ 'dbi:SQLite:foo.db', { quote_char => q{`} } ] |
377 | pop(@$info) if $used; |
378 | } |
379 | |
380 | $self->_connect_info($info); |
381 | } |
382 | |
383 | $self->_connect_info; |
384 | } |
385 | |
8b445e33 |
386 | sub _populate_dbh { |
387 | my ($self) = @_; |
1b45b01e |
388 | my @info = @{$self->_connect_info || []}; |
8b445e33 |
389 | $self->_dbh($self->_connect(@info)); |
843f8ecd |
390 | my $driver = $self->_dbh->{Driver}->{Name}; |
34470972 |
391 | eval "require DBIx::Class::Storage::DBI::${driver}"; |
392 | unless ($@) { |
843f8ecd |
393 | bless $self, "DBIx::Class::Storage::DBI::${driver}"; |
394 | } |
d7c4c15c |
395 | # if on-connect sql statements are given execute them |
396 | foreach my $sql_statement (@{$self->on_connect_do || []}) { |
397 | $self->_dbh->do($sql_statement); |
398 | } |
5ef3e508 |
399 | |
1346e22d |
400 | $self->_conn_pid($$); |
401 | $self->_conn_tid(threads->tid) if $INC{'threads.pm'}; |
8b445e33 |
402 | } |
403 | |
404 | sub _connect { |
405 | my ($self, @info) = @_; |
5ef3e508 |
406 | |
9d31f7dc |
407 | $self->throw_exception("You failed to provide any connection info") |
408 | if !@info; |
409 | |
90ec6cad |
410 | my ($old_connect_via, $dbh); |
411 | |
5ef3e508 |
412 | if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) { |
90ec6cad |
413 | $old_connect_via = $DBI::connect_via; |
5ef3e508 |
414 | $DBI::connect_via = 'connect'; |
5ef3e508 |
415 | } |
416 | |
90ec6cad |
417 | if(ref $info[0] eq 'CODE') { |
418 | $dbh = &{$info[0]}; |
419 | } |
420 | else { |
421 | $dbh = DBI->connect(@info); |
422 | } |
423 | |
424 | $DBI::connect_via = $old_connect_via if $old_connect_via; |
425 | |
1c339d71 |
426 | $self->throw_exception("DBI Connection failed: $DBI::errstr") |
e571e823 |
427 | unless $dbh; |
90ec6cad |
428 | |
e571e823 |
429 | $dbh; |
8b445e33 |
430 | } |
431 | |
8091aa91 |
432 | =head2 txn_begin |
8b445e33 |
433 | |
8091aa91 |
434 | Calls begin_work on the current dbh. |
8b445e33 |
435 | |
181a28f4 |
436 | See L<DBIx::Class::Schema> for the txn_do() method, which allows for |
437 | an entire code block to be executed transactionally. |
438 | |
8b445e33 |
439 | =cut |
440 | |
8091aa91 |
441 | sub txn_begin { |
d79f59b9 |
442 | my $self = shift; |
181a28f4 |
443 | $self->dbh->begin_work |
444 | if $self->{transaction_depth}++ == 0 and $self->dbh->{AutoCommit}; |
8091aa91 |
445 | } |
8b445e33 |
446 | |
8091aa91 |
447 | =head2 txn_commit |
8b445e33 |
448 | |
8091aa91 |
449 | Issues a commit against the current dbh. |
8b445e33 |
450 | |
8091aa91 |
451 | =cut |
452 | |
453 | sub txn_commit { |
d79f59b9 |
454 | my $self = shift; |
455 | if ($self->{transaction_depth} == 0) { |
456 | $self->dbh->commit unless $self->dbh->{AutoCommit}; |
8091aa91 |
457 | } |
458 | else { |
75d07914 |
459 | $self->dbh->commit if --$self->{transaction_depth} == 0; |
8091aa91 |
460 | } |
461 | } |
462 | |
463 | =head2 txn_rollback |
464 | |
181a28f4 |
465 | Issues a rollback against the current dbh. A nested rollback will |
466 | throw a L<DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION> exception, |
467 | which allows the rollback to propagate to the outermost transaction. |
8b445e33 |
468 | |
469 | =cut |
470 | |
8091aa91 |
471 | sub txn_rollback { |
d79f59b9 |
472 | my $self = shift; |
a62cf8d4 |
473 | |
474 | eval { |
475 | if ($self->{transaction_depth} == 0) { |
476 | $self->dbh->rollback unless $self->dbh->{AutoCommit}; |
477 | } |
478 | else { |
479 | --$self->{transaction_depth} == 0 ? |
480 | $self->dbh->rollback : |
1346e22d |
481 | die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new; |
a62cf8d4 |
482 | } |
483 | }; |
484 | |
485 | if ($@) { |
486 | my $error = $@; |
487 | my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION"; |
488 | $error =~ /$exception_class/ and $self->throw_exception($error); |
489 | $self->{transaction_depth} = 0; # ensure that a failed rollback |
490 | $self->throw_exception($error); # resets the transaction depth |
8091aa91 |
491 | } |
492 | } |
8b445e33 |
493 | |
223b8fe3 |
494 | sub _execute { |
495 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
496 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
497 | unshift(@bind, @$extra_bind) if $extra_bind; |
f59ffc79 |
498 | if ($self->debug) { |
181a28f4 |
499 | my @debug_bind = map { defined $_ ? qq{`$_'} : q{`NULL'} } @bind; |
500 | $self->debugfh->print("$sql: " . join(', ', @debug_bind) . "\n"); |
f59ffc79 |
501 | } |
2f5911b2 |
502 | my $sth = $self->sth($sql,$op); |
1c339d71 |
503 | $self->throw_exception("no sth generated via sql: $sql") unless $sth; |
438adc0e |
504 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
701da8c4 |
505 | my $rv; |
75d07914 |
506 | if ($sth) { |
bc0c9800 |
507 | $rv = $sth->execute(@bind) |
508 | or $self->throw_exception("Error executing '$sql': " . $sth->errstr); |
75d07914 |
509 | } else { |
1c339d71 |
510 | $self->throw_exception("'$sql' did not generate a statement."); |
701da8c4 |
511 | } |
223b8fe3 |
512 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
513 | } |
514 | |
8b445e33 |
515 | sub insert { |
516 | my ($self, $ident, $to_insert) = @_; |
bc0c9800 |
517 | $self->throw_exception( |
518 | "Couldn't insert ".join(', ', |
519 | map "$_ => $to_insert->{$_}", keys %$to_insert |
520 | )." into ${ident}" |
521 | ) unless ($self->_execute('insert' => [], $ident, $to_insert)); |
8b445e33 |
522 | return $to_insert; |
523 | } |
524 | |
525 | sub update { |
223b8fe3 |
526 | return shift->_execute('update' => [], @_); |
8b445e33 |
527 | } |
528 | |
529 | sub delete { |
223b8fe3 |
530 | return shift->_execute('delete' => [], @_); |
8b445e33 |
531 | } |
532 | |
de705b51 |
533 | sub _select { |
8b445e33 |
534 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
535 | my $order = $attrs->{order_by}; |
536 | if (ref $condition eq 'SCALAR') { |
537 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
538 | } |
8839560b |
539 | if (exists $attrs->{group_by} || $attrs->{having}) { |
bc0c9800 |
540 | $order = { |
541 | group_by => $attrs->{group_by}, |
542 | having => $attrs->{having}, |
543 | ($order ? (order_by => $order) : ()) |
544 | }; |
54540863 |
545 | } |
5c91499f |
546 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
547 | if ($attrs->{software_limit} || |
548 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
549 | $attrs->{software_limit} = 1; |
5c91499f |
550 | } else { |
551 | push @args, $attrs->{rows}, $attrs->{offset}; |
552 | } |
de705b51 |
553 | return $self->_execute(@args); |
554 | } |
555 | |
556 | sub select { |
557 | my $self = shift; |
558 | my ($ident, $select, $condition, $attrs) = @_; |
cb5f2eea |
559 | return $self->cursor->new($self, \@_, $attrs); |
8b445e33 |
560 | } |
561 | |
6157db4f |
562 | # Need to call finish() to work round broken DBDs |
563 | |
1a14aa3f |
564 | sub select_single { |
de705b51 |
565 | my $self = shift; |
566 | my ($rv, $sth, @bind) = $self->_select(@_); |
6157db4f |
567 | my @row = $sth->fetchrow_array; |
568 | $sth->finish(); |
569 | return @row; |
1a14aa3f |
570 | } |
571 | |
8b445e33 |
572 | sub sth { |
cb5f2eea |
573 | my ($self, $sql) = @_; |
91fa659e |
574 | # 3 is the if_active parameter which avoids active sth re-use |
575 | return $self->dbh->prepare_cached($sql, {}, 3); |
8b445e33 |
576 | } |
577 | |
a953d8d9 |
578 | =head2 columns_info_for |
579 | |
580 | Returns database type info for a given table columns. |
581 | |
582 | =cut |
583 | |
584 | sub columns_info_for { |
0d67fe74 |
585 | my ($self, $table) = @_; |
bfe10d87 |
586 | |
0d67fe74 |
587 | if ($self->dbh->can('column_info')) { |
a953d8d9 |
588 | my %result; |
0d67fe74 |
589 | my $old_raise_err = $self->dbh->{RaiseError}; |
590 | my $old_print_err = $self->dbh->{PrintError}; |
591 | $self->dbh->{RaiseError} = 1; |
592 | $self->dbh->{PrintError} = 0; |
593 | eval { |
594 | my $sth = $self->dbh->column_info( undef, undef, $table, '%' ); |
595 | $sth->execute(); |
596 | while ( my $info = $sth->fetchrow_hashref() ){ |
bfe10d87 |
597 | my %column_info; |
0d67fe74 |
598 | $column_info{data_type} = $info->{TYPE_NAME}; |
599 | $column_info{size} = $info->{COLUMN_SIZE}; |
600 | $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
601 | $column_info{default_value} = $info->{COLUMN_DEF}; |
602 | |
603 | $result{$info->{COLUMN_NAME}} = \%column_info; |
604 | } |
605 | }; |
606 | $self->dbh->{RaiseError} = $old_raise_err; |
607 | $self->dbh->{PrintError} = $old_print_err; |
608 | return \%result if !$@; |
609 | } |
610 | |
611 | my %result; |
612 | my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); |
613 | $sth->execute; |
614 | my @columns = @{$sth->{NAME_lc}}; |
615 | for my $i ( 0 .. $#columns ){ |
616 | my %column_info; |
617 | my $type_num = $sth->{TYPE}->[$i]; |
618 | my $type_name; |
619 | if(defined $type_num && $self->dbh->can('type_info')) { |
620 | my $type_info = $self->dbh->type_info($type_num); |
621 | $type_name = $type_info->{TYPE_NAME} if $type_info; |
a953d8d9 |
622 | } |
0d67fe74 |
623 | $column_info{data_type} = $type_name ? $type_name : $type_num; |
624 | $column_info{size} = $sth->{PRECISION}->[$i]; |
625 | $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0; |
626 | |
627 | if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
628 | $column_info{data_type} = $1; |
629 | $column_info{size} = $2; |
630 | } |
631 | |
632 | $result{$columns[$i]} = \%column_info; |
633 | } |
bfe10d87 |
634 | |
0d67fe74 |
635 | return \%result; |
a953d8d9 |
636 | } |
637 | |
843f8ecd |
638 | sub last_insert_id { |
639 | my ($self, $row) = @_; |
640 | |
641 | return $self->dbh->func('last_insert_rowid'); |
642 | |
643 | } |
644 | |
90ec6cad |
645 | sub sqlt_type { shift->dbh->{Driver}->{Name} } |
1c339d71 |
646 | |
647 | sub deployment_statements { |
cb561d1a |
648 | my ($self, $schema, $type, $sqltargs) = @_; |
1c339d71 |
649 | $type ||= $self->sqlt_type; |
650 | eval "use SQL::Translator"; |
651 | $self->throw_exception("Can't deploy without SQL::Translator: $@") if $@; |
652 | eval "use SQL::Translator::Parser::DBIx::Class;"; |
75d07914 |
653 | $self->throw_exception($@) if $@; |
1c339d71 |
654 | eval "use SQL::Translator::Producer::${type};"; |
655 | $self->throw_exception($@) if $@; |
cb561d1a |
656 | my $tr = SQL::Translator->new(%$sqltargs); |
1c339d71 |
657 | SQL::Translator::Parser::DBIx::Class::parse( $tr, $schema ); |
658 | return "SQL::Translator::Producer::${type}"->can('produce')->($tr); |
659 | } |
843f8ecd |
660 | |
1c339d71 |
661 | sub deploy { |
cb561d1a |
662 | my ($self, $schema, $type, $sqltargs) = @_; |
bc0c9800 |
663 | my @statements = $self->deployment_statements($schema, $type, $sqltargs); |
664 | foreach(split(";\n", @statements)) { |
665 | $self->debugfh->print("$_\n") if $self->debug; |
666 | $self->dbh->do($_) or warn "SQL was:\n $_"; |
75d07914 |
667 | } |
1c339d71 |
668 | } |
843f8ecd |
669 | |
92925617 |
670 | sub DESTROY { shift->disconnect } |
671 | |
8b445e33 |
672 | 1; |
673 | |
92b858c9 |
674 | =head1 ENVIRONMENT VARIABLES |
675 | |
676 | =head2 DBIX_CLASS_STORAGE_DBI_DEBUG |
677 | |
678 | If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information |
679 | is produced (as when the L<debug> method is set). |
680 | |
681 | If the value is of the form C<1=/path/name> then the trace output is |
682 | written to the file C</path/name>. |
683 | |
8b445e33 |
684 | =head1 AUTHORS |
685 | |
daec44b8 |
686 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
687 | |
9f19b1d6 |
688 | Andy Grundman <andy@hybridized.org> |
689 | |
8b445e33 |
690 | =head1 LICENSE |
691 | |
692 | You may distribute this code under the same terms as Perl itself. |
693 | |
694 | =cut |
695 | |