Merge 'trunk' into 'sybase'
Rafael Kitover [Wed, 10 Jun 2009 11:12:08 +0000 (11:12 +0000)]
r5385@hlagh (orig r6528):  ribasushi | 2009-06-06 01:45:10 -0700
rename MC test dir
r5386@hlagh (orig r6529):  ribasushi | 2009-06-06 01:47:47 -0700
TODOified reentrancy counter - this shall be used to optimize MC some day
r5387@hlagh (orig r6530):  ribasushi | 2009-06-06 01:50:56 -0700
Port another forgotten MC fix
r5393@hlagh (orig r6537):  ribasushi | 2009-06-07 14:07:55 -0700
Fix for mysql subquery problem
r5394@hlagh (orig r6538):  ribasushi | 2009-06-07 14:36:43 -0700
Make empty/default inserts use standard SQL
r5395@hlagh (orig r6539):  ribasushi | 2009-06-07 15:59:21 -0700
Add mysql empty insert SQL override
Make SQLAHacks parts loadable at runtime via ensure_class_loaded
r5396@hlagh (orig r6540):  ribasushi | 2009-06-07 16:03:04 -0700
Make podcoverage happy
r5397@hlagh (orig r6541):  ribasushi | 2009-06-07 16:24:06 -0700
Fix find_or_new/create to stop returning random rows when default value insert is requested
r5398@hlagh (orig r6542):  ribasushi | 2009-06-08 02:36:56 -0700
Simply order_by/_virtual_order_by handling
r5406@hlagh (orig r6552):  ribasushi | 2009-06-08 14:56:41 -0700
duh
r5410@hlagh (orig r6556):  ash | 2009-06-09 03:20:34 -0700
Addjust bug to show problem with rows => 1 + child rel

r5411@hlagh (orig r6557):  ribasushi | 2009-06-09 04:12:46 -0700
Require a recent bugfixed Devel::Cycle
r5413@hlagh (orig r6559):  ash | 2009-06-09 06:07:30 -0700
Make IC::DT extra warning state the column name too

r5420@hlagh (orig r6574):  ribasushi | 2009-06-09 15:19:48 -0700
AuthorCheck fixes
r5424@hlagh (orig r6578):  ribasushi | 2009-06-09 15:52:17 -0700
 r6522@Thesaurus (orig r6521):  ribasushi | 2009-06-05 19:27:55 +0200
 New branch to try resultsource related stuff
 r6545@Thesaurus (orig r6544):  ribasushi | 2009-06-08 13:00:54 +0200
 First stab at adding resultsources to each join in select - works won-der-ful-ly
 r6546@Thesaurus (orig r6545):  ribasushi | 2009-06-08 13:14:08 +0200
 Commit failing test and thoughts on search arg deflation
 r6576@Thesaurus (orig r6575):  ribasushi | 2009-06-10 00:31:55 +0200
 Todoify DT in search deflation test until after 0.09
 r6577@Thesaurus (orig r6576):  ribasushi | 2009-06-10 00:48:07 +0200
 Factor out the $ident resolver

r5426@hlagh (orig r6580):  ribasushi | 2009-06-09 16:21:50 -0700
Move as_query out of the cursor
r5427@hlagh (orig r6581):  ribasushi | 2009-06-09 16:27:19 -0700
Think before commit
r5428@hlagh (orig r6582):  ribasushi | 2009-06-10 00:37:19 -0700
Clarify and disable rows/prefetch test - fix is easy, but architecturally unsound - need more time

Makefile.PL
lib/DBIx/Class/ResultSource/Table.pm
lib/DBIx/Class/Storage/DBI.pm
lib/DBIx/Class/Storage/DBI/NoBindVars.pm
lib/DBIx/Class/Storage/DBI/Sybase.pm
lib/DBIx/Class/Storage/DBI/Sybase/DateTime.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm [new file with mode: 0644]
t/746sybase.t
t/lib/DBICNSTest/Bogus/B.pm [new file with mode: 0644]

