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) {
37 push(@sqlf, ' JOIN ');
38 if (ref $to eq 'ARRAY') {
39 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
41 push(@sqlf, $self->_make_as($to));
43 push(@sqlf, ' ON ', $self->_join_condition($on));
45 return join('', @sqlf);
49 my ($self, $from) = @_;
50 return join(' ', reverse each %$from);
54 my ($self, $cond) = @_;
55 die "no chance" unless ref $cond eq 'HASH';
57 for (keys %$cond) { my $x = '= '.$cond->{$_}; $j{$_} = \$x; };
58 return $self->_recurse_where(\%j);
61 } # End of BEGIN block
63 use base qw/DBIx::Class/;
65 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
67 __PACKAGE__->mk_group_accessors('simple' =>
68 qw/connect_info _dbh _sql_maker debug cursor/);
71 my $new = bless({}, ref $_[0] || $_[0]);
72 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
73 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
78 my ($self, $get) = @_;
83 my ($self, $set, $val) = @_;
84 return $self->{$set} = $val;
89 DBIx::Class::Storage::DBI - DBI storage handler
95 This class represents the connection to the database
106 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
107 $self->_populate_dbh;
114 unless ($self->_sql_maker) {
115 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
117 return $self->_sql_maker;
122 my @info = @{$self->connect_info || []};
123 $self->_dbh($self->_connect(@info));
127 my ($self, @info) = @_;
128 return DBI->connect(@info);
135 Issues a commit again the current dbh
139 sub commit { $_[0]->dbh->commit; }
145 Issues a rollback again the current dbh
149 sub rollback { $_[0]->dbh->rollback; }
152 my ($self, $op, $extra_bind, $ident, @args) = @_;
153 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
154 unshift(@bind, @$extra_bind) if $extra_bind;
155 warn "$sql: @bind" if $self->debug;
156 my $sth = $self->sth($sql);
157 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
158 my $rv = $sth->execute(@bind);
159 return (wantarray ? ($rv, $sth, @bind) : $rv);
163 my ($self, $ident, $to_insert) = @_;
164 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
165 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
170 return shift->_execute('update' => [], @_);
174 return shift->_execute('delete' => [], @_);
178 my ($self, $ident, $select, $condition, $attrs) = @_;
179 my $order = $attrs->{order_by};
180 if (ref $condition eq 'SCALAR') {
181 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
183 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
184 if ($attrs->{software_limit} ||
185 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
186 $attrs->{software_limit} = 1;
188 push @args, $attrs->{rows}, $attrs->{offset};
190 return $self->_execute(@args);
195 my ($ident, $select, $condition, $attrs) = @_;
196 my ($rv, $sth, @bind) = $self->_select(@_);
197 return $self->cursor->new($sth, \@bind, $attrs);
202 my ($rv, $sth, @bind) = $self->_select(@_);
203 return $sth->fetchrow_array;
207 shift->dbh->prepare(@_);
216 Matt S. Trout <mst@shadowcatsystems.co.uk>
218 Andy Grundman <andy@hybridized.org>
222 You may distribute this code under the same terms as Perl itself.