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