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 preliminary
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.
30 For ODBC support, see L<DBIx::Class::Storage::DBI::ODBC::Firebird>.
32 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
33 L</connect_call_datetime_setup>.
37 sub _prep_for_execute {
39 my ($op, $extra_bind, $ident, $args) = @_;
41 if ($op eq 'insert') {
42 my @pk = $ident->_pri_cols;
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([]);
62 $self->_auto_incs->[0] = \@auto_inc_cols;
66 return $self->next::method(@_);
73 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
75 if ($op eq 'insert' && $self->_auto_incs) {
77 my (@auto_incs) = eval {
78 local $SIG{__WARN__} = sub {};
81 $self->_auto_incs->[1] = \@auto_incs;
85 return wantarray ? ($rv, $sth, @bind) : $rv;
89 my ($self, $source, @cols) = @_;
93 @auto_incs{ @{ $self->_auto_incs->[0] } } =
94 @{ $self->_auto_incs->[1] };
96 push @result, $auto_incs{$_} for @cols;
101 # this sub stolen from DB2
103 sub _sql_maker_opts {
104 my ( $self, $opts ) = @_;
107 $self->{_sql_maker_opts} = { %$opts };
110 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
114 my ($self, $name) = @_;
116 $self->_get_dbh->do("SAVEPOINT $name");
120 my ($self, $name) = @_;
122 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
126 my ($self, $name) = @_;
128 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
134 my $dbh = $self->_dbh or return 0;
136 local $dbh->{RaiseError} = 1;
139 $dbh->do('select 1 from rdb$database');
145 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
146 # dialect 1 (interbase compat) by default.
149 $self->_set_sql_dialect(3);
152 sub _set_sql_dialect {
154 my $val = shift || 3;
156 my $dsn = $self->_dbi_connect_info->[0];
158 return if ref($dsn) eq 'CODE';
160 if ($dsn !~ /ib_dialect=/) {
161 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
162 my $connected = defined $self->_dbh;
164 $self->ensure_connected if $connected;
168 =head2 connect_call_use_softcommit
172 on_connect_call => 'use_softcommit'
174 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
175 L<DBD::InterBase> C<ib_softcommit> option.
177 You need either this option or C<< disable_sth_caching => 1 >> for
178 L<DBIx::Class> code to function correctly.
180 The downside of using this option is that your process will B<NOT> see UPDATEs,
181 INSERTs and DELETEs from other processes for already open statements.
185 sub connect_call_use_softcommit {
188 $self->_dbh->{ib_softcommit} = 1;
191 =head2 connect_call_datetime_setup
195 on_connect_call => 'datetime_setup'
197 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
198 timestamp formats using:
200 $dbh->{ib_time_all} = 'ISO';
202 See L<DBD::InterBase> for more details.
204 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
205 second precision. The full precision is used.
207 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
212 in your Result class.
214 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
216 You will need the L<DateTime::Format::Strptime> module for inflation to work.
218 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
219 precision is not currently available.
223 sub connect_call_datetime_setup {
226 $self->_get_dbh->{ib_time_all} = 'ISO';
229 sub datetime_parser_type {
230 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
233 package # hide from PAUSE
234 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
236 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
237 my $date_format = '%Y-%m-%d';
239 my ($timestamp_parser, $date_parser);
243 require DateTime::Format::Strptime;
244 $timestamp_parser ||= DateTime::Format::Strptime->new(
245 pattern => $timestamp_format,
248 return $timestamp_parser->parse_datetime(shift);
251 sub format_datetime {
253 require DateTime::Format::Strptime;
254 $timestamp_parser ||= DateTime::Format::Strptime->new(
255 pattern => $timestamp_format,
258 return $timestamp_parser->format_datetime(shift);
263 require DateTime::Format::Strptime;
264 $date_parser ||= DateTime::Format::Strptime->new(
265 pattern => $date_format,
268 return $date_parser->parse_datetime(shift);
273 require DateTime::Format::Strptime;
274 $date_parser ||= DateTime::Format::Strptime->new(
275 pattern => $date_format,
278 return $date_parser->format_datetime(shift);
289 with L</connect_call_use_softcommit>, you will not be able to see changes made
290 to data in other processes. If this is an issue, use
291 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching>, this of
292 course adversely affects performance.
296 C<last_insert_id> support only works for Firebird versions 2 or greater. To
297 work with earlier versions, we'll need to figure out how to retrieve the bodies
298 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
304 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
308 You may distribute this code under the same terms as Perl itself.