1 package DBIx::Class::Storage::DBI;
6 use SQL::Abstract::Limit;
7 use DBIx::Class::Storage::DBI::Cursor;
11 package DBIC::SQL::Abstract; # Temporary. Merge upstream.
13 use base qw/SQL::Abstract::Limit/;
16 my ($self, $from) = @_;
17 if (ref $from eq 'ARRAY') {
18 return $self->_recurse_from(@$from);
19 } elsif (ref $from eq 'HASH') {
20 return $self->_make_as($from);
27 my ($self, $from, @join) = @_;
29 push(@sqlf, $self->_make_as($from));
30 foreach my $j (@join) {
33 # check whether a join type exists
35 if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
36 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
38 $join_clause = ' JOIN ';
40 push(@sqlf, $join_clause);
42 if (ref $to eq 'ARRAY') {
43 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
45 push(@sqlf, $self->_make_as($to));
47 push(@sqlf, ' ON ', $self->_join_condition($on));
49 return join('', @sqlf);
53 my ($self, $from) = @_;
54 return join(' ', map { $self->_quote($_) }
55 reverse each %{$self->_skip_options($from)});
59 my ($self, $hash) = @_;
61 $clean_hash->{$_} = $hash->{$_}
62 for grep {!/^-/} keys %$hash;
67 my ($self, $cond) = @_;
68 die "no chance" unless ref $cond eq 'HASH';
70 for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
71 return $self->_recurse_where(\%j);
75 my ($self, $label) = @_;
76 return '' unless defined $label;
77 return $self->SUPER::_quote($label);
80 } # End of BEGIN block
82 use base qw/DBIx::Class/;
84 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
86 __PACKAGE__->mk_group_accessors('simple' =>
87 qw/connect_info _dbh _sql_maker debug cursor/);
92 my $new = bless({}, ref $_[0] || $_[0]);
93 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
94 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
100 DBIx::Class::Storage::DBI - DBI storage handler
106 This class represents the connection to the database
115 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
116 $self->_populate_dbh;
123 unless ($self->_sql_maker) {
124 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
126 return $self->_sql_maker;
131 my @info = @{$self->connect_info || []};
132 $self->_dbh($self->_connect(@info));
136 my ($self, @info) = @_;
137 return DBI->connect(@info);
142 Calls begin_work on the current dbh.
147 $_[0]->dbh->begin_work if $TRANSACTION++ == 0 and $_[0]->dbh->{AutoCommit};
152 Issues a commit against the current dbh.
157 $_[0]->dbh->commit if --$TRANSACTION == 0;
162 Issues a rollback against the current dbh.
167 --$TRANSACTION == 0 ? $_[0]->dbh->rollback : die $@;
171 my ($self, $op, $extra_bind, $ident, @args) = @_;
172 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
173 unshift(@bind, @$extra_bind) if $extra_bind;
174 warn "$sql: @bind" if $self->debug;
175 my $sth = $self->sth($sql);
176 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
177 my $rv = $sth->execute(@bind);
178 return (wantarray ? ($rv, $sth, @bind) : $rv);
182 my ($self, $ident, $to_insert) = @_;
183 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
184 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
189 return shift->_execute('update' => [], @_);
193 return shift->_execute('delete' => [], @_);
197 my ($self, $ident, $select, $condition, $attrs) = @_;
198 my $order = $attrs->{order_by};
199 if (ref $condition eq 'SCALAR') {
200 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
202 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
203 if ($attrs->{software_limit} ||
204 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
205 $attrs->{software_limit} = 1;
207 push @args, $attrs->{rows}, $attrs->{offset};
209 return $self->_execute(@args);
214 my ($ident, $select, $condition, $attrs) = @_;
215 my ($rv, $sth, @bind) = $self->_select(@_);
216 return $self->cursor->new($sth, \@bind, $attrs);
221 my ($rv, $sth, @bind) = $self->_select(@_);
222 return $sth->fetchrow_array;
226 shift->dbh->prepare(@_);
233 Matt S. Trout <mst@shadowcatsystems.co.uk>
235 Andy Grundman <andy@hybridized.org>
239 You may distribute this code under the same terms as Perl itself.