switch to DateTime::Format::Sybase
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index e5acf8d..e60b5e2 100644 (file)
@@ -3,6 +3,7 @@ package DBIx::Class::Storage::DBI::Sybase;
 use strict;
 use warnings;
 
+use Class::C3;
 use base qw/DBIx::Class::Storage::DBI/;
 
 use Carp::Clan qw/^DBIx::Class/;
@@ -22,19 +23,29 @@ sub _rebless {
     if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
       bless $self, $subclass;
       $self->_rebless;
-    } elsif (not $self->dbh->{syb_dynamic_supported}) {
-      bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
-      $self->_rebless;
+    } else {
+      # real Sybase
+      if (not $self->dbh->{syb_dynamic_supported}) {
+        bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
+        $self->_rebless;
+      }
+      $self->_init_date_fmt;
     }
   }
 }
 
+sub _populate_dbh {
+  my $self = shift;
+  $self->next::method(@_);
+  $self->_init_date_fmt;
+  1;
+}
+
 {
   my $old_dbd_warned = 0;
 
-  sub _populate_dbh {
+  sub _init_date_fmt {
     my $self = shift;
-    $self->next::method(@_);
     my $dbh = $self->_dbh;
 
     if ($dbh->can('syb_date_fmt')) {
@@ -60,36 +71,35 @@ sub _dbh_last_insert_id {
 }
 
 sub count {
-  my ($self, $source, $attrs) = @_;
+  my $self = shift;
+  my ($source, $attrs) = @_;
 
-  if (exists $attrs->{rows}) {
-    my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
+  if (not exists $attrs->{rows}) {
+    return $self->next::method(@_);
+  }
 
-    $new_attrs->{select} = '1';
-    $new_attrs->{as}     = ['dummy'];
+  my $offset = $attrs->{offset} || 0;
+  my $total  = $attrs->{rows} + $offset;
 
-# speed things up at least *a little*
-    $new_attrs->{result_class} = 'DBIx::Class::ResultClass::HashRefInflator';
+  my $new_attrs = $self->_copy_attributes_for_count($source, $attrs);
 
-    my $offset = $attrs->{offset} || 0;
-    my $total  = $attrs->{rows} + $offset;
-    
-    $self->dbh->do("set rowcount $total");
+  my $first_pk = ($source->primary_columns)[0];
 
-    my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
-    
-    my $count = 0;
-    $count++ while $tmp_rs->cursor->next;
+  $new_attrs->{select} = $first_pk ? "me.$first_pk" : 1;
 
-    $self->dbh->do("set rowcount 0");
+  my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
 
-    return $count;
-  }
+  $self->dbh->{syb_rowcount} = $total;
+
+  my $count = 0;
+  $count++ while $tmp_rs->cursor->next;
+
+  $self->dbh->{syb_rowcount} = 0;
 
-  return $self->next::method(@_);
+  return $count - $offset;
 }
 
-sub datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
+sub datetime_parser_type { "DateTime::Format::Sybase" }
 
 1;
 
@@ -123,7 +133,7 @@ C<08/13/1979 18:08:55.080>.
 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
 C<SMALLDATETIME> columns only have minute precision.
 
-You will need the L<DateTime::Format::Strptime> module if you are going to use
+You will need the L<DateTime::Format::Sybase> module if you are going to use
 L<DBIx::Class::InflateColumn::DateTime>.
 
 =head1 AUTHORS