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, $ident, @rest) = @_;
17 return $self->SUPER::select($self->from($ident), @rest);
21 my ($self, $from) = @_;
22 if (ref $from eq 'ARRAY') {
23 return $self->_recurse_from(@$from);
24 } elsif (ref $from eq 'HASH') {
25 return $self->_make_as($from);
32 my ($self, $from, @join) = @_;
34 push(@sqlf, $self->_make_as($from));
35 foreach my $j (@join) {
38 # check whether a join type exists
40 if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
41 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
43 $join_clause = ' JOIN ';
45 push(@sqlf, $join_clause);
47 if (ref $to eq 'ARRAY') {
48 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
50 push(@sqlf, $self->_make_as($to));
52 push(@sqlf, ' ON ', $self->_join_condition($on));
54 return join('', @sqlf);
58 my ($self, $from) = @_;
59 return join(' ', reverse each %{$self->_skip_options($from)});
63 my ($self, $hash) = @_;
65 $clean_hash->{$_} = $hash->{$_}
66 for grep {!/^-/} keys %$hash;
71 my ($self, $cond) = @_;
72 die "no chance" unless ref $cond eq 'HASH';
74 for (keys %$cond) { my $x = '= '.$cond->{$_}; $j{$_} = \$x; };
75 return $self->_recurse_where(\%j);
78 } # End of BEGIN block
80 use base qw/DBIx::Class/;
82 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
84 __PACKAGE__->mk_group_accessors('simple' =>
85 qw/connect_info _dbh _sql_maker debug cursor/);
88 my $new = bless({}, ref $_[0] || $_[0]);
89 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
90 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
95 my ($self, $get) = @_;
100 my ($self, $set, $val) = @_;
101 return $self->{$set} = $val;
106 DBIx::Class::Storage::DBI - DBI storage handler
112 This class represents the connection to the database
123 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
124 $self->_populate_dbh;
131 unless ($self->_sql_maker) {
132 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
134 return $self->_sql_maker;
139 my @info = @{$self->connect_info || []};
140 $self->_dbh($self->_connect(@info));
144 my ($self, @info) = @_;
145 return DBI->connect(@info);
152 Issues a commit again the current dbh
156 sub commit { $_[0]->dbh->commit; }
162 Issues a rollback again the current dbh
166 sub rollback { $_[0]->dbh->rollback; }
169 my ($self, $op, $extra_bind, $ident, @args) = @_;
170 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
171 unshift(@bind, @$extra_bind) if $extra_bind;
172 warn "$sql: @bind" if $self->debug;
173 my $sth = $self->sth($sql);
174 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
175 my $rv = $sth->execute(@bind);
176 return (wantarray ? ($rv, $sth, @bind) : $rv);
180 my ($self, $ident, $to_insert) = @_;
181 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
182 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
187 return shift->_execute('update' => [], @_);
191 return shift->_execute('delete' => [], @_);
195 my ($self, $ident, $select, $condition, $attrs) = @_;
196 my $order = $attrs->{order_by};
197 if (ref $condition eq 'SCALAR') {
198 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
200 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
201 if ($attrs->{software_limit} ||
202 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
203 $attrs->{software_limit} = 1;
205 push @args, $attrs->{rows}, $attrs->{offset};
207 return $self->_execute(@args);
212 my ($ident, $select, $condition, $attrs) = @_;
213 my ($rv, $sth, @bind) = $self->_select(@_);
214 return $self->cursor->new($sth, \@bind, $attrs);
219 my ($rv, $sth, @bind) = $self->_select(@_);
220 return $sth->fetchrow_array;
224 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.