Commit | Line | Data |
8b445e33 |
1 | package DBIx::Class::Storage::DBI; |
2 | |
a62cf8d4 |
3 | use base 'DBIx::Class::Storage'; |
4 | |
20a2c954 |
5 | use strict; |
6 | use warnings; |
8b445e33 |
7 | use DBI; |
aeaf3ce2 |
8 | use SQL::Abstract::Limit; |
28927b50 |
9 | use DBIx::Class::Storage::DBI::Cursor; |
92b858c9 |
10 | use IO::File; |
701da8c4 |
11 | use Carp::Clan qw/DBIx::Class/; |
8b445e33 |
12 | |
bd7efd39 |
13 | BEGIN { |
14 | |
cb5f2eea |
15 | package DBIC::SQL::Abstract; # Would merge upstream, but nate doesn't reply :( |
bd7efd39 |
16 | |
17 | use base qw/SQL::Abstract::Limit/; |
18 | |
54540863 |
19 | sub select { |
20 | my ($self, $table, $fields, $where, $order, @rest) = @_; |
6346a152 |
21 | $table = $self->_quote($table) unless ref($table); |
54540863 |
22 | @rest = (-1) unless defined $rest[0]; |
0823196c |
23 | die "LIMIT 0 Does Not Compute" if $rest[0] == 0; |
24 | # and anyway, SQL::Abstract::Limit will cause a barf if we don't first |
8839560b |
25 | local $self->{having_bind} = []; |
bc0c9800 |
26 | my ($sql, @ret) = $self->SUPER::select( |
27 | $table, $self->_recurse_fields($fields), $where, $order, @rest |
28 | ); |
8839560b |
29 | return wantarray ? ($sql, @ret, @{$self->{having_bind}}) : $sql; |
54540863 |
30 | } |
31 | |
6346a152 |
32 | sub insert { |
33 | my $self = shift; |
34 | my $table = shift; |
35 | $table = $self->_quote($table) unless ref($table); |
36 | $self->SUPER::insert($table, @_); |
37 | } |
38 | |
39 | sub update { |
40 | my $self = shift; |
41 | my $table = shift; |
42 | $table = $self->_quote($table) unless ref($table); |
43 | $self->SUPER::update($table, @_); |
44 | } |
45 | |
46 | sub delete { |
47 | my $self = shift; |
48 | my $table = shift; |
49 | $table = $self->_quote($table) unless ref($table); |
50 | $self->SUPER::delete($table, @_); |
51 | } |
52 | |
54540863 |
53 | sub _emulate_limit { |
54 | my $self = shift; |
55 | if ($_[3] == -1) { |
56 | return $_[1].$self->_order_by($_[2]); |
57 | } else { |
58 | return $self->SUPER::_emulate_limit(@_); |
59 | } |
60 | } |
61 | |
62 | sub _recurse_fields { |
63 | my ($self, $fields) = @_; |
64 | my $ref = ref $fields; |
65 | return $self->_quote($fields) unless $ref; |
66 | return $$fields if $ref eq 'SCALAR'; |
67 | |
68 | if ($ref eq 'ARRAY') { |
69 | return join(', ', map { $self->_recurse_fields($_) } @$fields); |
70 | } elsif ($ref eq 'HASH') { |
71 | foreach my $func (keys %$fields) { |
72 | return $self->_sqlcase($func) |
73 | .'( '.$self->_recurse_fields($fields->{$func}).' )'; |
74 | } |
75 | } |
76 | } |
77 | |
78 | sub _order_by { |
79 | my $self = shift; |
80 | my $ret = ''; |
8839560b |
81 | my @extra; |
54540863 |
82 | if (ref $_[0] eq 'HASH') { |
83 | if (defined $_[0]->{group_by}) { |
84 | $ret = $self->_sqlcase(' group by ') |
85 | .$self->_recurse_fields($_[0]->{group_by}); |
86 | } |
8839560b |
87 | if (defined $_[0]->{having}) { |
88 | my $frag; |
89 | ($frag, @extra) = $self->_recurse_where($_[0]->{having}); |
90 | push(@{$self->{having_bind}}, @extra); |
91 | $ret .= $self->_sqlcase(' having ').$frag; |
92 | } |
54540863 |
93 | if (defined $_[0]->{order_by}) { |
94 | $ret .= $self->SUPER::_order_by($_[0]->{order_by}); |
95 | } |
e535069e |
96 | } elsif(ref $_[0] eq 'SCALAR') { |
97 | $ret = $self->_sqlcase(' order by ').${ $_[0] }; |
54540863 |
98 | } else { |
99 | $ret = $self->SUPER::_order_by(@_); |
100 | } |
101 | return $ret; |
102 | } |
103 | |
f48dd03f |
104 | sub _order_directions { |
105 | my ($self, $order) = @_; |
106 | $order = $order->{order_by} if ref $order eq 'HASH'; |
107 | return $self->SUPER::_order_directions($order); |
108 | } |
109 | |
2a816814 |
110 | sub _table { |
bd7efd39 |
111 | my ($self, $from) = @_; |
112 | if (ref $from eq 'ARRAY') { |
113 | return $self->_recurse_from(@$from); |
114 | } elsif (ref $from eq 'HASH') { |
115 | return $self->_make_as($from); |
116 | } else { |
6346a152 |
117 | return $from; # would love to quote here but _table ends up getting called |
118 | # twice during an ->select without a limit clause due to |
119 | # the way S::A::Limit->select works. should maybe consider |
120 | # bypassing this and doing S::A::select($self, ...) in |
121 | # our select method above. meantime, quoting shims have |
122 | # been added to select/insert/update/delete here |
bd7efd39 |
123 | } |
124 | } |
125 | |
126 | sub _recurse_from { |
127 | my ($self, $from, @join) = @_; |
128 | my @sqlf; |
129 | push(@sqlf, $self->_make_as($from)); |
130 | foreach my $j (@join) { |
c80f10db |
131 | push @sqlf, ', ' . $self->_quote($j) and next unless ref $j; |
132 | push @sqlf, ', ' . $$j and next if ref $j eq 'SCALAR'; |
133 | |
bd7efd39 |
134 | my ($to, $on) = @$j; |
73856587 |
135 | |
54540863 |
136 | # check whether a join type exists |
137 | my $join_clause = ''; |
138 | if (ref($to) eq 'HASH' and exists($to->{-join_type})) { |
c80f10db |
139 | $join_clause = $self->_sqlcase(' ' . $to->{-join_type} . ' JOIN '); |
54540863 |
140 | } else { |
c80f10db |
141 | $join_clause = $self->_sqlcase(' JOIN '); |
54540863 |
142 | } |
73856587 |
143 | push(@sqlf, $join_clause); |
144 | |
bd7efd39 |
145 | if (ref $to eq 'ARRAY') { |
146 | push(@sqlf, '(', $self->_recurse_from(@$to), ')'); |
147 | } else { |
96cdbbab |
148 | push(@sqlf, $self->_make_as($to)); |
bd7efd39 |
149 | } |
c80f10db |
150 | push(@sqlf, $self->_sqlcase(' ON '), $self->_join_condition($on)); |
bd7efd39 |
151 | } |
152 | return join('', @sqlf); |
153 | } |
154 | |
155 | sub _make_as { |
156 | my ($self, $from) = @_; |
c80f10db |
157 | return $self->_quote($from) unless ref $from; |
158 | return $$from if ref $from eq 'SCALAR'; |
54540863 |
159 | return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) } |
bc0c9800 |
160 | reverse each %{$self->_skip_options($from)}); |
73856587 |
161 | } |
73856587 |
162 | sub _skip_options { |
54540863 |
163 | my ($self, $hash) = @_; |
164 | my $clean_hash = {}; |
165 | $clean_hash->{$_} = $hash->{$_} |
166 | for grep {!/^-/} keys %$hash; |
167 | return $clean_hash; |
bd7efd39 |
168 | } |
169 | |
170 | sub _join_condition { |
171 | my ($self, $cond) = @_; |
5efe4c79 |
172 | if (ref $cond eq 'HASH') { |
173 | my %j; |
bc0c9800 |
174 | for (keys %$cond) { |
175 | my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; |
176 | }; |
5efe4c79 |
177 | return $self->_recurse_where(\%j); |
178 | } elsif (ref $cond eq 'ARRAY') { |
179 | return join(' OR ', map { $self->_join_condition($_) } @$cond); |
180 | } else { |
181 | die "Can't handle this yet!"; |
182 | } |
bd7efd39 |
183 | } |
184 | |
2a816814 |
185 | sub _quote { |
186 | my ($self, $label) = @_; |
187 | return '' unless defined $label; |
3b24f6ea |
188 | return "*" if $label eq '*'; |
41728a6e |
189 | return $label unless $self->{quote_char}; |
3b24f6ea |
190 | if(ref $self->{quote_char} eq "ARRAY"){ |
191 | return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1] |
192 | if !defined $self->{name_sep}; |
193 | my $sep = $self->{name_sep}; |
194 | return join($self->{name_sep}, |
195 | map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } |
196 | split(/\Q$sep\E/,$label)); |
197 | } |
2a816814 |
198 | return $self->SUPER::_quote($label); |
199 | } |
200 | |
f66596f9 |
201 | sub _RowNum { |
202 | my $self = shift; |
203 | my $c; |
204 | $_[0] =~ s/SELECT (.*?) FROM/ |
205 | 'SELECT '.join(', ', map { $_.' AS col'.++$c } split(', ', $1)).' FROM'/e; |
206 | $self->SUPER::_RowNum(@_); |
207 | } |
208 | |
7be93b07 |
209 | # Accessor for setting limit dialect. This is useful |
210 | # for JDBC-bridge among others where the remote SQL-dialect cannot |
211 | # be determined by the name of the driver alone. |
212 | # |
213 | sub limit_dialect { |
214 | my $self = shift; |
215 | $self->{limit_dialect} = shift if @_; |
216 | return $self->{limit_dialect}; |
217 | } |
218 | |
2437a1e3 |
219 | sub quote_char { |
220 | my $self = shift; |
221 | $self->{quote_char} = shift if @_; |
222 | return $self->{quote_char}; |
223 | } |
224 | |
225 | sub name_sep { |
226 | my $self = shift; |
227 | $self->{name_sep} = shift if @_; |
228 | return $self->{name_sep}; |
229 | } |
230 | |
231 | |
232 | |
233 | |
486ad69b |
234 | package DBIx::Class::Storage::DBI::DebugCallback; |
235 | |
236 | sub print { |
237 | my ($self, $string) = @_; |
238 | $string =~ m/^(\w+)/; |
239 | ${$self}->($1, $string); |
240 | } |
241 | |
bd7efd39 |
242 | } # End of BEGIN block |
243 | |
8b445e33 |
244 | use base qw/DBIx::Class/; |
245 | |
1f692767 |
246 | __PACKAGE__->load_components(qw/AccessorGroup/); |
8b445e33 |
247 | |
223b8fe3 |
248 | __PACKAGE__->mk_group_accessors('simple' => |
1346e22d |
249 | qw/connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugfh |
250 | cursor on_connect_do transaction_depth/); |
8091aa91 |
251 | |
8b445e33 |
252 | sub new { |
223b8fe3 |
253 | my $new = bless({}, ref $_[0] || $_[0]); |
28927b50 |
254 | $new->cursor("DBIx::Class::Storage::DBI::Cursor"); |
d79f59b9 |
255 | $new->transaction_depth(0); |
5e65c358 |
256 | if (defined($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}) && |
257 | ($ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} =~ /=(.+)$/)) { |
bc0c9800 |
258 | $new->debugfh(IO::File->new($1, 'w')) |
259 | or $new->throw_exception("Cannot open trace file $1"); |
92b858c9 |
260 | } else { |
261 | $new->debugfh(IO::File->new('>&STDERR')); |
262 | } |
28927b50 |
263 | $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; |
223b8fe3 |
264 | return $new; |
8b445e33 |
265 | } |
266 | |
1c339d71 |
267 | sub throw_exception { |
268 | my ($self, $msg) = @_; |
3b042bcb |
269 | croak($msg); |
1c339d71 |
270 | } |
271 | |
75d07914 |
272 | =head1 NAME |
8b445e33 |
273 | |
274 | DBIx::Class::Storage::DBI - DBI storage handler |
275 | |
276 | =head1 SYNOPSIS |
277 | |
278 | =head1 DESCRIPTION |
279 | |
280 | This class represents the connection to the database |
281 | |
282 | =head1 METHODS |
283 | |
8b445e33 |
284 | =cut |
285 | |
d7c4c15c |
286 | =head2 on_connect_do |
287 | |
288 | Executes the sql statements given as a listref on every db connect. |
289 | |
6789ebe3 |
290 | =head2 quote_char |
291 | |
292 | Specifies what characters to use to quote table and column names. If |
293 | you use this you will want to specify L<name_sep> as well. |
294 | |
295 | quote_char expectes either a single character, in which case is it is placed |
296 | on either side of the table/column, or an array of length 2 in which case the |
297 | table/column name is placed between the elements. |
298 | |
299 | For example under MySQL you'd use C<quote_char('`')>, and user SQL Server you'd |
300 | use C<quote_char(qw/[ ]/)>. |
301 | |
302 | =head2 name_sep |
303 | |
304 | This only needs to be used in conjunction with L<quote_char>, and is used to |
305 | specify the charecter that seperates elements (schemas, tables, columns) from |
306 | each other. In most cases this is simply a C<.>. |
307 | |
92b858c9 |
308 | =head2 debug |
309 | |
310 | Causes SQL trace information to be emitted on C<debugfh> filehandle |
311 | (or C<STDERR> if C<debugfh> has not specifically been set). |
312 | |
313 | =head2 debugfh |
314 | |
315 | Sets or retrieves the filehandle used for trace/debug output. This |
316 | should be an IO::Handle compatible object (only the C<print> method is |
317 | used). Initially set to be STDERR - although see information on the |
318 | L<DBIX_CLASS_STORAGE_DBI_DEBUG> environment variable. |
319 | |
486ad69b |
320 | =head2 debugcb |
321 | |
322 | Sets a callback to be executed each time a statement is run; takes a sub |
323 | reference. Overrides debugfh. Callback is executed as $sub->($op, $info) |
324 | where $op is SELECT/INSERT/UPDATE/DELETE and $info is what would normally |
325 | be printed. |
326 | |
d7c4c15c |
327 | =cut |
328 | |
486ad69b |
329 | sub debugcb { |
330 | my ($self, $cb) = @_; |
331 | my $cb_obj = bless(\$cb, 'DBIx::Class::Storage::DBI::DebugCallback'); |
332 | $self->debugfh($cb_obj); |
333 | } |
334 | |
412db1f4 |
335 | sub disconnect { |
336 | my ($self) = @_; |
337 | |
92925617 |
338 | if( $self->connected ) { |
339 | $self->_dbh->rollback unless $self->_dbh->{AutoCommit}; |
340 | $self->_dbh->disconnect; |
341 | $self->_dbh(undef); |
342 | } |
412db1f4 |
343 | } |
344 | |
345 | sub connected { |
8b445e33 |
346 | my ($self) = @_; |
412db1f4 |
347 | |
1346e22d |
348 | if(my $dbh = $self->_dbh) { |
349 | if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) { |
350 | $self->_sql_maker(undef); |
351 | return $self->_dbh(undef); |
352 | } |
353 | elsif($self->_conn_pid != $$) { |
354 | $self->_dbh->{InactiveDestroy} = 1; |
355 | $self->_sql_maker(undef); |
356 | return $self->_dbh(undef) |
357 | } |
358 | return ($dbh->FETCH('Active') && $dbh->ping); |
359 | } |
360 | |
361 | return 0; |
412db1f4 |
362 | } |
363 | |
364 | sub ensure_connected { |
365 | my ($self) = @_; |
366 | |
367 | unless ($self->connected) { |
8b445e33 |
368 | $self->_populate_dbh; |
369 | } |
412db1f4 |
370 | } |
371 | |
c235bbae |
372 | =head2 dbh |
373 | |
374 | Returns the dbh - a data base handle of class L<DBI>. |
375 | |
376 | =cut |
377 | |
412db1f4 |
378 | sub dbh { |
379 | my ($self) = @_; |
380 | |
381 | $self->ensure_connected; |
8b445e33 |
382 | return $self->_dbh; |
383 | } |
384 | |
48c69e7c |
385 | sub sql_maker { |
386 | my ($self) = @_; |
fdc1c3d0 |
387 | unless ($self->_sql_maker) { |
bd7efd39 |
388 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
389 | } |
390 | return $self->_sql_maker; |
391 | } |
392 | |
8b445e33 |
393 | sub _populate_dbh { |
394 | my ($self) = @_; |
395 | my @info = @{$self->connect_info || []}; |
396 | $self->_dbh($self->_connect(@info)); |
843f8ecd |
397 | my $driver = $self->_dbh->{Driver}->{Name}; |
34470972 |
398 | eval "require DBIx::Class::Storage::DBI::${driver}"; |
399 | unless ($@) { |
843f8ecd |
400 | bless $self, "DBIx::Class::Storage::DBI::${driver}"; |
401 | } |
d7c4c15c |
402 | # if on-connect sql statements are given execute them |
403 | foreach my $sql_statement (@{$self->on_connect_do || []}) { |
404 | $self->_dbh->do($sql_statement); |
405 | } |
5ef3e508 |
406 | |
1346e22d |
407 | $self->_conn_pid($$); |
408 | $self->_conn_tid(threads->tid) if $INC{'threads.pm'}; |
8b445e33 |
409 | } |
410 | |
411 | sub _connect { |
412 | my ($self, @info) = @_; |
5ef3e508 |
413 | |
9d31f7dc |
414 | $self->throw_exception("You failed to provide any connection info") |
415 | if !@info; |
416 | |
90ec6cad |
417 | my ($old_connect_via, $dbh); |
418 | |
5ef3e508 |
419 | if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) { |
90ec6cad |
420 | $old_connect_via = $DBI::connect_via; |
5ef3e508 |
421 | $DBI::connect_via = 'connect'; |
5ef3e508 |
422 | } |
423 | |
75db246c |
424 | eval { |
425 | if(ref $info[0] eq 'CODE') { |
426 | $dbh = &{$info[0]}; |
427 | } |
428 | else { |
429 | $dbh = DBI->connect(@info); |
430 | } |
431 | }; |
90ec6cad |
432 | |
433 | $DBI::connect_via = $old_connect_via if $old_connect_via; |
434 | |
75db246c |
435 | if (!$dbh || $@) { |
436 | $self->throw_exception("DBI Connection failed: " . ($@ || $DBI::errstr)); |
437 | } |
90ec6cad |
438 | |
e571e823 |
439 | $dbh; |
8b445e33 |
440 | } |
441 | |
8091aa91 |
442 | =head2 txn_begin |
8b445e33 |
443 | |
8091aa91 |
444 | Calls begin_work on the current dbh. |
8b445e33 |
445 | |
181a28f4 |
446 | See L<DBIx::Class::Schema> for the txn_do() method, which allows for |
447 | an entire code block to be executed transactionally. |
448 | |
8b445e33 |
449 | =cut |
450 | |
8091aa91 |
451 | sub txn_begin { |
d79f59b9 |
452 | my $self = shift; |
a32e8402 |
453 | if ($self->{transaction_depth}++ == 0) { |
454 | my $dbh = $self->dbh; |
455 | if ($dbh->{AutoCommit}) { |
456 | $self->debugfh->print("BEGIN WORK\n") |
457 | if ($self->debug); |
458 | $dbh->begin_work; |
459 | } |
986e4fca |
460 | } |
8091aa91 |
461 | } |
8b445e33 |
462 | |
8091aa91 |
463 | =head2 txn_commit |
8b445e33 |
464 | |
8091aa91 |
465 | Issues a commit against the current dbh. |
8b445e33 |
466 | |
8091aa91 |
467 | =cut |
468 | |
469 | sub txn_commit { |
d79f59b9 |
470 | my $self = shift; |
7c5a8b60 |
471 | my $dbh = $self->dbh; |
d79f59b9 |
472 | if ($self->{transaction_depth} == 0) { |
a32e8402 |
473 | unless ($dbh->{AutoCommit}) { |
986e4fca |
474 | $self->debugfh->print("COMMIT\n") |
475 | if ($self->debug); |
a32e8402 |
476 | $dbh->commit; |
986e4fca |
477 | } |
8091aa91 |
478 | } |
479 | else { |
986e4fca |
480 | if (--$self->{transaction_depth} == 0) { |
481 | $self->debugfh->print("COMMIT\n") |
482 | if ($self->debug); |
7c5a8b60 |
483 | $dbh->commit; |
986e4fca |
484 | } |
8091aa91 |
485 | } |
486 | } |
487 | |
488 | =head2 txn_rollback |
489 | |
181a28f4 |
490 | Issues a rollback against the current dbh. A nested rollback will |
491 | throw a L<DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION> exception, |
492 | which allows the rollback to propagate to the outermost transaction. |
8b445e33 |
493 | |
494 | =cut |
495 | |
8091aa91 |
496 | sub txn_rollback { |
d79f59b9 |
497 | my $self = shift; |
a62cf8d4 |
498 | |
499 | eval { |
7c5a8b60 |
500 | my $dbh = $self->dbh; |
a62cf8d4 |
501 | if ($self->{transaction_depth} == 0) { |
a32e8402 |
502 | unless ($dbh->{AutoCommit}) { |
986e4fca |
503 | $self->debugfh->print("ROLLBACK\n") |
504 | if ($self->debug); |
a32e8402 |
505 | $dbh->rollback; |
986e4fca |
506 | } |
a62cf8d4 |
507 | } |
508 | else { |
986e4fca |
509 | if (--$self->{transaction_depth} == 0) { |
510 | $self->debugfh->print("ROLLBACK\n") |
511 | if ($self->debug); |
7c5a8b60 |
512 | $dbh->rollback; |
986e4fca |
513 | } |
514 | else { |
1346e22d |
515 | die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new; |
986e4fca |
516 | } |
a62cf8d4 |
517 | } |
518 | }; |
519 | |
520 | if ($@) { |
521 | my $error = $@; |
522 | my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION"; |
523 | $error =~ /$exception_class/ and $self->throw_exception($error); |
524 | $self->{transaction_depth} = 0; # ensure that a failed rollback |
525 | $self->throw_exception($error); # resets the transaction depth |
8091aa91 |
526 | } |
527 | } |
8b445e33 |
528 | |
223b8fe3 |
529 | sub _execute { |
530 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
531 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
532 | unshift(@bind, @$extra_bind) if $extra_bind; |
f59ffc79 |
533 | if ($self->debug) { |
ec0ff6f6 |
534 | my $bind_str = join(', ', map { |
535 | defined $_ ? qq{`$_'} : q{`NULL'} |
536 | } @bind); |
537 | $self->debugfh->print("$sql ($bind_str)\n"); |
f59ffc79 |
538 | } |
75db246c |
539 | my $sth = eval { $self->sth($sql,$op) }; |
540 | |
541 | if (!$sth || $@) { |
ec0ff6f6 |
542 | $self->throw_exception( |
543 | 'no sth generated via sql (' . ($@ || $self->_dbh->errstr) . "): $sql" |
544 | ); |
75db246c |
545 | } |
438adc0e |
546 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
ec0ff6f6 |
547 | my $rv = eval { $sth->execute(@bind) }; |
548 | if ($@ || !$rv) { |
549 | my $bind_str = join(', ', map { |
550 | defined $_ ? qq{`$_'} : q{`NULL'} |
551 | } @bind); |
552 | $self->throw_exception( |
553 | "Error executing '$sql' ($bind_str): ".($@ || $sth->errstr) |
554 | ); |
701da8c4 |
555 | } |
223b8fe3 |
556 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
557 | } |
558 | |
8b445e33 |
559 | sub insert { |
560 | my ($self, $ident, $to_insert) = @_; |
bc0c9800 |
561 | $self->throw_exception( |
562 | "Couldn't insert ".join(', ', |
563 | map "$_ => $to_insert->{$_}", keys %$to_insert |
564 | )." into ${ident}" |
565 | ) unless ($self->_execute('insert' => [], $ident, $to_insert)); |
8b445e33 |
566 | return $to_insert; |
567 | } |
568 | |
569 | sub update { |
223b8fe3 |
570 | return shift->_execute('update' => [], @_); |
8b445e33 |
571 | } |
572 | |
573 | sub delete { |
223b8fe3 |
574 | return shift->_execute('delete' => [], @_); |
8b445e33 |
575 | } |
576 | |
de705b51 |
577 | sub _select { |
8b445e33 |
578 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
579 | my $order = $attrs->{order_by}; |
580 | if (ref $condition eq 'SCALAR') { |
581 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
582 | } |
8839560b |
583 | if (exists $attrs->{group_by} || $attrs->{having}) { |
bc0c9800 |
584 | $order = { |
585 | group_by => $attrs->{group_by}, |
586 | having => $attrs->{having}, |
587 | ($order ? (order_by => $order) : ()) |
588 | }; |
54540863 |
589 | } |
5c91499f |
590 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
591 | if ($attrs->{software_limit} || |
592 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
593 | $attrs->{software_limit} = 1; |
5c91499f |
594 | } else { |
0823196c |
595 | $self->throw_exception("rows attribute must be positive if present") |
596 | if (defined($attrs->{rows}) && !($attrs->{rows} > 0)); |
5c91499f |
597 | push @args, $attrs->{rows}, $attrs->{offset}; |
598 | } |
de705b51 |
599 | return $self->_execute(@args); |
600 | } |
601 | |
602 | sub select { |
603 | my $self = shift; |
604 | my ($ident, $select, $condition, $attrs) = @_; |
cb5f2eea |
605 | return $self->cursor->new($self, \@_, $attrs); |
8b445e33 |
606 | } |
607 | |
6157db4f |
608 | # Need to call finish() to work round broken DBDs |
609 | |
1a14aa3f |
610 | sub select_single { |
de705b51 |
611 | my $self = shift; |
612 | my ($rv, $sth, @bind) = $self->_select(@_); |
6157db4f |
613 | my @row = $sth->fetchrow_array; |
614 | $sth->finish(); |
615 | return @row; |
1a14aa3f |
616 | } |
617 | |
8b445e33 |
618 | sub sth { |
cb5f2eea |
619 | my ($self, $sql) = @_; |
91fa659e |
620 | # 3 is the if_active parameter which avoids active sth re-use |
621 | return $self->dbh->prepare_cached($sql, {}, 3); |
8b445e33 |
622 | } |
623 | |
a953d8d9 |
624 | =head2 columns_info_for |
625 | |
626 | Returns database type info for a given table columns. |
627 | |
628 | =cut |
629 | |
630 | sub columns_info_for { |
0d67fe74 |
631 | my ($self, $table) = @_; |
bfe10d87 |
632 | |
a32e8402 |
633 | my $dbh = $self->dbh; |
634 | |
635 | if ($dbh->can('column_info')) { |
a953d8d9 |
636 | my %result; |
a32e8402 |
637 | my $old_raise_err = $dbh->{RaiseError}; |
638 | my $old_print_err = $dbh->{PrintError}; |
639 | $dbh->{RaiseError} = 1; |
640 | $dbh->{PrintError} = 0; |
0d67fe74 |
641 | eval { |
a32e8402 |
642 | my $sth = $dbh->column_info( undef, undef, $table, '%' ); |
0d67fe74 |
643 | $sth->execute(); |
644 | while ( my $info = $sth->fetchrow_hashref() ){ |
bfe10d87 |
645 | my %column_info; |
0d67fe74 |
646 | $column_info{data_type} = $info->{TYPE_NAME}; |
647 | $column_info{size} = $info->{COLUMN_SIZE}; |
648 | $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
649 | $column_info{default_value} = $info->{COLUMN_DEF}; |
650 | |
651 | $result{$info->{COLUMN_NAME}} = \%column_info; |
652 | } |
653 | }; |
a32e8402 |
654 | $dbh->{RaiseError} = $old_raise_err; |
655 | $dbh->{PrintError} = $old_print_err; |
0d67fe74 |
656 | return \%result if !$@; |
657 | } |
658 | |
659 | my %result; |
a32e8402 |
660 | my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0"); |
0d67fe74 |
661 | $sth->execute; |
662 | my @columns = @{$sth->{NAME_lc}}; |
663 | for my $i ( 0 .. $#columns ){ |
664 | my %column_info; |
665 | my $type_num = $sth->{TYPE}->[$i]; |
666 | my $type_name; |
a32e8402 |
667 | if(defined $type_num && $dbh->can('type_info')) { |
668 | my $type_info = $dbh->type_info($type_num); |
0d67fe74 |
669 | $type_name = $type_info->{TYPE_NAME} if $type_info; |
a953d8d9 |
670 | } |
0d67fe74 |
671 | $column_info{data_type} = $type_name ? $type_name : $type_num; |
672 | $column_info{size} = $sth->{PRECISION}->[$i]; |
673 | $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0; |
674 | |
675 | if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
676 | $column_info{data_type} = $1; |
677 | $column_info{size} = $2; |
678 | } |
679 | |
680 | $result{$columns[$i]} = \%column_info; |
681 | } |
bfe10d87 |
682 | |
0d67fe74 |
683 | return \%result; |
a953d8d9 |
684 | } |
685 | |
843f8ecd |
686 | sub last_insert_id { |
687 | my ($self, $row) = @_; |
688 | |
689 | return $self->dbh->func('last_insert_rowid'); |
690 | |
691 | } |
692 | |
90ec6cad |
693 | sub sqlt_type { shift->dbh->{Driver}->{Name} } |
1c339d71 |
694 | |
695 | sub deployment_statements { |
cb561d1a |
696 | my ($self, $schema, $type, $sqltargs) = @_; |
1c339d71 |
697 | $type ||= $self->sqlt_type; |
698 | eval "use SQL::Translator"; |
699 | $self->throw_exception("Can't deploy without SQL::Translator: $@") if $@; |
700 | eval "use SQL::Translator::Parser::DBIx::Class;"; |
75d07914 |
701 | $self->throw_exception($@) if $@; |
1c339d71 |
702 | eval "use SQL::Translator::Producer::${type};"; |
703 | $self->throw_exception($@) if $@; |
cb561d1a |
704 | my $tr = SQL::Translator->new(%$sqltargs); |
1c339d71 |
705 | SQL::Translator::Parser::DBIx::Class::parse( $tr, $schema ); |
706 | return "SQL::Translator::Producer::${type}"->can('produce')->($tr); |
707 | } |
843f8ecd |
708 | |
1c339d71 |
709 | sub deploy { |
cb561d1a |
710 | my ($self, $schema, $type, $sqltargs) = @_; |
e4fe9ba3 |
711 | foreach my $statement ( $self->deployment_statements($schema, $type, $sqltargs) ) { |
712 | for ( split(";\n", $statement)) { |
713 | $self->debugfh->print("$_\n") if $self->debug; |
714 | $self->dbh->do($_) or warn "SQL was:\n $_"; |
715 | } |
75d07914 |
716 | } |
1c339d71 |
717 | } |
843f8ecd |
718 | |
92925617 |
719 | sub DESTROY { shift->disconnect } |
720 | |
8b445e33 |
721 | 1; |
722 | |
92b858c9 |
723 | =head1 ENVIRONMENT VARIABLES |
724 | |
725 | =head2 DBIX_CLASS_STORAGE_DBI_DEBUG |
726 | |
727 | If C<DBIX_CLASS_STORAGE_DBI_DEBUG> is set then SQL trace information |
728 | is produced (as when the L<debug> method is set). |
729 | |
730 | If the value is of the form C<1=/path/name> then the trace output is |
731 | written to the file C</path/name>. |
732 | |
d1cceec4 |
733 | This environment variable is checked when the storage object is first |
734 | created (when you call connect on your schema). So, run-time changes |
735 | to this environment variable will not take effect unless you also |
736 | re-connect on your schema. |
737 | |
8b445e33 |
738 | =head1 AUTHORS |
739 | |
daec44b8 |
740 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
741 | |
9f19b1d6 |
742 | Andy Grundman <andy@hybridized.org> |
743 | |
8b445e33 |
744 | =head1 LICENSE |
745 | |
746 | You may distribute this code under the same terms as Perl itself. |
747 | |
748 | =cut |
749 | |