b1e06b6c68050c08ce906ced93597f38d7ef098e
[dbsrgits/DBIx-Class-Historic.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 =cut
109
110 sub dbh {
111   my ($self) = @_;
112   my $dbh;
113   unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
114     $self->_populate_dbh;
115   }
116   return $self->_dbh;
117 }
118
119 sub sql_maker {
120   my ($self) = @_;
121   unless ($self->_sql_maker) {
122     $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
123   }
124   return $self->_sql_maker;
125 }
126
127 sub _populate_dbh {
128   my ($self) = @_;
129   my @info = @{$self->connect_info || []};
130   $self->_dbh($self->_connect(@info));
131 }
132
133 sub _connect {
134   my ($self, @info) = @_;
135   return DBI->connect(@info);
136 }
137
138 =head2 commit
139
140   $class->commit;
141
142 Issues a commit again the current dbh
143
144 =cut
145
146 sub commit { $_[0]->dbh->commit; }
147
148 =head2 rollback
149
150   $class->rollback;
151
152 Issues a rollback again the current dbh
153
154 =cut
155
156 sub rollback { $_[0]->dbh->rollback; }
157
158 sub _execute {
159   my ($self, $op, $extra_bind, $ident, @args) = @_;
160   my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
161   unshift(@bind, @$extra_bind) if $extra_bind;
162   warn "$sql: @bind" if $self->debug;
163   my $sth = $self->sth($sql);
164   @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
165   my $rv = $sth->execute(@bind);
166   return (wantarray ? ($rv, $sth, @bind) : $rv);
167 }
168
169 sub insert {
170   my ($self, $ident, $to_insert) = @_;
171   $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
172     unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
173   return $to_insert;
174 }
175
176 sub update {
177   return shift->_execute('update' => [], @_);
178 }
179
180 sub delete {
181   return shift->_execute('delete' => [], @_);
182 }
183
184 sub _select {
185   my ($self, $ident, $select, $condition, $attrs) = @_;
186   my $order = $attrs->{order_by};
187   if (ref $condition eq 'SCALAR') {
188     $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
189   }
190   my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
191   if ($attrs->{software_limit} ||
192       $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
193         $attrs->{software_limit} = 1;
194   } else {
195     push @args, $attrs->{rows}, $attrs->{offset};
196   }
197   return $self->_execute(@args);
198 }
199
200 sub select {
201   my $self = shift;
202   my ($ident, $select, $condition, $attrs) = @_;
203   my ($rv, $sth, @bind) = $self->_select(@_);
204   return $self->cursor->new($sth, \@bind, $attrs);
205 }
206
207 sub select_single {
208   my $self = shift;
209   my ($rv, $sth, @bind) = $self->_select(@_);
210   return $sth->fetchrow_array;
211 }
212
213 sub sth {
214   shift->dbh->prepare(@_);
215 }
216
217 1;
218
219 =head1 AUTHORS
220
221 Matt S. Trout <mst@shadowcatsystems.co.uk>
222
223 Andy Grundman <andy@hybridized.org>
224
225 =head1 LICENSE
226
227 You may distribute this code under the same terms as Perl itself.
228
229 =cut
230