Refactoring, cleanup, lose unnecessary resultset/cursor objects
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI.pm
CommitLineData
8b445e33 1package DBIx::Class::Storage::DBI;
2
20a2c954 3use strict;
4use warnings;
8b445e33 5use DBI;
223b8fe3 6use SQL::Abstract;
28927b50 7use DBIx::Class::Storage::DBI::Cursor;
8b445e33 8
9use base qw/DBIx::Class/;
10
438adc0e 11__PACKAGE__->load_components(qw/Exception AccessorGroup/);
8b445e33 12
223b8fe3 13__PACKAGE__->mk_group_accessors('simple' =>
14 qw/connect_info _dbh sql_maker debug cursor/);
8b445e33 15
16sub new {
223b8fe3 17 my $new = bless({}, ref $_[0] || $_[0]);
18 $new->sql_maker(new SQL::Abstract);
28927b50 19 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
20 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
223b8fe3 21 return $new;
8b445e33 22}
23
24sub get_simple {
25 my ($self, $get) = @_;
26 return $self->{$get};
27}
28
29sub set_simple {
30 my ($self, $set, $val) = @_;
31 return $self->{$set} = $val;
32}
33
34=head1 NAME
35
36DBIx::Class::Storage::DBI - DBI storage handler
37
38=head1 SYNOPSIS
39
40=head1 DESCRIPTION
41
42This class represents the connection to the database
43
44=head1 METHODS
45
46=over 4
47
48=cut
49
50sub dbh {
51 my ($self) = @_;
52 my $dbh;
53 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
54 $self->_populate_dbh;
55 }
56 return $self->_dbh;
57}
58
59sub _populate_dbh {
60 my ($self) = @_;
61 my @info = @{$self->connect_info || []};
62 $self->_dbh($self->_connect(@info));
63}
64
65sub _connect {
66 my ($self, @info) = @_;
67 return DBI->connect(@info);
68}
69
70=item commit
71
72 $class->commit;
73
74Issues a commit again the current dbh
75
76=cut
77
78sub commit { $_[0]->dbh->commit; }
79
80=item rollback
81
82 $class->rollback;
83
84Issues a rollback again the current dbh
85
86=cut
87
88sub rollback { $_[0]->dbh->rollback; }
89
223b8fe3 90sub _execute {
91 my ($self, $op, $extra_bind, $ident, @args) = @_;
92 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
944f30bf 93 unshift(@bind, @$extra_bind) if $extra_bind;
223b8fe3 94 warn "$sql: @bind" if $self->debug;
95 my $sth = $self->sth($sql);
438adc0e 96 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
97 my $rv = $sth->execute(@bind);
223b8fe3 98 return (wantarray ? ($rv, $sth, @bind) : $rv);
99}
100
8b445e33 101sub insert {
102 my ($self, $ident, $to_insert) = @_;
20a2c954 103 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
223b8fe3 104 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
8b445e33 105 return $to_insert;
106}
107
108sub update {
223b8fe3 109 return shift->_execute('update' => [], @_);
8b445e33 110}
111
112sub delete {
223b8fe3 113 return shift->_execute('delete' => [], @_);
8b445e33 114}
115
116sub select {
117 my ($self, $ident, $select, $condition, $attrs) = @_;
223b8fe3 118 my $order = $attrs->{order_by};
119 if (ref $condition eq 'SCALAR') {
120 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
121 }
122 my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order);
123 return $self->cursor->new($sth, \@bind, $attrs);
8b445e33 124}
125
1a14aa3f 126sub select_single {
127 my ($self, $ident, $select, $condition, $attrs) = @_;
128 my $order = $attrs->{order_by};
129 if (ref $condition eq 'SCALAR') {
130 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
131 }
132 my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order);
133 return $sth->fetchrow_array;
134}
135
8b445e33 136sub sth {
137 shift->dbh->prepare(@_);
138}
139
1401;
141
142=back
143
144=head1 AUTHORS
145
daec44b8 146Matt S. Trout <mst@shadowcatsystems.co.uk>
8b445e33 147
148=head1 LICENSE
149
150You may distribute this code under the same terms as Perl itself.
151
152=cut
153