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