1 package DBIx::Class::Storage::DBI::InterBase;
3 # partly stolen from DBIx::Class::Storage::DBI::MSSQL
7 use base qw/DBIx::Class::Storage::DBI/;
11 __PACKAGE__->mk_group_accessors(simple => qw/
17 DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
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.
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
28 correctly with this driver. Otherwise you will likely get bizarre error messages
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>.
33 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
34 L</connect_call_datetime_setup>.
38 sub _prep_for_execute {
40 my ($op, $extra_bind, $ident, $args) = @_;
42 if ($op eq 'insert') {
43 $self->_auto_incs([]);
46 @pk{$ident->primary_columns} = ();
48 my @auto_inc_cols = grep {
49 my $inserting = $args->[0]{$_};
51 ($ident->column_info($_)->{is_auto_increment}
54 (not defined $inserting)
56 (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
61 $args->[1]{returning} = \@auto_inc_cols;
63 $self->_auto_incs->[0] = \@auto_inc_cols;
67 return $self->next::method(@_);
74 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
76 if ($op eq 'insert' && $self->_auto_incs) {
78 my (@auto_incs) = eval {
79 local $SIG{__WARN__} = sub {};
82 $self->_auto_incs->[1] = \@auto_incs;
86 return wantarray ? ($rv, $sth, @bind) : $rv;
90 my ($self, $nextval, $sequence) = @_;
92 if ($nextval ne 'nextval') {
93 $self->throw_exception("Can only fetch 'nextval' for a sequence");
96 $self->throw_exception('No sequence to fetch') unless $sequence;
98 my ($val) = $self->_get_dbh->selectrow_array(
99 'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) .
100 ', 1) FROM rdb$database');
105 sub _dbh_get_autoinc_seq {
106 my ($self, $dbh, $source, $col) = @_;
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);
112 local $dbh->{LongReadLen} = 100000;
113 local $dbh->{LongTruncOk} = 1;
115 my $sth = $dbh->prepare(<<'EOF');
116 SELECT t.rdb$trigger_source
118 WHERE t.rdb$relation_name = ?
119 AND t.rdb$system_flag = 0 -- user defined
120 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
122 $sth->execute($table_name);
124 while (my ($trigger) = $sth->fetchrow_array) {
125 my @trig_cols = map {
126 /^"([^"]+)/ ? $1 : uc($1)
127 } $trigger =~ /new\.("?\w+"?)/ig;
129 my ($quoted, $generator) = $trigger =~
130 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
133 $generator = uc $generator unless $quoted;
136 if List::Util::first {
137 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
146 my ($self, $source, @cols) = @_;
150 @auto_incs{ @{ $self->_auto_incs->[0] } } =
151 @{ $self->_auto_incs->[1] };
153 push @result, $auto_incs{$_} for @cols;
161 my $updated_cols = $self->next::method(@_);
163 if ($self->_auto_incs->[0]) {
165 @auto_incs{ @{ $self->_auto_incs->[0] } } = @{ $self->_auto_incs->[1] };
167 $updated_cols = { %$updated_cols, %auto_incs };
170 return $updated_cols;
173 # this sub stolen from DB2
175 sub _sql_maker_opts {
176 my ( $self, $opts ) = @_;
179 $self->{_sql_maker_opts} = { %$opts };
182 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
186 my ($self, $name) = @_;
188 $self->_get_dbh->do("SAVEPOINT $name");
192 my ($self, $name) = @_;
194 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
198 my ($self, $name) = @_;
200 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
206 my $dbh = $self->_dbh or return 0;
208 local $dbh->{RaiseError} = 1;
211 $dbh->do('select 1 from rdb$database');
217 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
218 # dialect 1 (interbase compat) by default.
221 $self->_set_sql_dialect(3);
224 sub _set_sql_dialect {
226 my $val = shift || 3;
228 my $dsn = $self->_dbi_connect_info->[0];
230 return if ref($dsn) eq 'CODE';
232 if ($dsn !~ /ib_dialect=/) {
233 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
234 my $connected = defined $self->_dbh;
236 $self->ensure_connected if $connected;
240 =head2 connect_call_use_softcommit
244 on_connect_call => 'use_softcommit'
246 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
247 L<DBD::InterBase> C<ib_softcommit> option.
249 You need either this option or C<< disable_sth_caching => 1 >> for
250 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
251 executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
254 The downside of using this option is that your process will B<NOT> see UPDATEs,
255 INSERTs and DELETEs from other processes for already open statements.
259 sub connect_call_use_softcommit {
262 $self->_dbh->{ib_softcommit} = 1;
265 =head2 connect_call_datetime_setup
269 on_connect_call => 'datetime_setup'
271 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
272 timestamp formats using:
274 $dbh->{ib_time_all} = 'ISO';
276 See L<DBD::InterBase> for more details.
278 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
279 second precision. The full precision is used.
281 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
286 in your Result class.
288 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
290 You will need the L<DateTime::Format::Strptime> module for inflation to work.
292 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
293 precision is not currently available.
297 sub connect_call_datetime_setup {
300 $self->_get_dbh->{ib_time_all} = 'ISO';
303 sub datetime_parser_type {
304 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
307 package # hide from PAUSE
308 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
310 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
311 my $date_format = '%Y-%m-%d';
313 my ($timestamp_parser, $date_parser);
317 require DateTime::Format::Strptime;
318 $timestamp_parser ||= DateTime::Format::Strptime->new(
319 pattern => $timestamp_format,
322 return $timestamp_parser->parse_datetime(shift);
325 sub format_datetime {
327 require DateTime::Format::Strptime;
328 $timestamp_parser ||= DateTime::Format::Strptime->new(
329 pattern => $timestamp_format,
332 return $timestamp_parser->format_datetime(shift);
337 require DateTime::Format::Strptime;
338 $date_parser ||= DateTime::Format::Strptime->new(
339 pattern => $date_format,
342 return $date_parser->parse_datetime(shift);
347 require DateTime::Format::Strptime;
348 $date_parser ||= DateTime::Format::Strptime->new(
349 pattern => $date_format,
352 return $date_parser->format_datetime(shift);
363 with L</connect_call_use_softcommit>, you will not be able to see changes made
364 to data in other processes. If this is an issue, use
365 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
366 workaround for the C<no statement executing> errors, this of course adversely
369 Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
373 C<last_insert_id> support by default only works for Firebird versions 2 or
374 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
375 work with earlier versions.
379 Sub-second precision for TIMESTAMPs is not currently available when using the
380 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
386 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
390 You may distribute this code under the same terms as Perl itself.