Merge 'trunk' into 'table_name_ref'
Rafael Kitover [Tue, 28 Jul 2009 01:03:47 +0000 (21:03 -0400)]
17 files changed:
Makefile.PL
lib/DBIx/Class/InflateColumn/DateTime.pm
lib/DBIx/Class/Manual/Cookbook.pod
lib/DBIx/Class/Manual/Example.pod
lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm
lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
lib/DBIx/Class/Storage/DBI/Replicated.pm
t/746mssql.t
t/74mssql.t
t/99dbic_sqlt_parser.t
t/bind/attribute.t
t/inflate/datetime_pg.t
t/lib/DBICTest/Schema.pm
t/lib/DBICTest/Schema/CustomSql.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Event.pm
t/lib/DBICTest/Schema/EventTZPg.pm
t/lib/DBICTest/Schema/Money.pm [new file with mode: 0644]

index 5442378..2c65cfe 100644 (file)
@@ -84,9 +84,9 @@ my %force_requires_if_author = (
   'DateTime::Format::Strptime'=> 0,
 
   # t/93storage_replication.t
-  'Moose',                    => 0.77,
-  'MooseX::AttributeHelpers'  => 0.12,
-  'MooseX::Types',            => 0.10,
+  'Moose',                    => 0.87,
+  'MooseX::AttributeHelpers'  => 0.21,
+  'MooseX::Types',            => 0.16,
   'namespace::clean'          => 0.11,
   'Hash::Merge',              => 0.11,
 
index 1d19012..7e50807 100644 (file)
@@ -119,6 +119,9 @@ sub register_column {
     if ($type eq "timestamp with time zone" || $type eq "timestamptz") {
       $type = "timestamp";
       $info->{_ic_dt_method} ||= "timestamp_with_timezone";
+    } elsif ($type eq "timestamp without time zone") {
+      $type = "timestamp";
+      $info->{_ic_dt_method} ||= "timestamp_without_timezone";
     } elsif ($type eq "smalldatetime") {
       $type = "datetime";
       $info->{_ic_dt_method} ||= "datetime";
index 2e3e984..050a433 100644 (file)
@@ -145,13 +145,12 @@ that you cannot modify the rows it contains, ie. cannot call L</update>,
 L</delete>, ...  on it).
 
 If you prefer to have the definitions of these custom ResultSources in separate
-files (instead of stuffing all of them into the same resultset class), you can
-achieve the same with subclassing the resultset class and defining the
-ResultSource there:
+files (instead of stuffing all of them into the same ResultSource class), you
+can achieve the same with subclassing the ResultSource class and defining the
+new ResultSource there:
 
   package My::Schema::Result::UserFriendsComplex;
 
-  use My::Schema::Result::User;
   use base qw/My::Schema::Result::User/;
 
   __PACKAGE__->table('dummy');  # currently must be called before anything else
@@ -159,7 +158,7 @@ ResultSource there:
   # Hand in your query as a scalar reference
   # It will be added as a sub-select after FROM,
   # so pay attention to the surrounding brackets!
-  __PACKAGE__->name( \<<SQL );
+  __PACKAGE__->result_source_instance->name( \<<SQL );
   ( SELECT u.* FROM user u
   INNER JOIN user_friends f ON u.id = f.user_id
   WHERE f.friend_user_id = ?
@@ -169,6 +168,8 @@ ResultSource there:
   WHERE f.user_id = ? )
   SQL
 
+  1;
+
 TIMTOWDI.
 
 =head2 Using specific columns
index 9ebb2ba..1f332fc 100644 (file)
@@ -349,13 +349,13 @@ It should output:
 
 A reference implentation of the database and scripts in this example
 are available in the main distribution for DBIx::Class under the
-directory C<t/examples/Schema>.
+directory F<t/examples/Schema>.
 
 With these scripts we're relying on @INC looking in the current
 working directory.  You may want to add the MyDatabase namespaces to
 @INC in a different way when it comes to deployment.
 
-The C<testdb.pl> script is an excellent start for testing your database
+The F<testdb.pl> script is an excellent start for testing your database
 model.
 
 This example uses L<DBIx::Class::Schema/load_namespaces> to load in the
index 2fde285..48f281b 100644 (file)
@@ -38,6 +38,19 @@ sub _prep_for_execute {
   my $self = shift;
   my ($op, $extra_bind, $ident, $args) = @_;
 
+# cast MONEY values properly
+  if ($op eq 'insert' || $op eq 'update') {
+    my $fields = $args->[0];
+    my $col_info = $self->_resolve_column_info($ident, [keys %$fields]);
+
+    for my $col (keys %$fields) {
+      if ($col_info->{$col}{data_type} =~ /^money\z/i) {
+        my $val = $fields->{$col};
+        $fields->{$col} = \['CAST(? AS MONEY)', [ $col => $val ]];
+      }
+    }
+  }
+
   my ($sql, $bind) = $self->next::method (@_);
 
   if ($op eq 'insert') {
index ac1a3a1..9c50a3c 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 =head1 NAME
 
-DBIx::Class::Storage::DBI::Oracle::Generic - Automatic primary key class for Oracle
+DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
 
 =head1 SYNOPSIS
 
@@ -52,7 +52,7 @@ sub _dbh_get_autoinc_seq {
   };
 
   # trigger_body is a LONG
-  $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
+  local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
 
   my $sth;
 
@@ -195,7 +195,6 @@ for your timestamps, use something like this:
 
 sub connect_call_datetime_setup {
   my $self = shift;
-  my $dbh  = $self->dbh;
 
   my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
   my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
@@ -203,9 +202,11 @@ sub connect_call_datetime_setup {
   my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
     'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
 
-  $dbh->do("alter session set nls_date_format = '$date_format'");
-  $dbh->do("alter session set nls_timestamp_format = '$timestamp_format'");
-  $dbh->do("alter session set nls_timestamp_tz_format='$timestamp_tz_format'");
+  $self->_do_query("alter session set nls_date_format = '$date_format'");
+  $self->_do_query(
+"alter session set nls_timestamp_format = '$timestamp_format'");
+  $self->_do_query(
+"alter session set nls_timestamp_tz_format='$timestamp_tz_format'");
 }
 
 sub _svp_begin {
@@ -266,11 +267,9 @@ sub _svp_rollback {
     $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
 }
 
-=head1 AUTHORS
+=head1 AUTHOR
 
-Andy Grundman <andy@hybridized.org>
-
-Scott Connelly <scottsweep@yahoo.com>
+See L<DBIx::Class/CONTRIBUTORS>.
 
 =head1 LICENSE
 
index adfbbf1..ac9a877 100644 (file)
@@ -8,7 +8,7 @@ BEGIN {
 
   my %replication_required = (
     'Moose' => '0.87',
-    'MooseX::AttributeHelpers' => '0.20',
+    'MooseX::AttributeHelpers' => '0.21',
     'MooseX::Types' => '0.16',
     'namespace::clean' => '0.11',
     'Hash::Merge' => '0.11'
index f5c0071..fa8f137 100644 (file)
@@ -12,7 +12,7 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PA
 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $user);
 
-plan tests => 27;
+plan tests => 33;
 
 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
 
@@ -75,6 +75,42 @@ $it->next;
 is( $it->next->name, "Artist 2", "iterator->next ok" );
 is( $it->next, undef, "next past end of resultset ok" );
 
+# test MONEY type
+$schema->storage->dbh_do (sub {
+    my ($storage, $dbh) = @_;
+    eval { $dbh->do("DROP TABLE money_test") };
+    $dbh->do(<<'SQL');
+
+CREATE TABLE money_test (
+   id INT IDENTITY PRIMARY KEY,
+   amount MONEY NULL
+)
+
+SQL
+
+});
+
+my $rs = $schema->resultset('Money');
+
+my $row;
+lives_ok {
+  $row = $rs->create({ amount => 100 });
+} 'inserted a money value';
+
+is $rs->find($row->id)->amount, '100.00', 'money value round-trip';
+
+lives_ok {
+  $row->update({ amount => 200 });
+} 'updated a money value';
+
+is $rs->find($row->id)->amount, '200.00', 'updated money value round-trip';
+
+lives_ok {
+  $row->update({ amount => undef });
+} 'updated a money value to NULL';
+
+is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
+
 $schema->storage->dbh_do (sub {
     my ($storage, $dbh) = @_;
     eval { $dbh->do("DROP TABLE Owners") };
@@ -232,7 +268,11 @@ $schema->storage->_sql_maker->{name_sep} = '.';
 
 # 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 money_test');
+      $dbh->do('DROP TABLE Books');
+      $dbh->do('DROP TABLE Owners');
+    }
 }
 # vim:sw=2 sts=2
index c0cea0c..cbaffc0 100644 (file)
@@ -18,7 +18,7 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
   unless ($dsn);
 
-plan tests => 7;
+plan tests => 13;
 
 my $schema = DBICTest::Schema->clone;
 $schema->connection($dsn, $user, $pass);
@@ -74,10 +74,48 @@ $it->next;
 $it->next;
 is( $it->next, undef, "next past end of resultset ok" );
 
+# test MONEY column support
+$schema->storage->dbh_do (sub {
+    my ($storage, $dbh) = @_;
+    eval { $dbh->do("DROP TABLE money_test") };
+    $dbh->do(<<'SQL');
+
+CREATE TABLE money_test (
+   id INT IDENTITY PRIMARY KEY,
+   amount MONEY NULL
+)
+
+SQL
+
+});
+
+my $rs = $schema->resultset('Money');
+
+my $row;
+lives_ok {
+  $row = $rs->create({ amount => 100 });
+} 'inserted a money value';
+
+is $rs->find($row->id)->amount, 100, 'money value round-trip';
+
+lives_ok {
+  $row->update({ amount => 200 });
+} 'updated a money value';
+
+is $rs->find($row->id)->amount, 200, 'updated money value round-trip';
+
+lives_ok {
+  $row->update({ amount => undef });
+} 'updated a money value to NULL';
+
+is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
+
 # clean up our mess
 END {
     $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist")
         if $dbh;
     $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd")
         if $dbh;
+    $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test")
+        if $dbh;
 }
index d95f698..5bbd302 100644 (file)
@@ -15,10 +15,13 @@ BEGIN {
 
 my $schema = DBICTest->init_schema();
 # Dummy was yanked out by the sqlt hook test
+# CustomSql tests the horrific/deprecated ->name(\$sql) hack
 # YearXXXXCDs are views
-
-my @sources = grep { $_ ne 'Dummy' && $_ !~ /^Year\d{4}CDs$/ } 
-                $schema->sources;
+#
+my @sources = grep
+  { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
+  $schema->sources
+;
 
 plan tests => ( @sources * 3);
 
index b9946a4..1a1d0c7 100644 (file)
@@ -13,7 +13,7 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 9 );
+        : ( tests => 13 );
 }
 
 my $where_bind = {
@@ -45,34 +45,34 @@ TODO: {
     is ( $rs->count, 1, 'where/bind last' );
 }
 
-# More complex cases, based primarily on the Cookbook
-# "Arbitrary SQL through a custom ResultSource" technique,
-# which seems to be the only place the bind attribute is
-# documented.  Breaking this technique probably breaks existing
-# application code.
-my $source = DBICTest::Artist->result_source_instance;
-my $new_source = $source->new($source);
-$new_source->source_name('Complex');
+{
+  # More complex cases, based primarily on the Cookbook
+  # "Arbitrary SQL through a custom ResultSource" technique,
+  # which seems to be the only place the bind attribute is
+  # documented.  Breaking this technique probably breaks existing
+  # application code.
+  my $source = DBICTest::Artist->result_source_instance;
+  my $new_source = $source->new($source);
+  $new_source->source_name('Complex');
 
-$new_source->name(\<<'');
-( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year 
-  FROM artist a
-  JOIN cd ON cd.artist = a.artistid
-  WHERE cd.year = ?)
+  $new_source->name(\<<'');
+  ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year 
+    FROM artist a
+    JOIN cd ON cd.artist = a.artistid
+    WHERE cd.year = ?)
 
-$schema->register_extra_source('Complex' => $new_source);
+  $schema->register_extra_source('Complex' => $new_source);
 
-$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
-is ( $rs->count, 1, 'cookbook arbitrary sql example' );
+  $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
+  is ( $rs->count, 1, 'cookbook arbitrary sql example' );
 
-$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
-is ( $rs->count, 1, '...coobook + search condition' );
+  $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
+  is ( $rs->count, 1, '...cookbook + search condition' );
 
-$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
-    ->search({ 'artistid' => 1 });
-is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
+  $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
+      ->search({ 'artistid' => 1 });
+  is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
 
-{
   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
   is_same_sql_bind(
     $rs->as_query,
@@ -82,8 +82,36 @@ is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
       [ '!!dummy' => 'Spoon%' ]
     ],
     'got correct SQL'
-);
+  );
+}
+
+{
+  # More complex cases, based primarily on the Cookbook
+  # "Arbitrary SQL through a custom ResultSource" technique,
+  # which seems to be the only place the bind attribute is
+  # documented.  Breaking this technique probably breaks existing
+  # application code.
+
+  $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
+  is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
+
+  $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
+  is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
+
+  $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
+      ->search({ 'artistid' => 1 });
+  is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
 
+  $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
+  is_same_sql_bind(
+    $rs->as_query,
+    "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) WHERE title LIKE ?)",
+    [
+      [ '!!dummy' => '1999' ], 
+      [ '!!dummy' => 'Spoon%' ]
+    ],
+    'got correct SQL (cookbook arbitrary SQL, in separate file)'
+  );
 }
 
 TODO: {
index 054edf1..2b19df4 100644 (file)
@@ -13,7 +13,7 @@ use DBICTest;
 eval { require DateTime::Format::Pg };
 plan $@
   ? ( skip_all =>  'Need DateTime::Format::Pg for timestamp inflation tests')
-  : ( tests => 3 )
+  : ( tests => 6 )
 ;
 
 
@@ -27,4 +27,14 @@ my $schema = DBICTest->init_schema();
   is($event->created_on->time_zone->name, "America/Chicago", "Timezone changed");
   # Time zone difference -> -6hours
   is($event->created_on->iso8601, "2009-01-15T11:00:00", "Time with TZ correct");
+
+# test 'timestamp without time zone'
+  my $dt = DateTime->from_epoch(epoch => time);
+  $dt->set_nanosecond(int 500_000_000);
+  $event->update({ts_without_tz => $dt});
+  $event->discard_changes;
+  isa_ok($event->ts_without_tz, "DateTime") or diag $event->created_on;
+  is($event->ts_without_tz, $dt, 'timestamp without time zone inflation');
+  is($event->ts_without_tz->microsecond, $dt->microsecond,
+    'timestamp without time zone microseconds survived');
 }
index b03d090..a3e4484 100644 (file)
@@ -20,6 +20,8 @@ __PACKAGE__->load_classes(qw/
   Tag
   Year2000CDs
   Year1999CDs
+  CustomSql
+  Money
   /,
   { 'DBICTest::Schema' => [qw/
     LinerNotes
diff --git a/t/lib/DBICTest/Schema/CustomSql.pm b/t/lib/DBICTest/Schema/CustomSql.pm
new file mode 100644 (file)
index 0000000..d169d72
--- /dev/null
@@ -0,0 +1,15 @@
+package # hide from PAUSE 
+    DBICTest::Schema::CustomSql;
+
+use base qw/DBICTest::Schema::Artist/;
+
+__PACKAGE__->table('dummy');
+
+__PACKAGE__->result_source_instance->name(\<<SQL);
+  ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year 
+  FROM artist a
+  JOIN cd ON cd.artist = a.artistid
+  WHERE cd.year = ?)
+SQL
+
+1;
index c56c1bd..22b655e 100644 (file)
@@ -15,6 +15,7 @@ __PACKAGE__->add_columns(
   varchar_date => { data_type => 'varchar', inflate_date => 1, size => 20, is_nullable => 1 },
   varchar_datetime => { data_type => 'varchar', inflate_datetime => 1, size => 20, is_nullable => 1 },
   skip_inflation => { data_type => 'datetime', inflate_datetime => 0, is_nullable => 1 },
+  ts_without_tz => { data_type => 'datetime', is_nullable => 1 }, # used in EventTZPg
 );
 
 __PACKAGE__->set_primary_key('id');
index e2b512e..444fe69 100644 (file)
@@ -12,6 +12,7 @@ __PACKAGE__->add_columns(
   id => { data_type => 'integer', is_auto_increment => 1 },
   starts_at => { data_type => 'datetime', timezone => "America/Chicago", locale => 'de_DE' },
   created_on => { data_type => 'timestamp with time zone', timezone => "America/Chicago" },
+  ts_without_tz => { data_type => 'timestamp without time zone' },
 );
 
 __PACKAGE__->set_primary_key('id');
diff --git a/t/lib/DBICTest/Schema/Money.pm b/t/lib/DBICTest/Schema/Money.pm
new file mode 100644 (file)
index 0000000..f4586eb
--- /dev/null
@@ -0,0 +1,21 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Money;
+
+use base qw/DBICTest::BaseResult/;
+
+__PACKAGE__->table('money_test');
+
+__PACKAGE__->add_columns(
+  'id' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'amount' => {
+    data_type => 'money',
+    is_nullable => 1,
+  },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;