From: Rafael Kitover Date: Tue, 30 Jun 2009 10:46:43 +0000 (+0000) Subject: code cleanups X-Git-Tag: v0.08112~14^2~113 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7d17f469081c4932520458014b820daa8d3e986e;p=dbsrgits%2FDBIx-Class.git code cleanups --- diff --git a/lib/DBIx/Class/ResultSource/Table.pm b/lib/DBIx/Class/ResultSource/Table.pm index 90c9f9a..cf263d1 100644 --- a/lib/DBIx/Class/ResultSource/Table.pm +++ b/lib/DBIx/Class/ResultSource/Table.pm @@ -26,9 +26,6 @@ Returns the FROM entry for the table (i.e. the table name) =cut -use overload - '""' => \&from; - sub from { shift->name; } 1; diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 938a2f0..1cbc7ef 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1065,22 +1065,6 @@ 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 ) = @_; diff --git a/lib/DBIx/Class/Storage/DBI/NoBindVars.pm b/lib/DBIx/Class/Storage/DBI/NoBindVars.pm index b900f58..588f47d 100644 --- a/lib/DBIx/Class/Storage/DBI/NoBindVars.pm +++ b/lib/DBIx/Class/Storage/DBI/NoBindVars.pm @@ -65,11 +65,12 @@ sub _prep_for_execute { 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 @@ sub _prep_for_execute { return ($new_sql, []); } -=head2 should_quote +=head2 should_quote_value This method is called by L for every column in order to determine if its value should be quoted or not. The arguments @@ -94,7 +95,7 @@ columns). The default method always returns true (do quote). =cut -sub should_quote { 1 } +sub should_quote_value { 1 } =head1 AUTHORS diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index fa31f68..c985019 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -101,21 +101,49 @@ sub _is_lob_type { } 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 { diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm b/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm index 8f80c3d..8c63d8c 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm @@ -31,21 +31,21 @@ my %noquote = ( numeric => $decimal, ); -sub should_quote { +sub should_quote_value { my $self = shift; my ($type, $value) = @_; - return $self->next::method(@_) if not defined $value; - - $type ||= ''; + return $self->next::method(@_) if not defined $value or not defined $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(@_); } diff --git a/t/746sybase.t b/t/746sybase.t index 2d18ef0..a8f9815 100644 --- a/t/746sybase.t +++ b/t/746sybase.t @@ -5,14 +5,17 @@ use Test::More; 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); - -plan tests => (26 + 4*2)*2; +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; +} my @storage_types = ( 'DBI::Sybase', @@ -29,7 +32,6 @@ for my $storage_type (@storage_types) { $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 @@ SQL 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 @@ SQL } 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'); } } diff --git a/t/inflate/datetime_sybase.t b/t/inflate/datetime_sybase.t new file mode 100644 index 0000000..13edcb5 --- /dev/null +++ b/t/inflate/datetime_sybase.t @@ -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'); + } +}