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