Commit | Line | Data |
88a8d0fa |
1 | package DBIx::Class::Storage::DBI::InterBase; |
2 | |
145b2a3d |
3 | # partly stolen from DBIx::Class::Storage::DBI::MSSQL |
88a8d0fa |
4 | |
5 | use strict; |
6 | use warnings; |
637d2fae |
7 | use base qw/DBIx::Class::Storage::DBI/; |
88a8d0fa |
8 | use mro 'c3'; |
88a8d0fa |
9 | use List::Util(); |
10 | |
11 | __PACKAGE__->mk_group_accessors(simple => qw/ |
2680ffe5 |
12 | _auto_incs |
88a8d0fa |
13 | /); |
14 | |
90489c23 |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS |
18 | |
19 | =head1 DESCRIPTION |
20 | |
21 | This class implements autoincrements for Firebird using C<RETURNING>, sets the |
47ec67c3 |
22 | limit dialect to C<FIRST X SKIP X> and provides |
90489c23 |
23 | L<DBIx::Class::InflateColumn::DateTime> support. |
24 | |
dd2109ee |
25 | You need to use either the |
26 | L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or |
27 | L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function |
47ec67c3 |
28 | correctly with this driver. Otherwise you will likely get bizarre error messages |
29 | such as C<no statement executing>. |
dd2109ee |
30 | |
90489c23 |
31 | For ODBC support, see L<DBIx::Class::Storage::DBI::ODBC::Firebird>. |
32 | |
c5827074 |
33 | To turn on L<DBIx::Class::InflateColumn::DateTime> support, see |
34 | L</connect_call_datetime_setup>. |
32323fc2 |
35 | |
90489c23 |
36 | =cut |
37 | |
88a8d0fa |
38 | sub _prep_for_execute { |
39 | my $self = shift; |
40 | my ($op, $extra_bind, $ident, $args) = @_; |
41 | |
88a8d0fa |
42 | if ($op eq 'insert') { |
1f55ac94 |
43 | my @pk = $ident->_pri_cols; |
6e8d182b |
44 | my %pk; |
45 | @pk{@pk} = (); |
46 | |
1ae0a36c |
47 | my @auto_inc_cols = grep { |
48 | my $inserting = $args->[0]{$_}; |
145b2a3d |
49 | |
6e8d182b |
50 | ($ident->column_info($_)->{is_auto_increment} |
51 | || exists $pk{$_}) |
52 | && ( |
1ae0a36c |
53 | (not defined $inserting) |
54 | || |
2680ffe5 |
55 | (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i) |
1ae0a36c |
56 | ) |
57 | } $ident->columns; |
88a8d0fa |
58 | |
145b2a3d |
59 | if (@auto_inc_cols) { |
1696e04f |
60 | $args->[1]{returning} = \@auto_inc_cols; |
145b2a3d |
61 | |
2680ffe5 |
62 | $self->_auto_incs([]); |
63 | $self->_auto_incs->[0] = \@auto_inc_cols; |
145b2a3d |
64 | } |
88a8d0fa |
65 | } |
66 | |
1696e04f |
67 | return $self->next::method(@_); |
1ae0a36c |
68 | } |
69 | |
88a8d0fa |
70 | sub _execute { |
71 | my $self = shift; |
72 | my ($op) = @_; |
73 | |
74 | my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_); |
75 | |
2680ffe5 |
76 | if ($op eq 'insert' && $self->_auto_incs) { |
88a8d0fa |
77 | local $@; |
145b2a3d |
78 | my (@auto_incs) = eval { |
79 | local $SIG{__WARN__} = sub {}; |
80 | $sth->fetchrow_array |
81 | }; |
2680ffe5 |
82 | $self->_auto_incs->[1] = \@auto_incs; |
88a8d0fa |
83 | $sth->finish; |
84 | } |
85 | |
86 | return wantarray ? ($rv, $sth, @bind) : $rv; |
87 | } |
88 | |
145b2a3d |
89 | sub last_insert_id { |
90 | my ($self, $source, @cols) = @_; |
91 | my @result; |
88a8d0fa |
92 | |
145b2a3d |
93 | my %auto_incs; |
2680ffe5 |
94 | @auto_incs{ @{ $self->_auto_incs->[0] } } = |
95 | @{ $self->_auto_incs->[1] }; |
145b2a3d |
96 | |
97 | push @result, $auto_incs{$_} for @cols; |
98 | |
99 | return @result; |
100 | } |
101 | |
102 | # this sub stolen from DB2 |
88a8d0fa |
103 | |
145b2a3d |
104 | sub _sql_maker_opts { |
105 | my ( $self, $opts ) = @_; |
106 | |
107 | if ( $opts ) { |
108 | $self->{_sql_maker_opts} = { %$opts }; |
109 | } |
110 | |
111 | return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} }; |
112 | } |
113 | |
32323fc2 |
114 | sub _svp_begin { |
115 | my ($self, $name) = @_; |
116 | |
117 | $self->_get_dbh->do("SAVEPOINT $name"); |
118 | } |
119 | |
120 | sub _svp_release { |
121 | my ($self, $name) = @_; |
122 | |
123 | $self->_get_dbh->do("RELEASE SAVEPOINT $name"); |
124 | } |
125 | |
126 | sub _svp_rollback { |
127 | my ($self, $name) = @_; |
128 | |
129 | $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name") |
130 | } |
131 | |
132 | sub _ping { |
133 | my $self = shift; |
134 | |
135 | my $dbh = $self->_dbh or return 0; |
136 | |
137 | local $dbh->{RaiseError} = 1; |
138 | |
139 | eval { |
140 | $dbh->do('select 1 from rdb$database'); |
141 | }; |
142 | |
143 | return $@ ? 0 : 1; |
144 | } |
145 | |
9633951d |
146 | # We want dialect 3 for new features and quoting to work, DBD::InterBase uses |
147 | # dialect 1 (interbase compat) by default. |
148 | sub _init { |
149 | my $self = shift; |
150 | $self->_set_sql_dialect(3); |
151 | } |
152 | |
153 | sub _set_sql_dialect { |
154 | my $self = shift; |
155 | my $val = shift || 3; |
156 | |
157 | my $dsn = $self->_dbi_connect_info->[0]; |
158 | |
159 | return if ref($dsn) eq 'CODE'; |
160 | |
161 | if ($dsn !~ /ib_dialect=/) { |
162 | $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val"; |
163 | my $connected = defined $self->_dbh; |
164 | $self->disconnect; |
165 | $self->ensure_connected if $connected; |
166 | } |
167 | } |
168 | |
dd2109ee |
169 | =head2 connect_call_use_softcommit |
170 | |
171 | Used as: |
172 | |
173 | on_connect_call => 'use_softcommit' |
174 | |
175 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the |
176 | L<DBD::InterBase> C<ib_softcommit> option. |
177 | |
178 | You need either this option or C<< disable_sth_caching => 1 >> for |
47ec67c3 |
179 | L<DBIx::Class> code to function correctly (otherwise you may get C<no statement |
180 | executing> errors.) |
dd2109ee |
181 | |
182 | The downside of using this option is that your process will B<NOT> see UPDATEs, |
183 | INSERTs and DELETEs from other processes for already open statements. |
184 | |
185 | =cut |
186 | |
187 | sub connect_call_use_softcommit { |
a499b173 |
188 | my $self = shift; |
189 | |
190 | $self->_dbh->{ib_softcommit} = 1; |
a499b173 |
191 | } |
192 | |
32323fc2 |
193 | =head2 connect_call_datetime_setup |
9cd0b325 |
194 | |
32323fc2 |
195 | Used as: |
9cd0b325 |
196 | |
32323fc2 |
197 | on_connect_call => 'datetime_setup' |
198 | |
f0f8ac86 |
199 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and |
200 | timestamp formats using: |
32323fc2 |
201 | |
202 | $dbh->{ib_time_all} = 'ISO'; |
203 | |
204 | See L<DBD::InterBase> for more details. |
205 | |
206 | The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for |
207 | second precision. The full precision is used. |
208 | |
c5827074 |
209 | The C<DATE> data type stores the date portion only, and it B<MUST> be declared |
210 | with: |
211 | |
212 | data_type => 'date' |
213 | |
214 | in your Result class. |
215 | |
216 | Timestamp columns can be declared with either C<datetime> or C<timestamp>. |
217 | |
32323fc2 |
218 | You will need the L<DateTime::Format::Strptime> module for inflation to work. |
219 | |
220 | For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second |
221 | precision is not currently available. |
222 | |
223 | =cut |
224 | |
225 | sub connect_call_datetime_setup { |
226 | my $self = shift; |
227 | |
228 | $self->_get_dbh->{ib_time_all} = 'ISO'; |
9cd0b325 |
229 | } |
230 | |
c5827074 |
231 | sub datetime_parser_type { |
232 | 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format' |
233 | } |
32323fc2 |
234 | |
c5827074 |
235 | package # hide from PAUSE |
236 | DBIx::Class::Storage::DBI::InterBase::DateTime::Format; |
32323fc2 |
237 | |
c5827074 |
238 | my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T |
239 | my $date_format = '%Y-%m-%d'; |
240 | |
241 | my ($timestamp_parser, $date_parser); |
242 | |
243 | sub parse_datetime { |
244 | shift; |
245 | require DateTime::Format::Strptime; |
246 | $timestamp_parser ||= DateTime::Format::Strptime->new( |
247 | pattern => $timestamp_format, |
248 | on_error => 'croak', |
249 | ); |
250 | return $timestamp_parser->parse_datetime(shift); |
251 | } |
252 | |
253 | sub format_datetime { |
254 | shift; |
255 | require DateTime::Format::Strptime; |
256 | $timestamp_parser ||= DateTime::Format::Strptime->new( |
257 | pattern => $timestamp_format, |
258 | on_error => 'croak', |
259 | ); |
260 | return $timestamp_parser->format_datetime(shift); |
261 | } |
262 | |
263 | sub parse_date { |
264 | shift; |
265 | require DateTime::Format::Strptime; |
266 | $date_parser ||= DateTime::Format::Strptime->new( |
267 | pattern => $date_format, |
268 | on_error => 'croak', |
269 | ); |
270 | return $date_parser->parse_datetime(shift); |
271 | } |
272 | |
273 | sub format_date { |
274 | shift; |
275 | require DateTime::Format::Strptime; |
276 | $date_parser ||= DateTime::Format::Strptime->new( |
277 | pattern => $date_format, |
32323fc2 |
278 | on_error => 'croak', |
279 | ); |
c5827074 |
280 | return $date_parser->format_datetime(shift); |
9cd0b325 |
281 | } |
282 | |
145b2a3d |
283 | 1; |
90489c23 |
284 | |
285 | =head1 CAVEATS |
286 | |
287 | =over 4 |
288 | |
289 | =item * |
290 | |
dd2109ee |
291 | with L</connect_call_use_softcommit>, you will not be able to see changes made |
292 | to data in other processes. If this is an issue, use |
47ec67c3 |
293 | L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a |
294 | workaround for the C<no statement executing> errors, this of course adversely |
295 | affects performance. |
dd2109ee |
296 | |
297 | =item * |
298 | |
90489c23 |
299 | C<last_insert_id> support only works for Firebird versions 2 or greater. To |
300 | work with earlier versions, we'll need to figure out how to retrieve the bodies |
301 | of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name. |
302 | |
47ec67c3 |
303 | =item * |
304 | |
305 | Sub-second precision for TIMESTAMPs is not currently available with ODBC. |
306 | |
90489c23 |
307 | =back |
308 | |
309 | =head1 AUTHOR |
310 | |
311 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>. |
312 | |
313 | =head1 LICENSE |
314 | |
315 | You may distribute this code under the same terms as Perl itself. |
316 | |
317 | =cut |