create now on resultset as well
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI.pm
CommitLineData
8b445e33 1package DBIx::Class::Storage::DBI;
2
20a2c954 3use strict;
4use warnings;
8b445e33 5use DBI;
aeaf3ce2 6use SQL::Abstract::Limit;
28927b50 7use DBIx::Class::Storage::DBI::Cursor;
8b445e33 8
bd7efd39 9BEGIN {
10
11package DBIC::SQL::Abstract; # Temporary. Merge upstream.
12
13use base qw/SQL::Abstract::Limit/;
14
54540863 15sub select {
16 my ($self, $table, $fields, $where, $order, @rest) = @_;
17 @rest = (-1) unless defined $rest[0];
18 $self->SUPER::select($table, $self->_recurse_fields($fields),
19 $where, $order, @rest);
20}
21
22sub _emulate_limit {
23 my $self = shift;
24 if ($_[3] == -1) {
25 return $_[1].$self->_order_by($_[2]);
26 } else {
27 return $self->SUPER::_emulate_limit(@_);
28 }
29}
30
31sub _recurse_fields {
32 my ($self, $fields) = @_;
33 my $ref = ref $fields;
34 return $self->_quote($fields) unless $ref;
35 return $$fields if $ref eq 'SCALAR';
36
37 if ($ref eq 'ARRAY') {
38 return join(', ', map { $self->_recurse_fields($_) } @$fields);
39 } elsif ($ref eq 'HASH') {
40 foreach my $func (keys %$fields) {
41 return $self->_sqlcase($func)
42 .'( '.$self->_recurse_fields($fields->{$func}).' )';
43 }
44 }
45}
46
47sub _order_by {
48 my $self = shift;
49 my $ret = '';
50 if (ref $_[0] eq 'HASH') {
51 if (defined $_[0]->{group_by}) {
52 $ret = $self->_sqlcase(' group by ')
53 .$self->_recurse_fields($_[0]->{group_by});
54 }
55 if (defined $_[0]->{order_by}) {
56 $ret .= $self->SUPER::_order_by($_[0]->{order_by});
57 }
58 } else {
59 $ret = $self->SUPER::_order_by(@_);
60 }
61 return $ret;
62}
63
2a816814 64sub _table {
bd7efd39 65 my ($self, $from) = @_;
66 if (ref $from eq 'ARRAY') {
67 return $self->_recurse_from(@$from);
68 } elsif (ref $from eq 'HASH') {
69 return $self->_make_as($from);
70 } else {
71 return $from;
72 }
73}
74
75sub _recurse_from {
76 my ($self, $from, @join) = @_;
77 my @sqlf;
78 push(@sqlf, $self->_make_as($from));
79 foreach my $j (@join) {
80 my ($to, $on) = @$j;
73856587 81
54540863 82 # check whether a join type exists
83 my $join_clause = '';
84 if (ref($to) eq 'HASH' and exists($to->{-join_type})) {
85 $join_clause = ' '.uc($to->{-join_type}).' JOIN ';
86 } else {
87 $join_clause = ' JOIN ';
88 }
73856587 89 push(@sqlf, $join_clause);
90
bd7efd39 91 if (ref $to eq 'ARRAY') {
92 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
93 } else {
96cdbbab 94 push(@sqlf, $self->_make_as($to));
bd7efd39 95 }
96 push(@sqlf, ' ON ', $self->_join_condition($on));
97 }
98 return join('', @sqlf);
99}
100
101sub _make_as {
102 my ($self, $from) = @_;
54540863 103 return join(' ', map { (ref $_ eq 'SCALAR' ? $$_ : $self->_quote($_)) }
2a816814 104 reverse each %{$self->_skip_options($from)});
73856587 105}
106
107sub _skip_options {
54540863 108 my ($self, $hash) = @_;
109 my $clean_hash = {};
110 $clean_hash->{$_} = $hash->{$_}
111 for grep {!/^-/} keys %$hash;
112 return $clean_hash;
bd7efd39 113}
114
115sub _join_condition {
116 my ($self, $cond) = @_;
117 die "no chance" unless ref $cond eq 'HASH';
118 my %j;
2a816814 119 for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
bd7efd39 120 return $self->_recurse_where(\%j);
121}
122
2a816814 123sub _quote {
124 my ($self, $label) = @_;
125 return '' unless defined $label;
126 return $self->SUPER::_quote($label);
127}
128
bd7efd39 129} # End of BEGIN block
130
8b445e33 131use base qw/DBIx::Class/;
132
438adc0e 133__PACKAGE__->load_components(qw/Exception AccessorGroup/);
8b445e33 134
223b8fe3 135__PACKAGE__->mk_group_accessors('simple' =>
48c69e7c 136 qw/connect_info _dbh _sql_maker debug cursor/);
8b445e33 137
8091aa91 138our $TRANSACTION = 0;
139
8b445e33 140sub new {
223b8fe3 141 my $new = bless({}, ref $_[0] || $_[0]);
28927b50 142 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
143 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
223b8fe3 144 return $new;
8b445e33 145}
146
8b445e33 147=head1 NAME
148
149DBIx::Class::Storage::DBI - DBI storage handler
150
151=head1 SYNOPSIS
152
153=head1 DESCRIPTION
154
155This class represents the connection to the database
156
157=head1 METHODS
158
8b445e33 159=cut
160
161sub dbh {
162 my ($self) = @_;
163 my $dbh;
164 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
165 $self->_populate_dbh;
166 }
167 return $self->_dbh;
168}
169
48c69e7c 170sub sql_maker {
171 my ($self) = @_;
fdc1c3d0 172 unless ($self->_sql_maker) {
bd7efd39 173 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
48c69e7c 174 }
175 return $self->_sql_maker;
176}
177
8b445e33 178sub _populate_dbh {
179 my ($self) = @_;
180 my @info = @{$self->connect_info || []};
181 $self->_dbh($self->_connect(@info));
182}
183
184sub _connect {
185 my ($self, @info) = @_;
186 return DBI->connect(@info);
187}
188
8091aa91 189=head2 txn_begin
8b445e33 190
8091aa91 191Calls begin_work on the current dbh.
8b445e33 192
193=cut
194
8091aa91 195sub txn_begin {
196 $_[0]->dbh->begin_work if $TRANSACTION++ == 0 and $_[0]->dbh->{AutoCommit};
197}
8b445e33 198
8091aa91 199=head2 txn_commit
8b445e33 200
8091aa91 201Issues a commit against the current dbh.
8b445e33 202
8091aa91 203=cut
204
205sub txn_commit {
206 if ($TRANSACTION == 0) {
207 $_[0]->dbh->commit;
208 }
209 else {
210 $_[0]->dbh->commit if --$TRANSACTION == 0;
211 }
212}
213
214=head2 txn_rollback
215
216Issues a rollback against the current dbh.
8b445e33 217
218=cut
219
8091aa91 220sub txn_rollback {
221 if ($TRANSACTION == 0) {
222 $_[0]->dbh->rollback;
223 }
224 else {
225 --$TRANSACTION == 0 ? $_[0]->dbh->rollback : die $@;
226 }
227}
8b445e33 228
223b8fe3 229sub _execute {
230 my ($self, $op, $extra_bind, $ident, @args) = @_;
231 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
944f30bf 232 unshift(@bind, @$extra_bind) if $extra_bind;
223b8fe3 233 warn "$sql: @bind" if $self->debug;
2f5911b2 234 my $sth = $self->sth($sql,$op);
438adc0e 235 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
236 my $rv = $sth->execute(@bind);
223b8fe3 237 return (wantarray ? ($rv, $sth, @bind) : $rv);
238}
239
8b445e33 240sub insert {
241 my ($self, $ident, $to_insert) = @_;
20a2c954 242 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
a4b2f17b 243 unless ($self->_execute('insert' => [], $ident, $to_insert));
8b445e33 244 return $to_insert;
245}
246
247sub update {
223b8fe3 248 return shift->_execute('update' => [], @_);
8b445e33 249}
250
251sub delete {
223b8fe3 252 return shift->_execute('delete' => [], @_);
8b445e33 253}
254
de705b51 255sub _select {
8b445e33 256 my ($self, $ident, $select, $condition, $attrs) = @_;
223b8fe3 257 my $order = $attrs->{order_by};
258 if (ref $condition eq 'SCALAR') {
259 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
260 }
54540863 261 if (exists $attrs->{group_by}) {
262 $order = { group_by => $attrs->{group_by},
263 ($order ? (order_by => $order) : ()) };
264 }
5c91499f 265 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
9229f20a 266 if ($attrs->{software_limit} ||
267 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
268 $attrs->{software_limit} = 1;
5c91499f 269 } else {
270 push @args, $attrs->{rows}, $attrs->{offset};
271 }
de705b51 272 return $self->_execute(@args);
273}
274
275sub select {
276 my $self = shift;
277 my ($ident, $select, $condition, $attrs) = @_;
278 my ($rv, $sth, @bind) = $self->_select(@_);
223b8fe3 279 return $self->cursor->new($sth, \@bind, $attrs);
8b445e33 280}
281
1a14aa3f 282sub select_single {
de705b51 283 my $self = shift;
284 my ($rv, $sth, @bind) = $self->_select(@_);
1a14aa3f 285 return $sth->fetchrow_array;
286}
287
8b445e33 288sub sth {
2f5911b2 289 my ($self, $sql, $op) = @_;
290 my $meth = (defined $op && $op ne 'select' ? 'prepare_cached' : 'prepare');
291 return $self->dbh->$meth($sql);
8b445e33 292}
293
2941;
295
8b445e33 296=head1 AUTHORS
297
daec44b8 298Matt S. Trout <mst@shadowcatsystems.co.uk>
8b445e33 299
9f19b1d6 300Andy Grundman <andy@hybridized.org>
301
8b445e33 302=head1 LICENSE
303
304You may distribute this code under the same terms as Perl itself.
305
306=cut
307