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; |
8b445e33 |
8 | |
bd7efd39 |
9 | BEGIN { |
10 | |
11 | package DBIC::SQL::Abstract; # Temporary. Merge upstream. |
12 | |
13 | use base qw/SQL::Abstract::Limit/; |
14 | |
2a816814 |
15 | sub _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 | |
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; |
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 | |
52 | sub _make_as { |
53 | my ($self, $from) = @_; |
2a816814 |
54 | return join(' ', map { $self->_quote($_) } |
55 | reverse each %{$self->_skip_options($from)}); |
73856587 |
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; |
bd7efd39 |
64 | } |
65 | |
66 | sub _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 |
74 | sub _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 |
82 | use 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 | |
89 | sub 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 | |
8b445e33 |
96 | =head1 NAME |
97 | |
98 | DBIx::Class::Storage::DBI - DBI storage handler |
99 | |
100 | =head1 SYNOPSIS |
101 | |
102 | =head1 DESCRIPTION |
103 | |
104 | This class represents the connection to the database |
105 | |
106 | =head1 METHODS |
107 | |
8b445e33 |
108 | =cut |
109 | |
110 | sub dbh { |
111 | my ($self) = @_; |
112 | my $dbh; |
113 | unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) { |
114 | $self->_populate_dbh; |
115 | } |
116 | return $self->_dbh; |
117 | } |
118 | |
48c69e7c |
119 | sub sql_maker { |
120 | my ($self) = @_; |
fdc1c3d0 |
121 | unless ($self->_sql_maker) { |
bd7efd39 |
122 | $self->_sql_maker(new DBIC::SQL::Abstract( limit_dialect => $self->dbh )); |
48c69e7c |
123 | } |
124 | return $self->_sql_maker; |
125 | } |
126 | |
8b445e33 |
127 | sub _populate_dbh { |
128 | my ($self) = @_; |
129 | my @info = @{$self->connect_info || []}; |
130 | $self->_dbh($self->_connect(@info)); |
131 | } |
132 | |
133 | sub _connect { |
134 | my ($self, @info) = @_; |
135 | return DBI->connect(@info); |
136 | } |
137 | |
130c6439 |
138 | =head2 commit |
8b445e33 |
139 | |
140 | $class->commit; |
141 | |
142 | Issues a commit again the current dbh |
143 | |
144 | =cut |
145 | |
146 | sub commit { $_[0]->dbh->commit; } |
147 | |
130c6439 |
148 | =head2 rollback |
8b445e33 |
149 | |
150 | $class->rollback; |
151 | |
152 | Issues a rollback again the current dbh |
153 | |
154 | =cut |
155 | |
156 | sub rollback { $_[0]->dbh->rollback; } |
157 | |
223b8fe3 |
158 | sub _execute { |
159 | my ($self, $op, $extra_bind, $ident, @args) = @_; |
160 | my ($sql, @bind) = $self->sql_maker->$op($ident, @args); |
944f30bf |
161 | unshift(@bind, @$extra_bind) if $extra_bind; |
223b8fe3 |
162 | warn "$sql: @bind" if $self->debug; |
163 | my $sth = $self->sth($sql); |
438adc0e |
164 | @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args |
165 | my $rv = $sth->execute(@bind); |
223b8fe3 |
166 | return (wantarray ? ($rv, $sth, @bind) : $rv); |
167 | } |
168 | |
8b445e33 |
169 | sub insert { |
170 | my ($self, $ident, $to_insert) = @_; |
20a2c954 |
171 | $self->throw( "Couldn't insert ".join(', ', map "$_ => $to_insert->{$_}", keys %$to_insert)." into ${ident}" ) |
223b8fe3 |
172 | unless ($self->_execute('insert' => [], $ident, $to_insert) > 0); |
8b445e33 |
173 | return $to_insert; |
174 | } |
175 | |
176 | sub update { |
223b8fe3 |
177 | return shift->_execute('update' => [], @_); |
8b445e33 |
178 | } |
179 | |
180 | sub delete { |
223b8fe3 |
181 | return shift->_execute('delete' => [], @_); |
8b445e33 |
182 | } |
183 | |
de705b51 |
184 | sub _select { |
8b445e33 |
185 | my ($self, $ident, $select, $condition, $attrs) = @_; |
223b8fe3 |
186 | my $order = $attrs->{order_by}; |
187 | if (ref $condition eq 'SCALAR') { |
188 | $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; |
189 | } |
5c91499f |
190 | my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); |
9229f20a |
191 | if ($attrs->{software_limit} || |
192 | $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { |
193 | $attrs->{software_limit} = 1; |
5c91499f |
194 | } else { |
195 | push @args, $attrs->{rows}, $attrs->{offset}; |
196 | } |
de705b51 |
197 | return $self->_execute(@args); |
198 | } |
199 | |
200 | sub select { |
201 | my $self = shift; |
202 | my ($ident, $select, $condition, $attrs) = @_; |
203 | my ($rv, $sth, @bind) = $self->_select(@_); |
223b8fe3 |
204 | return $self->cursor->new($sth, \@bind, $attrs); |
8b445e33 |
205 | } |
206 | |
1a14aa3f |
207 | sub select_single { |
de705b51 |
208 | my $self = shift; |
209 | my ($rv, $sth, @bind) = $self->_select(@_); |
1a14aa3f |
210 | return $sth->fetchrow_array; |
211 | } |
212 | |
8b445e33 |
213 | sub sth { |
214 | shift->dbh->prepare(@_); |
215 | } |
216 | |
217 | 1; |
218 | |
8b445e33 |
219 | =head1 AUTHORS |
220 | |
daec44b8 |
221 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
8b445e33 |
222 | |
9f19b1d6 |
223 | Andy Grundman <andy@hybridized.org> |
224 | |
8b445e33 |
225 | =head1 LICENSE |
226 | |
227 | You may distribute this code under the same terms as Perl itself. |
228 | |
229 | =cut |
230 | |