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