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>, sets the
22 limit dialect to C<FIRST X SKIP X> and provides
23 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>.
31 For ODBC support, see L<DBIx::Class::Storage::DBI::ODBC::Firebird>.
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 my @pk = $ident->_pri_cols;
47 my @auto_inc_cols = grep {
48 my $inserting = $args->[0]{$_};
50 ($ident->column_info($_)->{is_auto_increment}
53 (not defined $inserting)
55 (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
60 $args->[1]{returning} = \@auto_inc_cols;
62 $self->_auto_incs([]);
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, $source, @cols) = @_;
94 @auto_incs{ @{ $self->_auto_incs->[0] } } =
95 @{ $self->_auto_incs->[1] };
97 push @result, $auto_incs{$_} for @cols;
102 # this sub stolen from DB2
104 sub _sql_maker_opts {
105 my ( $self, $opts ) = @_;
108 $self->{_sql_maker_opts} = { %$opts };
111 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
115 my ($self, $name) = @_;
117 $self->_get_dbh->do("SAVEPOINT $name");
121 my ($self, $name) = @_;
123 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
127 my ($self, $name) = @_;
129 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
135 my $dbh = $self->_dbh or return 0;
137 local $dbh->{RaiseError} = 1;
140 $dbh->do('select 1 from rdb$database');
146 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
147 # dialect 1 (interbase compat) by default.
150 $self->_set_sql_dialect(3);
153 sub _set_sql_dialect {
155 my $val = shift || 3;
157 my $dsn = $self->_dbi_connect_info->[0];
159 return if ref($dsn) eq 'CODE';
161 if ($dsn !~ /ib_dialect=/) {
162 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
163 my $connected = defined $self->_dbh;
165 $self->ensure_connected if $connected;
169 =head2 connect_call_use_softcommit
173 on_connect_call => 'use_softcommit'
175 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
176 L<DBD::InterBase> C<ib_softcommit> option.
178 You need either this option or C<< disable_sth_caching => 1 >> for
179 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
182 The downside of using this option is that your process will B<NOT> see UPDATEs,
183 INSERTs and DELETEs from other processes for already open statements.
187 sub connect_call_use_softcommit {
190 $self->_dbh->{ib_softcommit} = 1;
193 =head2 connect_call_datetime_setup
197 on_connect_call => 'datetime_setup'
199 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
200 timestamp formats using:
202 $dbh->{ib_time_all} = 'ISO';
204 See L<DBD::InterBase> for more details.
206 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
207 second precision. The full precision is used.
209 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
214 in your Result class.
216 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
218 You will need the L<DateTime::Format::Strptime> module for inflation to work.
220 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
221 precision is not currently available.
225 sub connect_call_datetime_setup {
228 $self->_get_dbh->{ib_time_all} = 'ISO';
231 sub datetime_parser_type {
232 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
235 package # hide from PAUSE
236 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
238 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
239 my $date_format = '%Y-%m-%d';
241 my ($timestamp_parser, $date_parser);
245 require DateTime::Format::Strptime;
246 $timestamp_parser ||= DateTime::Format::Strptime->new(
247 pattern => $timestamp_format,
250 return $timestamp_parser->parse_datetime(shift);
253 sub format_datetime {
255 require DateTime::Format::Strptime;
256 $timestamp_parser ||= DateTime::Format::Strptime->new(
257 pattern => $timestamp_format,
260 return $timestamp_parser->format_datetime(shift);
265 require DateTime::Format::Strptime;
266 $date_parser ||= DateTime::Format::Strptime->new(
267 pattern => $date_format,
270 return $date_parser->parse_datetime(shift);
275 require DateTime::Format::Strptime;
276 $date_parser ||= DateTime::Format::Strptime->new(
277 pattern => $date_format,
280 return $date_parser->format_datetime(shift);
291 with L</connect_call_use_softcommit>, you will not be able to see changes made
292 to data in other processes. If this is an issue, use
293 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
294 workaround for the C<no statement executing> errors, this of course adversely
299 C<last_insert_id> support only works for Firebird versions 2 or greater. To
300 work with earlier versions, we'll need to figure out how to retrieve the bodies
301 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
305 Sub-second precision for TIMESTAMPs is not currently available with ODBC.
311 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
315 You may distribute this code under the same terms as Perl itself.