1 package DBIx::Class::Storage::DBI;
7 use DBIx::Class::Cursor;
9 use base qw/DBIx::Class/;
11 __PACKAGE__->load_components(qw/SQL SQL::Abstract Exception AccessorGroup/);
13 __PACKAGE__->mk_group_accessors('simple' =>
14 qw/connect_info _dbh sql_maker debug cursor/);
17 my $new = bless({}, ref $_[0] || $_[0]);
18 $new->sql_maker(new SQL::Abstract);
19 $new->cursor("DBIx::Class::Cursor");
25 my ($self, $get) = @_;
30 my ($self, $set, $val) = @_;
31 return $self->{$set} = $val;
36 DBIx::Class::Storage::DBI - DBI storage handler
42 This class represents the connection to the database
53 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
61 my @info = @{$self->connect_info || []};
62 $self->_dbh($self->_connect(@info));
66 my ($self, @info) = @_;
67 return DBI->connect(@info);
74 Issues a commit again the current dbh
78 sub commit { $_[0]->dbh->commit; }
84 Issues a rollback again the current dbh
88 sub rollback { $_[0]->dbh->rollback; }
91 my ($self, $op, $extra_bind, $ident, @args) = @_;
92 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
93 warn "$sql: @bind" if $self->debug;
94 my $sth = $self->sth($sql);
95 unshift(@bind, @$extra_bind) if $extra_bind;
96 @bind = map { ref $_ ? ''.$_ : $_ } @bind;
97 my $rv = $sth->execute(@bind); # stringify args
98 return (wantarray ? ($rv, $sth, @bind) : $rv);
102 my ($self, $ident, $to_insert) = @_;
103 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
104 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
109 return shift->_execute('update' => [], @_);
113 return shift->_execute('delete' => [], @_);
117 my ($self, $ident, $select, $condition, $attrs) = @_;
118 my $order = $attrs->{order_by};
119 if (ref $condition eq 'SCALAR') {
120 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
122 my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order);
123 return $self->cursor->new($sth, \@bind, $attrs);
127 shift->dbh->prepare(@_);
136 Matt S. Trout <perl-stuff@trout.me.uk>
140 You may distribute this code under the same terms as Perl itself.