1 package DBIx::Class::Storage::DBI;
6 use SQL::Abstract::Limit;
7 use DBIx::Class::Storage::DBI::Cursor;
11 package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :(
13 use base qw/SQL::Abstract::Limit/;
16 my ($self, $table, $fields, $where, $order, @rest) = @_;
17 @rest = (-1) unless defined $rest[0];
18 $self->SUPER::select($table, $self->_recurse_fields($fields),
19 $where, $order, @rest);
25 return $_[1].$self->_order_by($_[2]);
27 return $self->SUPER::_emulate_limit(@_);
32 my ($self, $fields) = @_;
33 my $ref = ref $fields;
34 return $self->_quote($fields) unless $ref;
35 return $$fields if $ref eq 'SCALAR';
37 if ($ref eq 'ARRAY') {
38 return join(', ', map { $self->_recurse_fields($_) } @$fields);
39 } elsif ($ref eq 'HASH') {
40 foreach my $func (keys %$fields) {
41 return $self->_sqlcase($func)
42 .'( '.$self->_recurse_fields($fields->{$func}).' )';
50 if (ref $_[0] eq 'HASH') {
51 if (defined $_[0]->{group_by}) {
52 $ret = $self->_sqlcase(' group by ')
53 .$self->_recurse_fields($_[0]->{group_by});
55 if (defined $_[0]->{order_by}) {
56 $ret .= $self->SUPER::_order_by($_[0]->{order_by});
59 $ret = $self->SUPER::_order_by(@_);
65 my ($self, $from) = @_;
66 if (ref $from eq 'ARRAY') {
67 return $self->_recurse_from(@$from);
68 } elsif (ref $from eq 'HASH') {
69 return $self->_make_as($from);
76 my ($self, $from, @join) = @_;
78 push(@sqlf, $self->_make_as($from));
79 foreach my $j (@join) {
82 # check whether a join type exists
84 if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
85 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
87 $join_clause = ' JOIN ';
89 push(@sqlf, $join_clause);
91 if (ref $to eq 'ARRAY') {
92 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
94 push(@sqlf, $self->_make_as($to));
96 push(@sqlf, ' ON ', $self->_join_condition($on));
98 return join('', @sqlf);
102 my ($self, $from) = @_;
103 return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) }
104 reverse each %{$self->_skip_options($from)});
108 my ($self, $hash) = @_;
110 $clean_hash->{$_} = $hash->{$_}
111 for grep {!/^-/} keys %$hash;
115 sub _join_condition {
116 my ($self, $cond) = @_;
117 if (ref $cond eq 'HASH') {
119 for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
120 return $self->_recurse_where(\%j);
121 } elsif (ref $cond eq 'ARRAY') {
122 return join(' OR ', map { $self->_join_condition($_) } @$cond);
124 die "Can't handle this yet!";
129 my ($self, $label) = @_;
130 return '' unless defined $label;
131 return $self->SUPER::_quote($label);
134 } # End of BEGIN block
136 use base qw/DBIx::Class/;
138 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
140 __PACKAGE__->mk_group_accessors('simple' =>
141 qw/connect_info _dbh _sql_maker debug cursor on_connect_do/);
143 our $TRANSACTION = 0;
146 my $new = bless({}, ref $_[0] || $_[0]);
147 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
148 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
154 DBIx::Class::Storage::DBI - DBI storage handler
160 This class represents the connection to the database
168 Executes the sql statements given as a listref on every db connect.
175 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
176 $self->_populate_dbh;
183 unless ($self->_sql_maker) {
184 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
186 return $self->_sql_maker;
191 my @info = @{$self->connect_info || []};
192 $self->_dbh($self->_connect(@info));
194 # if on-connect sql statements are given execute them
195 foreach my $sql_statement (@{$self->on_connect_do || []}) {
196 $self->_dbh->do($sql_statement);
201 my ($self, @info) = @_;
202 return DBI->connect(@info);
207 Calls begin_work on the current dbh.
212 $_[0]->dbh->begin_work if $TRANSACTION++ == 0 and $_[0]->dbh->{AutoCommit};
217 Issues a commit against the current dbh.
222 if ($TRANSACTION == 0) {
226 $_[0]->dbh->commit if --$TRANSACTION == 0;
232 Issues a rollback against the current dbh.
237 if ($TRANSACTION == 0) {
238 $_[0]->dbh->rollback;
241 --$TRANSACTION == 0 ? $_[0]->dbh->rollback : die $@;
246 my ($self, $op, $extra_bind, $ident, @args) = @_;
247 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
248 unshift(@bind, @$extra_bind) if $extra_bind;
249 warn "$sql: @bind" if $self->debug;
250 my $sth = $self->sth($sql,$op);
251 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
252 my $rv = $sth->execute(@bind);
253 return (wantarray ? ($rv, $sth, @bind) : $rv);
257 my ($self, $ident, $to_insert) = @_;
258 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
259 unless ($self->_execute('insert' => [], $ident, $to_insert));
264 return shift->_execute('update' => [], @_);
268 return shift->_execute('delete' => [], @_);
272 my ($self, $ident, $select, $condition, $attrs) = @_;
273 my $order = $attrs->{order_by};
274 if (ref $condition eq 'SCALAR') {
275 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
277 if (exists $attrs->{group_by}) {
278 $order = { group_by => $attrs->{group_by},
279 ($order ? (order_by => $order) : ()) };
281 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
282 if ($attrs->{software_limit} ||
283 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
284 $attrs->{software_limit} = 1;
286 push @args, $attrs->{rows}, $attrs->{offset};
288 return $self->_execute(@args);
293 my ($ident, $select, $condition, $attrs) = @_;
294 return $self->cursor->new($self, \@_, $attrs);
299 my ($rv, $sth, @bind) = $self->_select(@_);
300 return $sth->fetchrow_array;
304 my ($self, $sql) = @_;
305 # 3 is the if_active parameter which avoids active sth re-use
306 return $self->dbh->prepare_cached($sql, {}, 3);
309 =head2 columns_info_for
311 Returns database type info for a given table columns.
315 sub columns_info_for {
316 my ($self, $table) = @_;
318 if ( $self->dbh->can( 'column_info' ) ){
319 my $sth = $self->dbh->column_info( undef, undef, $table, '%' );
321 while ( my $info = $sth->fetchrow_hashref() ){
323 $column_info{data_type} = $info->{TYPE_NAME};
324 $column_info{size} = $info->{COLUMN_SIZE};
325 $column_info{is_nullable} = $info->{NULLABLE};
326 $result{$info->{COLUMN_NAME}} = \%column_info;
329 my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0");
331 my @columns = @{$sth->{NAME}};
332 for my $i ( 0 .. $#columns ){
333 $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i];
343 Matt S. Trout <mst@shadowcatsystems.co.uk>
345 Andy Grundman <andy@hybridized.org>
349 You may distribute this code under the same terms as Perl itself.