Commit | Line | Data |
8b445e33 |
1 | package DBIx::Class::Storage::DBI; |
2 | |
20a2c954 |
3 | use strict; |
4 | use warnings; |
8b445e33 |
5 | use DBI; |
aeaf3ce2 |
6 | use SQL::Abstract::Limit; |
28927b50 |
7 | use DBIx::Class::Storage::DBI::Cursor; |
92b858c9 |
8 | use IO::File; |
8b445e33 |
9 | |
bd7efd39 |
10 | BEGIN { |
11 | |
cb5f2eea |
12 | package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :( |
bd7efd39 |
13 | |
14 | use base qw/SQL::Abstract::Limit/; |
15 | |
54540863 |
16 | sub select { |
17 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
18 | @rest = (-1) unless defined $rest[0]; |
19 | $self->SUPER::select($table, $self->_recurse_fields($fields), |
20 | $where, $order, @rest); |
21 | } |
22 | |
23 | sub _emulate_limit { |
24 | my $self = shift; |
25 | if ($_[3] == -1) { |
26 | return $_[1].$self->_order_by($_[2]); |
27 | } else { |
28 | return $self->SUPER::_emulate_limit(@_); |
29 | } |
30 | } |
31 | |
32 | sub _recurse_fields { |
33 | my ($self, $fields) = @_; |
34 | my $ref = ref $fields; |
35 | return $self->_quote($fields) unless $ref; |
36 | return $$fields if $ref eq 'SCALAR'; |
37 | |
38 | if ($ref eq 'ARRAY') { |
39 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
40 | } elsif ($ref eq 'HASH') { |
41 | foreach my $func (keys %$fields) { |
42 | return $self->_sqlcase($func) |
43 | .'( '.$self->_recurse_fields($fields->{$func}).' )'; |
44 | } |
45 | } |
46 | } |
47 | |
48 | sub _order_by { |
49 | my $self = shift; |
50 | my $ret = ''; |
51 | if (ref $_[0] eq 'HASH') { |
52 | if (defined $_[0]->{group_by}) { |
53 | $ret = $self->_sqlcase(' group by ') |
54 | .$self->_recurse_fields($_[0]->{group_by}); |
55 | } |
56 | if (defined $_[0]->{order_by}) { |
57 | $ret .= $self->SUPER::_order_by($_[0]->{order_by}); |
58 | } |
59 | } else { |
60 | $ret = $self->SUPER::_order_by(@_); |
61 | } |
62 | return $ret; |
63 | } |
64 | |
2a816814 |
65 | sub _table { |
bd7efd39 |
66 | my ($self, $from) = @_; |
67 | if (ref $from eq 'ARRAY') { |
68 | return $self->_recurse_from(@$from); |
69 | } elsif (ref $from eq 'HASH') { |
70 | return $self->_make_as($from); |
71 | } else { |
72 | return $from; |
73 | } |
74 | } |
75 | |
76 | sub _recurse_from { |
77 | my ($self, $from, @join) = @_; |
78 | my @sqlf; |
79 | push(@sqlf, $self->_make_as($from)); |
80 | foreach my $j (@join) { |
81 | my ($to, $on) = @$j; |
73856587 |
82 | |
54540863 |
83 | # check whether a join type exists |
84 | my $join_clause = ''; |
85 | if (ref($to) eq 'HASH' and exists($to->{-join_type})) { |
86 | $join_clause = ' '.uc($to->{-join_type}).' JOIN '; |
87 | } else { |
88 | $join_clause = ' JOIN '; |
89 | } |
73856587 |
90 | push(@sqlf, $join_clause); |
91 | |
bd7efd39 |
92 | if (ref $to eq 'ARRAY') { |
93 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
94 | } else { |
96cdbbab |
95 | push(@sqlf, $self->_make_as($to)); |
bd7efd39 |
96 | } |
97 | push(@sqlf, ' ON ', $self->_join_condition($on)); |
98 | } |
99 | return join('', @sqlf); |
100 | } |
101 | |
102 | sub _make_as { |
103 | my ($self, $from) = @_; |
54540863 |
104 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) } |
2a816814 |
105 | reverse each %{$self->_skip_options($from)}); |
73856587 |
106 | } |
107 | |
108 | sub _skip_options { |
54540863 |
109 | my ($self, $hash) = @_; |
110 | my $clean_hash = {}; |
111 | $clean_hash->{$_} = $hash->{$_} |
112 | for grep {!/^-/} keys %$hash; |
113 | return $clean_hash; |
bd7efd39 |
114 | } |
115 | |
116 | sub _join_condition { |
117 | my ($self, $cond) = @_; |
5efe4c79 |
118 | if (ref $cond eq 'HASH') { |
119 | my %j; |
120 | for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; }; |
121 | return $self->_recurse_where(\%j); |
122 | } elsif (ref $cond eq 'ARRAY') { |
123 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
124 | } else { |
125 | die "Can't handle this yet!"; |
126 | } |
bd7efd39 |
127 | } |
128 | |
2a816814 |
129 | sub _quote { |
130 | my ($self, $label) = @_; |
131 | return '' unless defined $label; |
132 | return $self->SUPER::_quote($label); |
133 | } |
134 | |
7be93b07 |
135 | # Accessor for setting limit dialect. This is useful |
136 | # for JDBC-bridge among others where the remote SQL-dialect cannot |
137 | # be determined by the name of the driver alone. |
138 | # |
139 | sub limit_dialect { |
140 | my $self = shift; |
141 | $self->{limit_dialect} = shift if @_; |
142 | return $self->{limit_dialect}; |
143 | } |
144 | |
bd7efd39 |
145 | } # End of BEGIN block |
146 | |
8b445e33 |
147 | use base qw/DBIx::Class/; |
148 | |
438adc0e |
149 | __PACKAGE__->load_components(qw/Exception AccessorGroup/); |
8b445e33 |
150 | |
223b8fe3 |
151 | __PACKAGE__->mk_group_accessors('simple' => |
92b858c9 |
152 | qw/connect_info _dbh _sql_maker debug debugfh cursor on_connect_do transaction_depth/); |
8091aa91 |
153 | |
8b445e33 |
154 | sub new { |
223b8fe3 |
155 | my $new = bless({}, ref $_[0] || $_[0]); |
28927b50 |
156 | $new->cursor("DBIx::Class::Storage::DBI::Cursor"); |
d79f59b9 |
157 | $new->transaction_depth(0); |
5e65c358 |
158 | if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) && |
159 | ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) { |
92b858c9 |
160 | $new->debugfh(IO::File->new($1, 'w')||die "Cannot open trace file $1"); |
161 | } else { |
162 | $new->debugfh(IO::File->new('>&STDERR')); |
163 | } |
28927b50 |
164 | $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; |
223b8fe3 |
165 | return $new; |
8b445e33 |
166 | } |
167 | |
8b445e33 |
168 | =head1 NAME |
169 | |
170 | DBIx::Class::Storage::DBI - DBI storage handler |
171 | |
172 | =head1 SYNOPSIS |
173 | |
174 | =head1 DESCRIPTION |
175 | |
176 | This class represents the connection to the database |
177 | |
178 | =head1 METHODS |
179 | |
8b445e33 |
180 | =cut |
181 | |
d7c4c15c |
182 | =head2 on_connect_do |
183 | |
184 | Executes the sql statements given as a listref on every db connect. |
185 | |
92b858c9 |
186 | =head2 debug |
187 | |
188 | Causes SQL trace information to be emitted on C<debugfh> filehandle |
189 | (or C<STDERR> if C<debugfh> has not specifically been set). |
190 | |
191 | =head2 debugfh |
192 | |
193 | Sets or retrieves the filehandle used for trace/debug output. This |
194 | should be an IO::Handle compatible object (only the C<print> method is |
195 | used). Initially set to be STDERR - although see information on the |
196 | L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable. |
197 | |
d7c4c15c |
198 | =cut |
199 | |
8b445e33 |
200 | sub dbh { |
201 | my ($self) = @_; |
202 | my $dbh; |
203 | unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) { |
204 | $self->_populate_dbh; |
205 | } |
206 | return $self->_dbh; |
207 | } |
208 | |
48c69e7c |
209 | sub sql_maker { |
210 | my ($self) = @_; |
fdc1c3d0 |
211 | unless ($self->_sql_maker) { |
bd7efd39 |
212 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
213 | } |
214 | return $self->_sql_maker; |
215 | } |
216 | |
8b445e33 |
217 | sub _populate_dbh { |
218 | my ($self) = @_; |
219 | my @info = @{$self->connect_info || []}; |
220 | $self->_dbh($self->_connect(@info)); |
d7c4c15c |
221 | |
222 | # if on-connect sql statements are given execute them |
223 | foreach my $sql_statement (@{$self->on_connect_do || []}) { |
224 | $self->_dbh->do($sql_statement); |
225 | } |
8b445e33 |
226 | } |
227 | |
228 | sub _connect { |
229 | my ($self, @info) = @_; |
230 | return DBI->connect(@info); |
231 | } |
232 | |
8091aa91 |
233 | =head2 txn_begin |
8b445e33 |
234 | |
8091aa91 |
235 | Calls begin_work on the current dbh. |
8b445e33 |
236 | |
237 | =cut |
238 | |
8091aa91 |
239 | sub txn_begin { |
d79f59b9 |
240 | my $self = shift; |
241 | $self->dbh->begin_work if $self->{transaction_depth}++ == 0 and $self->dbh->{AutoCommit}; |
8091aa91 |
242 | } |
8b445e33 |
243 | |
8091aa91 |
244 | =head2 txn_commit |
8b445e33 |
245 | |
8091aa91 |
246 | Issues a commit against the current dbh. |
8b445e33 |
247 | |
8091aa91 |
248 | =cut |
249 | |
250 | sub txn_commit { |
d79f59b9 |
251 | my $self = shift; |
252 | if ($self->{transaction_depth} == 0) { |
253 | $self->dbh->commit unless $self->dbh->{AutoCommit}; |
8091aa91 |
254 | } |
255 | else { |
d79f59b9 |
256 | $self->dbh->commit if --$self->{transaction_depth} == 0; |
8091aa91 |
257 | } |
258 | } |
259 | |
260 | =head2 txn_rollback |
261 | |
262 | Issues a rollback against the current dbh. |
8b445e33 |
263 | |
264 | =cut |
265 | |
8091aa91 |
266 | sub txn_rollback { |
d79f59b9 |
267 | my $self = shift; |
268 | if ($self->{transaction_depth} == 0) { |
269 | $self->dbh->rollback unless $self->dbh->{AutoCommit}; |
8091aa91 |
270 | } |
271 | else { |
d79f59b9 |
272 | --$self->{transaction_depth} == 0 ? $self->dbh->rollback : die $@; |
8091aa91 |
273 | } |
274 | } |
8b445e33 |
275 | |
223b8fe3 |
276 | sub _execute { |
277 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
278 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
279 | unshift(@bind, @$extra_bind) if $extra_bind; |
92b858c9 |
280 | $self->debugfh->print("$sql: @bind\n") if $self->debug; |
2f5911b2 |
281 | my $sth = $self->sth($sql,$op); |
438adc0e |
282 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
283 | my $rv = $sth->execute(@bind); |
223b8fe3 |
284 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
285 | } |
286 | |
8b445e33 |
287 | sub insert { |
288 | my ($self, $ident, $to_insert) = @_; |
20a2c954 |
289 | $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" ) |
a4b2f17b |
290 | unless ($self->_execute('insert' => [], $ident, $to_insert)); |
8b445e33 |
291 | return $to_insert; |
292 | } |
293 | |
294 | sub update { |
223b8fe3 |
295 | return shift->_execute('update' => [], @_); |
8b445e33 |
296 | } |
297 | |
298 | sub delete { |
223b8fe3 |
299 | return shift->_execute('delete' => [], @_); |
8b445e33 |
300 | } |
301 | |
de705b51 |
302 | sub _select { |
8b445e33 |
303 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
304 | my $order = $attrs->{order_by}; |
305 | if (ref $condition eq 'SCALAR') { |
306 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
307 | } |
54540863 |
308 | if (exists $attrs->{group_by}) { |
309 | $order = { group_by => $attrs->{group_by}, |
310 | ($order ? (order_by => $order) : ()) }; |
311 | } |
5c91499f |
312 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
313 | if ($attrs->{software_limit} || |
314 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
315 | $attrs->{software_limit} = 1; |
5c91499f |
316 | } else { |
317 | push @args, $attrs->{rows}, $attrs->{offset}; |
318 | } |
de705b51 |
319 | return $self->_execute(@args); |
320 | } |
321 | |
322 | sub select { |
323 | my $self = shift; |
324 | my ($ident, $select, $condition, $attrs) = @_; |
cb5f2eea |
325 | return $self->cursor->new($self, \@_, $attrs); |
8b445e33 |
326 | } |
327 | |
1a14aa3f |
328 | sub select_single { |
de705b51 |
329 | my $self = shift; |
330 | my ($rv, $sth, @bind) = $self->_select(@_); |
1a14aa3f |
331 | return $sth->fetchrow_array; |
332 | } |
333 | |
8b445e33 |
334 | sub sth { |
cb5f2eea |
335 | my ($self, $sql) = @_; |
91fa659e |
336 | # 3 is the if_active parameter which avoids active sth re-use |
337 | return $self->dbh->prepare_cached($sql, {}, 3); |
8b445e33 |
338 | } |
339 | |
a953d8d9 |
340 | =head2 columns_info_for |
341 | |
342 | Returns database type info for a given table columns. |
343 | |
344 | =cut |
345 | |
346 | sub columns_info_for { |
347 | my ($self, $table) = @_; |
a953d8d9 |
348 | my %result; |
103e3e03 |
349 | if ( $self->dbh->can( 'column_info' ) ){ |
350 | my $sth = $self->dbh->column_info( undef, undef, $table, '%' ); |
351 | $sth->execute(); |
352 | while ( my $info = $sth->fetchrow_hashref() ){ |
353 | my %column_info; |
a953d8d9 |
354 | $column_info{data_type} = $info->{TYPE_NAME}; |
355 | $column_info{size} = $info->{COLUMN_SIZE}; |
356 | $column_info{is_nullable} = $info->{NULLABLE}; |
103e3e03 |
357 | $result{$info->{COLUMN_NAME}} = \%column_info; |
358 | } |
359 | }else{ |
360 | my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); |
361 | $sth->execute; |
362 | my @columns = @{$sth->{NAME}}; |
363 | for my $i ( 0 .. $#columns ){ |
364 | $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i]; |
a953d8d9 |
365 | } |
a953d8d9 |
366 | } |
367 | return \%result; |
368 | } |
369 | |
8b445e33 |
370 | 1; |
371 | |
92b858c9 |
372 | =head1 ENVIRONMENT VARIABLES |
373 | |
374 | =head2 DBIX_CLASS_STORAGE_DBI_DEBUG |
375 | |
376 | If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information |
377 | is produced (as when the L<debug> method is set). |
378 | |
379 | If the value is of the form C<1=/path/name> then the trace output is |
380 | written to the file C</path/name>. |
381 | |
8b445e33 |
382 | =head1 AUTHORS |
383 | |
daec44b8 |
384 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
385 | |
9f19b1d6 |
386 | Andy Grundman <andy@hybridized.org> |
387 | |
8b445e33 |
388 | =head1 LICENSE |
389 | |
390 | You may distribute this code under the same terms as Perl itself. |
391 | |
392 | =cut |
393 | |