Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Firebird / Common.pm
CommitLineData
e46df41a 1package DBIx::Class::Storage::DBI::Firebird::Common;
2
3use strict;
4use warnings;
5use base qw/DBIx::Class::Storage::DBI/;
6use mro 'c3';
e46df41a 7
8=head1 NAME
9
10DBIx::Class::Storage::DBI::Firebird::Common - Driver Base Class for the Firebird RDBMS
11
12=head1 DESCRIPTION
13
14This class implements autoincrements for Firebird using C<RETURNING> as well as
15L<auto_nextval|DBIx::Class::ResultSource/auto_nextval>, savepoints and server
16version detection.
17
18=cut
19
20# set default
21__PACKAGE__->_use_insert_returning (1);
22__PACKAGE__->sql_limit_dialect ('FirstSkip');
23__PACKAGE__->sql_quote_char ('"');
24
e5a62c46 25__PACKAGE__->datetime_parser_type(
26 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
27);
28
f7370154 29sub sqlt_type {
30 return 'Firebird';
31}
32
e46df41a 33sub _sequence_fetch {
34 my ($self, $nextval, $sequence) = @_;
35
36 $self->throw_exception("Can only fetch 'nextval' for a sequence")
37 if $nextval !~ /^nextval$/i;
38
39 $self->throw_exception('No sequence to fetch') unless $sequence;
40
41 my ($val) = $self->_get_dbh->selectrow_array(sprintf
42 'SELECT GEN_ID(%s, 1) FROM rdb$database',
43 $self->sql_maker->_quote($sequence)
44 );
45
46 return $val;
47}
48
49sub _dbh_get_autoinc_seq {
50 my ($self, $dbh, $source, $col) = @_;
51
52 my $table_name = $source->from;
53 $table_name = $$table_name if ref $table_name;
54 $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name);
55
56 local $dbh->{LongReadLen} = 100000;
7db939de 57
58 # FIXME - this is likely *WRONG*
e46df41a 59 local $dbh->{LongTruncOk} = 1;
60
61 my $sth = $dbh->prepare(<<'EOF');
62SELECT t.rdb$trigger_source
63FROM rdb$triggers t
64WHERE t.rdb$relation_name = ?
65AND t.rdb$system_flag = 0 -- user defined
66AND t.rdb$trigger_type = 1 -- BEFORE INSERT
67EOF
68 $sth->execute($table_name);
69
70 while (my ($trigger) = $sth->fetchrow_array) {
d77ee505 71 my @trig_cols = map
72 { /^"([^"]+)/ ? $1 : uc($_) }
73 $trigger =~ /new\.("?\w+"?)/ig
74 ;
e46df41a 75
76 my ($quoted, $generator) = $trigger =~
77/(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
78
79 if ($generator) {
80 $generator = uc $generator unless $quoted;
81
82 return $generator
87b12551 83 if grep {
e46df41a 84 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
85 } @trig_cols;
86 }
87 }
88
89 return undef;
90}
91
90d7422f 92sub _exec_svp_begin {
e46df41a 93 my ($self, $name) = @_;
94
95 $self->_dbh->do("SAVEPOINT $name");
96}
97
90d7422f 98sub _exec_svp_release {
e46df41a 99 my ($self, $name) = @_;
100
101 $self->_dbh->do("RELEASE SAVEPOINT $name");
102}
103
90d7422f 104sub _exec_svp_rollback {
e46df41a 105 my ($self, $name) = @_;
106
107 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
108}
109
110# http://www.firebirdfaq.org/faq223/
111sub _get_server_version {
112 my $self = shift;
113
114 return $self->_get_dbh->selectrow_array(q{
115SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') FROM rdb$database
116 });
117}
118
e5a62c46 119package # hide from PAUSE
120 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
121
122my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
123my $date_format = '%Y-%m-%d';
124
125my ($timestamp_parser, $date_parser);
126
127sub parse_datetime {
128 shift;
129 require DateTime::Format::Strptime;
130 $timestamp_parser ||= DateTime::Format::Strptime->new(
131 pattern => $timestamp_format,
132 on_error => 'croak',
133 );
134 return $timestamp_parser->parse_datetime(shift);
135}
136
137sub format_datetime {
138 shift;
139 require DateTime::Format::Strptime;
140 $timestamp_parser ||= DateTime::Format::Strptime->new(
141 pattern => $timestamp_format,
142 on_error => 'croak',
143 );
144 return $timestamp_parser->format_datetime(shift);
145}
146
147sub parse_date {
148 shift;
149 require DateTime::Format::Strptime;
150 $date_parser ||= DateTime::Format::Strptime->new(
151 pattern => $date_format,
152 on_error => 'croak',
153 );
154 return $date_parser->parse_datetime(shift);
155}
156
157sub format_date {
158 shift;
159 require DateTime::Format::Strptime;
160 $date_parser ||= DateTime::Format::Strptime->new(
161 pattern => $date_format,
162 on_error => 'croak',
163 );
164 return $date_parser->format_datetime(shift);
165}
166
e46df41a 167=head1 CAVEATS
168
169=over 4
170
171=item *
172
173C<last_insert_id> support by default only works for Firebird versions 2 or
174greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
175work with earlier versions.
176
177=back
178
a2bd3796 179=head1 FURTHER QUESTIONS?
e46df41a 180
a2bd3796 181Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
e46df41a 182
a2bd3796 183=head1 COPYRIGHT AND LICENSE
e46df41a 184
a2bd3796 185This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
186by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
187redistribute it and/or modify it under the same terms as the
188L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
e46df41a 189
190=cut
a2bd3796 191
1921;
193
e46df41a 194# vim:sts=2 sw=2: