New $dbh capability handling - allows someone to say
[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';
6298a324 7use List::Util 'first';
ed7ab0f4 8use Try::Tiny;
fd323bf1 9use namespace::clean;
88a8d0fa 10
90489c23 11=head1 NAME
12
13DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS
14
15=head1 DESCRIPTION
16
95570280 17This class implements autoincrements for Firebird using C<RETURNING> as well as
18L<auto_nextval|DBIx::Class::ResultSource/auto_nextval> sets the limit dialect to
19C<FIRST X SKIP X> and provides L<DBIx::Class::InflateColumn::DateTime> support.
90489c23 20
dd2109ee 21You need to use either the
22L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
23L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
47ec67c3 24correctly with this driver. Otherwise you will likely get bizarre error messages
d1fc96c7 25such as C<no statement executing>. The alternative is to use the
26L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
27for long running processes such as under L<Catalyst>.
90489c23 28
c5827074 29To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
30L</connect_call_datetime_setup>.
32323fc2 31
90489c23 32=cut
33
bbdda281 34# set default
35__PACKAGE__->_use_insert_returning (1);
bab40dee 36
e1958268 37sub _sequence_fetch {
38 my ($self, $nextval, $sequence) = @_;
39
40 if ($nextval ne 'nextval') {
41 $self->throw_exception("Can only fetch 'nextval' for a sequence");
42 }
43
44 $self->throw_exception('No sequence to fetch') unless $sequence;
fd323bf1 45
e1958268 46 my ($val) = $self->_get_dbh->selectrow_array(
47'SELECT GEN_ID(' . $self->sql_maker->_quote($sequence) .
48', 1) FROM rdb$database');
49
50 return $val;
fd323bf1 51}
e1958268 52
53sub _dbh_get_autoinc_seq {
54 my ($self, $dbh, $source, $col) = @_;
55
56 my $table_name = $source->from;
57 $table_name = $$table_name if ref $table_name;
58 $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name);
59
60 local $dbh->{LongReadLen} = 100000;
61 local $dbh->{LongTruncOk} = 1;
62
63 my $sth = $dbh->prepare(<<'EOF');
64SELECT t.rdb$trigger_source
65FROM rdb$triggers t
66WHERE t.rdb$relation_name = ?
67AND t.rdb$system_flag = 0 -- user defined
68AND t.rdb$trigger_type = 1 -- BEFORE INSERT
69EOF
70 $sth->execute($table_name);
71
72 while (my ($trigger) = $sth->fetchrow_array) {
73 my @trig_cols = map {
74 /^"([^"]+)/ ? $1 : uc($1)
75 } $trigger =~ /new\.("?\w+"?)/ig;
76
77 my ($quoted, $generator) = $trigger =~
78/(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
79
80 if ($generator) {
81 $generator = uc $generator unless $quoted;
82
83 return $generator
6298a324 84 if first {
e1958268 85 $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
86 } @trig_cols;
87 }
88 }
89
90 return undef;
91}
92
145b2a3d 93# this sub stolen from DB2
88a8d0fa 94
145b2a3d 95sub _sql_maker_opts {
96 my ( $self, $opts ) = @_;
97
98 if ( $opts ) {
99 $self->{_sql_maker_opts} = { %$opts };
100 }
101
102 return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
103}
104
32323fc2 105sub _svp_begin {
106 my ($self, $name) = @_;
107
108 $self->_get_dbh->do("SAVEPOINT $name");
109}
110
111sub _svp_release {
112 my ($self, $name) = @_;
113
114 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
115}
116
117sub _svp_rollback {
118 my ($self, $name) = @_;
119
120 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
121}
122
123sub _ping {
124 my $self = shift;
125
126 my $dbh = $self->_dbh or return 0;
127
128 local $dbh->{RaiseError} = 1;
ecdf1ac8 129 local $dbh->{PrintError} = 0;
32323fc2 130
52b420dd 131 return try {
32323fc2 132 $dbh->do('select 1 from rdb$database');
52b420dd 133 1;
ed7ab0f4 134 } catch {
52b420dd 135 0;
32323fc2 136 };
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