| 1 | package DateTime::Format::DBI; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use vars qw ($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use DBI 1.21; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | $VERSION = '0.041'; | |||||||||
| 11 | 4 | $VERSION = eval { $VERSION }; | |||||||||
| 12 | |||||||||||
| 13 | 4 | 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 | 1 | ▶ | sub _get_parser { | ||||||||
|
Subroutine
_get_parser: called CC=3 SCAR=12.5
| |||||||||||
| 25 | 1 | ▶ | while(@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | my $dbt = lc shift; | |||||||||
| 27 | return $db_to_parser{$dbt} | ||||||||||
| 28 | 1 | ▶ | if exists $db_to_parser{$dbt}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | } | ||||||||||
| 30 | 0 | return undef; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=8 SCAR=32.0
Pod
documented
| |||||||||||
| 34 | 1 | my ($name,$dbh) = @_; | |||||||||
| 35 | 1 | ▶ | UNIVERSAL::isa($dbh,'DBI::db') || croak('Not a DBI handle.'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | my @dbtypes = eval { DBI::_dbtype_names($dbh,0) }; | |||||||||
| 48 | 1 | my $pclass = _get_parser(@dbtypes); | |||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | croak("No supported database driver in '@dbtypes'") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | unless defined $pclass; | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | eval "use $pclass;"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 54 | 1 | ▶ | croak("Cannot load $pclass: $@") if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | ## some db formatters are singletons and don't have 'new' | ||||||||||
| 57 | ## | ||||||||||
| 58 | 0 | my $new = UNIVERSAL::can($pclass, 'new'); | |||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | my $parser; if(ref $new) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $parser = eval { $new->($pclass); }; | |||||||||
| 62 | 0 | ▶ | croak "Cannot create object for $pclass: $@" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } else { | ||||||||||
| 64 | 0 | $parser = $pclass; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | foreach(('format_datetime', 'parse_datetime')) { | |||||||||
| 68 | 0 | ▶ | croak "$pclass->$_ is missing" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | unless UNIVERSAL::can($parser, $_) | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 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<DateTime::Format::*> class that is suitable for the use with | ||||||||||
| 95 | a given DBI connection (and C<DBD::*> driver). | ||||||||||
| 96 | |||||||||||
| 97 | It currently supports the following format modules: | ||||||||||
| 98 | L<IBM DB2 (DB2)|DateTime::Format::DB2>, | ||||||||||
| 99 | L<Microsoft SQL (MSSQL)|DateTime::Format::MSSQL>, | ||||||||||
| 100 | L<MySQL|DateTime::Format::MySQL>, | ||||||||||
| 101 | L<Oracle|DateTime::Format::Oracle>, | ||||||||||
| 102 | L<PostgreSQL (Pg)|DateTime::Format::Pg>, | ||||||||||
| 103 | L<SQLite|DateTime::Format::SQLite>, and | ||||||||||
| 104 | L<Sybase|DateTime::Format::Sybase>. | ||||||||||
| 105 | |||||||||||
| 106 | B<NOTE:> 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<DateTime::Format::*> 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<DateTime::Format::DBI> 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<DateTime> object. | ||||||||||
| 142 | |||||||||||
| 143 | If given an improperly formatted string, this method may die. | ||||||||||
| 144 | |||||||||||
| 145 | =item * format_datetime( $dt ) | ||||||||||
| 146 | |||||||||||
| 147 | Given a C<DateTime> 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<DateTime::Duration> 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<UNIVERSAL::has|UNIVERSAL/has> to check for the availability of this method. | ||||||||||
| 159 | |||||||||||
| 160 | =item * format_duration( $du ) | ||||||||||
| 161 | |||||||||||
| 162 | Given a C<DateTime::Duration> 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<UNIVERSAL::has|UNIVERSAL/has> to check for the availability of this method. | ||||||||||
| 167 | |||||||||||
| 168 | =back | ||||||||||
| 169 | |||||||||||
| 170 | Parser/formatter classes may additionally define methods like parse_I<type> or | ||||||||||
| 171 | format_I<type> (where I<type> 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<https://rt.cpan.org/Public/Dist/Display.html?Name=DateTime-Format-DBI>. | ||||||||||
| 178 | |||||||||||
| 179 | =head1 AUTHOR | ||||||||||
| 180 | |||||||||||
| 181 | Claus FE<auml>rber <CFAERBER@cpan.org> | ||||||||||
| 182 | |||||||||||
| 183 | =head1 LICENSE | ||||||||||
| 184 | |||||||||||
| 185 | Copyright 2003-2013 Claus FE<auml>rber. 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<DateTime>, L<DBI> | ||||||||||
| 196 | |||||||||||
| 197 | datetime@perl.org mailing list | ||||||||||
| 198 | |||||||||||
| 199 | http://datetime.perl.org/ | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||