index 17871b8..ebdf8b0 100644 (file)
@@ -93,6 +93,7 @@ my %force_requires_if_author = (
   'Hash::Merge',                  => 0.11,
 
   # t/96_is_deteministic_value.t
+  # t/746sybase.t
   'DateTime::Format::Strptime' => 0,
 );
 
index cf263d1..90c9f9a 100644 (file)
@@ -26,6 +26,9 @@ Returns the FROM entry for the table (i.e. the table name)
 
 =cut
 
+use overload
+  '""' => \&from;    
+
 sub from { shift->name; }
 
 1;
index c9f816e..52cd456 100644 (file)
@@ -958,6 +958,22 @@ sub _fix_bind_params {
         } @bind;
 }
 
+sub _flatten_bind_params {
+    my ($self, @bind) = @_;
+
+    ### Turn @bind from something like this:
+    ###   ( [ "artist", 1 ], [ "cdid", 1, 3 ] )
+    ### to this:
+    ###   ( 1, 1, 3 )
+    return
+        map {
+            if ( defined( $_ && $_->[1] ) ) {
+                @{$_}[ 1 .. $#$_ ];
+            }
+            else { undef; }
+        } @bind;
+}
+
 sub _query_start {
     my ( $self, $sql, @bind ) = @_;
 
@@ -1308,13 +1324,20 @@ sub _resolve_ident_sources {
   return $alias2source;
 }
 
-sub count {
+sub _trim_attributes_for_count {
   my ($self, $source, $attrs) = @_;
+  my %attrs = %$attrs;
+
+  # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
+  delete @attrs{qw/select as rows offset page order_by record_filter/};
 
-  my $tmp_attrs = { %$attrs };
+  return \%attrs;
+}
+
+sub count {
+  my ($self, $source, $attrs) = @_;
 
-  # take off any pagers, record_filter is cdbi, and no point of ordering a count
-  delete $tmp_attrs->{$_} for (qw/select as rows offset page order_by record_filter/);
+  my $tmp_attrs = $self->_trim_attributes_for_count($source, $attrs);
 
   # overwrite the selector
   $tmp_attrs->{select} = { count => '*' };
index 7027ad6..7a48105 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 
 use base 'DBIx::Class::Storage::DBI';
+use Scalar::Util ();
+use Carp::Clan qw/^DBIx::Class/;
 
 =head1 NAME 
 
@@ -39,7 +41,7 @@ Manually subs in the values for the usual C<?> placeholders.
 sub _prep_for_execute {
   my $self = shift;
 
-  my ($op, $extra_bind, $ident) = @_;
+  my ($op, $extra_bind, $ident, $args) = @_;
 
   my ($sql, $bind) = $self->next::method(@_);
 
@@ -50,12 +52,17 @@ sub _prep_for_execute {
 
   foreach my $bound (@$bind) {
     my $col = shift @$bound;
+
     my $datatype = 'FIXME!!!';
+
+# this is what needs to happen:
+#    my $datatype = $rsrc->column_info($col)->{data_type};
+
     foreach my $data (@$bound) {
         if(ref $data) {
             $data = ''.$data;
         }
-        $data = $self->_dbh->quote($data);
+        $data = $self->_dbh->quote($data) if $self->should_quote_data_type($datatype, $data);
         $new_sql .= shift(@sql_part) . $data;
     }
   }
@@ -64,6 +71,25 @@ sub _prep_for_execute {
   return ($new_sql, []);
 }
 
+=head2 should_quote_data_type   
+                                
+This method is called by L</_prep_for_execute> for every column in
+order to determine if its value should be quoted or not. The arguments
+are the current column data type and the actual bind value. The return
+value is interpreted as: true - do quote, false - do not quote. You should
+override this in you Storage::DBI::<database> subclass, if your RDBMS
+does not like quotes around certain datatypes (e.g. Sybase and integer
+columns). The default method always returns true (do quote).
+                                
+ WARNING!!!                     
+                                
+ Always validate that the bind-value is valid for the current datatype.
+ Otherwise you may very well open the door to SQL injection attacks.
+                                
+=cut                            
+                                
+sub should_quote_data_type { 1 }
+
 =head1 AUTHORS
 
 Brandon Black <blblack@gmail.com>
index ec4fcf7..385ac30 100644 (file)
@@ -3,27 +3,102 @@ package DBIx::Class::Storage::DBI::Sybase;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI::NoBindVars/;
+use Class::C3;
+use base qw/DBIx::Class::Storage::DBI/;
+
+use Carp::Clan qw/^DBIx::Class/;
 
 sub _rebless {
-    my $self = shift;
+  my $self = shift;
+
+  if (ref($self) eq 'DBIx::Class::Storage::DBI::Sybase') {
+    my $dbtype = eval {
+      @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
+    } || '';
+
+    my $exception = $@;
+    $dbtype =~ s/\W/_/gi;
+    my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
+
+    if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
+      bless $self, $subclass;
+      $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;
 
-    my $dbtype = eval { @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2] };
-    unless ( $@ ) {
-        $dbtype =~ s/\W/_/gi;
-        my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
-        if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
-            bless $self, $subclass;
-            $self->_rebless;
-        }
+  sub _init_date_fmt {
+    my $self = shift;
+    my $dbh = $self->_dbh;
+
+    if ($dbh->can('syb_date_fmt')) {
+      $dbh->syb_date_fmt('ISO_strict');
+    } elsif (not $old_dbd_warned) {
+      carp "Your DBD::Sybase is too old to support ".
+      "DBIx::Class::InflateColumn::DateTime, please upgrade!";
+      $old_dbd_warned = 1;
     }
+
+    $dbh->do('set dateformat mdy');
+
+    1;
+  }
 }
 
 sub _dbh_last_insert_id {
-    my ($self, $dbh, $source, $col) = @_;
-    return ($dbh->selectrow_array('select @@identity'))[0];
+  my ($self, $dbh, $source, $col) = @_;
+
+  # sorry, there's no other way!
+  my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
+  return ($dbh->selectrow_array($sth))[0];
+}
+
+sub count {
+  my $self = shift;
+  my ($source, $attrs) = @_;
+
+  if (not exists $attrs->{rows}) {
+    return $self->next::method(@_);
+  }
+
+  my $offset = $attrs->{offset} || 0;
+  my $total  = $attrs->{rows} + $offset;
+
+  my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
+  $new_attrs->{select} = '1';
+  $new_attrs->{as}     = ['dummy'];
+
+  my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
+
+  $self->dbh->{syb_rowcount} = $total;
+
+  my $count = 0;
+  $count++ while $tmp_rs->cursor->next;
+
+  $self->dbh->{syb_rowcount} = 0;
+
+  return $count - $offset;
 }
 
+sub datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
+
 1;
 
 =head1 NAME
@@ -32,27 +107,40 @@ DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
 
 =head1 SYNOPSIS
 
-This subclass supports L<DBD::Sybase> for real Sybase databases.  If
-you are using an MSSQL database via L<DBD::Sybase>, see
-L<DBIx::Class::Storage::DBI::Sybase::MSSQL>.
+This subclass supports L<DBD::Sybase> for real Sybase databases.  If you are
+using an MSSQL database via L<DBD::Sybase>, your storage will be reblessed to
+L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
 
-=head1 CAVEATS
+=head1 DESCRIPTION
 
-This storage driver uses L<DBIx::Class::Storage::DBI::NoBindVars> as a base.
-This means that bind variables will be interpolated (properly quoted of course)
-into the SQL query itself, without using bind placeholders.
+If your version of Sybase does not support placeholders, then your storage
+will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
+also enable that driver explicitly, see the documentation for more details.
 
-More importantly this means that caching of prepared statements is explicitly
-disabled, as the interpolation renders it useless.
+With this driver there is unfortunately no way to get the C<last_insert_id>
+without doing a C<select max(col)>.
 
-=head1 AUTHORS
+But your queries will be cached.
+
+=head1 DATES
+
+On connection C<syb_date_fmt> is set to C<ISO_strict>, e.g.:
+C<2004-08-21T14:36:48.080Z> and C<dateformat> is set to C<mdy>, e.g.:
+C<08/13/1979 18:08:55.080>.
 
-Brandon L Black <blblack@gmail.com>
+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
+L<DBIx::Class::InflateColumn::DateTime>.
+
+=head1 AUTHORS
 
-Justin Hunter <justin.d.hunter@gmail.com>
+See L<DBIx::Class/CONTRIBUTORS>.
 
 =head1 LICENSE
 
 You may distribute this code under the same terms as Perl itself.
 
 =cut
+# vim:sts=2 sw=2:
diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/DateTime.pm b/lib/DBIx/Class/Storage/DBI/Sybase/DateTime.pm
new file mode 100644 (file)
index 0000000..9495665
--- /dev/null
@@ -0,0 +1,40 @@
+package # hide from PAUSE
+    DBIx::Class::Storage::DBI::Sybase::DateTime;
+
+use strict;
+use warnings;
+use DateTime::Format::Strptime;
+
+my $inflate_format = DateTime::Format::Strptime->new(
+  pattern => '%Y-%m-%dT%H:%M:%S.%3NZ'
+);
+
+my $deflate_format = DateTime::Format::Strptime->new(
+  pattern => '%m/%d/%Y %H:%M:%S.%3N'
+);
+
+sub parse_datetime  { shift; $inflate_format->parse_datetime(@_) }
+
+sub format_datetime { shift; $deflate_format->format_datetime(@_) }
+
+1;
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Sybase::DateTime - DateTime inflation/deflation
+support for Sybase in L<DBIx::Class>.
+
+=head1 DESCRIPTION
+
+This needs to become L<DateTime::Format::Sybase>.
+
+=head1 AUTHORS
+
+See L<DBIx::Class/CONTRIBUTORS>.
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+# vim:sts=2 sw=2:
index 58ac36f..cbde13d 100644 (file)
@@ -3,9 +3,10 @@ package DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server;
 use strict;
 use warnings;
 
+use Class::C3;
 use base qw/
   DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server
-  DBIx::Class::Storage::DBI::Sybase
+  DBIx::Class::Storage::DBI::NoBindVars
 /;
 
 1;
@@ -28,11 +29,12 @@ into the SQL query itself, without using bind placeholders.
 More importantly this means that caching of prepared statements is explicitly
 disabled, as the interpolation renders it useless.
 
-=head1 AUTHORS
+The actual driver code for MSSQL is in
+L<DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server>.
 
-Brandon L Black <blblack@gmail.com>
+=head1 AUTHORS
 
-Justin Hunter <justin.d.hunter@gmail.com>
+See L<DBIx::Class/CONTRIBUTORS>.
 
 =head1 LICENSE
 
diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm b/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm
new file mode 100644 (file)
index 0000000..f3a46f6
--- /dev/null
@@ -0,0 +1,94 @@
+package DBIx::Class::Storage::DBI::Sybase::NoBindVars;
+
+use Class::C3;
+use base qw/
+  DBIx::Class::Storage::DBI::NoBindVars
+  DBIx::Class::Storage::DBI::Sybase
+/;
+use List::Util ();
+use Scalar::Util ();
+
+sub _dbh_last_insert_id {
+  my ($self, $dbh, $source, $col) = @_;
+
+  # @@identity works only if not using placeholders
+  # Should this query be cached?
+  return ($dbh->selectrow_array('select @@identity'))[0];
+}
+
+my $number = sub { Scalar::Util::looks_like_number($_[0]) };
+
+my $decimal = sub { $_[0] =~ /^ [-+]? \d+ (?:\.\d*)? \z/x };
+
+my %noquote = (
+    int => sub { $_[0] =~ /^ [-+]? \d+ \z/x },
+    bit => => sub { $_[0] =~ /^[01]\z/ },
+    money => sub { $_[0] =~ /^\$ \d+ (?:\.\d*)? \z/x },
+    float => $number,
+    real => $number,
+    double => $number,
+    decimal => $decimal,
+    numeric => $decimal,
+);
+
+sub should_quote_data_type {
+  my $self = shift;
+  my ($type, $value) = @_;
+
+  return $self->next::method(@_) if not defined $value;
+
+## this is the correct method, but we have no type yet
+#  if (my $key = List::Util::first { $type =~ /$_/i } keys %noquote) {
+#    return 0 if $noquote{$key}->($value);
+#  }
+
+# temporary hack
+  return 0 if Scalar::Util::looks_like_number($value) ||
+    ($value =~ /^\$(\S*)\z/ && Scalar::Util::looks_like_number($1));
+
+  return $self->next::method(@_);
+}
+
+1;
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Sybase::NoBindVars - Storage::DBI subclass for Sybase
+without placeholder support
+
+=head1 DESCRIPTION
+
+If you're using this driver than your version of Sybase does not support
+placeholders. You can check with:
+
+  $dbh->{syb_dynamic_supported}
+
+You can also enable this driver explicitly using:
+
+  my $schema = SchemaClass->clone;
+  $schema->storage_type('::DBI::Sybase::NoBindVars');
+  $schema->connect($dsn, $user, $pass, \%opts);
+
+See the discussion in L<< DBD::Sybase/Using ? Placeholders & bind parameters to
+$sth->execute >> for details on the pros and cons of using placeholders.
+
+One advantage of not using placeholders is that C<select @@identity> will work
+for obtainging the last insert id of an C<IDENTITY> column, instead of having to
+do C<select max(col)> as the base Sybase driver does.
+
+When using this driver, bind variables will be interpolated (properly quoted of
+course) into the SQL query itself, without using placeholders.
+
+The caching of prepared statements is also explicitly disabled, as the
+interpolation renders it useless.
+
+=head1 AUTHORS
+
+See L<DBIx::Class/CONTRIBUTORS>.
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+# vim:sts=2 sw=2:
index f09862f..16823ba 100644 (file)
@@ -4,79 +4,145 @@ use warnings;
 use Test::More;
 use lib qw(t/lib);
 use DBICTest;
+use DBIx::Class::Storage::DBI::Sybase::DateTime;
 
 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
 
 plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $user);
 
-plan tests => 12;
+plan tests => (18 + 4*2)*2;
 
-my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
+my @storage_types = (
+  'DBI::Sybase',
+  'DBI::Sybase::NoBindVars',
+);
+my $schema;
 
-$schema->storage->ensure_connected;
-isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::Sybase' );
+for my $storage_type (@storage_types) {
+  $schema = DBICTest::Schema->clone;
 
-$schema->storage->dbh_do (sub {
-    my ($storage, $dbh) = @_;
-    eval { $dbh->do("DROP TABLE artist") };
-    $dbh->do(<<'SQL');
+  unless ($storage_type eq 'DBI::Sybase') { # autodetect
+    $schema->storage_type("::$storage_type");
+  }
+  $schema->connection($dsn, $user, $pass, {AutoCommit => 1});
 
+  $schema->storage->ensure_connected;
+
+  isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
+
+  $schema->storage->dbh_do (sub {
+      my ($storage, $dbh) = @_;
+      eval { $dbh->do("DROP TABLE artist") };
+      $dbh->do(<<'SQL');
 CREATE TABLE artist (
-   artistid INT IDENTITY NOT NULL,
+   artistid INT IDENTITY PRIMARY KEY,
    name VARCHAR(100),
    rank INT DEFAULT 13 NOT NULL,
-   charfield CHAR(10) NULL,
-   primary key(artistid)
+   charfield CHAR(10) NULL
 )
-
 SQL
+  });
 
-});
+  my %seen_id;
 
-my %seen_id;
-
-# fresh $schema so we start unconnected
-$schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
+# so we start unconnected
+  $schema->storage->disconnect;
 
 # test primary key handling
-my $new = $schema->resultset('Artist')->create({ name => 'foo' });
-ok($new->artistid > 0, "Auto-PK worked");
+  my $new = $schema->resultset('Artist')->create({ name => 'foo' });
+  ok($new->artistid > 0, "Auto-PK worked");
 
-$seen_id{$new->artistid}++;
+  $seen_id{$new->artistid}++;
 
-# test LIMIT support
-for (1..6) {
+  for (1..6) {
     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
     $seen_id{$new->artistid}++;
-}
+  }
 
-my $it;
+# test simple count
+  is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
 
-$it = $schema->resultset('Artist')->search( {}, {
-    rows => 3,
-    order_by => 'artistid',
-});
+# test LIMIT support
 
-TODO: {
-    local $TODO = 'Sybase is very very fucked in the limit department';
+## avoid quoting bug with NoBindVars for now
+#  my $it = $schema->resultset('Artist')->search({artistid => { '>' => 0 }}, {
 
-    is( $it->count, 3, "LIMIT count ok" );
-}
+  my $it = $schema->resultset('Artist')->search({}, {
+    rows => 3,
+    order_by => 'artistid',
+  });
 
-# The iterator still works correctly with rows => 3, even though the sql is
-# fucked, very interesting.
+  is( $it->count, 3, "LIMIT count ok" );
 
-is( $it->next->name, "foo", "iterator->next ok" );
-$it->next;
-is( $it->next->name, "Artist 2", "iterator->next ok" );
-is( $it->next, undef, "next past end of resultset ok" );
+  is( $it->next->name, "foo", "iterator->next ok" );
+  $it->next;
+  is( $it->next->name, "Artist 2", "iterator->next ok" );
+  is( $it->next, undef, "next past end of resultset ok" );
 
+# now try with offset
+  $it = $schema->resultset('Artist')->search({}, {
+    rows => 3,
+    offset => 3,
+    order_by => 'artistid',
+  });
+
+  is( $it->count, 3, "LIMIT with offset count ok" );
+
+  is( $it->next->name, "Artist 3", "iterator->next ok" );
+  $it->next;
+  is( $it->next->name, "Artist 5", "iterator->next ok" );
+  is( $it->next, undef, "next past end of resultset ok" );
+
+# now try a grouped count
+  $schema->resultset('Artist')->create({ name => 'Artist 6' })
+    for (1..6);
+
+  $it = $schema->resultset('Artist')->search({}, {
+    group_by => 'name'
+  });
+
+  is( $it->count, 7, 'COUNT of GROUP_BY ok' );
+
+# Test DateTime inflation with DATETIME
+  my @dt_types = (
+    ['DATETIME', '2004-08-21T14:36:48.080Z'],
+    ['SMALLDATETIME', '2004-08-21T14:36:00.000Z'], # minute precision
+  );
+  
+  for my $dt_type (@dt_types) {
+    my ($type, $sample_dt) = @$dt_type;
+
+    eval { $schema->storage->dbh->do("DROP TABLE track") };
+    $schema->storage->dbh->do(<<"SQL");
+CREATE TABLE track (
+   trackid INT IDENTITY PRIMARY KEY,
+   cd INT,
+   position INT,
+   last_updated_on $type,
+)
+SQL
+    ok(my $dt = DBIx::Class::Storage::DBI::Sybase::DateTime
+      ->parse_datetime($sample_dt));
+
+    my $row;
+    ok( $row = $schema->resultset('Track')->create({
+      last_updated_on => $dt,
+      cd => 1,
+    }));
+    ok( $row = $schema->resultset('Track')
+      ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
+      ->first
+    );
+    is( $row->updated_date, $dt, 'DateTime inflation works' );
+  }
+}
 
 # clean up our mess
 END {
-    my $dbh = eval { $schema->storage->_dbh };
-    $dbh->do('DROP TABLE artist') if $dbh;
+  if (my $dbh = eval { $schema->storage->_dbh }) {
+    $dbh->do('DROP TABLE artist');
+    $dbh->do('DROP TABLE track');
+  }
 }
-
diff --git a/t/lib/DBICNSTest/Bogus/B.pm b/t/lib/DBICNSTest/Bogus/B.pm
new file mode 100644 (file)
index 0000000..e9cdc37
--- /dev/null
@@ -0,0 +1,6 @@
+package DBICNSTest::Result::B;
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/PK::Auto Core/);
+__PACKAGE__->table('b');
+__PACKAGE__->add_columns('b');
+1;