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