1 package DBIx::Class::Storage::DBI::Firebird::Common;
5 use base qw/DBIx::Class::Storage::DBI/;
7 use List::Util 'first';
12 DBIx::Class::Storage::DBI::Firebird::Common - Driver Base Class 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>, savepoints and server
23 __PACKAGE__->_use_insert_returning (1);
24 __PACKAGE__->sql_limit_dialect ('FirstSkip');
25 __PACKAGE__->sql_quote_char ('"');
27 __PACKAGE__->datetime_parser_type(
28 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
36 my ($self, $nextval, $sequence) = @_;
38 $self->throw_exception("Can only fetch 'nextval' for a sequence")
39 if $nextval !~ /^nextval$/i;
41 $self->throw_exception('No sequence to fetch') unless $sequence;
43 my ($val) = $self->_get_dbh->selectrow_array(sprintf
44 'SELECT GEN_ID(%s, 1) FROM rdb$database',
45 $self->sql_maker->_quote($sequence)
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($_) }
73 $trigger =~ /new\.("?\w+"?)/ig
76 my ($quoted, $generator) = $trigger =~
77 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
80 $generator = uc $generator unless $quoted;
84 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
93 my ($self, $name) = @_;
95 $self->_dbh->do("SAVEPOINT $name");
98 sub _exec_svp_release {
99 my ($self, $name) = @_;
101 $self->_dbh->do("RELEASE SAVEPOINT $name");
104 sub _exec_svp_rollback {
105 my ($self, $name) = @_;
107 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
110 # http://www.firebirdfaq.org/faq223/
111 sub _get_server_version {
114 return $self->_get_dbh->selectrow_array(q{
115 SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') FROM rdb$database
119 package # hide from PAUSE
120 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
122 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
123 my $date_format = '%Y-%m-%d';
125 my ($timestamp_parser, $date_parser);
129 require DateTime::Format::Strptime;
130 $timestamp_parser ||= DateTime::Format::Strptime->new(
131 pattern => $timestamp_format,
134 return $timestamp_parser->parse_datetime(shift);
137 sub format_datetime {
139 require DateTime::Format::Strptime;
140 $timestamp_parser ||= DateTime::Format::Strptime->new(
141 pattern => $timestamp_format,
144 return $timestamp_parser->format_datetime(shift);
149 require DateTime::Format::Strptime;
150 $date_parser ||= DateTime::Format::Strptime->new(
151 pattern => $date_format,
154 return $date_parser->parse_datetime(shift);
159 require DateTime::Format::Strptime;
160 $date_parser ||= DateTime::Format::Strptime->new(
161 pattern => $date_format,
164 return $date_parser->format_datetime(shift);
173 C<last_insert_id> support by default only works for Firebird versions 2 or
174 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
175 work with earlier versions.
179 =head1 FURTHER QUESTIONS?
181 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
183 =head1 COPYRIGHT AND LICENSE
185 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
186 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
187 redistribute it and/or modify it under the same terms as the
188 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.