3664d9c90d03685bfde419f4bb30026e8d4a95a6
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / InterBase.pm
1 package DBIx::Class::Storage::DBI::InterBase;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Storage::DBI/;
6 use mro 'c3';
7 use List::Util();
8
9 =head1 NAME
10
11 DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
12
13 =head1 DESCRIPTION
14
15 This class implements autoincrements for Firebird using C<RETURNING> as well as
16 L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to
17 C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support.
18
19 You need to use either the
20 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
21 L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
22 correctly with this driver. Otherwise you will likely get bizarre error messages
23 such as C<no statement executing>. The alternative is to use the
24 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
25 for long running processes such as under L<Catalyst>.
26
27 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
28 L</connect_call_datetime_setup>.
29
30 =cut
31
32 sub can_insert_returning { 1 }
33
34 sub _sequence_fetch {
35   my ($self, $nextval, $sequence) = @_;
36
37   if ($nextval ne 'nextval') {
38     $self->throw_exception("Can only fetch 'nextval' for a sequence");
39   }
40
41   $self->throw_exception('No sequence to fetch') unless $sequence;
42   
43   my ($val) = $self->_get_dbh->selectrow_array(
44 'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) .
45 ', 1) FROM rdb$database');
46
47   return $val;
48
49
50 sub _dbh_get_autoinc_seq {
51   my ($self, $dbh, $source, $col) = @_;
52
53   my $table_name = $source->from;
54   $table_name    = $$table_name if ref $table_name;
55   $table_name    = $self->sql_maker->quote_char ? $table_name : uc($table_name);
56
57   local $dbh->{LongReadLen} = 100000;
58   local $dbh->{LongTruncOk} = 1;
59
60   my $sth = $dbh->prepare(<<'EOF');
61 SELECT t.rdb$trigger_source
62 FROM rdb$triggers t
63 WHERE t.rdb$relation_name = ?
64 AND t.rdb$system_flag = 0 -- user defined
65 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
66 EOF
67   $sth->execute($table_name);
68
69   while (my ($trigger) = $sth->fetchrow_array) {
70     my @trig_cols = map {
71       /^"([^"]+)/ ? $1 : uc($1)
72     } $trigger =~ /new\.("?\w+"?)/ig;
73
74     my ($quoted, $generator) = $trigger =~
75 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
76
77     if ($generator) {
78       $generator = uc $generator unless $quoted;
79
80       return $generator
81         if List::Util::first {
82           $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
83         } @trig_cols;
84     }
85   }
86
87   return undef;
88 }
89
90 # this sub stolen from DB2
91
92 sub _sql_maker_opts {
93   my ( $self, $opts ) = @_;
94
95   if ( $opts ) {
96     $self->{_sql_maker_opts} = { %$opts };
97   }
98
99   return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
100 }
101
102 sub _svp_begin {
103     my ($self, $name) = @_;
104
105     $self->_get_dbh->do("SAVEPOINT $name");
106 }
107
108 sub _svp_release {
109     my ($self, $name) = @_;
110
111     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
112 }
113
114 sub _svp_rollback {
115     my ($self, $name) = @_;
116
117     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
118 }
119
120 sub _ping {
121   my $self = shift;
122
123   my $dbh = $self->_dbh or return 0;
124
125   local $dbh->{RaiseError} = 1;
126   local $dbh->{PrintError} = 0;
127
128   eval {
129     $dbh->do('select 1 from rdb$database');
130   };
131
132   return $@ ? 0 : 1;
133 }
134
135 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
136 # dialect 1 (interbase compat) by default.
137 sub _init {
138   my $self = shift;
139   $self->_set_sql_dialect(3);
140 }
141
142 sub _set_sql_dialect {
143   my $self = shift;
144   my $val  = shift || 3;
145
146   my $dsn = $self->_dbi_connect_info->[0];
147
148   return if ref($dsn) eq 'CODE';
149
150   if ($dsn !~ /ib_dialect=/) {
151     $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
152     my $connected = defined $self->_dbh;
153     $self->disconnect;
154     $self->ensure_connected if $connected;
155   }
156 }
157
158 =head2 connect_call_use_softcommit
159
160 Used as:
161
162   on_connect_call => 'use_softcommit'
163
164 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
165 L<DBD::InterBase> C<ib_softcommit> option.
166
167 You need either this option or C<< disable_sth_caching => 1 >> for
168 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
169 executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
170 driver.
171
172 The downside of using this option is that your process will B<NOT> see UPDATEs,
173 INSERTs and DELETEs from other processes for already open statements.
174
175 =cut
176
177 sub connect_call_use_softcommit {
178   my $self = shift;
179
180   $self->_dbh->{ib_softcommit} = 1;
181 }
182
183 =head2 connect_call_datetime_setup
184
185 Used as:
186
187   on_connect_call => 'datetime_setup'
188
189 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
190 timestamp formats using:
191
192   $dbh->{ib_time_all} = 'ISO';
193
194 See L<DBD::InterBase> for more details.
195
196 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
197 second precision. The full precision is used.
198
199 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
200 with:
201
202   data_type => 'date'
203
204 in your Result class.
205
206 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
207
208 You will need the L<DateTime::Format::Strptime> module for inflation to work.
209
210 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
211 precision is not currently available.
212
213 =cut
214
215 sub connect_call_datetime_setup {
216   my $self = shift;
217
218   $self->_get_dbh->{ib_time_all} = 'ISO';
219 }
220
221 sub datetime_parser_type {
222   'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
223 }
224
225 package # hide from PAUSE
226   DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
227
228 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
229 my $date_format      = '%Y-%m-%d';
230
231 my ($timestamp_parser, $date_parser);
232
233 sub parse_datetime {
234   shift;
235   require DateTime::Format::Strptime;
236   $timestamp_parser ||= DateTime::Format::Strptime->new(
237     pattern  => $timestamp_format,
238     on_error => 'croak',
239   );
240   return $timestamp_parser->parse_datetime(shift);
241 }
242
243 sub format_datetime {
244   shift;
245   require DateTime::Format::Strptime;
246   $timestamp_parser ||= DateTime::Format::Strptime->new(
247     pattern  => $timestamp_format,
248     on_error => 'croak',
249   );
250   return $timestamp_parser->format_datetime(shift);
251 }
252
253 sub parse_date {
254   shift;
255   require DateTime::Format::Strptime;
256   $date_parser ||= DateTime::Format::Strptime->new(
257     pattern  => $date_format,
258     on_error => 'croak',
259   );
260   return $date_parser->parse_datetime(shift);
261 }
262
263 sub format_date {
264   shift;
265   require DateTime::Format::Strptime;
266   $date_parser ||= DateTime::Format::Strptime->new(
267     pattern  => $date_format,
268     on_error => 'croak',
269   );
270   return $date_parser->format_datetime(shift);
271 }
272
273 1;
274
275 =head1 CAVEATS
276
277 =over 4
278
279 =item *
280
281 with L</connect_call_use_softcommit>, you will not be able to see changes made
282 to data in other processes. If this is an issue, use
283 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
284 workaround for the C<no statement executing> errors, this of course adversely
285 affects performance.
286
287 Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
288
289 =item *
290
291 C<last_insert_id> support by default only works for Firebird versions 2 or
292 greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
293 work with earlier versions.
294
295 =item *
296
297 Sub-second precision for TIMESTAMPs is not currently available when using the
298 L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
299
300 =back
301
302 =head1 AUTHOR
303
304 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
305
306 =head1 LICENSE
307
308 You may distribute this code under the same terms as Perl itself.
309
310 =cut