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 | |
32323fc2 |
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 | |
90489c23 |
33 | =cut |
34 | |
88a8d0fa |
35 | sub _prep_for_execute { |
36 | my $self = shift; |
37 | my ($op, $extra_bind, $ident, $args) = @_; |
38 | |
88a8d0fa |
39 | if ($op eq 'insert') { |
6e8d182b |
40 | my @pk = $ident->primary_columns; |
41 | my %pk; |
42 | @pk{@pk} = (); |
43 | |
1ae0a36c |
44 | my @auto_inc_cols = grep { |
45 | my $inserting = $args->[0]{$_}; |
145b2a3d |
46 | |
6e8d182b |
47 | ($ident->column_info($_)->{is_auto_increment} |
48 | || exists $pk{$_}) |
49 | && ( |
1ae0a36c |
50 | (not defined $inserting) |
51 | || |
2680ffe5 |
52 | (ref $inserting eq 'SCALAR' && $$inserting =~ /^null\z/i) |
1ae0a36c |
53 | ) |
54 | } $ident->columns; |
88a8d0fa |
55 | |
145b2a3d |
56 | if (@auto_inc_cols) { |
1696e04f |
57 | $args->[1]{returning} = \@auto_inc_cols; |
145b2a3d |
58 | |
2680ffe5 |
59 | $self->_auto_incs([]); |
60 | $self->_auto_incs->[0] = \@auto_inc_cols; |
145b2a3d |
61 | } |
88a8d0fa |
62 | } |
63 | |
1696e04f |
64 | return $self->next::method(@_); |
1ae0a36c |
65 | } |
66 | |
88a8d0fa |
67 | sub _execute { |
68 | my $self = shift; |
69 | my ($op) = @_; |
70 | |
71 | my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_); |
72 | |
2680ffe5 |
73 | if ($op eq 'insert' && $self->_auto_incs) { |
88a8d0fa |
74 | local $@; |
145b2a3d |
75 | my (@auto_incs) = eval { |
76 | local $SIG{__WARN__} = sub {}; |
77 | $sth->fetchrow_array |
78 | }; |
2680ffe5 |
79 | $self->_auto_incs->[1] = \@auto_incs; |
88a8d0fa |
80 | $sth->finish; |
81 | } |
82 | |
83 | return wantarray ? ($rv, $sth, @bind) : $rv; |
84 | } |
85 | |
145b2a3d |
86 | sub last_insert_id { |
87 | my ($self, $source, @cols) = @_; |
88 | my @result; |
88a8d0fa |
89 | |
145b2a3d |
90 | my %auto_incs; |
2680ffe5 |
91 | @auto_incs{ @{ $self->_auto_incs->[0] } } = |
92 | @{ $self->_auto_incs->[1] }; |
145b2a3d |
93 | |
94 | push @result, $auto_incs{$_} for @cols; |
95 | |
96 | return @result; |
97 | } |
98 | |
99 | # this sub stolen from DB2 |
88a8d0fa |
100 | |
145b2a3d |
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 | |
32323fc2 |
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 | |
9633951d |
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 | |
a499b173 |
166 | # softcommit makes savepoints work |
167 | sub _run_connection_actions { |
168 | my $self = shift; |
169 | |
170 | $self->_dbh->{ib_softcommit} = 1; |
171 | |
172 | $self->next::method(@_); |
173 | } |
174 | |
32323fc2 |
175 | =head2 connect_call_datetime_setup |
9cd0b325 |
176 | |
32323fc2 |
177 | Used as: |
9cd0b325 |
178 | |
32323fc2 |
179 | on_connect_call => 'datetime_setup' |
180 | |
181 | In L<DBIx::Class::Storage::DBI/connect_info> to set the date and timestamp |
182 | formats using: |
183 | |
184 | $dbh->{ib_time_all} = 'ISO'; |
185 | |
186 | See L<DBD::InterBase> for more details. |
187 | |
188 | The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for |
189 | second precision. The full precision is used. |
190 | |
191 | You will need the L<DateTime::Format::Strptime> module for inflation to work. |
192 | |
193 | For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop and sub-second |
194 | precision is not currently available. |
195 | |
196 | =cut |
197 | |
198 | sub connect_call_datetime_setup { |
199 | my $self = shift; |
200 | |
201 | $self->_get_dbh->{ib_time_all} = 'ISO'; |
9cd0b325 |
202 | } |
203 | |
32323fc2 |
204 | |
205 | # from MSSQL |
206 | |
207 | sub build_datetime_parser { |
208 | my $self = shift; |
209 | my $type = "DateTime::Format::Strptime"; |
210 | eval "use ${type}"; |
211 | $self->throw_exception("Couldn't load ${type}: $@") if $@; |
212 | return $type->new( |
213 | pattern => '%Y-%m-%d %H:%M:%S.%4N', # %F %T |
214 | on_error => 'croak', |
215 | ); |
9cd0b325 |
216 | } |
217 | |
145b2a3d |
218 | 1; |
90489c23 |
219 | |
220 | =head1 CAVEATS |
221 | |
222 | =over 4 |
223 | |
224 | =item * |
225 | |
226 | C<last_insert_id> support only works for Firebird versions 2 or greater. To |
227 | work with earlier versions, we'll need to figure out how to retrieve the bodies |
228 | of C<BEFORE INSERT> triggers and parse them for the C<GENERATOR> name. |
229 | |
90489c23 |
230 | =back |
231 | |
232 | =head1 AUTHOR |
233 | |
234 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>. |
235 | |
236 | =head1 LICENSE |
237 | |
238 | You may distribute this code under the same terms as Perl itself. |
239 | |
240 | =cut |