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
113 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
114 $self->_populate_dbh;
121 unless ($self->_sql_maker) {
122 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
124 return $self->_sql_maker;
129 my @info = @{$self->connect_info || []};
130 $self->_dbh($self->_connect(@info));
134 my ($self, @info) = @_;
135 return DBI->connect(@info);
142 Issues a commit again the current dbh
146 sub commit { $_[0]->dbh->commit; }
152 Issues a rollback again the current dbh
156 sub rollback { $_[0]->dbh->rollback; }
159 my ($self, $op, $extra_bind, $ident, @args) = @_;
160 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
161 unshift(@bind, @$extra_bind) if $extra_bind;
162 warn "$sql: @bind" if $self->debug;
163 my $sth = $self->sth($sql);
164 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
165 my $rv = $sth->execute(@bind);
166 return (wantarray ? ($rv, $sth, @bind) : $rv);
170 my ($self, $ident, $to_insert) = @_;
171 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
172 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
177 return shift->_execute('update' => [], @_);
181 return shift->_execute('delete' => [], @_);
185 my ($self, $ident, $select, $condition, $attrs) = @_;
186 my $order = $attrs->{order_by};
187 if (ref $condition eq 'SCALAR') {
188 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
190 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
191 if ($attrs->{software_limit} ||
192 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
193 $attrs->{software_limit} = 1;
195 push @args, $attrs->{rows}, $attrs->{offset};
197 return $self->_execute(@args);
202 my ($ident, $select, $condition, $attrs) = @_;
203 my ($rv, $sth, @bind) = $self->_select(@_);
204 return $self->cursor->new($sth, \@bind, $attrs);
209 my ($rv, $sth, @bind) = $self->_select(@_);
210 return $sth->fetchrow_array;
214 shift->dbh->prepare(@_);
221 Matt S. Trout <mst@shadowcatsystems.co.uk>
223 Andy Grundman <andy@hybridized.org>
227 You may distribute this code under the same terms as Perl itself.