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/);
90 my $new = bless({}, ref $_[0] || $_[0]);
91 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
92 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
98 DBIx::Class::Storage::DBI - DBI storage handler
104 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);
144 Issues a commit again the current dbh
148 sub commit { $_[0]->dbh->commit; }
154 Issues a rollback again the current dbh
158 sub rollback { $_[0]->dbh->rollback; }
161 my ($self, $op, $extra_bind, $ident, @args) = @_;
162 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
163 unshift(@bind, @$extra_bind) if $extra_bind;
164 warn "$sql: @bind" if $self->debug;
165 my $sth = $self->sth($sql);
166 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
167 my $rv = $sth->execute(@bind);
168 return (wantarray ? ($rv, $sth, @bind) : $rv);
172 my ($self, $ident, $to_insert) = @_;
173 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
174 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
179 return shift->_execute('update' => [], @_);
183 return shift->_execute('delete' => [], @_);
187 my ($self, $ident, $select, $condition, $attrs) = @_;
188 my $order = $attrs->{order_by};
189 if (ref $condition eq 'SCALAR') {
190 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
192 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
193 if ($attrs->{software_limit} ||
194 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
195 $attrs->{software_limit} = 1;
197 push @args, $attrs->{rows}, $attrs->{offset};
199 return $self->_execute(@args);
204 my ($ident, $select, $condition, $attrs) = @_;
205 my ($rv, $sth, @bind) = $self->_select(@_);
206 return $self->cursor->new($sth, \@bind, $attrs);
211 my ($rv, $sth, @bind) = $self->_select(@_);
212 return $sth->fetchrow_array;
216 shift->dbh->prepare(@_);
225 Matt S. Trout <mst@shadowcatsystems.co.uk>
227 Andy Grundman <andy@hybridized.org>
231 You may distribute this code under the same terms as Perl itself.