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