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