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