From: Rafael Kitover Date: Thu, 22 Apr 2010 10:46:20 +0000 (+0000) Subject: refactor Strptime-based DateTime parsers X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=13ede1a9f0b8a9ad455b047da213b307b31ee724;p=dbsrgits%2FDBIx-Class-Historic.git refactor Strptime-based DateTime parsers Add a new Storage API, ->datetime_parse_via to replace ->datetime_parser_type which also takes Strptime formats and creates the parsers based on those formats instead of having to define parser classes for those formats in the Storage classes. --- diff --git a/Changes b/Changes index 2fef676..4d64a57 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for DBIx::Class + - Deprecate $storage->datetime_parser_type in favor of new method + datetime_parse_via, which also accepts strptime formats (see docs in + ::Storage::DBI.) This only affects storage driver writers. + 0.08200 2012-08-24 (UTC) * Fixes - Change one of the new tests for the previous release to not require diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index ac84176..8bfe7f3 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -23,13 +23,13 @@ use namespace::clean; __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::Cursor'); __PACKAGE__->mk_group_accessors('inherited' => qw/ - sql_limit_dialect sql_quote_char sql_name_sep + sql_limit_dialect sql_quote_char sql_name_sep datetime_strptime_formats /); -__PACKAGE__->mk_group_accessors('component_class' => qw/sql_maker_class datetime_parser_type/); +__PACKAGE__->mk_group_accessors('component_class' => qw/sql_maker_class datetime_parser_class/); __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker'); -__PACKAGE__->datetime_parser_type('DateTime::Format::MySQL'); # historic default +__PACKAGE__->datetime_parser_class('DateTime::Format::MySQL'); # historic default __PACKAGE__->sql_name_sep('.'); @@ -91,7 +91,9 @@ my @rdbms_specific_methods = qw/ sqlt_type sql_maker build_datetime_parser + datetime_parser_class datetime_parser_type + datetime_parse_via txn_begin insert @@ -2842,7 +2844,101 @@ sub datetime_parser { =head2 datetime_parser_type -Defines the datetime parser class - currently defaults to L +B<*DEPRECATED*> + +Defines the datetime parser class - currently defaults to +L. Use L instead. + +=cut + +sub datetime_parser_type { + my $self = shift; + carp_unique 'this method is deprecated, use datetime_parse_via instead'; + return $self->datetime_parse_via(@_); +} + +=head2 datetime_parse_via + +Sets or returns the class used for parsing datetimes. + +Can also take a hashref specifying L formats, in +which case the parser class will be generated dynamically (and returned.) + +The format of the hashref entries is C<< type => strptime_format >> or +C<< type => { format => strptime_format, parse => strptime_format } >>. + +For example: + + { + datetime => '%Y-%m-%d %H:%M:%S.%3N', + smalldatetime => '%Y-%m-%d %H:%M:%S', + } + +or + + { + datetime => { + parse => '%Y-%m-%dT%H:%M:%S.%3NZ', + format => '%Y-%m-%d %H:%M:%S.%3N', + } + } + +=cut + +sub datetime_parse_via { + my $self = shift; + + return $self->datetime_parser_class if not @_; + + my $via = shift; + + if (not ref $via) { + $self->datetime_parser_class($via); + return $via; + } + elsif (ref $via ne 'HASH') { + $self->throw_exception( +'datetime_parse_via argument must be class name or hashref of Strptime formats' + ); + } + + my $class = ref $self || $self; + my ($class_basename) = $class =~ /^@{[__PACKAGE__]}::(.*)\z/; + + $class_basename ||= $class; # custom storage + + my $parser_class = "DBIx::Class::DateTime::Format::${class_basename}"; + + while (my ($type, $format) = each %$via) { + $format = { parse => $format, format => $format } if not ref $format; + + for my $action (qw/parse format/) { + my $method = "${parser_class}::${action}_$type"; + + { + no strict 'refs'; + + my $parser; + my $strptime_method; + + *$method = subname $method => sub { + shift; + $parser ||= DateTime::Format::Strptime->new( + pattern => $format->{$action}, + on_error => 'croak', + ); + $strptime_method ||= $parser->can("${action}_datetime"); + return $parser->$strptime_method(shift); + }; + } + } + } + + $self->datetime_strptime_formats($via); + $self->datetime_parser_class($parser_class); + + return $parser_class; +} =head2 build_datetime_parser @@ -2852,8 +2948,14 @@ See L sub build_datetime_parser { my $self = shift; - my $type = $self->datetime_parser_type(@_); - return $type; + + my $parser_class = $self->datetime_parse_via(@_); + + if ($self->datetime_strptime_formats) { + require DateTime::Format::Strptime; + } + + return $parser_class; } diff --git a/lib/DBIx/Class/Storage/DBI/ADO/MS_Jet.pm b/lib/DBIx/Class/Storage/DBI/ADO/MS_Jet.pm index 8eb1719..1b1b02c 100644 --- a/lib/DBIx/Class/Storage/DBI/ADO/MS_Jet.pm +++ b/lib/DBIx/Class/Storage/DBI/ADO/MS_Jet.pm @@ -12,6 +12,10 @@ use namespace::clean; __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::ADO::MS_Jet::Cursor'); +__PACKAGE__->datetime_parse_via({ + datetime => '%m/%d/%Y %I:%M:%S %p', +}); + =head1 NAME DBIx::Class::Storage::DBI::ADO::MS_Jet - Support for MS Access over ADO @@ -111,36 +115,6 @@ sub select_single { return @row; } -sub datetime_parser_type { - 'DBIx::Class::Storage::DBI::ADO::MS_Jet::DateTime::Format' -} - -package # hide from PAUSE - DBIx::Class::Storage::DBI::ADO::MS_Jet::DateTime::Format; - -my $datetime_format = '%m/%d/%Y %I:%M:%S %p'; -my $datetime_parser; - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server.pm index 6fb1b19..ed53ffc 100644 --- a/lib/DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server.pm @@ -27,6 +27,10 @@ __PACKAGE__->new_guid(sub { return $guid; }); +__PACKAGE__->datetime_parse_via({ + datetime => '%m/%d/%Y %I:%M:%S %p', +}); + =head1 NAME DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server - Support for Microsoft @@ -410,32 +414,6 @@ sub _mssql_max_data_type_representation_size_in_bytes { } } -package # hide from PAUSE - DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format; - -my $datetime_format = '%m/%d/%Y %I:%M:%S %p'; -my $datetime_parser; - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/DB2.pm b/lib/DBIx/Class/Storage/DBI/DB2.pm index 7634eb6..8d37469 100644 --- a/lib/DBIx/Class/Storage/DBI/DB2.pm +++ b/lib/DBIx/Class/Storage/DBI/DB2.pm @@ -8,7 +8,7 @@ use mro 'c3'; use Try::Tiny; use namespace::clean; -__PACKAGE__->datetime_parser_type('DateTime::Format::DB2'); +__PACKAGE__->datetime_parse_via('DateTime::Format::DB2'); __PACKAGE__->sql_quote_char ('"'); # lazy-default kind of thing diff --git a/lib/DBIx/Class/Storage/DBI/Informix.pm b/lib/DBIx/Class/Storage/DBI/Informix.pm index db953d4..7239705 100644 --- a/lib/DBIx/Class/Storage/DBI/Informix.pm +++ b/lib/DBIx/Class/Storage/DBI/Informix.pm @@ -11,10 +11,11 @@ use namespace::clean; __PACKAGE__->sql_limit_dialect ('SkipFirst'); __PACKAGE__->sql_quote_char ('"'); -__PACKAGE__->datetime_parser_type ( - 'DBIx::Class::Storage::DBI::Informix::DateTime::Format' -); +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S.%5N', + date => '%m/%d/%Y', +}); __PACKAGE__->mk_group_accessors('simple' => '__last_insert_id'); @@ -122,54 +123,6 @@ sub connect_call_datetime_setup { $ENV{GL_DATETIME} = "%Y-%m-%d %H:%M:%S%F5"; } -package # hide from PAUSE - DBIx::Class::Storage::DBI::Informix::DateTime::Format; - -my $timestamp_format = '%Y-%m-%d %H:%M:%S.%5N'; # %F %T -my $date_format = '%m/%d/%Y'; - -my ($timestamp_parser, $date_parser); - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->format_datetime(shift); -} - -sub parse_date { - shift; - require DateTime::Format::Strptime; - $date_parser ||= DateTime::Format::Strptime->new( - pattern => $date_format, - on_error => 'croak', - ); - return $date_parser->parse_datetime(shift); -} - -sub format_date { - shift; - require DateTime::Format::Strptime; - $date_parser ||= DateTime::Format::Strptime->new( - pattern => $date_format, - on_error => 'croak', - ); - return $date_parser->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/InterBase.pm b/lib/DBIx/Class/Storage/DBI/InterBase.pm index 5f5043b..4e2a8ab 100644 --- a/lib/DBIx/Class/Storage/DBI/InterBase.pm +++ b/lib/DBIx/Class/Storage/DBI/InterBase.pm @@ -7,6 +7,11 @@ use mro 'c3'; use Try::Tiny; use namespace::clean; +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S.%4N', + date => '%Y-%m-%d', +}); + =head1 NAME DBIx::Class::Storage::DBI::InterBase - Driver for the Firebird RDBMS via @@ -30,10 +35,6 @@ L. =cut -__PACKAGE__->datetime_parser_type( - 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format' -); - sub _ping { my $self = shift; @@ -135,55 +136,6 @@ sub connect_call_datetime_setup { $self->_get_dbh->{ib_time_all} = 'ISO'; } - -package # hide from PAUSE - DBIx::Class::Storage::DBI::InterBase::DateTime::Format; - -my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T -my $date_format = '%Y-%m-%d'; - -my ($timestamp_parser, $date_parser); - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->format_datetime(shift); -} - -sub parse_date { - shift; - require DateTime::Format::Strptime; - $date_parser ||= DateTime::Format::Strptime->new( - pattern => $date_format, - on_error => 'croak', - ); - return $date_parser->parse_datetime(shift); -} - -sub format_date { - shift; - require DateTime::Format::Strptime; - $date_parser ||= DateTime::Format::Strptime->new( - pattern => $date_format, - on_error => 'croak', - ); - return $date_parser->format_datetime(shift); -} - 1; =head1 CAVEATS diff --git a/lib/DBIx/Class/Storage/DBI/MSSQL.pm b/lib/DBIx/Class/Storage/DBI/MSSQL.pm index b20db9f..0ee20b4 100644 --- a/lib/DBIx/Class/Storage/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Storage/DBI/MSSQL.pm @@ -21,9 +21,10 @@ __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MSSQL'); __PACKAGE__->sql_quote_char([qw/[ ]/]); -__PACKAGE__->datetime_parser_type ( - 'DBIx::Class::Storage::DBI::MSSQL::DateTime::Format' -); +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S.%3N', # %F %T + smalldatetime => '%Y-%m-%d %H:%M:%S', +}); __PACKAGE__->new_guid('NEWID()'); @@ -189,54 +190,6 @@ sub _ping { }; } -package # hide from PAUSE - DBIx::Class::Storage::DBI::MSSQL::DateTime::Format; - -my $datetime_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T -my $smalldatetime_format = '%Y-%m-%d %H:%M:%S'; - -my ($datetime_parser, $smalldatetime_parser); - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->format_datetime(shift); -} - -sub parse_smalldatetime { - shift; - require DateTime::Format::Strptime; - $smalldatetime_parser ||= DateTime::Format::Strptime->new( - pattern => $smalldatetime_format, - on_error => 'croak', - ); - return $smalldatetime_parser->parse_datetime(shift); -} - -sub format_smalldatetime { - shift; - require DateTime::Format::Strptime; - $smalldatetime_parser ||= DateTime::Format::Strptime->new( - pattern => $smalldatetime_format, - on_error => 'croak', - ); - return $smalldatetime_parser->format_datetime(shift); -} - 1; =head1 NAME diff --git a/lib/DBIx/Class/Storage/DBI/ODBC/ACCESS.pm b/lib/DBIx/Class/Storage/DBI/ODBC/ACCESS.pm index 3a630cc..87d0dc3 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC/ACCESS.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/ACCESS.pm @@ -14,6 +14,10 @@ __PACKAGE__->mk_group_accessors(inherited => __PACKAGE__->disable_sth_caching_for_image_insert_or_update(1); +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S', # %F %T, no fractional part +}); + =head1 NAME DBIx::Class::Storage::DBI::ODBC::ACCESS - Support specific to MS Access over ODBC @@ -113,36 +117,6 @@ sub update { return $self->next::method(@_); } -sub datetime_parser_type { - 'DBIx::Class::Storage::DBI::ODBC::ACCESS::DateTime::Format' -} - -package # hide from PAUSE - DBIx::Class::Storage::DBI::ODBC::ACCESS::DateTime::Format; - -my $datetime_format = '%Y-%m-%d %H:%M:%S'; # %F %T, no fractional part -my $datetime_parser; - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_format, - on_error => 'croak', - ); - return $datetime_parser->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/ODBC/Firebird.pm b/lib/DBIx/Class/Storage/DBI/ODBC/Firebird.pm index ac0afbb..926ae8a 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC/Firebird.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/Firebird.pm @@ -10,6 +10,11 @@ use mro 'c3'; use Try::Tiny; use namespace::clean; +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S.%4N', + date => '%Y-%m-%d', +}); + =head1 NAME DBIx::Class::Storage::DBI::ODBC::Firebird - Driver for using the Firebird RDBMS @@ -31,8 +36,6 @@ makes it more suitable for long running processes such as under L. =cut -__PACKAGE__->datetime_parser_type ('DBIx::Class::Storage::DBI::ODBC::Firebird::DateTime::Format'); - # batch operations in DBD::ODBC 1.35 do not work with the official ODBC driver sub _run_connection_actions { my $self = shift; @@ -61,35 +64,6 @@ sub _exec_svp_rollback { }; } -package # hide from PAUSE - DBIx::Class::Storage::DBI::ODBC::Firebird::DateTime::Format; - -# inherit parse/format date -our @ISA = 'DBIx::Class::Storage::DBI::InterBase::DateTime::Format'; - -my $timestamp_format = '%Y-%m-%d %H:%M:%S.%4N'; # %F %T -my $timestamp_parser; - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $timestamp_parser ||= DateTime::Format::Strptime->new( - pattern => $timestamp_format, - on_error => 'croak', - ); - return $timestamp_parser->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm index c107934..6485537 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm @@ -14,7 +14,7 @@ use namespace::clean; __PACKAGE__->sql_limit_dialect ('RowNum'); __PACKAGE__->sql_quote_char ('"'); __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle'); -__PACKAGE__->datetime_parser_type('DateTime::Format::Oracle'); +__PACKAGE__->datetime_parse_via('DateTime::Format::Oracle'); sub __cache_queries_with_max_lob_parts { 2 } diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index d38f84c..eaf6361 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -13,7 +13,7 @@ use namespace::clean; __PACKAGE__->sql_limit_dialect ('LimitOffset'); __PACKAGE__->sql_quote_char ('"'); -__PACKAGE__->datetime_parser_type ('DateTime::Format::Pg'); +__PACKAGE__->datetime_parse_via('DateTime::Format::Pg'); __PACKAGE__->_use_multicolumn_in (1); sub _determine_supports_insert_returning { diff --git a/lib/DBIx/Class/Storage/DBI/Replicated.pm b/lib/DBIx/Class/Storage/DBI/Replicated.pm index 84d0c5d..8c379c6 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated.pm @@ -262,6 +262,9 @@ my $method_dispatch = { deployment_statements datetime_parser datetime_parser_type + datetime_parser_class + datetime_strptime_formats + datetime_parse_via build_datetime_parser last_insert_id insert @@ -1111,8 +1114,8 @@ using the Schema clone method. Based on code originated by: - Norbert Csongrádi - Peter Siklósi + Norbert Csongr?di + Peter Sikl?si =head1 LICENSE diff --git a/lib/DBIx/Class/Storage/DBI/SQLAnywhere.pm b/lib/DBIx/Class/Storage/DBI/SQLAnywhere.pm index 834a4d5..3ba58d0 100644 --- a/lib/DBIx/Class/Storage/DBI/SQLAnywhere.pm +++ b/lib/DBIx/Class/Storage/DBI/SQLAnywhere.pm @@ -17,6 +17,10 @@ __PACKAGE__->new_guid('UUIDTOSTR(NEWID())'); # default to the UUID decoding cursor, overridable by the user __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor'); +__PACKAGE__->datetime_parse_via({ + datetime => '%Y-%m-%d %H:%M:%S.%6N' +}); + =head1 NAME DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere @@ -137,21 +141,6 @@ sub select_single { return @row; } -# this sub stolen from MSSQL - -sub build_datetime_parser { - my $self = shift; - my $type = "DateTime::Format::Strptime"; - try { - eval "require ${type}" - } - catch { - $self->throw_exception("Couldn't load ${type}: $_"); - }; - - return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' ); -} - =head2 connect_call_datetime_setup Used as: diff --git a/lib/DBIx/Class/Storage/DBI/SQLite.pm b/lib/DBIx/Class/Storage/DBI/SQLite.pm index 6943c77..a7029ab 100644 --- a/lib/DBIx/Class/Storage/DBI/SQLite.pm +++ b/lib/DBIx/Class/Storage/DBI/SQLite.pm @@ -13,7 +13,7 @@ use namespace::clean; __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite'); __PACKAGE__->sql_limit_dialect ('LimitOffset'); __PACKAGE__->sql_quote_char ('"'); -__PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite'); +__PACKAGE__->datetime_parse_via ('DateTime::Format::SQLite'); =head1 NAME diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm index 8d1419f..30414f3 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm @@ -20,9 +20,13 @@ use namespace::clean; __PACKAGE__->sql_limit_dialect ('RowCountOrGenericSubQ'); __PACKAGE__->sql_quote_char ([qw/[ ]/]); -__PACKAGE__->datetime_parser_type( - 'DBIx::Class::Storage::DBI::Sybase::ASE::DateTime::Format' -); + +__PACKAGE__->datetime_parse_via({ + datetime => { + parse => '%Y-%m-%dT%H:%M:%S.%3NZ', + format => '%m/%d/%Y %H:%M:%S.%3N', + }, +}); __PACKAGE__->mk_group_accessors('simple' => qw/_identity _identity_method _blob_log_on_update _parent_storage @@ -883,9 +887,8 @@ sub connect_call_datetime_setup { # of format. Not changing as a bugwards compat, though in reality # the only piece that sees the results of $dt object formatting # (as opposed to parsing) is the database itself, so theoretically - # changing both this SET command and the formatter definition of - # ::S::D::Sybase::ASE::DateTime::Format below should be safe and - # transparent + # changing both this SET command and the formatter definition + # should be safe and transparent $dbh->do('SET DATEFORMAT mdy'); } @@ -921,34 +924,6 @@ sub _exec_svp_rollback { $self->_dbh->do("ROLLBACK TRANSACTION $name"); } -package # hide from PAUSE - DBIx::Class::Storage::DBI::Sybase::ASE::DateTime::Format; - -my $datetime_parse_format = '%Y-%m-%dT%H:%M:%S.%3NZ'; -my $datetime_format_format = '%m/%d/%Y %H:%M:%S.%3N'; - -my ($datetime_parser, $datetime_formatter); - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_parse_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_formatter ||= DateTime::Format::Strptime->new( - pattern => $datetime_format_format, - on_error => 'croak', - ); - return $datetime_formatter->format_datetime(shift); -} - 1; =head1 Schema::Loader Support diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm index b3f048c..7139ddb 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm @@ -8,8 +8,15 @@ use base qw/ DBIx::Class::Storage::DBI::MSSQL /; use mro 'c3'; - use DBIx::Class::Carp; +use namespace::clean; + +__PACKAGE__->datetime_parse_via({ + datetime => { + parse => '%Y-%m-%dT%H:%M:%S.%3NZ', + format => '%Y-%m-%d %H:%M:%S.%3N', + }, +}); =head1 NAME @@ -34,10 +41,6 @@ L. =cut -__PACKAGE__->datetime_parser_type( - 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format' -); - sub _rebless { my $self = shift; my $dbh = $self->_get_dbh; @@ -141,35 +144,6 @@ sub connect_call_datetime_setup { } } - -package # hide from PAUSE - DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format; - -my $datetime_parse_format = '%Y-%m-%dT%H:%M:%S.%3NZ'; -my $datetime_format_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T - -my ($datetime_parser, $datetime_formatter); - -sub parse_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_parser ||= DateTime::Format::Strptime->new( - pattern => $datetime_parse_format, - on_error => 'croak', - ); - return $datetime_parser->parse_datetime(shift); -} - -sub format_datetime { - shift; - require DateTime::Format::Strptime; - $datetime_formatter ||= DateTime::Format::Strptime->new( - pattern => $datetime_format_format, - on_error => 'croak', - ); - return $datetime_formatter->format_datetime(shift); -} - 1; =head1 AUTHOR diff --git a/t/inflate/datetime_missing_deps.t b/t/inflate/datetime_missing_deps.t index 680a3f1..460d871 100644 --- a/t/inflate/datetime_missing_deps.t +++ b/t/inflate/datetime_missing_deps.t @@ -9,7 +9,7 @@ use DBICTest; my $no_class = '_DBICTEST_NONEXISTENT_CLASS_'; my $schema = DBICTest->init_schema(); -$schema->storage->datetime_parser_type($no_class); +$schema->storage->datetime_parse_via($no_class); my $event = $schema->resultset('Event')->find(1); diff --git a/t/inflate/datetime_sybase.t b/t/inflate/datetime_sybase.t index 597f6a3..fb91af3 100644 --- a/t/inflate/datetime_sybase.t +++ b/t/inflate/datetime_sybase.t @@ -15,6 +15,8 @@ DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ase') unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt') && DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ase'); +$ENV{DBIC_SYBASE_FREETDS_NOWARN} = 1; + my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; if (not ($dsn && $user)) {