Refactor SQLA/select interaction (in reality just cleanup)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Informix.pm
CommitLineData
193590c2 1package DBIx::Class::Storage::DBI::Informix;
2use strict;
3use warnings;
4
5use base qw/DBIx::Class::Storage::DBI/;
193590c2 6use mro 'c3';
7
d3774d9b 8use Scope::Guard ();
9use Context::Preserve ();
10
193590c2 11__PACKAGE__->mk_group_accessors('simple' => '__last_insert_id');
12
b0a4cf8e 13=head1 NAME
14
15DBIx::Class::Storage::DBI::Informix - Base Storage Class for Informix Support
16
17=head1 DESCRIPTION
18
19This class implements storage-specific support for the Informix RDBMS
20
21=head1 METHODS
22
23=cut
24
193590c2 25sub _execute {
26 my $self = shift;
27 my ($op) = @_;
28 my ($rv, $sth, @rest) = $self->next::method(@_);
29 if ($op eq 'insert') {
30 $self->__last_insert_id($sth->{ix_sqlerrd}[1]);
31 }
32 return (wantarray ? ($rv, $sth, @rest) : $rv);
33}
34
35sub last_insert_id {
36 shift->__last_insert_id;
37}
38
39sub _sql_maker_opts {
40 my ( $self, $opts ) = @_;
41
42 if ( $opts ) {
43 $self->{_sql_maker_opts} = { %$opts };
44 }
45
46 return { limit_dialect => 'SkipFirst', %{$self->{_sql_maker_opts}||{}} };
47}
48
9fb04139 49sub _svp_begin {
50 my ($self, $name) = @_;
51
52 $self->_get_dbh->do("SAVEPOINT $name");
53}
54
55# can't release savepoints
56sub _svp_release { 1 }
57
58sub _svp_rollback {
59 my ($self, $name) = @_;
60
61 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
62}
63
d3774d9b 64sub with_deferred_fk_checks {
65 my ($self, $sub) = @_;
66
67 my $txn_scope_guard = $self->txn_scope_guard;
68
69 $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
70
71 my $sg = Scope::Guard->new(sub {
72 $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
73 });
74
75 return Context::Preserve::preserve_context(sub { $sub->() },
76 after => sub { $txn_scope_guard->commit });
77}
78
b0a4cf8e 79=head2 connect_call_datetime_setup
9fb04139 80
b0a4cf8e 81Used as:
193590c2 82
b0a4cf8e 83 on_connect_call => 'datetime_setup'
193590c2 84
b0a4cf8e 85In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the C<DATE> and
86C<DATETIME> formats.
193590c2 87
b0a4cf8e 88Sets the following environment variables:
193590c2 89
b0a4cf8e 90 GL_DATE="%m/%d/%Y"
91 GL_DATETIME="%Y-%m-%d %H:%M:%S%F5"
193590c2 92
b0a4cf8e 93The C<DBDATE> and C<DBCENTURY> environment variables are cleared.
94
95B<NOTE:> setting the C<GL_DATE> environment variable seems to have no effect
96after the process has started, so the default format is used. The C<GL_DATETIME>
97setting does take effect however.
98
99The C<DATETIME> data type supports up to 5 digits after the decimal point for
100second precision, depending on how you have declared your column. The full
101possible precision is used.
102
103The column declaration for a C<DATETIME> with maximum precision is:
104
105 column_name DATETIME YEAR TO FRACTION(5)
193590c2 106
b0a4cf8e 107The C<DATE> data type stores the date portion only, and it B<MUST> be declared
108with:
109
110 data_type => 'date'
111
112in your Result class.
113
114You will need the L<DateTime::Format::Strptime> module for inflation to work.
115
116=cut
117
118sub connect_call_datetime_setup {
119 my $self = shift;
120
121 delete @ENV{qw/DBDATE DBCENTURY/};
122
123 $ENV{GL_DATE} = "%m/%d/%Y";
124 $ENV{GL_DATETIME} = "%Y-%m-%d %H:%M:%S%F5";
125}
126
127sub datetime_parser_type {
128 'DBIx::Class::Storage::DBI::Informix::DateTime::Format'
129}
130
131package # hide from PAUSE
132 DBIx::Class::Storage::DBI::Informix::DateTime::Format;
133
134my $timestamp_format = '%Y-%m-%d %H:%M:%S.%5N'; # %F %T
135my $date_format = '%m/%d/%Y';
136
137my ($timestamp_parser, $date_parser);
138
139sub parse_datetime {
140 shift;
141 require DateTime::Format::Strptime;
142 $timestamp_parser ||= DateTime::Format::Strptime->new(
143 pattern => $timestamp_format,
144 on_error => 'croak',
145 );
146 return $timestamp_parser->parse_datetime(shift);
147}
148
149sub format_datetime {
150 shift;
151 require DateTime::Format::Strptime;
152 $timestamp_parser ||= DateTime::Format::Strptime->new(
153 pattern => $timestamp_format,
154 on_error => 'croak',
155 );
156 return $timestamp_parser->format_datetime(shift);
157}
158
159sub parse_date {
160 shift;
161 require DateTime::Format::Strptime;
162 $date_parser ||= DateTime::Format::Strptime->new(
163 pattern => $date_format,
164 on_error => 'croak',
165 );
166 return $date_parser->parse_datetime(shift);
167}
168
169sub format_date {
170 shift;
171 require DateTime::Format::Strptime;
172 $date_parser ||= DateTime::Format::Strptime->new(
173 pattern => $date_format,
174 on_error => 'croak',
175 );
176 return $date_parser->format_datetime(shift);
177}
178
1791;
193590c2 180
b0a4cf8e 181=head1 AUTHOR
193590c2 182
b0a4cf8e 183See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
193590c2 184
185=head1 LICENSE
186
187You may distribute this code under the same terms as Perl itself.
188
189=cut