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