I was wrong about 2d12a809 - the crash is real
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / InterBase.pm
CommitLineData
88a8d0fa 1package DBIx::Class::Storage::DBI::InterBase;
2
88a8d0fa 3use strict;
4use warnings;
e46df41a 5use base qw/DBIx::Class::Storage::DBI::Firebird::Common/;
88a8d0fa 6use mro 'c3';
ed7ab0f4 7use Try::Tiny;
fd323bf1 8use namespace::clean;
88a8d0fa 9
90489c23 10=head1 NAME
11
e46df41a 12DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS via
13L<DBD::InterBase>
90489c23 14
15=head1 DESCRIPTION
16
e46df41a 17This driver is a subclass of L<DBIx::Class::Storage::DBI::Firebird::Common> for
18use with L<DBD::InterBase>, see that driver for general details.
90489c23 19
dd2109ee 20You need to use either the
21L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> option or
22L</connect_call_use_softcommit> (see L</CAVEATS>) for your code to function
47ec67c3 23correctly with this driver. Otherwise you will likely get bizarre error messages
d1fc96c7 24such as C<no statement executing>. The alternative is to use the
25L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver, which is more suitable
26for long running processes such as under L<Catalyst>.
90489c23 27
c5827074 28To turn on L<DBIx::Class::InflateColumn::DateTime> support, see
29L</connect_call_datetime_setup>.
32323fc2 30
90489c23 31=cut
32
6f7a118e 33__PACKAGE__->datetime_parser_type(
34 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'
35);
bab40dee 36
32323fc2 37sub _ping {
38 my $self = shift;
39
40 my $dbh = $self->_dbh or return 0;
41
42 local $dbh->{RaiseError} = 1;
ecdf1ac8 43 local $dbh->{PrintError} = 0;
32323fc2 44
52b420dd 45 return try {
32323fc2 46 $dbh->do('select 1 from rdb$database');
52b420dd 47 1;
ed7ab0f4 48 } catch {
52b420dd 49 0;
32323fc2 50 };
32323fc2 51}
52
9633951d 53# We want dialect 3 for new features and quoting to work, DBD::InterBase uses
54# dialect 1 (interbase compat) by default.
55sub _init {
56 my $self = shift;
57 $self->_set_sql_dialect(3);
58}
59
60sub _set_sql_dialect {
61 my $self = shift;
62 my $val = shift || 3;
63
64 my $dsn = $self->_dbi_connect_info->[0];
65
66 return if ref($dsn) eq 'CODE';
67
68 if ($dsn !~ /ib_dialect=/) {
69 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
70 my $connected = defined $self->_dbh;
71 $self->disconnect;
72 $self->ensure_connected if $connected;
73 }
74}
75
dd2109ee 76=head2 connect_call_use_softcommit
77
78Used as:
79
80 on_connect_call => 'use_softcommit'
81
82In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the
83L<DBD::InterBase> C<ib_softcommit> option.
84
85You need either this option or C<< disable_sth_caching => 1 >> for
47ec67c3 86L<DBIx::Class> code to function correctly (otherwise you may get C<no statement
d1fc96c7 87executing> errors.) Or use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird>
88driver.
dd2109ee 89
90The downside of using this option is that your process will B<NOT> see UPDATEs,
91INSERTs and DELETEs from other processes for already open statements.
92
93=cut
94
95sub connect_call_use_softcommit {
a499b173 96 my $self = shift;
97
98 $self->_dbh->{ib_softcommit} = 1;
a499b173 99}
100
32323fc2 101=head2 connect_call_datetime_setup
9cd0b325 102
32323fc2 103Used as:
9cd0b325 104
32323fc2 105 on_connect_call => 'datetime_setup'
106
f0f8ac86 107In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
108timestamp formats using:
32323fc2 109
110 $dbh->{ib_time_all} = 'ISO';
111
112See L<DBD::InterBase> for more details.
113
114The C<TIMESTAMP> data type supports up to 4 digits after the decimal point for
115second precision. The full precision is used.
116
c5827074 117The C<DATE> data type stores the date portion only, and it B<MUST> be declared
118with:
119
120 data_type => 'date'
121
122in your Result class.
123
124Timestamp columns can be declared with either C<datetime> or C<timestamp>.
125
32323fc2 126You will need the L<DateTime::Format::Strptime> module for inflation to work.
127
a870aa85 128For L<DBIx::Class::Storage::DBI::ODBC::Firebird>, this is a noop.
32323fc2 129
130=cut
131
132sub connect_call_datetime_setup {
133 my $self = shift;
134
135 $self->_get_dbh->{ib_time_all} = 'ISO';
9cd0b325 136}
137
32323fc2 138
c5827074 139package # hide from PAUSE
140 DBIx::Class::Storage::DBI::InterBase::DateTime::Format;
32323fc2 141
c5827074 142my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T
143my $date_format = '%Y-%m-%d';
144
145my ($timestamp_parser, $date_parser);
146
147sub parse_datetime {
148 shift;
149 require DateTime::Format::Strptime;
150 $timestamp_parser ||= DateTime::Format::Strptime->new(
151 pattern => $timestamp_format,
152 on_error => 'croak',
153 );
154 return $timestamp_parser->parse_datetime(shift);
155}
156
157sub format_datetime {
158 shift;
159 require DateTime::Format::Strptime;
160 $timestamp_parser ||= DateTime::Format::Strptime->new(
161 pattern => $timestamp_format,
162 on_error => 'croak',
163 );
164 return $timestamp_parser->format_datetime(shift);
165}
166
167sub parse_date {
168 shift;
169 require DateTime::Format::Strptime;
170 $date_parser ||= DateTime::Format::Strptime->new(
171 pattern => $date_format,
172 on_error => 'croak',
173 );
174 return $date_parser->parse_datetime(shift);
175}
176
177sub format_date {
178 shift;
179 require DateTime::Format::Strptime;
180 $date_parser ||= DateTime::Format::Strptime->new(
181 pattern => $date_format,
32323fc2 182 on_error => 'croak',
183 );
c5827074 184 return $date_parser->format_datetime(shift);
9cd0b325 185}
186
145b2a3d 1871;
90489c23 188
189=head1 CAVEATS
190
191=over 4
192
193=item *
194
dd2109ee 195with L</connect_call_use_softcommit>, you will not be able to see changes made
196to data in other processes. If this is an issue, use
47ec67c3 197L<disable_sth_caching|DBIx::Class::Storage::DBI/disable_sth_caching> as a
198workaround for the C<no statement executing> errors, this of course adversely
199affects performance.
dd2109ee 200
d1fc96c7 201Alternately, use the L<ODBC|DBIx::Class::Storage::DBI::ODBC::Firebird> driver.
202
90489c23 203=back
204
205=head1 AUTHOR
206
207See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
208
209=head1 LICENSE
210
211You may distribute this code under the same terms as Perl itself.
212
213=cut
e02b39b4 214# vim:sts=2 sw=2: