4baa26c3e2c3fa9c03930012a02a9bfd95ca2c62
[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 =head2 connect_call_datetime_setup
144
145 Used as:
146
147   on_connect_call => 'datetime_setup'
148
149 In L<DBIx::Class::Storage::DBI/connect_info> to set the date and timestamp
150 formats using:
151
152   $dbh->{ib_time_all} = 'ISO';
153
154 See L<DBD::InterBase> for more details.
155
156 The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
157 second precision. The full precision is used.
158
159 You will need the L<DateTime::Format::Strptime> module for inflation to work.
160
161 For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second
162 precision is not currently available.
163
164 =cut
165
166 sub connect_call_datetime_setup {
167   my $self = shift;
168
169   $self->_get_dbh->{ib_time_all} = 'ISO';
170 }
171
172
173 # from MSSQL
174
175 sub build_datetime_parser {
176   my $self = shift;
177   my $type = "DateTime::Format::Strptime";
178   eval "use ${type}";
179   $self->throw_exception("Couldn't load ${type}: $@") if $@;
180   return $type->new(
181     pattern => '%Y-%m-%d %H:%M:%S.%4N', # %F %T
182     on_error => 'croak',
183   );
184 }
185
186 1;
187
188 =head1 CAVEATS
189
190 =over 4
191
192 =item *
193
194 C<last_insert_id> support only works for Firebird versions 2 or greater. To
195 work with earlier versions, we'll need to figure out how to retrieve the bodies
196 of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name.
197
198 =back
199
200 =head1 AUTHOR
201
202 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
203
204 =head1 LICENSE
205
206 You may distribute this code under the same terms as Perl itself.
207
208 =cut