1 package DBIx::Class::Storage::DBI;
6 use SQL::Abstract::Limit;
7 use DBIx::Class::Storage::DBI::Cursor;
9 use Carp::Clan qw/DBIx::Class/;
13 package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :(
15 use base qw/SQL::Abstract::Limit/;
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);
27 return $_[1].$self->_order_by($_[2]);
29 return $self->SUPER::_emulate_limit(@_);
34 my ($self, $fields) = @_;
35 my $ref = ref $fields;
36 return $self->_quote($fields) unless $ref;
37 return $$fields if $ref eq 'SCALAR';
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}).' )';
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});
57 if (defined $_[0]->{order_by}) {
58 $ret .= $self->SUPER::_order_by($_[0]->{order_by});
61 $ret = $self->SUPER::_order_by(@_);
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);
78 my ($self, $from, @join) = @_;
80 push(@sqlf, $self->_make_as($from));
81 foreach my $j (@join) {
84 # check whether a join type exists
86 if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
87 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
89 $join_clause = ' JOIN ';
91 push(@sqlf, $join_clause);
93 if (ref $to eq 'ARRAY') {
94 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
96 push(@sqlf, $self->_make_as($to));
98 push(@sqlf, ' ON ', $self->_join_condition($on));
100 return join('', @sqlf);
104 my ($self, $from) = @_;
105 return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) }
106 reverse each %{$self->_skip_options($from)});
110 my ($self, $hash) = @_;
112 $clean_hash->{$_} = $hash->{$_}
113 for grep {!/^-/} keys %$hash;
117 sub _join_condition {
118 my ($self, $cond) = @_;
119 if (ref $cond eq 'HASH') {
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);
126 die "Can't handle this yet!";
131 my ($self, $label) = @_;
132 return '' unless defined $label;
133 return $self->SUPER::_quote($label);
136 # Accessor for setting limit dialect. This is useful
137 # for JDBC-bridge among others where the remote SQL-dialect cannot
138 # be determined by the name of the driver alone.
142 $self->{limit_dialect} = shift if @_;
143 return $self->{limit_dialect};
146 } # End of BEGIN block
148 use base qw/DBIx::Class/;
150 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
152 __PACKAGE__->mk_group_accessors('simple' =>
153 qw/connect_info _dbh _sql_maker debug debugfh cursor on_connect_do transaction_depth/);
156 my $new = bless({}, ref $_[0] || $_[0]);
157 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
158 $new->transaction_depth(0);
159 if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) &&
160 ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) {
161 $new->debugfh(IO::File->new($1, 'w')||croak "Cannot open trace file $1");
163 $new->debugfh(IO::File->new('>&STDERR'));
165 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
171 DBIx::Class::Storage::DBI - DBI storage handler
177 This class represents the connection to the database
185 Executes the sql statements given as a listref on every db connect.
189 Causes SQL trace information to be emitted on C<debugfh> filehandle
190 (or C<STDERR> if C<debugfh> has not specifically been set).
194 Sets or retrieves the filehandle used for trace/debug output. This
195 should be an IO::Handle compatible object (only the C<print> method is
196 used). Initially set to be STDERR - although see information on the
197 L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable.
204 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
205 $self->_populate_dbh;
212 unless ($self->_sql_maker) {
213 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
215 return $self->_sql_maker;
220 my @info = @{$self->connect_info || []};
221 $self->_dbh($self->_connect(@info));
223 # if on-connect sql statements are given execute them
224 foreach my $sql_statement (@{$self->on_connect_do || []}) {
225 $self->_dbh->do($sql_statement);
230 my ($self, @info) = @_;
231 return DBI->connect(@info);
236 Calls begin_work on the current dbh.
242 $self->dbh->begin_work if $self->{transaction_depth}++ == 0 and $self->dbh->{AutoCommit};
247 Issues a commit against the current dbh.
253 if ($self->{transaction_depth} == 0) {
254 $self->dbh->commit unless $self->dbh->{AutoCommit};
257 $self->dbh->commit if --$self->{transaction_depth} == 0;
263 Issues a rollback against the current dbh.
269 if ($self->{transaction_depth} == 0) {
270 $self->dbh->rollback unless $self->dbh->{AutoCommit};
273 --$self->{transaction_depth} == 0 ? $self->dbh->rollback : die $@;
278 my ($self, $op, $extra_bind, $ident, @args) = @_;
279 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
280 unshift(@bind, @$extra_bind) if $extra_bind;
281 $self->debugfh->print("$sql: @bind\n") if $self->debug;
282 my $sth = $self->sth($sql,$op);
283 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
286 $rv = $sth->execute(@bind);
288 croak "'$sql' did not generate a statement.";
290 return (wantarray ? ($rv, $sth, @bind) : $rv);
294 my ($self, $ident, $to_insert) = @_;
295 croak( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
296 unless ($self->_execute('insert' => [], $ident, $to_insert));
301 return shift->_execute('update' => [], @_);
305 return shift->_execute('delete' => [], @_);
309 my ($self, $ident, $select, $condition, $attrs) = @_;
310 my $order = $attrs->{order_by};
311 if (ref $condition eq 'SCALAR') {
312 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
314 if (exists $attrs->{group_by}) {
315 $order = { group_by => $attrs->{group_by},
316 ($order ? (order_by => $order) : ()) };
318 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
319 if ($attrs->{software_limit} ||
320 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
321 $attrs->{software_limit} = 1;
323 push @args, $attrs->{rows}, $attrs->{offset};
325 return $self->_execute(@args);
330 my ($ident, $select, $condition, $attrs) = @_;
331 return $self->cursor->new($self, \@_, $attrs);
336 my ($rv, $sth, @bind) = $self->_select(@_);
337 return $sth->fetchrow_array;
341 my ($self, $sql) = @_;
342 # 3 is the if_active parameter which avoids active sth re-use
343 return $self->dbh->prepare_cached($sql, {}, 3);
346 =head2 columns_info_for
348 Returns database type info for a given table columns.
352 sub columns_info_for {
353 my ($self, $table) = @_;
355 if ( $self->dbh->can( 'column_info' ) ){
356 my $sth = $self->dbh->column_info( undef, undef, $table, '%' );
358 while ( my $info = $sth->fetchrow_hashref() ){
360 $column_info{data_type} = $info->{TYPE_NAME};
361 $column_info{size} = $info->{COLUMN_SIZE};
362 $column_info{is_nullable} = $info->{NULLABLE};
363 $result{$info->{COLUMN_NAME}} = \%column_info;
366 my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0");
368 my @columns = @{$sth->{NAME}};
369 for my $i ( 0 .. $#columns ){
370 $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i];
378 =head1 ENVIRONMENT VARIABLES
380 =head2 DBIX_CLASS_STORAGE_DBI_DEBUG
382 If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information
383 is produced (as when the L<debug> method is set).
385 If the value is of the form C<1=/path/name> then the trace output is
386 written to the file C</path/name>.
390 Matt S. Trout <mst@shadowcatsystems.co.uk>
392 Andy Grundman <andy@hybridized.org>
396 You may distribute this code under the same terms as Perl itself.