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