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 For ODBC support, see L<DBIx::Class::Storage::DBI::ODBC::Firebird>.
27 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
28 L</connect_call_datetime_setup>.
32 sub _prep_for_execute {
34 my ($op, $extra_bind, $ident, $args) = @_;
36 if ($op eq 'insert') {
37 my @pk = $ident->primary_columns;
41 my @auto_inc_cols = grep {
42 my $inserting = $args->[0]{$_};
44 ($ident->column_info($_)->{is_auto_increment}
47 (not defined $inserting)
49 (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
54 $args->[1]{returning} = \@auto_inc_cols;
56 $self->_auto_incs([]);
57 $self->_auto_incs->[0] = \@auto_inc_cols;
61 return $self->next::method(@_);
68 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
70 if ($op eq 'insert' && $self->_auto_incs) {
72 my (@auto_incs) = eval {
73 local $SIG{__WARN__} = sub {};
76 $self->_auto_incs->[1] = \@auto_incs;
80 return wantarray ? ($rv, $sth, @bind) : $rv;
84 my ($self, $source, @cols) = @_;
88 @auto_incs{ @{ $self->_auto_incs->[0] } } =
89 @{ $self->_auto_incs->[1] };
91 push @result, $auto_incs{$_} for @cols;
96 # this sub stolen from DB2
99 my ( $self, $opts ) = @_;
102 $self->{_sql_maker_opts} = { %$opts };
105 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
109 my ($self, $name) = @_;
111 $self->_get_dbh->do("SAVEPOINT $name");
115 my ($self, $name) = @_;
117 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
121 my ($self, $name) = @_;
123 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
129 my $dbh = $self->_dbh or return 0;
131 local $dbh->{RaiseError} = 1;
134 $dbh->do('select 1 from rdb$database');
140 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
141 # dialect 1 (interbase compat) by default.
144 $self->_set_sql_dialect(3);
147 sub _set_sql_dialect {
149 my $val = shift || 3;
151 my $dsn = $self->_dbi_connect_info->[0];
153 return if ref($dsn) eq 'CODE';
155 if ($dsn !~ /ib_dialect=/) {
156 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
157 my $connected = defined $self->_dbh;
159 $self->ensure_connected if $connected;
163 # softcommit makes savepoints work
164 sub _run_connection_actions {
167 $self->_dbh->{ib_softcommit} = 1;
169 $self->next::method(@_);
172 =head2 connect_call_datetime_setup
176 on_connect_call => 'datetime_setup'
178 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
179 timestamp formats using:
181 $dbh->{ib_time_all} = 'ISO';
183 See L<DBD::InterBase> for more details.
185 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
186 second precision. The full precision is used.
188 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
193 in your Result class.
195 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
197 You will need the L<DateTime::Format::Strptime> module for inflation to work.
199 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
200 precision is not currently available.
204 sub connect_call_datetime_setup {
207 $self->_get_dbh->{ib_time_all} = 'ISO';
210 sub datetime_parser_type {
211 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
214 package # hide from PAUSE
215 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
217 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
218 my $date_format = '%Y-%m-%d';
220 my ($timestamp_parser, $date_parser);
224 require DateTime::Format::Strptime;
225 $timestamp_parser ||= DateTime::Format::Strptime->new(
226 pattern => $timestamp_format,
229 return $timestamp_parser->parse_datetime(shift);
232 sub format_datetime {
234 require DateTime::Format::Strptime;
235 $timestamp_parser ||= DateTime::Format::Strptime->new(
236 pattern => $timestamp_format,
239 return $timestamp_parser->format_datetime(shift);
244 require DateTime::Format::Strptime;
245 $date_parser ||= DateTime::Format::Strptime->new(
246 pattern => $date_format,
249 return $date_parser->parse_datetime(shift);
254 require DateTime::Format::Strptime;
255 $date_parser ||= DateTime::Format::Strptime->new(
256 pattern => $date_format,
259 return $date_parser->format_datetime(shift);
270 C<last_insert_id> support only works for Firebird versions 2 or greater. To
271 work with earlier versions, we'll need to figure out how to retrieve the bodies
272 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
278 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
282 You may distribute this code under the same terms as Perl itself.