update POD
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / InterBase.pm
1 package DBIx::Class::Storage::DBI::InterBase;
2
3 # partly stolen from DBIx::Class::Storage::DBI::MSSQL
4
5 use strict;
6 use warnings;
7 use base qw/DBIx::Class::Storage::DBI/;
8 use mro 'c3';
9 use List::Util();
10
11 __PACKAGE__->mk_group_accessors(simple => qw/
12   _auto_incs
13 /);
14
15 =head1 NAME
16
17 DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
18
19 =head1 DESCRIPTION
20
21 This class implements autoincrements for Firebird using C<RETURNING>, sets the
22 limit dialect to C<FIRST X SKIP X> and provides
23 L<DBIx::Class::InflateColumn::DateTime> support.
24
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. Otherwise you will likely get bizarre error messages
29 such as C<no statement executing>.
30
31 For ODBC support, see L<DBIx::Class::Storage::DBI::ODBC::Firebird>.
32
33 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
34 L</connect_call_datetime_setup>.
35
36 =cut
37
38 sub _prep_for_execute {
39   my $self = shift;
40   my ($op, $extra_bind, $ident, $args) = @_;
41
42   if ($op eq 'insert') {
43     my @pk = $ident->_pri_cols;
44     my %pk;
45     @pk{@pk} = ();
46
47     my @auto_inc_cols = grep {
48       my $inserting = $args->[0]{$_};
49
50       ($ident->column_info($_)->{is_auto_increment}
51         || exists $pk{$_})
52       && (
53         (not defined $inserting)
54         ||
55         (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
56       )
57     } $ident->columns;
58
59     if (@auto_inc_cols) {
60       $args->[1]{returning} = \@auto_inc_cols;
61
62       $self->_auto_incs([]);
63       $self->_auto_incs->[0] = \@auto_inc_cols;
64     }
65   }
66
67   return $self->next::method(@_);
68 }
69
70 sub _execute {
71   my $self = shift;
72   my ($op) = @_;
73
74   my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
75
76   if ($op eq 'insert' && $self->_auto_incs) {
77     local $@;
78     my (@auto_incs) = eval {
79       local $SIG{__WARN__} = sub {};
80       $sth->fetchrow_array
81     };
82     $self->_auto_incs->[1] = \@auto_incs;
83     $sth->finish;
84   }
85
86   return wantarray ? ($rv, $sth, @bind) : $rv;
87 }
88
89 sub last_insert_id {
90   my ($self, $source, @cols) = @_;
91   my @result;
92
93   my %auto_incs;
94   @auto_incs{ @{ $self->_auto_incs->[0] } } =
95     @{ $self->_auto_incs->[1] };
96
97   push @result, $auto_incs{$_} for @cols;
98
99   return @result;
100 }
101
102 # this sub stolen from DB2
103
104 sub _sql_maker_opts {
105   my ( $self, $opts ) = @_;
106
107   if ( $opts ) {
108     $self->{_sql_maker_opts} = { %$opts };
109   }
110
111   return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
112 }
113
114 sub _svp_begin {
115     my ($self, $name) = @_;
116
117     $self->_get_dbh->do("SAVEPOINT $name");
118 }
119
120 sub _svp_release {
121     my ($self, $name) = @_;
122
123     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
124 }
125
126 sub _svp_rollback {
127     my ($self, $name) = @_;
128
129     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
130 }
131
132 sub _ping {
133   my $self = shift;
134
135   my $dbh = $self->_dbh or return 0;
136
137   local $dbh->{RaiseError} = 1;
138
139   eval {
140     $dbh->do('select 1 from rdb$database');
141   };
142
143   return $@ ? 0 : 1;
144 }
145
146 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
147 # dialect 1 (interbase compat) by default.
148 sub _init {
149   my $self = shift;
150   $self->_set_sql_dialect(3);
151 }
152
153 sub _set_sql_dialect {
154   my $self = shift;
155   my $val  = shift || 3;
156
157   my $dsn = $self->_dbi_connect_info->[0];
158
159   return if ref($dsn) eq 'CODE';
160
161   if ($dsn !~ /ib_dialect=/) {
162     $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
163     my $connected = defined $self->_dbh;
164     $self->disconnect;
165     $self->ensure_connected if $connected;
166   }
167 }
168
169 =head2 connect_call_use_softcommit
170
171 Used as:
172
173   on_connect_call => 'use_softcommit'
174
175 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
176 L<DBD::InterBase> C<ib_softcommit> option.
177
178 You need either this option or C<< disable_sth_caching => 1 >> for
179 L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
180 executing> errors.)
181
182 The downside of using this option is that your process will B<NOT> see UPDATEs,
183 INSERTs and DELETEs from other processes for already open statements.
184
185 =cut
186
187 sub connect_call_use_softcommit {
188   my $self = shift;
189
190   $self->_dbh->{ib_softcommit} = 1;
191 }
192
193 =head2 connect_call_datetime_setup
194
195 Used as:
196
197   on_connect_call => 'datetime_setup'
198
199 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
200 timestamp formats using:
201
202   $dbh->{ib_time_all} = 'ISO';
203
204 See L<DBD::InterBase> for more details.
205
206 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
207 second precision. The full precision is used.
208
209 The C<DATE> data type stores the date portion only, and it B<MUST> be declared
210 with:
211
212   data_type => 'date'
213
214 in your Result class.
215
216 Timestamp columns can be declared with either C<datetime> or C<timestamp>.
217
218 You will need the L<DateTime::Format::Strptime> module for inflation to work.
219
220 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
221 precision is not currently available.
222
223 =cut
224
225 sub connect_call_datetime_setup {
226   my $self = shift;
227
228   $self->_get_dbh->{ib_time_all} = 'ISO';
229 }
230
231 sub datetime_parser_type {
232   'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
233 }
234
235 package # hide from PAUSE
236   DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
237
238 my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
239 my $date_format      = '%Y-%m-%d';
240
241 my ($timestamp_parser, $date_parser);
242
243 sub parse_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->parse_datetime(shift);
251 }
252
253 sub format_datetime {
254   shift;
255   require DateTime::Format::Strptime;
256   $timestamp_parser ||= DateTime::Format::Strptime->new(
257     pattern  => $timestamp_format,
258     on_error => 'croak',
259   );
260   return $timestamp_parser->format_datetime(shift);
261 }
262
263 sub parse_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->parse_datetime(shift);
271 }
272
273 sub format_date {
274   shift;
275   require DateTime::Format::Strptime;
276   $date_parser ||= DateTime::Format::Strptime->new(
277     pattern  => $date_format,
278     on_error => 'croak',
279   );
280   return $date_parser->format_datetime(shift);
281 }
282
283 1;
284
285 =head1 CAVEATS
286
287 =over 4
288
289 =item *
290
291 with L</connect_call_use_softcommit>, you will not be able to see changes made
292 to data in other processes. If this is an issue, use
293 L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
294 workaround for the C<no statement executing> errors, this of course adversely
295 affects performance.
296
297 =item *
298
299 C<last_insert_id> support only works for Firebird versions 2 or greater. To
300 work with earlier versions, we'll need to figure out how to retrieve the bodies
301 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
302
303 =item *
304
305 Sub-second precision for TIMESTAMPs is not currently available with ODBC.
306
307 =back
308
309 =head1 AUTHOR
310
311 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
312
313 =head1 LICENSE
314
315 You may distribute this code under the same terms as Perl itself.
316
317 =cut