[Bast-commits] r6876 - in DBIx-Class/0.08/branches/sybase: lib/DBIx/Class/ResultSource lib/DBIx/Class/Storage lib/DBIx/Class/Storage/DBI lib/DBIx/Class/Storage/DBI/Sybase t t/inflate

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Tue Jun 30 10:46:43 GMT 2009


Author: caelum
Date: 2009-06-30 10:46:43 +0000 (Tue, 30 Jun 2009)
New Revision: 6876

Added:
   DBIx-Class/0.08/branches/sybase/t/inflate/datetime_sybase.t
Modified:
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/ResultSource/Table.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/NoBindVars.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm
   DBIx-Class/0.08/branches/sybase/t/746sybase.t
Log:
code cleanups

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/ResultSource/Table.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/ResultSource/Table.pm	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/ResultSource/Table.pm	2009-06-30 10:46:43 UTC (rev 6876)
@@ -26,9 +26,6 @@
 
 =cut
 
-use overload
-  '""' => \&from;    
-
 sub from { shift->name; }
 
 1;

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/NoBindVars.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/NoBindVars.pm	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/NoBindVars.pm	2009-06-30 10:46:43 UTC (rev 6876)
@@ -65,11 +65,12 @@
     my $datatype = $rsrc && $rsrc->column_info($col)->{data_type};
 
     foreach my $data (@$bound) {
-        $data = ''.$data if ref $data;
+      $data = ''.$data if ref $data;
 
-        $data = $self->_dbh->quote($data) if $self->should_quote($datatype, $data);
+      $data = $self->_dbh->quote($data)
+        if $self->should_quote_value($datatype, $data);
 
-        $new_sql .= shift(@sql_part) . $data;
+      $new_sql .= shift(@sql_part) . $data;
     }
   }
   $new_sql .= join '', @sql_part;
@@ -77,7 +78,7 @@
   return ($new_sql, []);
 }
 
-=head2 should_quote
+=head2 should_quote_value
                                 
 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
@@ -94,7 +95,7 @@
                                 
 =cut                            
                                 
-sub should_quote { 1 }
+sub should_quote_value { 1 }
 
 =head1 AUTHORS
 

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm	2009-06-30 10:46:43 UTC (rev 6876)
@@ -31,21 +31,21 @@
     numeric => $decimal,
 );
 
-sub should_quote {
+sub should_quote_value {
   my $self = shift;
   my ($type, $value) = @_;
 
-  return $self->next::method(@_) if not defined $value;
+  return $self->next::method(@_) if not defined $value or not defined $type;
 
-  $type ||= '';
-
   if (my $key = List::Util::first { $type =~ /$_/i } keys %noquote) {
     return 0 if $noquote{$key}->($value);
-  } elsif (not $type) {
-# try to guess based on value
-    return 0 if $number->($value) || $noquote->{money}->($value);
   }
 
+## try to guess based on value
+#  elsif (not $type) {
+#    return 0 if $number->($value) || $noquote->{money}->($value);
+#  }
+
   return $self->next::method(@_);
 }
 

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-06-30 10:46:43 UTC (rev 6876)
@@ -101,21 +101,49 @@
 }
 
 sub insert {
-  my $self = shift;
-  my ($source, $to_insert) = @_;
+  my ($self, $source, $to_insert) = splice @_, 0, 3;
 
+  my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
+
+  my $updated_cols = $self->next::method($source, $to_insert, @_);
+
+  $self->_update_blobs($source, $blob_cols, $to_insert) if %$blob_cols;
+
+  return $updated_cols;
+}
+
+#sub update {
+#  my ($self, $source) = splice @_, 0, 2;
+#  my ($fields)        = @_;
+#
+#  my $blob_cols = $self->_remove_blob_cols($source, $fields);
+#
+#  my @res = 1;
+#
+#  if (%$fields) {
+#    if (wantarray) {
+#      @res    = $self->next::method($source, @_);
+#    } else {
+#      $res[0] = $self->next::method($source, @_);
+#    }
+#  }
+#
+#  $self->_update_blobs($source, $blob_cols, $fields) if %$blob_cols;
+#
+#  return wantarray ? @res : $res[0];
+#}
+
+sub _remove_blob_cols {
+  my ($self, $source, $fields) = @_;
+
   my %blob_cols;
 
-  for my $col (keys %$to_insert) {
-    $blob_cols{$col} = delete $to_insert->{$col}
+  for my $col (keys %$fields) {
+    $blob_cols{$col} = delete $fields->{$col}
       if $self->_is_lob_type($source->column_info($col)->{data_type});
   }
 
-  my $updated_cols = $self->next::method(@_);
-
-  $self->_update_blobs($source, \%blob_cols, $to_insert) if %blob_cols;
-
-  return $updated_cols;
+  return \%blob_cols;
 }
 
 sub _update_blobs {

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI.pm	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI.pm	2009-06-30 10:46:43 UTC (rev 6876)
@@ -1065,22 +1065,6 @@
         } @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 ) = @_;
 

