Reverted andyg's fixes to DBI.pm, updated tests accordingly.
[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 BEGIN {
10
11 package DBIC::SQL::Abstract; # Temporary. Merge upstream.
12
13 use base qw/SQL::Abstract::Limit/;
14
15 sub select {
16   my ($self, $ident, @rest) = @_;
17   return $self->SUPER::select($self->from($ident), @rest);
18 }
19
20 sub from {
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);
26   } else {
27     return $from;
28   }
29 }
30
31 sub _recurse_from {
32   my ($self, $from, @join) = @_;
33   my @sqlf;
34   push(@sqlf, $self->_make_as($from));
35   foreach my $j (@join) {
36     my ($to, $on) = @$j;
37
38         # check whether a join type exists
39         my $join_clause = '';
40         if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
41                 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
42         } else {
43                 $join_clause = ' JOIN ';
44         }
45     push(@sqlf, $join_clause);
46
47     if (ref $to eq 'ARRAY') {
48       push(@sqlf, '(', $self->_recurse_from(@$to), ')');
49     } else {
50       push(@sqlf, $self->_make_as($to));
51     }
52     push(@sqlf, ' ON ', $self->_join_condition($on));
53   }
54   return join('', @sqlf);
55 }
56
57 sub _make_as {
58   my ($self, $from) = @_;
59         return join(' ', reverse each %{$self->_skip_options($from)});
60 }
61
62 sub _skip_options {
63         my ($self, $hash) = @_;
64         my $clean_hash = {};
65         $clean_hash->{$_} = $hash->{$_}
66                 for grep {!/^-/} keys %$hash;
67         return $clean_hash;
68 }
69
70 sub _join_condition {
71   my ($self, $cond) = @_;
72   die "no chance" unless ref $cond eq 'HASH';
73   my %j;
74   for (keys %$cond) { my $x = '= '.$cond->{$_}; $j{$_} = \$x; };
75   return $self->_recurse_where(\%j);
76 }
77
78 } # End of BEGIN block
79
80 use base qw/DBIx::Class/;
81
82 __PACKAGE__->load_components(qw/Exception AccessorGroup/);
83
84 __PACKAGE__->mk_group_accessors('simple' =>
85   qw/connect_info _dbh _sql_maker debug cursor/);
86
87 sub new {
88   my $new = bless({}, ref $_[0] || $_[0]);
89   $new->cursor("DBIx::Class::Storage::DBI::Cursor");
90   $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
91   return $new;
92 }
93
94 sub get_simple {
95   my ($self, $get) = @_;
96   return $self->{$get};
97 }
98
99 sub set_simple {
100   my ($self, $set, $val) = @_;
101   return $self->{$set} = $val;
102 }
103
104 =head1 NAME 
105
106 DBIx::Class::Storage::DBI - DBI storage handler
107
108 =head1 SYNOPSIS
109
110 =head1 DESCRIPTION
111
112 This class represents the connection to the database
113
114 =head1 METHODS
115
116 =over 4
117
118 =cut
119
120 sub dbh {
121   my ($self) = @_;
122   my $dbh;
123   unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
124     $self->_populate_dbh;
125   }
126   return $self->_dbh;
127 }
128
129 sub sql_maker {
130   my ($self) = @_;
131   unless ($self->_sql_maker) {
132     $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
133   }
134   return $self->_sql_maker;
135 }
136
137 sub _populate_dbh {
138   my ($self) = @_;
139   my @info = @{$self->connect_info || []};
140   $self->_dbh($self->_connect(@info));
141 }
142
143 sub _connect {
144   my ($self, @info) = @_;
145   return DBI->connect(@info);
146 }
147
148 =item commit
149
150   $class->commit;
151
152 Issues a commit again the current dbh
153
154 =cut
155
156 sub commit { $_[0]->dbh->commit; }
157
158 =item rollback
159
160   $class->rollback;
161
162 Issues a rollback again the current dbh
163
164 =cut
165
166 sub rollback { $_[0]->dbh->rollback; }
167
168 sub _execute {
169   my ($self, $op, $extra_bind, $ident, @args) = @_;
170   my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
171   unshift(@bind, @$extra_bind) if $extra_bind;
172   warn "$sql: @bind" if $self->debug;
173   my $sth = $self->sth($sql);
174   @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
175   my $rv = $sth->execute(@bind);
176   return (wantarray ? ($rv, $sth, @bind) : $rv);
177 }
178
179 sub insert {
180   my ($self, $ident, $to_insert) = @_;
181   $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
182     unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
183   return $to_insert;
184 }
185
186 sub update {
187   return shift->_execute('update' => [], @_);
188 }
189
190 sub delete {
191   return shift->_execute('delete' => [], @_);
192 }
193
194 sub _select {
195   my ($self, $ident, $select, $condition, $attrs) = @_;
196   my $order = $attrs->{order_by};
197   if (ref $condition eq 'SCALAR') {
198     $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
199   }
200   my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
201   if ($attrs->{software_limit} ||
202       $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
203         $attrs->{software_limit} = 1;
204   } else {
205     push @args, $attrs->{rows}, $attrs->{offset};
206   }
207   return $self->_execute(@args);
208 }
209
210 sub select {
211   my $self = shift;
212   my ($ident, $select, $condition, $attrs) = @_;
213   my ($rv, $sth, @bind) = $self->_select(@_);
214   return $self->cursor->new($sth, \@bind, $attrs);
215 }
216
217 sub select_single {
218   my $self = shift;
219   my ($rv, $sth, @bind) = $self->_select(@_);
220   return $sth->fetchrow_array;
221 }
222
223 sub sth {
224   shift->dbh->prepare(@_);
225 }
226
227 1;
228
229 =back
230
231 =head1 AUTHORS
232
233 Matt S. Trout <mst@shadowcatsystems.co.uk>
234
235 Andy Grundman <andy@hybridized.org>
236
237 =head1 LICENSE
238
239 You may distribute this code under the same terms as Perl itself.
240
241 =cut
242