1 package DBIx::Class::Storage::DBI::InterBase;
5 use base qw/DBIx::Class::Storage::DBI/;
12 DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
16 This class implements autoincrements for Firebird using C<RETURNING> as well as
17 L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to
18 C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support.
20 You need to use either the
21 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
22 L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
23 correctly with this driver. Otherwise you will likely get bizarre error messages
24 such as C<no statement executing>. The alternative is to use the
25 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
26 for long running processes such as under L<Catalyst>.
28 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
29 L</connect_call_datetime_setup>.
33 sub _supports_insert_returning { 1 }
36 my ($self, $nextval, $sequence) = @_;
38 if ($nextval ne 'nextval') {
39 $self->throw_exception("Can only fetch 'nextval' for a sequence");
42 $self->throw_exception('No sequence to fetch') unless $sequence;
44 my ($val) = $self->_get_dbh->selectrow_array(
45 'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) .
46 ', 1) FROM rdb$database');
51 sub _dbh_get_autoinc_seq {
52 my ($self, $dbh, $source, $col) = @_;
54 my $table_name = $source->from;
55 $table_name = $$table_name if ref $table_name;
56 $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name);
58 local $dbh->{LongReadLen} = 100000;
59 local $dbh->{LongTruncOk} = 1;
61 my $sth = $dbh->prepare(<<'EOF');
62 SELECT t.rdb$trigger_source
64 WHERE t.rdb$relation_name = ?
65 AND t.rdb$system_flag = 0 -- user defined
66 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
68 $sth->execute($table_name);
70 while (my ($trigger) = $sth->fetchrow_array) {
72 /^"([^"]+)/ ? $1 : uc($1)
73 } $trigger =~ /new\.("?\w+"?)/ig;
75 my ($quoted, $generator) = $trigger =~
76 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
79 $generator = uc $generator unless $quoted;
82 if List::Util::first {
83 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
91 # this sub stolen from DB2
94 my ( $self, $opts ) = @_;
97 $self->{_sql_maker_opts} = { %$opts };
100 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
104 my ($self, $name) = @_;
106 $self->_get_dbh->do("SAVEPOINT $name");
110 my ($self, $name) = @_;
112 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
116 my ($self, $name) = @_;
118 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
124 my $dbh = $self->_dbh or return 0;
126 local $dbh->{RaiseError} = 1;
127 local $dbh->{PrintError} = 0;
131 $dbh->do('select 1 from rdb$database');
139 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
140 # dialect 1 (interbase compat) by default.
143 $self->_set_sql_dialect(3);
146 sub _set_sql_dialect {
148 my $val = shift || 3;
150 my $dsn = $self->_dbi_connect_info->[0];
152 return if ref($dsn) eq 'CODE';
154 if ($dsn !~ /ib_dialect=/) {
155 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
156 my $connected = defined $self->_dbh;
158 $self->ensure_connected if $connected;
162 sub _get_server_version {
165 return $self->next::method(@_) if ref $self ne __PACKAGE__;
167 local $SIG{__WARN__} = sub {}; # silence warning due to bug in DBD::InterBase
169 return $self->next::method(@_);
172 =head2 connect_call_use_softcommit
176 on_connect_call => 'use_softcommit'
178 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
179 L<DBD::InterBase> C<ib_softcommit> option.
181 You need either this option or C<< disable_sth_caching => 1 >> for
182 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
183 executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
186 The downside of using this option is that your process will B<NOT> see UPDATEs,
187 INSERTs and DELETEs from other processes for already open statements.
191 sub connect_call_use_softcommit {
194 $self->_dbh->{ib_softcommit} = 1;
197 =head2 connect_call_datetime_setup
201 on_connect_call => 'datetime_setup'
203 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
204 timestamp formats using:
206 $dbh->{ib_time_all} = 'ISO';
208 See L<DBD::InterBase> for more details.
210 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
211 second precision. The full precision is used.
213 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
218 in your Result class.
220 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
222 You will need the L<DateTime::Format::Strptime> module for inflation to work.
224 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
225 precision is not currently available.
229 sub connect_call_datetime_setup {
232 $self->_get_dbh->{ib_time_all} = 'ISO';
235 sub datetime_parser_type {
236 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
239 package # hide from PAUSE
240 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
242 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
243 my $date_format = '%Y-%m-%d';
245 my ($timestamp_parser, $date_parser);
249 require DateTime::Format::Strptime;
250 $timestamp_parser ||= DateTime::Format::Strptime->new(
251 pattern => $timestamp_format,
254 return $timestamp_parser->parse_datetime(shift);
257 sub format_datetime {
259 require DateTime::Format::Strptime;
260 $timestamp_parser ||= DateTime::Format::Strptime->new(
261 pattern => $timestamp_format,
264 return $timestamp_parser->format_datetime(shift);
269 require DateTime::Format::Strptime;
270 $date_parser ||= DateTime::Format::Strptime->new(
271 pattern => $date_format,
274 return $date_parser->parse_datetime(shift);
279 require DateTime::Format::Strptime;
280 $date_parser ||= DateTime::Format::Strptime->new(
281 pattern => $date_format,
284 return $date_parser->format_datetime(shift);
295 with L</connect_call_use_softcommit>, you will not be able to see changes made
296 to data in other processes. If this is an issue, use
297 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
298 workaround for the C<no statement executing> errors, this of course adversely
301 Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
305 C<last_insert_id> support by default only works for Firebird versions 2 or
306 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
307 work with earlier versions.
311 Sub-second precision for TIMESTAMPs is not currently available when using the
312 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
318 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
322 You may distribute this code under the same terms as Perl itself.