Modified: DBIx-Class/0.08/branches/sybase/t/746sybase.t
===================================================================
--- DBIx-Class/0.08/branches/sybase/t/746sybase.t	2009-06-30 10:40:46 UTC (rev 6875)
+++ DBIx-Class/0.08/branches/sybase/t/746sybase.t	2009-06-30 10:46:43 UTC (rev 6876)
@@ -5,15 +5,18 @@
 use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
-use DateTime::Format::Sybase;
 
 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);
+if (not ($dsn && $user)) {
+  plan skip_all =>
+    'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
+    "\nWarning: This test drops and creates the tables " .
+    "'artist' and 'bindtype_test'";
+} else {
+  plan tests => (26 + 2)*2;
+}
 
-plan tests => (26 + 4*2)*2;
-
 my @storage_types = (
   'DBI::Sybase',
   'DBI::Sybase::NoBindVars',
@@ -29,7 +32,6 @@
   $schema->connection($dsn, $user, $pass, {
     AutoCommit => 1,
     on_connect_call => [
-      [ 'datetime_setup' ],
       [ blob_setup => log_on_update => 1 ], # this is a safer option
     ],
   });
@@ -110,38 +112,6 @@
 
   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 = DateTime::Format::Sybase->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' );
-  }
-
 # mostly stole the blob stuff Nniuq wrote for t/73oracle.t
   my $dbh = $schema->storage->dbh;
   {
@@ -183,13 +153,26 @@
       } eq $binstr{$size}, "verified inserted $size $type" );
     }
   }
+
+  # try a blob update
+  TODO: {
+    local $TODO = 'updating TEXT/IMAGE does not work yet';
+
+    my $new_str = $binstr{large} . 'foo';
+    eval { $rs->search({ id => $id })->update({ blob => $new_str }) };
+    ok !$@, 'updated blob successfully';
+    diag $@ if $@;
+    ok(eval {
+      $rs->search({ id=> $id }, { select => ['blob'] })->single->blob
+    } eq $new_str, "verified updated blob" );
+    diag $@ if $@;
+  }
 }
 
 # clean up our mess
 END {
   if (my $dbh = eval { $schema->storage->_dbh }) {
     $dbh->do('DROP TABLE artist');
-    $dbh->do('DROP TABLE track');
     $dbh->do('DROP TABLE bindtype_test');
   }
 }

Added: DBIx-Class/0.08/branches/sybase/t/inflate/datetime_sybase.t
===================================================================
--- DBIx-Class/0.08/branches/sybase/t/inflate/datetime_sybase.t	                        (rev 0)
+++ DBIx-Class/0.08/branches/sybase/t/inflate/datetime_sybase.t	2009-06-30 10:46:43 UTC (rev 6876)
@@ -0,0 +1,83 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use Test::Exception;
+use lib qw(t/lib);
+use DBICTest;
+
+my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
+
+if (not ($dsn && $user)) {
+  plan skip_all =>
+    'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
+    "\nWarning: This test drops and creates a table called 'track'";
+} else {
+  eval "use DateTime; use DateTime::Format::Sybase;";
+  if ($@) {
+    plan skip_all => 'needs DateTime and DateTime::Format::Sybase for testing';
+  }
+  else {
+    plan tests => (4 * 2 * 2) + 2; # (tests * dt_types * storage_types) + storage_tests
+  }
+}
+
+my @storage_types = (
+  'DBI::Sybase',
+  'DBI::Sybase::NoBindVars',
+);
+my $schema;
+
+for my $storage_type (@storage_types) {
+  $schema = DBICTest::Schema->clone;
+
+  unless ($storage_type eq 'DBI::Sybase') { # autodetect
+    $schema->storage_type("::$storage_type");
+  }
+  $schema->connection($dsn, $user, $pass, {
+    AutoCommit => 1,
+    on_connect_call => [ 'datetime_setup' ],
+  });
+
+  $schema->storage->ensure_connected;
+
+  isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
+
+  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 = DateTime::Format::Sybase->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 roundtrip' );
+  }
+}
+
+# clean up our mess
+END {
+  if (my $dbh = eval { $schema->storage->_dbh }) {
+    $dbh->do('DROP TABLE track');
+  }
+}




More information about the Bast-commits mailing list