move warning suppression into ::DBI::InterBase
[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();
8
90489c23 9=head1 NAME
10
11DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
12
13=head1 DESCRIPTION
14
95570280 15This class implements autoincrements for Firebird using C<RETURNING> as well as
16L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to
17C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support.
90489c23 18
dd2109ee 19You need to use either the
20L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
21L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
47ec67c3 22correctly with this driver. Otherwise you will likely get bizarre error messages
d1fc96c7 23such as C<no statement executing>. The alternative is to use the
24L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
25for long running processes such as under L<Catalyst>.
90489c23 26
c5827074 27To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
28L</connect_call_datetime_setup>.
32323fc2 29
90489c23 30=cut
31
bab40dee 32sub can_insert_returning { 1 }
33
e1958268 34sub _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
50sub _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');
61SELECT t.rdb$trigger_source
62FROM rdb$triggers t
63WHERE t.rdb$relation_name = ?
64AND t.rdb$system_flag = 0 -- user defined
65AND t.rdb$trigger_type = 1 -- BEFORE INSERT
66EOF
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
145b2a3d 90# this sub stolen from DB2
88a8d0fa 91
145b2a3d 92sub _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
32323fc2 102sub _svp_begin {
103 my ($self, $name) = @_;
104
105 $self->_get_dbh->do("SAVEPOINT $name");
106}
107
108sub _svp_release {
109 my ($self, $name) = @_;
110
111 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
112}
113
114sub _svp_rollback {
115 my ($self, $name) = @_;
116
117 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
118}
119
120sub _ping {
121 my $self = shift;
122
123 my $dbh = $self->_dbh or return 0;
124
125 local $dbh->{RaiseError} = 1;
ecdf1ac8 126 local $dbh->{PrintError} = 0;
32323fc2 127
128 eval {
129 $dbh->do('select 1 from rdb$database');
130 };
131
132 return $@ ? 0 : 1;
133}
134
9633951d 135# We want dialect 3 for new features and quoting to work, DBD::InterBase uses
136# dialect 1 (interbase compat) by default.
137sub _init {
138 my $self = shift;
139 $self->_set_sql_dialect(3);
140}
141
142sub _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
8f0a1e07 158sub _populate_server_info {
159 my $self = shift;
160
161 local $SIG{__WARN__} = sub {}; # silence warning due to bug in DBD::InterBase
162
163 return $self->next::method(@_);
164}
165
dd2109ee 166=head2 connect_call_use_softcommit
167
168Used as:
169
170 on_connect_call => 'use_softcommit'
171
172In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
173L<DBD::InterBase> C<ib_softcommit> option.
174
175You need either this option or C<< disable_sth_caching => 1 >> for
47ec67c3 176L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
d1fc96c7 177executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
178driver.
dd2109ee 179
180The downside of using this option is that your process will B<NOT> see UPDATEs,
181INSERTs and DELETEs from other processes for already open statements.
182
183=cut
184
185sub connect_call_use_softcommit {
a499b173 186 my $self = shift;
187
188 $self->_dbh->{ib_softcommit} = 1;
a499b173 189}
190
32323fc2 191=head2 connect_call_datetime_setup
9cd0b325 192
32323fc2 193Used as:
9cd0b325 194
32323fc2 195 on_connect_call => 'datetime_setup'
196
f0f8ac86 197In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
198timestamp formats using:
32323fc2 199
200 $dbh->{ib_time_all} = 'ISO';
201
202See L<DBD::InterBase> for more details.
203
204The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
205second precision. The full precision is used.
206
c5827074 207The C<DATE> data type stores the date portion only, and it B<MUST> be declared
208with:
209
210 data_type => 'date'
211
212in your Result class.
213
214Timestamp columns can be declared with either C<datetime> or C<timestamp>.
215
32323fc2 216You will need the L<DateTime::Format::Strptime> module for inflation to work.
217
218For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
219precision is not currently available.
220
221=cut
222
223sub connect_call_datetime_setup {
224 my $self = shift;
225
226 $self->_get_dbh->{ib_time_all} = 'ISO';
9cd0b325 227}
228
c5827074 229sub datetime_parser_type {
230 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
231}
32323fc2 232
c5827074 233package # hide from PAUSE
234 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
32323fc2 235
c5827074 236my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
237my $date_format = '%Y-%m-%d';
238
239my ($timestamp_parser, $date_parser);
240
241sub parse_datetime {
242 shift;
243 require DateTime::Format::Strptime;
244 $timestamp_parser ||= DateTime::Format::Strptime->new(
245 pattern => $timestamp_format,
246 on_error => 'croak',
247 );
248 return $timestamp_parser->parse_datetime(shift);
249}
250
251sub format_datetime {
252 shift;
253 require DateTime::Format::Strptime;
254 $timestamp_parser ||= DateTime::Format::Strptime->new(
255 pattern => $timestamp_format,
256 on_error => 'croak',
257 );
258 return $timestamp_parser->format_datetime(shift);
259}
260
261sub parse_date {
262 shift;
263 require DateTime::Format::Strptime;
264 $date_parser ||= DateTime::Format::Strptime->new(
265 pattern => $date_format,
266 on_error => 'croak',
267 );
268 return $date_parser->parse_datetime(shift);
269}
270
271sub format_date {
272 shift;
273 require DateTime::Format::Strptime;
274 $date_parser ||= DateTime::Format::Strptime->new(
275 pattern => $date_format,
32323fc2 276 on_error => 'croak',
277 );
c5827074 278 return $date_parser->format_datetime(shift);
9cd0b325 279}
280
145b2a3d 2811;
90489c23 282
283=head1 CAVEATS
284
285=over 4
286
287=item *
288
dd2109ee 289with L</connect_call_use_softcommit>, you will not be able to see changes made
290to data in other processes. If this is an issue, use
47ec67c3 291L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
292workaround for the C<no statement executing> errors, this of course adversely
293affects performance.
dd2109ee 294
d1fc96c7 295Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
296
dd2109ee 297=item *
298
e1958268 299C<last_insert_id> support by default only works for Firebird versions 2 or
300greater, L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> however should
301work with earlier versions.
90489c23 302
47ec67c3 303=item *
304
d1fc96c7 305Sub-second precision for TIMESTAMPs is not currently available when using the
306L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
47ec67c3 307
90489c23 308=back
309
310=head1 AUTHOR
311
312See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
313
314=head1 LICENSE
315
316You may distribute this code under the same terms as Perl itself.
317
318=cut