[Bast-commits] r9196 - in
DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI:
. Sybase
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Thu Apr 22 10:46:21 GMT 2010
Author: caelum
Date: 2010-04-22 11:46:20 +0100 (Thu, 22 Apr 2010)
New Revision: 9196
Added:
DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/StrptimeParserMaker.pm
Modified:
DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/MSSQL.pm
DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
Log:
implementation for MSSQL (ODBC and DBD::Sybase)
Modified: DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/MSSQL.pm
===================================================================
--- DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/MSSQL.pm 2010-04-22 10:31:47 UTC (rev 9195)
+++ DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/MSSQL.pm 2010-04-22 10:46:20 UTC (rev 9196)
@@ -7,6 +7,7 @@
use mro 'c3';
use List::Util();
+use DBIx::Class::Storage::DBI::StrptimeParserMaker();
__PACKAGE__->mk_group_accessors(simple => qw/
_identity _identity_method
@@ -190,10 +191,6 @@
$self->_get_dbh->do("ROLLBACK TRANSACTION $name");
}
-sub datetime_parser_type {
- 'DBIx::Class::Storage::DBI::MSSQL::DateTime::Format'
-}
-
sub sqlt_type { 'SQLServer' }
sub sql_maker {
@@ -231,54 +228,17 @@
return $@ ? 0 : 1;
}
-package # hide from PAUSE
- DBIx::Class::Storage::DBI::MSSQL::DateTime::Format;
+my $dt_parser_class = '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';
+DBIx::Class::Storage::DBI::StrptimeParserMaker->make_parser(
+ $dt_parser_class, {
+ datetime => '%Y-%m-%d %H:%M:%S.%3N', # %F %T
+ smalldatetime => '%Y-%m-%d %H:%M:%S',
+ },
+);
-my ($datetime_parser, $smalldatetime_parser);
+sub datetime_parser_type { $dt_parser_class }
-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
Added: DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/StrptimeParserMaker.pm
===================================================================
--- DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/StrptimeParserMaker.pm (rev 0)
+++ DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/StrptimeParserMaker.pm 2010-04-22 10:46:20 UTC (rev 9196)
@@ -0,0 +1,39 @@
+package # hide from PAUSE
+ DBIx::Class::Storage::DBI::StrptimeParserMaker;
+
+use strict;
+use warnings;
+use Sub::Name ();
+
+sub make_parser {
+ my ($class, $dest_class, $methods) = @_;
+
+ while (my ($type, $format) = each %$methods) {
+ $format = { parse => $format, format => $format } if not ref $format;
+
+ for my $action (qw/parse format/) {
+ my $method = "${dest_class}::${action}_$type";
+
+ {
+ no strict 'refs';
+
+ my $parser;
+ my $strptime_method;
+
+ *$method = Sub::Name::subname $method => sub {
+ shift;
+ require DateTime::Format::Strptime;
+ $parser ||= DateTime::Format::Strptime->new(
+ pattern => $format->{$action},
+ on_error => 'croak',
+ );
+ $strptime_method ||= $parser->can("${action}_datetime");
+ return $parser->$strptime_method(shift);
+ };
+ }
+ }
+ }
+}
+
+1;
+# vim:et sts=2 sw=2 tw=80:
Modified: DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
===================================================================
--- DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm 2010-04-22 10:31:47 UTC (rev 9195)
+++ DBIx-Class/0.08/branches/strptime_parser_maker/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm 2010-04-22 10:46:20 UTC (rev 9196)
@@ -9,6 +9,7 @@
/;
use mro 'c3';
use Carp::Clan qw/^DBIx::Class/;
+use DBIx::Class::Storage::DBI::StrptimeParserMaker ();
sub _rebless {
my $self = shift;
@@ -106,38 +107,20 @@
}
}
-sub datetime_parser_type {
- 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format'
-}
+my $dt_parser_class =
+ 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format';
-package # hide from PAUSE
- DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format;
+DBIx::Class::Storage::DBI::StrptimeParserMaker->make_parser(
+ $dt_parser_class, {
+ datetime => {
+ parse => '%Y-%m-%dT%H:%M:%S.%3NZ',
+ format => '%Y-%m-%d %H:%M:%S.%3N',
+ },
+ },
+);
-my $datetime_parse_format = '%Y-%m-%dT%H:%M:%S.%3NZ';
-my $datetime_format_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T
+sub datetime_parser_type { $dt_parser_class }
-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 NAME
More information about the Bast-commits
mailing list