line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DateTime::Format::DBI; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
77153
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
124
|
|
4
|
3
|
|
|
3
|
|
65
|
use vars qw ($VERSION); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
168
|
|
5
|
3
|
|
|
3
|
|
21
|
use warnings; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
98
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
25
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
315
|
|
8
|
3
|
|
|
3
|
|
786062
|
use DBI 1.21; |
|
3
|
|
|
|
|
171619
|
|
|
3
|
|
|
|
|
1428
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$VERSION = '0.041'; |
11
|
|
|
|
|
|
|
$VERSION = eval { $VERSION }; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %db_to_parser = ( |
14
|
|
|
|
|
|
|
# lowercase for case-insensitivity! |
15
|
|
|
|
|
|
|
'mysql' => 'DateTime::Format::MySQL', |
16
|
|
|
|
|
|
|
'pg' => 'DateTime::Format::Pg', |
17
|
|
|
|
|
|
|
'db2' => 'DateTime::Format::DB2', |
18
|
|
|
|
|
|
|
'mssql' => 'DateTime::Format::MSSQL', # experimental |
19
|
|
|
|
|
|
|
'oracle' => 'DateTime::Format::Oracle', |
20
|
|
|
|
|
|
|
'sqlite' => 'DateTime::Format::SQLite', |
21
|
|
|
|
|
|
|
'sybase' => 'DateTime::Format::Sybase', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _get_parser { |
25
|
1
|
|
|
1
|
|
12
|
while(@_) { |
26
|
1
|
|
|
|
|
5
|
my $dbt = lc shift; |
27
|
1
|
50
|
|
|
|
22
|
return $db_to_parser{$dbt} |
28
|
|
|
|
|
|
|
if exists $db_to_parser{$dbt}; |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
|
|
|
0
|
return undef; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
1
|
|
|
1
|
1
|
516348
|
my ($name,$dbh) = @_; |
35
|
1
|
50
|
|
|
|
22
|
UNIVERSAL::isa($dbh,'DBI::db') || croak('Not a DBI handle.'); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# NB: Using $dbh->{Driver}->{Name} this call does not work with drivers that |
38
|
|
|
|
|
|
|
# connect to multiple differnt types of databases, such as DBD::Proxy, |
39
|
|
|
|
|
|
|
# DBD::ODBC, DBD::JDBC,... |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
# DBI already has code to determine the underlying database type, which is NOT |
42
|
|
|
|
|
|
|
# trivial. I don't want to duplicate that here (although it's only available |
43
|
|
|
|
|
|
|
# through a private API). |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# my $dbtype = $dbh->{Driver}->{Name}; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
5
|
my @dbtypes = eval { DBI::_dbtype_names($dbh,0) }; |
|
1
|
|
|
|
|
25
|
|
48
|
1
|
|
|
|
|
93
|
my $pclass = _get_parser(@dbtypes); |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
13
|
croak("No supported database driver in '@dbtypes'") |
51
|
|
|
|
|
|
|
unless defined $pclass; |
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
1
|
|
192
|
eval "use $pclass;"; |
|
1
|
|
|
|
|
982
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
54
|
1
|
50
|
|
|
|
3412
|
croak("Cannot load $pclass: $@") if $@; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
## some db formatters are singletons and don't have 'new' |
57
|
|
|
|
|
|
|
## |
58
|
0
|
|
|
|
|
0
|
my $new = UNIVERSAL::can($pclass, 'new'); |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
0
|
my $parser; if(ref $new) { |
|
0
|
|
|
|
|
0
|
|
61
|
0
|
|
|
|
|
0
|
$parser = eval { $new->($pclass); }; |
|
0
|
|
|
|
|
0
|
|
62
|
0
|
0
|
|
|
|
0
|
croak "Cannot create object for $pclass: $@" if $@; |
63
|
|
|
|
|
|
|
} else { |
64
|
0
|
|
|
|
|
0
|
$parser = $pclass; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
0
|
foreach(('format_datetime', 'parse_datetime')) { |
68
|
0
|
0
|
|
|
|
0
|
croak "$pclass->$_ is missing" |
69
|
|
|
|
|
|
|
unless UNIVERSAL::can($parser, $_) |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
0
|
return $parser; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 NAME |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
DateTime::Format::DBI - Find a parser class for a database connection. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use DBI; |
82
|
|
|
|
|
|
|
use DateTime; |
83
|
|
|
|
|
|
|
use DateTime::Format::DBI; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $db = DBI->connect('dbi:...'); |
86
|
|
|
|
|
|
|
my $db_parser = DateTime::Format::DBI->new($dbh); |
87
|
|
|
|
|
|
|
my $dt = DateTime->now(); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$db->do("UPDATE table SET dt=? WHERE foo='bar'",undef, |
90
|
|
|
|
|
|
|
$db_parser->format_datetime($dt); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 DESCRIPTION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This module finds a C class that is suitable for the use with |
95
|
|
|
|
|
|
|
a given DBI connection (and C driver). |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
It currently supports the following format modules: |
98
|
|
|
|
|
|
|
L, |
99
|
|
|
|
|
|
|
L, |
100
|
|
|
|
|
|
|
L, |
101
|
|
|
|
|
|
|
L, |
102
|
|
|
|
|
|
|
L, |
103
|
|
|
|
|
|
|
L, and |
104
|
|
|
|
|
|
|
L. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
B This module provides a quick method to find the correct parser and |
107
|
|
|
|
|
|
|
formatter class. However, this is usually not sufficient for full database |
108
|
|
|
|
|
|
|
abstraction. You will also have to cater for differences in the syntax and |
109
|
|
|
|
|
|
|
semantics of SQL datetime functions (and other SQL commands). |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 CLASS METHODS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This module provides a single factory method: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=over 4 |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * new( $dbh ) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Creates a new C instance, the exact class of which depends |
120
|
|
|
|
|
|
|
on the database driver used for the database connection referenced by C<$dbh>. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 PARSER/FORMATTER INTERFACE |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
C is just a front-end class factory that will return one |
127
|
|
|
|
|
|
|
of the format classes based on the nature of your C<$dbh>. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
For information on the interface of the returned parser object, please see the |
130
|
|
|
|
|
|
|
documentation for the class pertaining to your particular C<$dbh>. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
In general, parser classes for databases will implement the following methods. |
133
|
|
|
|
|
|
|
For more information on the exact behaviour of these methods, see the |
134
|
|
|
|
|
|
|
documentation of the parser class. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over 4 |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item * parse_datetime( $string ) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Given a string containing a date and/or time representation from the database |
141
|
|
|
|
|
|
|
used, this method will return a new C object. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
If given an improperly formatted string, this method may die. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * format_datetime( $dt ) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Given a C object, this method returns a string appropriate as input |
148
|
|
|
|
|
|
|
for all or the most common date and date/time types of the database used. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * parse_duration( $string ) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Given a string containing a duration representation from the database used, |
153
|
|
|
|
|
|
|
this method will return a new C object. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
If given an improperly formatted string, this method may die. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Not all databases and format/formatter classes support durations; please use |
158
|
|
|
|
|
|
|
L to check for the availability of this method. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * format_duration( $du ) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Given a C object, this method returns a string appropriate |
163
|
|
|
|
|
|
|
as input for the duration or interval type of the database used. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Not all databases and parser/formatter classes support durations; please use |
166
|
|
|
|
|
|
|
L to check for the availability of this method. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=back |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Parser/formatter classes may additionally define methods like parse_I or |
171
|
|
|
|
|
|
|
format_I (where I is derived from the SQL type); please see the |
172
|
|
|
|
|
|
|
documentation of the individual format class for more information. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 SUPPORT |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Please report bugs and other requests to the rt tracker: |
177
|
|
|
|
|
|
|
L. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 AUTHOR |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Claus FErber |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 LICENSE |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Copyright 2003-2013 Claus FErber. All rights reserved. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
188
|
|
|
|
|
|
|
the same terms as Perl itself. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
The full text of the license can be found in the LICENSE file included with |
191
|
|
|
|
|
|
|
this module. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 SEE ALSO |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L, L |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
datetime@perl.org mailing list |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
http://datetime.perl.org/ |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |