default to sql dialect 3 unless overridden
[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, add:
28
29     on_connect_call => 'datetime_setup'
30
31 to your L<DBIx::Class::Storage::DBI/connect_info>.
32
33 =cut
34
35 sub _prep_for_execute {
36   my $self = shift;
37   my ($op, $extra_bind, $ident, $args) = @_;
38
39   if ($op eq 'insert') {
40     my @pk = $ident->primary_columns;
41     my %pk;
42     @pk{@pk} = ();
43
44     my @auto_inc_cols = grep {
45       my $inserting = $args->[0]{$_};
46
47       ($ident->column_info($_)->{is_auto_increment}
48         || exists $pk{$_})
49       && (
50         (not defined $inserting)
51         ||
52         (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i)
53       )
54     } $ident->columns;
55
56     if (@auto_inc_cols) {
57       $args->[1]{returning} = \@auto_inc_cols;
58
59       $self->_auto_incs([]);
60       $self->_auto_incs->[0] = \@auto_inc_cols;
61     }
62   }
63
64   return $self->next::method(@_);
65 }
66
67 sub _execute {
68   my $self = shift;
69   my ($op) = @_;
70
71   my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
72
73   if ($op eq 'insert' && $self->_auto_incs) {
74     local $@;
75     my (@auto_incs) = eval {
76       local $SIG{__WARN__} = sub {};
77       $sth->fetchrow_array
78     };
79     $self->_auto_incs->[1] = \@auto_incs;
80     $sth->finish;
81   }
82
83   return wantarray ? ($rv, $sth, @bind) : $rv;
84 }
85
86 sub last_insert_id {
87   my ($self, $source, @cols) = @_;
88   my @result;
89
90   my %auto_incs;
91   @auto_incs{ @{ $self->_auto_incs->[0] } } =
92     @{ $self->_auto_incs->[1] };
93
94   push @result, $auto_incs{$_} for @cols;
95
96   return @result;
97 }
98
99 # this sub stolen from DB2
100
101 sub _sql_maker_opts {
102   my ( $self, $opts ) = @_;
103
104   if ( $opts ) {
105     $self->{_sql_maker_opts} = { %$opts };
106   }
107
108   return { limit_dialect => 'FirstSkip', %{$self->{_sql_maker_opts}||{}} };
109 }
110
111 sub _svp_begin {
112     my ($self, $name) = @_;
113
114     $self->_get_dbh->do("SAVEPOINT $name");
115 }
116
117 sub _svp_release {
118     my ($self, $name) = @_;
119
120     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
121 }
122
123 sub _svp_rollback {
124     my ($self, $name) = @_;
125
126     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
127 }
128
129 sub _ping {
130   my $self = shift;
131
132   my $dbh = $self->_dbh or return 0;
133
134   local $dbh->{RaiseError} = 1;
135
136   eval {
137     $dbh->do('select 1 from rdb$database');
138   };
139
140   return $@ ? 0 : 1;
141 }
142
143 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
144 # dialect 1 (interbase compat) by default.
145 sub _init {
146   my $self = shift;
147   $self->_set_sql_dialect(3);
148 }
149
150 sub _set_sql_dialect {
151   my $self = shift;
152   my $val  = shift || 3;
153
154   my $dsn = $self->_dbi_connect_info->[0];
155
156   return if ref($dsn) eq 'CODE';
157
158   if ($dsn !~ /ib_dialect=/) {
159     $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
160     my $connected = defined $self->_dbh;
161     $self->disconnect;
162     $self->ensure_connected if $connected;
163   }
164 }
165
166 =head2 connect_call_datetime_setup
167
168 Used as:
169
170   on_connect_call => 'datetime_setup'
171
172 In L<DBIx::Class::Storage::DBI/connect_info> to set the date and timestamp
173 formats using:
174
175   $dbh->{ib_time_all} = 'ISO';
176
177 See L<DBD::InterBase> for more details.
178
179 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
180 second precision. The full precision is used.
181
182 You will need the L<DateTime::Format::Strptime> module for inflation to work.
183
184 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
185 precision is not currently available.
186
187 =cut
188
189 sub connect_call_datetime_setup {
190   my $self = shift;
191
192   $self->_get_dbh->{ib_time_all} = 'ISO';
193 }
194
195
196 # from MSSQL
197
198 sub build_datetime_parser {
199   my $self = shift;
200   my $type = "DateTime::Format::Strptime";
201   eval "use ${type}";
202   $self->throw_exception("Couldn't load ${type}: $@") if $@;
203   return $type->new(
204     pattern => '%Y-%m-%d %H:%M:%S.%4N', # %F %T
205     on_error => 'croak',
206   );
207 }
208
209 1;
210
211 =head1 CAVEATS
212
213 =over 4
214
215 =item *
216
217 C<last_insert_id> support only works for Firebird versions 2 or greater. To
218 work with earlier versions, we'll need to figure out how to retrieve the bodies
219 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
220
221 =back
222
223 =head1 AUTHOR
224
225 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
226
227 =head1 LICENSE
228
229 You may distribute this code under the same terms as Perl itself.
230
231 =cut