1 package DBIx::Class::Storage::DBI;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/SQL::Abstract SQL Exception AccessorGroup/);
11 __PACKAGE__->mk_group_accessors('simple' => qw/connect_info _dbh/);
14 bless({}, ref $_[0] || $_[0]);
18 my ($self, $get) = @_;
23 my ($self, $set, $val) = @_;
24 return $self->{$set} = $val;
29 DBIx::Class::Storage::DBI - DBI storage handler
35 This class represents the connection to the database
46 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
54 my @info = @{$self->connect_info || []};
55 $self->_dbh($self->_connect(@info));
59 my ($self, @info) = @_;
60 return DBI->connect(@info);
67 Issues a commit again the current dbh
71 sub commit { $_[0]->dbh->commit; }
77 Issues a rollback again the current dbh
81 sub rollback { $_[0]->dbh->rollback; }
84 my ($self, $ident, $to_insert) = @_;
85 my $sql = $self->create_sql('insert', [ keys %{$to_insert} ], $ident, undef);
86 my $sth = $self->sth($sql);
87 $sth->execute(values %{$to_insert});
88 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
94 my ($self, $ident, $to_update, $condition) = @_;
96 my $set_sql = $self->_cond_resolve($to_update, $attrs, ',');
99 my $cond_sql = $self->_cond_resolve($condition, $attrs);
100 my $sql = $self->create_sql('update', $set_sql, $ident, $cond_sql);
101 my $sth = $self->sth($sql);
102 my $rows = $sth->execute( @{$attrs->{bind}||[]} );
107 my ($self, $ident, $condition) = @_;
109 my $cond_sql = $self->_cond_resolve($condition, $attrs);
110 my $sql = $self->create_sql('delete', undef, $ident, $cond_sql);
111 #warn "$sql ".join(', ',@{$attrs->{bind}||[]});
112 my $sth = $self->sth($sql);
113 return $sth->execute( @{$attrs->{bind}||[]} );
117 my ($self, $ident, $select, $condition, $attrs) = @_;
119 #my $select_sql = $self->_cond_resolve($select, $attrs, ',');
120 my $cond_sql = $self->_cond_resolve($condition, $attrs);
121 1 while $cond_sql =~ s/^\s*\(\s*(.*ORDER.*)\s*\)\s*$/$1/;
122 my $sql = $self->create_sql('select', $select, $ident, $cond_sql);
123 #warn $sql.' '.join(', ', @{$attrs->{bind}||[]});
124 my $sth = $self->sth($sql);
125 $sth->execute( @{$attrs->{bind}||[]} );
130 shift->dbh->prepare(@_);
139 Matt S. Trout <perl-stuff@trout.me.uk>
143 You may distribute this code under the same terms as Perl itself.