described schema better, and added note about when you want to use it
[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
2a816814 15sub _table {
bd7efd39 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
26sub _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;
73856587 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
bd7efd39 42 if (ref $to eq 'ARRAY') {
43 push(@sqlf, '(', $self->_recurse_from(@$to), ')');
44 } else {
96cdbbab 45 push(@sqlf, $self->_make_as($to));
bd7efd39 46 }
47 push(@sqlf, ' ON ', $self->_join_condition($on));
48 }
49 return join('', @sqlf);
50}
51
52sub _make_as {
53 my ($self, $from) = @_;
2a816814 54 return join(' ', map { $self->_quote($_) }
55 reverse each %{$self->_skip_options($from)});
73856587 56}
57
58sub _skip_options {
59 my ($self, $hash) = @_;
60 my $clean_hash = {};
61 $clean_hash->{$_} = $hash->{$_}
62 for grep {!/^-/} keys %$hash;
63 return $clean_hash;
bd7efd39 64}
65
66sub _join_condition {
67 my ($self, $cond) = @_;
68 die "no chance" unless ref $cond eq 'HASH';
69 my %j;
2a816814 70 for (keys %$cond) { my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; };
bd7efd39 71 return $self->_recurse_where(\%j);
72}
73
2a816814 74sub _quote {
75 my ($self, $label) = @_;
76 return '' unless defined $label;
77 return $self->SUPER::_quote($label);
78}
79
bd7efd39 80} # End of BEGIN block
81
8b445e33 82use base qw/DBIx::Class/;
83
438adc0e 84__PACKAGE__->load_components(qw/Exception AccessorGroup/);
8b445e33 85
223b8fe3 86__PACKAGE__->mk_group_accessors('simple' =>
48c69e7c 87 qw/connect_info _dbh _sql_maker debug cursor/);
8b445e33 88
89sub new {
223b8fe3 90 my $new = bless({}, ref $_[0] || $_[0]);
28927b50 91 $new->cursor("DBIx::Class::Storage::DBI::Cursor");
92 $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG};
223b8fe3 93 return $new;
8b445e33 94}
95
96sub get_simple {
97 my ($self, $get) = @_;
98 return $self->{$get};
99}
100
101sub set_simple {
102 my ($self, $set, $val) = @_;
103 return $self->{$set} = $val;
104}
105
106=head1 NAME
107
108DBIx::Class::Storage::DBI - DBI storage handler
109
110=head1 SYNOPSIS
111
112=head1 DESCRIPTION
113
114This class represents the connection to the database
115
116=head1 METHODS
117
118=over 4
119
120=cut
121
122sub 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
48c69e7c 131sub sql_maker {
132 my ($self) = @_;
fdc1c3d0 133 unless ($self->_sql_maker) {
bd7efd39 134 $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh ));
48c69e7c 135 }
136 return $self->_sql_maker;
137}
138
8b445e33 139sub _populate_dbh {
140 my ($self) = @_;
141 my @info = @{$self->connect_info || []};
142 $self->_dbh($self->_connect(@info));
143}
144
145sub _connect {
146 my ($self, @info) = @_;
147 return DBI->connect(@info);
148}
149
150=item commit
151
152 $class->commit;
153
154Issues a commit again the current dbh
155
156=cut
157
158sub commit { $_[0]->dbh->commit; }
159
160=item rollback
161
162 $class->rollback;
163
164Issues a rollback again the current dbh
165
166=cut
167
168sub rollback { $_[0]->dbh->rollback; }
169
223b8fe3 170sub _execute {
171 my ($self, $op, $extra_bind, $ident, @args) = @_;
172 my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
944f30bf 173 unshift(@bind, @$extra_bind) if $extra_bind;
223b8fe3 174 warn "$sql: @bind" if $self->debug;
175 my $sth = $self->sth($sql);
438adc0e 176 @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
177 my $rv = $sth->execute(@bind);
223b8fe3 178 return (wantarray ? ($rv, $sth, @bind) : $rv);
179}
180
8b445e33 181sub insert {
182 my ($self, $ident, $to_insert) = @_;
20a2c954 183 $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" )
223b8fe3 184 unless ($self->_execute('insert' => [], $ident, $to_insert) > 0);
8b445e33 185 return $to_insert;
186}
187
188sub update {
223b8fe3 189 return shift->_execute('update' => [], @_);
8b445e33 190}
191
192sub delete {
223b8fe3 193 return shift->_execute('delete' => [], @_);
8b445e33 194}
195
de705b51 196sub _select {
8b445e33 197 my ($self, $ident, $select, $condition, $attrs) = @_;
223b8fe3 198 my $order = $attrs->{order_by};
199 if (ref $condition eq 'SCALAR') {
200 $order = $1 if $$condition =~ s/ORDER BY (.*)$//i;
201 }
5c91499f 202 my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order);
9229f20a 203 if ($attrs->{software_limit} ||
204 $self->sql_maker->_default_limit_syntax eq "GenericSubQ") {
205 $attrs->{software_limit} = 1;
5c91499f 206 } else {
207 push @args, $attrs->{rows}, $attrs->{offset};
208 }
de705b51 209 return $self->_execute(@args);
210}
211
212sub select {
213 my $self = shift;
214 my ($ident, $select, $condition, $attrs) = @_;
215 my ($rv, $sth, @bind) = $self->_select(@_);
223b8fe3 216 return $self->cursor->new($sth, \@bind, $attrs);
8b445e33 217}
218
1a14aa3f 219sub select_single {
de705b51 220 my $self = shift;
221 my ($rv, $sth, @bind) = $self->_select(@_);
1a14aa3f 222 return $sth->fetchrow_array;
223}
224
8b445e33 225sub sth {
226 shift->dbh->prepare(@_);
227}
228
2291;
230
231=back
232
233=head1 AUTHORS
234
daec44b8 235Matt S. Trout <mst@shadowcatsystems.co.uk>
8b445e33 236
9f19b1d6 237Andy Grundman <andy@hybridized.org>
238
8b445e33 239=head1 LICENSE
240
241You may distribute this code under the same terms as Perl itself.
242
243=cut
244