bugfix for join-types in nested joins using the from attribute (+ test)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI.pm
1 package DBIx::Class::Storage::DBI;
2 # -*- mode: cperl; cperl-indent-level: 2 -*-
3
4 use base 'DBIx::Class::Storage';
5
6 use strict;
7 use warnings;
8 use DBI;
9 use SQL::Abstract::Limit;
10 use DBIx::Class::Storage::DBI::Cursor;
11 use DBIx::Class::Storage::Statistics;
12 use IO::File;
13 use Carp::Clan qw/DBIx::Class/;
14 BEGIN {
15
16 package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :(
17
18 use base qw/SQL::Abstract::Limit/;
19
20 sub select {
21   my ($self, $table, $fields, $where, $order, @rest) = @_;
22   $table = $self->_quote($table) unless ref($table);
23   @rest = (-1) unless defined $rest[0];
24   die "LIMIT 0 Does Not Compute" if $rest[0] == 0;
25     # and anyway, SQL::Abstract::Limit will cause a barf if we don't first
26   local $self->{having_bind} = [];
27   my ($sql, @ret) = $self->SUPER::select(
28     $table, $self->_recurse_fields($fields), $where, $order, @rest
29   );
30   return wantarray ? ($sql, @ret, @{$self->{having_bind}}) : $sql;
31 }
32
33 sub insert {
34   my $self = shift;
35   my $table = shift;
36   $table = $self->_quote($table) unless ref($table);
37   $self->SUPER::insert($table, @_);
38 }
39
40 sub update {
41   my $self = shift;
42   my $table = shift;
43   $table = $self->_quote($table) unless ref($table);
44   $self->SUPER::update($table, @_);
45 }
46
47 sub delete {
48   my $self = shift;
49   my $table = shift;
50   $table = $self->_quote($table) unless ref($table);
51   $self->SUPER::delete($table, @_);
52 }
53
54 sub _emulate_limit {
55   my $self = shift;
56   if ($_[3] == -1) {
57     return $_[1].$self->_order_by($_[2]);
58   } else {
59     return $self->SUPER::_emulate_limit(@_);
60   }
61 }
62
63 sub _recurse_fields {
64   my ($self, $fields) = @_;
65   my $ref = ref $fields;
66   return $self->_quote($fields) unless $ref;
67   return $$fields if $ref eq 'SCALAR';
68
69   if ($ref eq 'ARRAY') {
70     return join(', ', map { $self->_recurse_fields($_) } @$fields);
71   } elsif ($ref eq 'HASH') {
72     foreach my $func (keys %$fields) {
73       return $self->_sqlcase($func)
74         .'( '.$self->_recurse_fields($fields->{$func}).' )';
75     }
76   }
77 }
78
79 sub _order_by {
80   my $self = shift;
81   my $ret = '';
82   my @extra;
83   if (ref $_[0] eq 'HASH') {
84     if (defined $_[0]->{group_by}) {
85       $ret = $self->_sqlcase(' group by ')
86                .$self->_recurse_fields($_[0]->{group_by});
87     }
88     if (defined $_[0]->{having}) {
89       my $frag;
90       ($frag, @extra) = $self->_recurse_where($_[0]->{having});
91       push(@{$self->{having_bind}}, @extra);
92       $ret .= $self->_sqlcase(' having ').$frag;
93     }
94     if (defined $_[0]->{order_by}) {
95       $ret .= $self->SUPER::_order_by($_[0]->{order_by});
96     }
97   } elsif(ref $_[0] eq 'SCALAR') {
98     $ret = $self->_sqlcase(' order by ').${ $_[0] };
99   } else {
100     $ret = $self->SUPER::_order_by(@_);
101   }
102   return $ret;
103 }
104
105 sub _order_directions {
106   my ($self, $order) = @_;
107   $order = $order->{order_by} if ref $order eq 'HASH';
108   return $self->SUPER::_order_directions($order);
109 }
110
111 sub _table {
112   my ($self, $from) = @_;
113   if (ref $from eq 'ARRAY') {
114     return $self->_recurse_from(@$from);
115   } elsif (ref $from eq 'HASH') {
116     return $self->_make_as($from);
117   } else {
118     return $from; # would love to quote here but _table ends up getting called
119                   # twice during an ->select without a limit clause due to
120                   # the way S::A::Limit->select works. should maybe consider
121                   # bypassing this and doing S::A::select($self, ...) in
122                   # our select method above. meantime, quoting shims have
123                   # been added to select/insert/update/delete here
124   }
125 }
126
127 sub _recurse_from {
128   my ($self, $from, @join) = @_;
129   my @sqlf;
130   push(@sqlf, $self->_make_as($from));
131   foreach my $j (@join) {
132     my ($to, $on) = @$j;
133
134     # check whether a join type exists
135     my $join_clause = '';
136     my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
137     if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) {
138       $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN ';
139     } else {
140       $join_clause = ' JOIN ';
141     }
142     push(@sqlf, $join_clause);
143
144     if (ref $to eq 'ARRAY') {
145       push(@sqlf, '(', $self->_recurse_from(@$to), ')');
146     } else {
147       push(@sqlf, $self->_make_as($to));
148     }
149     push(@sqlf, ' ON ', $self->_join_condition($on));
150   }
151   return join('', @sqlf);
152 }
153
154 sub _make_as {
155   my ($self, $from) = @_;
156   return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) }
157                      reverse each %{$self->_skip_options($from)});
158 }
159
160 sub _skip_options {
161   my ($self, $hash) = @_;
162   my $clean_hash = {};
163   $clean_hash->{$_} = $hash->{$_}
164     for grep {!/^-/} keys %$hash;
165   return $clean_hash;
166 }
167
168 sub _join_condition {
169   my ($self, $cond) = @_;
170   if (ref $cond eq 'HASH') {
171     my %j;
172     for (keys %$cond) {
173       my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x;
174     };
175     return $self->_recurse_where(\%j);
176   } elsif (ref $cond eq 'ARRAY') {
177     return join(' OR ', map { $self->_join_condition($_) } @$cond);
178   } else {
179     die "Can't handle this yet!";
180   }
181 }
182
183 sub _quote {
184   my ($self, $label) = @_;
185   return '' unless defined $label;
186   return "*" if $label eq '*';
187   return $label unless $self->{quote_char};
188   if(ref $self->{quote_char} eq "ARRAY"){
189     return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1]
190       if !defined $self->{name_sep};
191     my $sep = $self->{name_sep};
192     return join($self->{name_sep},
193         map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1]  }
194        split(/\Q$sep\E/,$label));
195   }
196   return $self->SUPER::_quote($label);
197 }
198
199 sub _RowNum {
200    my $self = shift;
201    my $c;
202    $_[0] =~ s/SELECT (.*?) FROM/
203      'SELECT '.join(', ', map { $_.' AS col'.++$c } split(', ', $1)).' FROM'/e;
204    $self->SUPER::_RowNum(@_);
205 }
206
207 sub limit_dialect {
208     my $self = shift;
209     $self->{limit_dialect} = shift if @_;
210     return $self->{limit_dialect};
211 }
212
213 sub quote_char {
214     my $self = shift;
215     $self->{quote_char} = shift if @_;
216     return $self->{quote_char};
217 }
218
219 sub name_sep {
220     my $self = shift;
221     $self->{name_sep} = shift if @_;
222     return $self->{name_sep};
223 }
224
225 } # End of BEGIN block
226
227 use base qw/DBIx::Class/;
228
229 __PACKAGE__->load_components(qw/AccessorGroup/);
230
231 __PACKAGE__->mk_group_accessors('simple' =>
232   qw/_connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugobj
233      cursor on_connect_do transaction_depth/);
234
235 =head2 new
236
237 =cut
238
239 sub new {
240   my $new = bless({}, ref $_[0] || $_[0]);
241   $new->cursor("DBIx::Class::Storage::DBI::Cursor");
242   $new->transaction_depth(0);
243
244   $new->debugobj(new DBIx::Class::Storage::Statistics());
245
246   my $fh;
247   if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) &&
248      ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) {
249     $fh = IO::File->new($1, 'w')
250       or $new->throw_exception("Cannot open trace file $1");
251   } else {
252     $fh = IO::File->new('>&STDERR');
253   }
254   $new->debugfh($fh);
255   $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
256   return $new;
257 }
258
259 =head2 throw_exception
260
261 Throws an exception - croaks.
262
263 =cut
264
265 sub throw_exception {
266   my ($self, $msg) = @_;
267   croak($msg);
268 }
269
270 =head1 NAME
271
272 DBIx::Class::Storage::DBI - DBI storage handler
273
274 =head1 SYNOPSIS
275
276 =head1 DESCRIPTION
277
278 This class represents the connection to the database
279
280 =head1 METHODS
281
282 =cut
283
284 =head2 connect_info
285
286 The arguments of C<connect_info> are always a single array reference.
287
288 This is normally accessed via L<DBIx::Class::Schema/connection>, which
289 encapsulates its argument list in an arrayref before calling
290 C<connect_info> here.
291
292 The arrayref can either contain the same set of arguments one would
293 normally pass to L<DBI/connect>, or a lone code reference which returns
294 a connected database handle.
295
296 In either case, there is an optional final element within the arrayref
297 which can hold a hashref of connection-specific Storage::DBI options.
298 These include C<on_connect_do>, and the sql_maker options
299 C<limit_dialect>, C<quote_char>, and C<name_sep>.  Examples:
300
301   ->connect_info([ 'dbi:SQLite:./foo.db' ]);
302
303   ->connect_info([ sub { DBI->connect(...) } ]);
304
305   ->connect_info(
306     [
307       'dbi:Pg:dbname=foo',
308       'postgres',
309       'my_pg_password',
310       { AutoCommit => 0 },
311       { quote_char => q{`}, name_sep => q{@} },
312     ]
313   );
314
315   ->connect_info(
316     [
317       sub { DBI->connect(...) },
318       { quote_char => q{`}, name_sep => q{@} },
319     ]
320   );
321
322 =head2 on_connect_do
323
324 Executes the sql statements given as a listref on every db connect.
325
326 This option can also be set via L</connect_info>.
327
328 =head2 debug
329
330 Causes SQL trace information to be emitted on the C<debugobj> object.
331 (or C<STDERR> if C<debugobj> has not specifically been set).
332
333 =head2 debugfh
334
335 Set or retrieve the filehandle used for trace/debug output.  This should be
336 an IO::Handle compatible ojbect (only the C<print> method is used.  Initially
337 set to be STDERR - although see information on the
338 L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable.
339
340 =cut
341
342 sub debugfh {
343     my $self = shift;
344
345     if ($self->debugobj->can('debugfh')) {
346         return $self->debugobj->debugfh(@_);
347     }
348 }
349
350 =head2 debugobj
351
352 Sets or retrieves the object used for metric collection. Defaults to an instance
353 of L<DBIx::Class::Storage::Statistics> that is campatible with the original
354 method of using a coderef as a callback.  See the aforementioned Statistics
355 class for more information.
356
357 =head2 debugcb
358
359 Sets a callback to be executed each time a statement is run; takes a sub
360 reference.  Callback is executed as $sub->($op, $info) where $op is
361 SELECT/INSERT/UPDATE/DELETE and $info is what would normally be printed.
362
363 See L<debugobj> for a better way.
364
365 =cut
366
367 sub debugcb {
368     my $self = shift;
369
370     if ($self->debugobj->can('callback')) {
371         return $self->debugobj->callback(@_);
372     }
373 }
374
375 =head2 disconnect
376
377 Disconnect the L<DBI> handle, performing a rollback first if the
378 database is not in C<AutoCommit> mode.
379
380 =cut
381
382 sub disconnect {
383   my ($self) = @_;
384
385   if( $self->connected ) {
386     $self->_dbh->rollback unless $self->_dbh->{AutoCommit};
387     $self->_dbh->disconnect;
388     $self->_dbh(undef);
389   }
390 }
391
392 =head2 connected
393
394 Check if the L<DBI> handle is connected.  Returns true if the handle
395 is connected.
396
397 =cut
398
399 sub connected { my ($self) = @_;
400
401   if(my $dbh = $self->_dbh) {
402       if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) {
403           $self->_sql_maker(undef);
404           return $self->_dbh(undef);
405       }
406       elsif($self->_conn_pid != $$) {
407           $self->_dbh->{InactiveDestroy} = 1;
408           $self->_sql_maker(undef);
409           return $self->_dbh(undef)
410       }
411       return ($dbh->FETCH('Active') && $dbh->ping);
412   }
413
414   return 0;
415 }
416
417 =head2 ensure_connected
418
419 Check whether the database handle is connected - if not then make a
420 connection.
421
422 =cut
423
424 sub ensure_connected {
425   my ($self) = @_;
426
427   unless ($self->connected) {
428     $self->_populate_dbh;
429   }
430 }
431
432 =head2 dbh
433
434 Returns the dbh - a data base handle of class L<DBI>.
435
436 =cut
437
438 sub dbh {
439   my ($self) = @_;
440
441   $self->ensure_connected;
442   return $self->_dbh;
443 }
444
445 sub _sql_maker_args {
446     my ($self) = @_;
447     
448     return ( limit_dialect => $self->dbh );
449 }
450
451 =head2 sql_maker
452
453 Returns a C<sql_maker> object - normally an object of class
454 C<DBIC::SQL::Abstract>.
455
456 =cut
457
458 sub sql_maker {
459   my ($self) = @_;
460   unless ($self->_sql_maker) {
461     $self->_sql_maker(new DBIC::SQL::Abstract( $self->_sql_maker_args ));
462   }
463   return $self->_sql_maker;
464 }
465
466 sub connect_info {
467   my ($self, $info_arg) = @_;
468
469   if($info_arg) {
470     my $info = [ @$info_arg ]; # copy because we can alter it
471     my $last_info = $info->[-1];
472     if(ref $last_info eq 'HASH') {
473       my $used;
474       if(my $on_connect_do = $last_info->{on_connect_do}) {
475         $used = 1;
476         $self->on_connect_do($on_connect_do);
477       }
478       for my $sql_maker_opt (qw/limit_dialect quote_char name_sep/) {
479         if(my $opt_val = $last_info->{$sql_maker_opt}) {
480           $used = 1;
481           $self->sql_maker->$sql_maker_opt($opt_val);
482         }
483       }
484
485       # remove our options hashref if it was there, to avoid confusing
486       #   DBI in the case the user didn't use all 4 DBI options, as in:
487       #   [ 'dbi:SQLite:foo.db', { quote_char => q{`} } ]
488       pop(@$info) if $used;
489     }
490
491     $self->_connect_info($info);
492   }
493
494   $self->_connect_info;
495 }
496
497 sub _populate_dbh {
498   my ($self) = @_;
499   my @info = @{$self->_connect_info || []};
500   $self->_dbh($self->_connect(@info));
501   my $driver = $self->_dbh->{Driver}->{Name};
502   eval "require DBIx::Class::Storage::DBI::${driver}";
503   unless ($@) {
504     bless $self, "DBIx::Class::Storage::DBI::${driver}";
505     $self->_rebless() if $self->can('_rebless');
506   }
507   # if on-connect sql statements are given execute them
508   foreach my $sql_statement (@{$self->on_connect_do || []}) {
509     $self->debugobj->query_start($sql_statement) if $self->debug();
510     $self->_dbh->do($sql_statement);
511     $self->debugobj->query_end($sql_statement) if $self->debug();
512   }
513
514   $self->_conn_pid($$);
515   $self->_conn_tid(threads->tid) if $INC{'threads.pm'};
516 }
517
518 sub _connect {
519   my ($self, @info) = @_;
520
521   $self->throw_exception("You failed to provide any connection info")
522       if !@info;
523
524   my ($old_connect_via, $dbh);
525
526   if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
527       $old_connect_via = $DBI::connect_via;
528       $DBI::connect_via = 'connect';
529   }
530
531   eval {
532     $dbh = ref $info[0] eq 'CODE'
533          ? &{$info[0]}
534          : DBI->connect(@info);
535   };
536
537   $DBI::connect_via = $old_connect_via if $old_connect_via;
538
539   if (!$dbh || $@) {
540     $self->throw_exception("DBI Connection failed: " . ($@ || $DBI::errstr));
541   }
542
543   $dbh;
544 }
545
546 =head2 txn_begin
547
548 Calls begin_work on the current dbh.
549
550 See L<DBIx::Class::Schema> for the txn_do() method, which allows for
551 an entire code block to be executed transactionally.
552
553 =cut
554
555 sub txn_begin {
556   my $self = shift;
557   if ($self->{transaction_depth}++ == 0) {
558     my $dbh = $self->dbh;
559     if ($dbh->{AutoCommit}) {
560       $self->debugobj->txn_begin()
561         if ($self->debug);
562       $dbh->begin_work;
563     }
564   }
565 }
566
567 =head2 txn_commit
568
569 Issues a commit against the current dbh.
570
571 =cut
572
573 sub txn_commit {
574   my $self = shift;
575   my $dbh = $self->dbh;
576   if ($self->{transaction_depth} == 0) {
577     unless ($dbh->{AutoCommit}) {
578       $self->debugobj->txn_commit()
579         if ($self->debug);
580       $dbh->commit;
581     }
582   }
583   else {
584     if (--$self->{transaction_depth} == 0) {
585       $self->debugobj->txn_commit()
586         if ($self->debug);
587       $dbh->commit;
588     }
589   }
590 }
591
592 =head2 txn_rollback
593
594 Issues a rollback against the current dbh. A nested rollback will
595 throw a L<DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION> exception,
596 which allows the rollback to propagate to the outermost transaction.
597
598 =cut
599
600 sub txn_rollback {
601   my $self = shift;
602
603   eval {
604     my $dbh = $self->dbh;
605     if ($self->{transaction_depth} == 0) {
606       unless ($dbh->{AutoCommit}) {
607         $self->debugobj->txn_rollback()
608           if ($self->debug);
609         $dbh->rollback;
610       }
611     }
612     else {
613       if (--$self->{transaction_depth} == 0) {
614         $self->debugobj->txn_rollback()
615           if ($self->debug);
616         $dbh->rollback;
617       }
618       else {
619         die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new;
620       }
621     }
622   };
623
624   if ($@) {
625     my $error = $@;
626     my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
627     $error =~ /$exception_class/ and $self->throw_exception($error);
628     $self->{transaction_depth} = 0;          # ensure that a failed rollback
629     $self->throw_exception($error);          # resets the transaction depth
630   }
631 }
632
633 sub _execute {
634   my ($self, $op, $extra_bind, $ident, @args) = @_;
635   my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
636   unshift(@bind, @$extra_bind) if $extra_bind;
637   if ($self->debug) {
638       my @debug_bind = map { defined $_ ? qq{'$_'} : q{'NULL'} } @bind;
639       $self->debugobj->query_start($sql, @debug_bind);
640   }
641   my $sth = eval { $self->sth($sql,$op) };
642
643   if (!$sth || $@) {
644     $self->throw_exception(
645       'no sth generated via sql (' . ($@ || $self->_dbh->errstr) . "): $sql"
646     );
647   }
648   @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
649   my $rv;
650   if ($sth) {
651     my $time = time();
652     $rv = eval { $sth->execute(@bind) };
653
654     if ($@ || !$rv) {
655       $self->throw_exception("Error executing '$sql': ".($@ || $sth->errstr));
656     }
657   } else {
658     $self->throw_exception("'$sql' did not generate a statement.");
659   }
660   if ($self->debug) {
661       my @debug_bind = map { defined $_ ? qq{`$_'} : q{`NULL'} } @bind;
662       $self->debugobj->query_end($sql, @debug_bind);
663   }
664   return (wantarray ? ($rv, $sth, @bind) : $rv);
665 }
666
667 sub insert {
668   my ($self, $ident, $to_insert) = @_;
669   $self->throw_exception(
670     "Couldn't insert ".join(', ',
671       map "$_ => $to_insert->{$_}", keys %$to_insert
672     )." into ${ident}"
673   ) unless ($self->_execute('insert' => [], $ident, $to_insert));
674   return $to_insert;
675 }
676
677 sub update {
678   return shift->_execute('update' => [], @_);
679 }
680
681 sub delete {
682   return shift->_execute('delete' => [], @_);
683 }
684
685 sub _select {
686   my ($self, $ident, $select, $condition, $attrs) = @_;
687   my $order = $attrs->{order_by};
688   if (ref $condition eq 'SCALAR') {
689     $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
690   }
691   if (exists $attrs->{group_by} || $attrs->{having}) {
692     $order = {
693       group_by => $attrs->{group_by},
694       having => $attrs->{having},
695       ($order ? (order_by => $order) : ())
696     };
697   }
698   my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
699   if ($attrs->{software_limit} ||
700       $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
701         $attrs->{software_limit} = 1;
702   } else {
703     $self->throw_exception("rows attribute must be positive if present")
704       if (defined($attrs->{rows}) && !($attrs->{rows} > 0));
705     push @args, $attrs->{rows}, $attrs->{offset};
706   }
707   return $self->_execute(@args);
708 }
709
710 =head2 select
711
712 Handle a SQL select statement.
713
714 =cut
715
716 sub select {
717   my $self = shift;
718   my ($ident, $select, $condition, $attrs) = @_;
719   return $self->cursor->new($self, \@_, $attrs);
720 }
721
722 =head2 select_single
723
724 Performs a select, fetch and return of data - handles a single row
725 only.
726
727 =cut
728
729 # Need to call finish() to work round broken DBDs
730
731 sub select_single {
732   my $self = shift;
733   my ($rv, $sth, @bind) = $self->_select(@_);
734   my @row = $sth->fetchrow_array;
735   $sth->finish();
736   return @row;
737 }
738
739 =head2 sth
740
741 Returns a L<DBI> sth (statement handle) for the supplied SQL.
742
743 =cut
744
745 sub sth {
746   my ($self, $sql) = @_;
747   # 3 is the if_active parameter which avoids active sth re-use
748   return $self->dbh->prepare_cached($sql, {}, 3);
749 }
750
751 =head2 columns_info_for
752
753 Returns database type info for a given table columns.
754
755 =cut
756
757 sub columns_info_for {
758   my ($self, $table) = @_;
759
760   my $dbh = $self->dbh;
761
762   if ($dbh->can('column_info')) {
763     my %result;
764     my $old_raise_err = $dbh->{RaiseError};
765     my $old_print_err = $dbh->{PrintError};
766     $dbh->{RaiseError} = 1;
767     $dbh->{PrintError} = 0;
768     eval {
769       my ($schema,$tab) = $table =~ /^(.+?)\.(.+)$/ ? ($1,$2) : (undef,$table);
770       my $sth = $dbh->column_info( undef,$schema, $tab, '%' );
771       $sth->execute();
772       while ( my $info = $sth->fetchrow_hashref() ){
773         my %column_info;
774         $column_info{data_type}   = $info->{TYPE_NAME};
775         $column_info{size}      = $info->{COLUMN_SIZE};
776         $column_info{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
777         $column_info{default_value} = $info->{COLUMN_DEF};
778         my $col_name = $info->{COLUMN_NAME};
779         $col_name =~ s/^\"(.*)\"$/$1/;
780
781         $result{$col_name} = \%column_info;
782       }
783     };
784     $dbh->{RaiseError} = $old_raise_err;
785     $dbh->{PrintError} = $old_print_err;
786     return \%result if !$@;
787   }
788
789   my %result;
790   my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0");
791   $sth->execute;
792   my @columns = @{$sth->{NAME_lc}};
793   for my $i ( 0 .. $#columns ){
794     my %column_info;
795     my $type_num = $sth->{TYPE}->[$i];
796     my $type_name;
797     if(defined $type_num && $dbh->can('type_info')) {
798       my $type_info = $dbh->type_info($type_num);
799       $type_name = $type_info->{TYPE_NAME} if $type_info;
800     }
801     $column_info{data_type} = $type_name ? $type_name : $type_num;
802     $column_info{size} = $sth->{PRECISION}->[$i];
803     $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
804
805     if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
806       $column_info{data_type} = $1;
807       $column_info{size}    = $2;
808     }
809
810     $result{$columns[$i]} = \%column_info;
811   }
812
813   return \%result;
814 }
815
816 =head2 last_insert_id
817
818 Return the row id of the last insert.
819
820 =cut
821
822 sub last_insert_id {
823   my ($self, $row) = @_;
824     
825   return $self->dbh->func('last_insert_rowid');
826
827 }
828
829 =head2 sqlt_type
830
831 Returns the database driver name.
832
833 =cut
834
835 sub sqlt_type { shift->dbh->{Driver}->{Name} }
836
837 =head2 create_ddl_dir (EXPERIMENTAL)
838
839 =over 4
840
841 =item Arguments: $schema \@databases, $version, $directory, $sqlt_args
842
843 =back
844
845 Creates an SQL file based on the Schema, for each of the specified
846 database types, in the given directory.
847
848 Note that this feature is currently EXPERIMENTAL and may not work correctly
849 across all databases, or fully handle complex relationships.
850
851 =cut
852
853 sub create_ddl_dir
854 {
855   my ($self, $schema, $databases, $version, $dir, $sqltargs) = @_;
856
857   if(!$dir || !-d $dir)
858   {
859     warn "No directory given, using ./\n";
860     $dir = "./";
861   }
862   $databases ||= ['MySQL', 'SQLite', 'PostgreSQL'];
863   $databases = [ $databases ] if(ref($databases) ne 'ARRAY');
864   $version ||= $schema->VERSION || '1.x';
865
866   eval "use SQL::Translator";
867   $self->throw_exception("Can't deploy without SQL::Translator: $@") if $@;
868
869   my $sqlt = SQL::Translator->new({
870 #      debug => 1,
871       add_drop_table => 1,
872   });
873   foreach my $db (@$databases)
874   {
875     $sqlt->reset();
876     $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
877 #    $sqlt->parser_args({'DBIx::Class' => $schema);
878     $sqlt->data($schema);
879     $sqlt->producer($db);
880
881     my $file;
882     my $filename = $schema->ddl_filename($db, $dir, $version);
883     if(-e $filename)
884     {
885       $self->throw_exception("$filename already exists, skipping $db");
886       next;
887     }
888     open($file, ">$filename") 
889       or $self->throw_exception("Can't open $filename for writing ($!)");
890     my $output = $sqlt->translate;
891 #use Data::Dumper;
892 #    print join(":", keys %{$schema->source_registrations});
893 #    print Dumper($sqlt->schema);
894     if(!$output)
895     {
896       $self->throw_exception("Failed to translate to $db. (" . $sqlt->error . ")");
897       next;
898     }
899     print $file $output;
900     close($file);
901   }
902
903 }
904
905 =head2 deployment_statements
906
907 Create the statements for L</deploy> and
908 L<DBIx::Class::Schema/deploy>.
909
910 =cut
911
912 sub deployment_statements {
913   my ($self, $schema, $type, $version, $dir, $sqltargs) = @_;
914   $type ||= $self->sqlt_type;
915   $version ||= $schema->VERSION || '1.x';
916   $dir ||= './';
917   eval "use SQL::Translator";
918   if(!$@)
919   {
920     eval "use SQL::Translator::Parser::DBIx::Class;";
921     $self->throw_exception($@) if $@;
922     eval "use SQL::Translator::Producer::${type};";
923     $self->throw_exception($@) if $@;
924     my $tr = SQL::Translator->new(%$sqltargs);
925     SQL::Translator::Parser::DBIx::Class::parse( $tr, $schema );
926     return "SQL::Translator::Producer::${type}"->can('produce')->($tr);
927   }
928
929   my $filename = $schema->ddl_filename($type, $dir, $version);
930   if(!-f $filename)
931   {
932 #      $schema->create_ddl_dir([ $type ], $version, $dir, $sqltargs);
933       $self->throw_exception("No SQL::Translator, and no Schema file found, aborting deploy");
934       return;
935   }
936   my $file;
937   open($file, "<$filename") 
938       or $self->throw_exception("Can't open $filename ($!)");
939   my @rows = <$file>;
940   close($file);
941
942   return join('', @rows);
943   
944 }
945
946 =head2 deploy
947
948 Sends the appropriate statements to create or modify tables to the
949 db. This would normally be called through
950 L<DBIx::Class::Schema/deploy>.
951
952 =cut
953
954 sub deploy {
955   my ($self, $schema, $type, $sqltargs) = @_;
956   foreach my $statement ( $self->deployment_statements($schema, $type, undef, undef, $sqltargs) ) {
957     for ( split(";\n", $statement)) {
958       next if($_ =~ /^--/);
959       next if(!$_);
960 #      next if($_ =~ /^DROP/m);
961       next if($_ =~ /^BEGIN TRANSACTION/m);
962       next if($_ =~ /^COMMIT/m);
963       $self->debugobj->query_begin($_) if $self->debug;
964       $self->dbh->do($_) or warn "SQL was:\n $_";
965       $self->debugobj->query_end($_) if $self->debug;
966     }
967   }
968 }
969
970 =head2 datetime_parser
971
972 Returns the datetime parser class
973
974 =cut
975
976 sub datetime_parser {
977   my $self = shift;
978   return $self->{datetime_parser} ||= $self->build_datetime_parser(@_);
979 }
980
981 =head2 datetime_parser_type
982
983 Defines (returns) the datetime parser class - currently hardwired to
984 L<DateTime::Format::MySQL>
985
986 =cut
987
988 sub datetime_parser_type { "DateTime::Format::MySQL"; }
989
990 =head2 build_datetime_parser
991
992 See L</datetime_parser>
993
994 =cut
995
996 sub build_datetime_parser {
997   my $self = shift;
998   my $type = $self->datetime_parser_type(@_);
999   eval "use ${type}";
1000   $self->throw_exception("Couldn't load ${type}: $@") if $@;
1001   return $type;
1002 }
1003
1004 sub DESTROY { shift->disconnect }
1005
1006 1;
1007
1008 =head1 SQL METHODS
1009
1010 The module defines a set of methods within the DBIC::SQL::Abstract
1011 namespace.  These build on L<SQL::Abstract::Limit> to provide the
1012 SQL query functions.
1013
1014 The following methods are extended:-
1015
1016 =over 4
1017
1018 =item delete
1019
1020 =item insert
1021
1022 =item select
1023
1024 =item update
1025
1026 =item limit_dialect
1027
1028 Accessor for setting limit dialect. This is useful
1029 for JDBC-bridge among others where the remote SQL-dialect cannot
1030 be determined by the name of the driver alone.
1031
1032 This option can also be set via L</connect_info>.
1033
1034 =item quote_char
1035
1036 Specifies what characters to use to quote table and column names. If 
1037 you use this you will want to specify L<name_sep> as well.
1038
1039 quote_char expectes either a single character, in which case is it is placed
1040 on either side of the table/column, or an arrayref of length 2 in which case the
1041 table/column name is placed between the elements.
1042
1043 For example under MySQL you'd use C<quote_char('`')>, and user SQL Server you'd 
1044 use C<quote_char(qw/[ ]/)>.
1045
1046 This option can also be set via L</connect_info>.
1047
1048 =item name_sep
1049
1050 This only needs to be used in conjunction with L<quote_char>, and is used to 
1051 specify the charecter that seperates elements (schemas, tables, columns) from 
1052 each other. In most cases this is simply a C<.>.
1053
1054 This option can also be set via L</connect_info>.
1055
1056 =back
1057
1058 =head1 ENVIRONMENT VARIABLES
1059
1060 =head2 DBIX_CLASS_STORAGE_DBI_DEBUG
1061
1062 If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information
1063 is produced (as when the L<debug> method is set).
1064
1065 If the value is of the form C<1=/path/name> then the trace output is
1066 written to the file C</path/name>.
1067
1068 This environment variable is checked when the storage object is first
1069 created (when you call connect on your schema).  So, run-time changes 
1070 to this environment variable will not take effect unless you also 
1071 re-connect on your schema.
1072
1073 =head1 AUTHORS
1074
1075 Matt S. Trout <mst@shadowcatsystems.co.uk>
1076
1077 Andy Grundman <andy@hybridized.org>
1078
1079 =head1 LICENSE
1080
1081 You may distribute this code under the same terms as Perl itself.
1082
1083 =cut
1084