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