1 package DBIx::Class::Storage::DBI::InterBase;
5 use base qw/DBIx::Class::Storage::DBI/;
9 __PACKAGE__->mk_group_accessors(simple => qw/
15 DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
19 This class implements autoincrements for Firebird using C<RETURNING> as well as
20 L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to
21 C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support.
23 You need to use either the
24 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
25 L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
26 correctly with this driver. Otherwise you will likely get bizarre error messages
27 such as C<no statement executing>. The alternative is to use the
28 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
29 for long running processes such as under L<Catalyst>.
31 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
32 L</connect_call_datetime_setup>.
36 sub _prep_for_execute {
38 my ($op, $extra_bind, $ident, $args) = @_;
40 if ($op eq 'insert') {
41 $self->_auto_incs([]);
44 @pk{$ident->primary_columns} = ();
46 my @auto_inc_cols = grep {
47 my $inserting = $args->[0]{$_};
49 ($ident->column_info($_)->{is_auto_increment}
52 (not defined $inserting)
54 (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
59 $args->[1]{returning} = \@auto_inc_cols;
61 $self->_auto_incs->[0] = \@auto_inc_cols;
65 return $self->next::method(@_);
72 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
74 if ($op eq 'insert' && $self->_auto_incs) {
76 my (@auto_incs) = eval {
77 local $SIG{__WARN__} = sub {};
80 $self->_auto_incs->[1] = \@auto_incs;
84 return wantarray ? ($rv, $sth, @bind) : $rv;
88 my ($self, $nextval, $sequence) = @_;
90 if ($nextval ne 'nextval') {
91 $self->throw_exception("Can only fetch 'nextval' for a sequence");
94 $self->throw_exception('No sequence to fetch') unless $sequence;
96 my ($val) = $self->_get_dbh->selectrow_array(
97 'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) .
98 ', 1) FROM rdb$database');
103 sub _dbh_get_autoinc_seq {
104 my ($self, $dbh, $source, $col) = @_;
106 my $table_name = $source->from;
107 $table_name = $$table_name if ref $table_name;
108 $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name);
110 local $dbh->{LongReadLen} = 100000;
111 local $dbh->{LongTruncOk} = 1;
113 my $sth = $dbh->prepare(<<'EOF');
114 SELECT t.rdb$trigger_source
116 WHERE t.rdb$relation_name = ?
117 AND t.rdb$system_flag = 0 -- user defined
118 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
120 $sth->execute($table_name);
122 while (my ($trigger) = $sth->fetchrow_array) {
123 my @trig_cols = map {
124 /^"([^"]+)/ ? $1 : uc($1)
125 } $trigger =~ /new\.("?\w+"?)/ig;
127 my ($quoted, $generator) = $trigger =~
128 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
131 $generator = uc $generator unless $quoted;
134 if List::Util::first {
135 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
144 my ($self, $source, @cols) = @_;
148 @auto_incs{ @{ $self->_auto_incs->[0] } } =
149 @{ $self->_auto_incs->[1] };
151 push @result, $auto_incs{$_} for @cols;
159 my $updated_cols = $self->next::method(@_);
161 if ($self->_auto_incs->[0]) {
163 @auto_incs{ @{ $self->_auto_incs->[0] } } = @{ $self->_auto_incs->[1] };
165 $updated_cols = { %$updated_cols, %auto_incs };
168 return $updated_cols;
171 # this sub stolen from DB2
173 sub _sql_maker_opts {
174 my ( $self, $opts ) = @_;
177 $self->{_sql_maker_opts} = { %$opts };
180 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
184 my ($self, $name) = @_;
186 $self->_get_dbh->do("SAVEPOINT $name");
190 my ($self, $name) = @_;
192 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
196 my ($self, $name) = @_;
198 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
204 my $dbh = $self->_dbh or return 0;
206 local $dbh->{RaiseError} = 1;
207 local $dbh->{PrintError} = 0;
210 $dbh->do('select 1 from rdb$database');
216 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
217 # dialect 1 (interbase compat) by default.
220 $self->_set_sql_dialect(3);
223 sub _set_sql_dialect {
225 my $val = shift || 3;
227 my $dsn = $self->_dbi_connect_info->[0];
229 return if ref($dsn) eq 'CODE';
231 if ($dsn !~ /ib_dialect=/) {
232 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
233 my $connected = defined $self->_dbh;
235 $self->ensure_connected if $connected;
239 =head2 connect_call_use_softcommit
243 on_connect_call => 'use_softcommit'
245 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
246 L<DBD::InterBase> C<ib_softcommit> option.
248 You need either this option or C<< disable_sth_caching => 1 >> for
249 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
250 executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
253 The downside of using this option is that your process will B<NOT> see UPDATEs,
254 INSERTs and DELETEs from other processes for already open statements.
258 sub connect_call_use_softcommit {
261 $self->_dbh->{ib_softcommit} = 1;
264 =head2 connect_call_datetime_setup
268 on_connect_call => 'datetime_setup'
270 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
271 timestamp formats using:
273 $dbh->{ib_time_all} = 'ISO';
275 See L<DBD::InterBase> for more details.
277 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
278 second precision. The full precision is used.
280 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
285 in your Result class.
287 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
289 You will need the L<DateTime::Format::Strptime> module for inflation to work.
291 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
292 precision is not currently available.
296 sub connect_call_datetime_setup {
299 $self->_get_dbh->{ib_time_all} = 'ISO';
302 sub datetime_parser_type {
303 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
306 package # hide from PAUSE
307 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
309 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
310 my $date_format = '%Y-%m-%d';
312 my ($timestamp_parser, $date_parser);
316 require DateTime::Format::Strptime;
317 $timestamp_parser ||= DateTime::Format::Strptime->new(
318 pattern => $timestamp_format,
321 return $timestamp_parser->parse_datetime(shift);
324 sub format_datetime {
326 require DateTime::Format::Strptime;
327 $timestamp_parser ||= DateTime::Format::Strptime->new(
328 pattern => $timestamp_format,
331 return $timestamp_parser->format_datetime(shift);
336 require DateTime::Format::Strptime;
337 $date_parser ||= DateTime::Format::Strptime->new(
338 pattern => $date_format,
341 return $date_parser->parse_datetime(shift);
346 require DateTime::Format::Strptime;
347 $date_parser ||= DateTime::Format::Strptime->new(
348 pattern => $date_format,
351 return $date_parser->format_datetime(shift);
362 with L</connect_call_use_softcommit>, you will not be able to see changes made
363 to data in other processes. If this is an issue, use
364 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
365 workaround for the C<no statement executing> errors, this of course adversely
368 Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
372 C<last_insert_id> support by default only works for Firebird versions 2 or
373 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
374 work with earlier versions.
378 Sub-second precision for TIMESTAMPs is not currently available when using the
379 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
385 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
389 You may distribute this code under the same terms as Perl itself.