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