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