22cb19ea462b9d22fb16ab86a06cf3499f5b143d
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / InterBase.pm
1 package DBIx::Class::Storage::DBI::InterBase;
2
3 # partly stolen from DBIx::Class::Storage::DBI::MSSQL
4
5 use strict;
6 use warnings;
7 use base qw/DBIx::Class::Storage::DBI/;
8 use mro 'c3';
9 use List::Util();
10
11 __PACKAGE__->mk_group_accessors(simple => qw/
12   _auto_incs
13 /);
14
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
27 To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
28 L</connect_call_datetime_setup>.
29
30 =cut
31
32 sub _prep_for_execute {
33   my $self = shift;
34   my ($op, $extra_bind, $ident, $args) = @_;
35
36   if ($op eq 'insert') {
37     my @pk = $ident->primary_columns;
38     my %pk;
39     @pk{@pk} = ();
40
41     my @auto_inc_cols = grep {
42       my $inserting = $args->[0]{$_};
43
44       ($ident->column_info($_)->{is_auto_increment}
45         || exists $pk{$_})
46       && (
47         (not defined $inserting)
48         ||
49         (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
50       )
51     } $ident->columns;
52
53     if (@auto_inc_cols) {
54       $args->[1]{returning} = \@auto_inc_cols;
55
56       $self->_auto_incs([]);
57       $self->_auto_incs->[0] = \@auto_inc_cols;
58     }
59   }
60
61   return $self->next::method(@_);
62 }
63
64 sub _execute {
65   my $self = shift;
66   my ($op) = @_;
67
68   my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
69
70   if ($op eq 'insert' && $self->_auto_incs) {
71     local $@;
72     my (@auto_incs) = eval {
73       local $SIG{__WARN__} = sub {};
74       $sth->fetchrow_array
75     };
76     $self->_auto_incs->[1] = \@auto_incs;
77     $sth->finish;
78   }
79
80   return wantarray ? ($rv, $sth, @bind) : $rv;
81 }
82
83 sub last_insert_id {
84   my ($self, $source, @cols) = @_;
85   my @result;
86
87   my %auto_incs;
88   @auto_incs{ @{ $self->_auto_incs->[0] } } =
89     @{ $self->_auto_incs->[1] };
90
91   push @result, $auto_incs{$_} for @cols;
92
93   return @result;
94 }
95
96 # this sub stolen from DB2
97
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
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
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
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
172 =head2 connect_call_datetime_setup
173
174 Used as:
175
176   on_connect_call => 'datetime_setup'
177
178 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
179 timestamp formats using:
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
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
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';
208 }
209
210 sub datetime_parser_type {
211   'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
212 }
213
214 package # hide from PAUSE
215   DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
216
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,
257     on_error => 'croak',
258   );
259   return $date_parser->format_datetime(shift);
260 }
261
262 1;
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
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