Commit | Line | Data |
88a8d0fa |
1 | package DBIx::Class::Storage::DBI::InterBase; |
2 | |
145b2a3d |
3 | # partly stolen from DBIx::Class::Storage::DBI::MSSQL |
88a8d0fa |
4 | |
5 | use strict; |
6 | use warnings; |
637d2fae |
7 | use base qw/DBIx::Class::Storage::DBI/; |
88a8d0fa |
8 | use mro 'c3'; |
88a8d0fa |
9 | use List::Util(); |
10 | |
11 | __PACKAGE__->mk_group_accessors(simple => qw/ |
2680ffe5 |
12 | _auto_incs |
88a8d0fa |
13 | /); |
14 | |
90489c23 |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS |
18 | |
19 | =head1 DESCRIPTION |
20 | |
95570280 |
21 | This class implements autoincrements for Firebird using C<RETURNING> as well as |
22 | L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to |
23 | C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support. |
90489c23 |
24 | |
dd2109ee |
25 | You need to use either the |
26 | L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or |
27 | L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function |
47ec67c3 |
28 | correctly with this driver. Otherwise you will likely get bizarre error messages |
d1fc96c7 |
29 | such as C<no statement executing>. The alternative is to use the |
30 | L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable |
31 | for long running processes such as under L<Catalyst>. |
90489c23 |
32 | |
c5827074 |
33 | To turn on L<DBIx::Class::InflateColumn::DateTime> support, see |
34 | L</connect_call_datetime_setup>. |
32323fc2 |
35 | |
90489c23 |
36 | =cut |
37 | |
88a8d0fa |
38 | sub _prep_for_execute { |
39 | my $self = shift; |
40 | my ($op, $extra_bind, $ident, $args) = @_; |
41 | |
88a8d0fa |
42 | if ($op eq 'insert') { |
95570280 |
43 | $self->_auto_incs([]); |
44 | |
28d28903 |
45 | my %pk; |
0a145954 |
46 | @pk{$ident->primary_columns} = (); |
6e8d182b |
47 | |
1ae0a36c |
48 | my @auto_inc_cols = grep { |
49 | my $inserting = $args->[0]{$_}; |
145b2a3d |
50 | |
6e8d182b |
51 | ($ident->column_info($_)->{is_auto_increment} |
52 | || exists $pk{$_}) |
53 | && ( |
1ae0a36c |
54 | (not defined $inserting) |
55 | || |
2680ffe5 |
56 | (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i) |
1ae0a36c |
57 | ) |
58 | } $ident->columns; |
88a8d0fa |
59 | |
145b2a3d |
60 | if (@auto_inc_cols) { |
1696e04f |
61 | $args->[1]{returning} = \@auto_inc_cols; |
145b2a3d |
62 | |
2680ffe5 |
63 | $self->_auto_incs->[0] = \@auto_inc_cols; |
145b2a3d |
64 | } |
88a8d0fa |
65 | } |
66 | |
1696e04f |
67 | return $self->next::method(@_); |
1ae0a36c |
68 | } |
69 | |
88a8d0fa |
70 | sub _execute { |
71 | my $self = shift; |
72 | my ($op) = @_; |
73 | |
74 | my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_); |
75 | |
2680ffe5 |
76 | if ($op eq 'insert' && $self->_auto_incs) { |
88a8d0fa |
77 | local $@; |
145b2a3d |
78 | my (@auto_incs) = eval { |
79 | local $SIG{__WARN__} = sub {}; |
80 | $sth->fetchrow_array |
81 | }; |
2680ffe5 |
82 | $self->_auto_incs->[1] = \@auto_incs; |
88a8d0fa |
83 | $sth->finish; |
84 | } |
85 | |
86 | return wantarray ? ($rv, $sth, @bind) : $rv; |
87 | } |
88 | |
e1958268 |
89 | sub _sequence_fetch { |
90 | my ($self, $nextval, $sequence) = @_; |
91 | |
92 | if ($nextval ne 'nextval') { |
93 | $self->throw_exception("Can only fetch 'nextval' for a sequence"); |
94 | } |
95 | |
96 | $self->throw_exception('No sequence to fetch') unless $sequence; |
97 | |
98 | my ($val) = $self->_get_dbh->selectrow_array( |
99 | 'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) . |
100 | ', 1) FROM rdb$database'); |
101 | |
102 | return $val; |
103 | } |
104 | |
105 | sub _dbh_get_autoinc_seq { |
106 | my ($self, $dbh, $source, $col) = @_; |
107 | |
108 | my $table_name = $source->from; |
109 | $table_name = $$table_name if ref $table_name; |
110 | $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name); |
111 | |
112 | local $dbh->{LongReadLen} = 100000; |
113 | local $dbh->{LongTruncOk} = 1; |
114 | |
115 | my $sth = $dbh->prepare(<<'EOF'); |
116 | SELECT t.rdb$trigger_source |
117 | FROM rdb$triggers t |
118 | WHERE t.rdb$relation_name = ? |
119 | AND t.rdb$system_flag = 0 -- user defined |
120 | AND t.rdb$trigger_type = 1 -- BEFORE INSERT |
121 | EOF |
122 | $sth->execute($table_name); |
123 | |
124 | while (my ($trigger) = $sth->fetchrow_array) { |
125 | my @trig_cols = map { |
126 | /^"([^"]+)/ ? $1 : uc($1) |
127 | } $trigger =~ /new\.("?\w+"?)/ig; |
128 | |
129 | my ($quoted, $generator) = $trigger =~ |
130 | /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix; |
131 | |
132 | if ($generator) { |
133 | $generator = uc $generator unless $quoted; |
134 | |
135 | return $generator |
136 | if List::Util::first { |
137 | $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col)) |
138 | } @trig_cols; |
139 | } |
140 | } |
141 | |
142 | return undef; |
143 | } |
144 | |
145b2a3d |
145 | sub last_insert_id { |
146 | my ($self, $source, @cols) = @_; |
147 | my @result; |
88a8d0fa |
148 | |
145b2a3d |
149 | my %auto_incs; |
2680ffe5 |
150 | @auto_incs{ @{ $self->_auto_incs->[0] } } = |
151 | @{ $self->_auto_incs->[1] }; |
145b2a3d |
152 | |
153 | push @result, $auto_incs{$_} for @cols; |
154 | |
155 | return @result; |
156 | } |
157 | |
95570280 |
158 | sub insert { |
159 | my $self = shift; |
160 | |
161 | my $updated_cols = $self->next::method(@_); |
162 | |
163 | if ($self->_auto_incs->[0]) { |
164 | my %auto_incs; |
165 | @auto_incs{ @{ $self->_auto_incs->[0] } } = @{ $self->_auto_incs->[1] }; |
166 | |
167 | $updated_cols = { %$updated_cols, %auto_incs }; |
168 | } |
169 | |
170 | return $updated_cols; |
171 | } |
172 | |
145b2a3d |
173 | # this sub stolen from DB2 |
88a8d0fa |
174 | |
145b2a3d |
175 | sub _sql_maker_opts { |
176 | my ( $self, $opts ) = @_; |
177 | |
178 | if ( $opts ) { |
179 | $self->{_sql_maker_opts} = { %$opts }; |
180 | } |
181 | |
182 | return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} }; |
183 | } |
184 | |
32323fc2 |
185 | sub _svp_begin { |
186 | my ($self, $name) = @_; |
187 | |
188 | $self->_get_dbh->do("SAVEPOINT $name"); |
189 | } |
190 | |
191 | sub _svp_release { |
192 | my ($self, $name) = @_; |
193 | |
194 | $self->_get_dbh->do("RELEASE SAVEPOINT $name"); |
195 | } |
196 | |
197 | sub _svp_rollback { |
198 | my ($self, $name) = @_; |
199 | |
200 | $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name") |
201 | } |
202 | |
203 | sub _ping { |
204 | my $self = shift; |
205 | |
206 | my $dbh = $self->_dbh or return 0; |
207 | |
208 | local $dbh->{RaiseError} = 1; |
ecdf1ac8 |
209 | local $dbh->{PrintError} = 0; |
32323fc2 |
210 | |
211 | eval { |
212 | $dbh->do('select 1 from rdb$database'); |
213 | }; |
214 | |
215 | return $@ ? 0 : 1; |
216 | } |
217 | |
9633951d |
218 | # We want dialect 3 for new features and quoting to work, DBD::InterBase uses |
219 | # dialect 1 (interbase compat) by default. |
220 | sub _init { |
221 | my $self = shift; |
222 | $self->_set_sql_dialect(3); |
223 | } |
224 | |
225 | sub _set_sql_dialect { |
226 | my $self = shift; |
227 | my $val = shift || 3; |
228 | |
229 | my $dsn = $self->_dbi_connect_info->[0]; |
230 | |
231 | return if ref($dsn) eq 'CODE'; |
232 | |
233 | if ($dsn !~ /ib_dialect=/) { |
234 | $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val"; |
235 | my $connected = defined $self->_dbh; |
236 | $self->disconnect; |
237 | $self->ensure_connected if $connected; |
238 | } |
239 | } |
240 | |
dd2109ee |
241 | =head2 connect_call_use_softcommit |
242 | |
243 | Used as: |
244 | |
245 | on_connect_call => 'use_softcommit' |
246 | |
247 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the |
248 | L<DBD::InterBase> C<ib_softcommit> option. |
249 | |
250 | You need either this option or C<< disable_sth_caching => 1 >> for |
47ec67c3 |
251 | L<DBIx::Class> code to function correctly (otherwise you may get C<no statement |
d1fc96c7 |
252 | executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> |
253 | driver. |
dd2109ee |
254 | |
255 | The downside of using this option is that your process will B<NOT> see UPDATEs, |
256 | INSERTs and DELETEs from other processes for already open statements. |
257 | |
258 | =cut |
259 | |
260 | sub connect_call_use_softcommit { |
a499b173 |
261 | my $self = shift; |
262 | |
263 | $self->_dbh->{ib_softcommit} = 1; |
a499b173 |
264 | } |
265 | |
32323fc2 |
266 | =head2 connect_call_datetime_setup |
9cd0b325 |
267 | |
32323fc2 |
268 | Used as: |
9cd0b325 |
269 | |
32323fc2 |
270 | on_connect_call => 'datetime_setup' |
271 | |
f0f8ac86 |
272 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and |
273 | timestamp formats using: |
32323fc2 |
274 | |
275 | $dbh->{ib_time_all} = 'ISO'; |
276 | |
277 | See L<DBD::InterBase> for more details. |
278 | |
279 | The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for |
280 | second precision. The full precision is used. |
281 | |
c5827074 |
282 | The C<DATE> data type stores the date portion only, and it B<MUST> be declared |
283 | with: |
284 | |
285 | data_type => 'date' |
286 | |
287 | in your Result class. |
288 | |
289 | Timestamp columns can be declared with either C<datetime> or C<timestamp>. |
290 | |
32323fc2 |
291 | You will need the L<DateTime::Format::Strptime> module for inflation to work. |
292 | |
293 | For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second |
294 | precision is not currently available. |
295 | |
296 | =cut |
297 | |
298 | sub connect_call_datetime_setup { |
299 | my $self = shift; |
300 | |
301 | $self->_get_dbh->{ib_time_all} = 'ISO'; |
9cd0b325 |
302 | } |
303 | |
c5827074 |
304 | sub datetime_parser_type { |
305 | 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format' |
306 | } |
32323fc2 |
307 | |
c5827074 |
308 | package # hide from PAUSE |
309 | DBIx::Class::Storage::DBI::InterBase::DateTime::Format; |
32323fc2 |
310 | |
c5827074 |
311 | my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T |
312 | my $date_format = '%Y-%m-%d'; |
313 | |
314 | my ($timestamp_parser, $date_parser); |
315 | |
316 | sub parse_datetime { |
317 | shift; |
318 | require DateTime::Format::Strptime; |
319 | $timestamp_parser ||= DateTime::Format::Strptime->new( |
320 | pattern => $timestamp_format, |
321 | on_error => 'croak', |
322 | ); |
323 | return $timestamp_parser->parse_datetime(shift); |
324 | } |
325 | |
326 | sub format_datetime { |
327 | shift; |
328 | require DateTime::Format::Strptime; |
329 | $timestamp_parser ||= DateTime::Format::Strptime->new( |
330 | pattern => $timestamp_format, |
331 | on_error => 'croak', |
332 | ); |
333 | return $timestamp_parser->format_datetime(shift); |
334 | } |
335 | |
336 | sub parse_date { |
337 | shift; |
338 | require DateTime::Format::Strptime; |
339 | $date_parser ||= DateTime::Format::Strptime->new( |
340 | pattern => $date_format, |
341 | on_error => 'croak', |
342 | ); |
343 | return $date_parser->parse_datetime(shift); |
344 | } |
345 | |
346 | sub format_date { |
347 | shift; |
348 | require DateTime::Format::Strptime; |
349 | $date_parser ||= DateTime::Format::Strptime->new( |
350 | pattern => $date_format, |
32323fc2 |
351 | on_error => 'croak', |
352 | ); |
c5827074 |
353 | return $date_parser->format_datetime(shift); |
9cd0b325 |
354 | } |
355 | |
145b2a3d |
356 | 1; |
90489c23 |
357 | |
358 | =head1 CAVEATS |
359 | |
360 | =over 4 |
361 | |
362 | =item * |
363 | |
dd2109ee |
364 | with L</connect_call_use_softcommit>, you will not be able to see changes made |
365 | to data in other processes. If this is an issue, use |
47ec67c3 |
366 | L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a |
367 | workaround for the C<no statement executing> errors, this of course adversely |
368 | affects performance. |
dd2109ee |
369 | |
d1fc96c7 |
370 | Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver. |
371 | |
dd2109ee |
372 | =item * |
373 | |
e1958268 |
374 | C<last_insert_id> support by default only works for Firebird versions 2 or |
375 | greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should |
376 | work with earlier versions. |
90489c23 |
377 | |
47ec67c3 |
378 | =item * |
379 | |
d1fc96c7 |
380 | Sub-second precision for TIMESTAMPs is not currently available when using the |
381 | L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver. |
47ec67c3 |
382 | |
90489c23 |
383 | =back |
384 | |
385 | =head1 AUTHOR |
386 | |
387 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>. |
388 | |
389 | =head1 LICENSE |
390 | |
391 | You may distribute this code under the same terms as Perl itself. |
392 | |
393 | =cut |