Merge 'trunk' into 'subquery'
Rob Kinyon [Wed, 18 Feb 2009 02:26:58 +0000 (02:26 +0000)]
r5500@rkinyon-lt-osx (orig r5499):  norbi | 2009-02-17 15:09:40 -0500
 r5616@vger:  mendel | 2009-02-17 20:39:09 +0100
  * Fixed to make it work with [colname => value] bind value format of SQLA.

r5501@rkinyon-lt-osx (orig r5500):  norbi | 2009-02-17 15:09:47 -0500
 r5621@vger:  mendel | 2009-02-17 21:09:08 +0100
  * Updated POD for using PostgreSQL arrays because of the \[$sql, @bind] bind value format change of SQLA.

r5502@rkinyon-lt-osx (orig r5501):  norbi | 2009-02-17 15:44:19 -0500
 r5624@vger:  mendel | 2009-02-17 21:11:58 +0100
  * Fixed the initial DROP TABLE statement (used to drop the wrong table).

r5503@rkinyon-lt-osx (orig r5502):  norbi | 2009-02-17 15:44:26 -0500
 r5625@vger:  mendel | 2009-02-17 21:43:54 +0100
  * Fixed the bug with omitting 'FOR UPDATE'/'FOR SHARED' (introduced from the 'subquery' branch, made 72pg.t fail).

r5504@rkinyon-lt-osx (orig r5503):  norbi | 2009-02-17 15:56:09 -0500
 r5628@vger:  mendel | 2009-02-17 21:55:54 +0100
  * Added the missing POD of a sub.

r5505@rkinyon-lt-osx (orig r5504):  norbi | 2009-02-17 16:08:05 -0500
 r5630@vger:  mendel | 2009-02-17 22:07:56 +0100
  * Fixed the bug in the test that made it fail in the END block even if all tests were SKIPped.

r5506@rkinyon-lt-osx (orig r5505):  castaway | 2009-02-17 16:22:05 -0500
Add multiple database/schema FAQs

r5507@rkinyon-lt-osx (orig r5506):  norbi | 2009-02-17 16:22:57 -0500
 r5632@vger:  mendel | 2009-02-17 22:22:50 +0100
  * Added new test subs (is_same_sql, is_same_bind) and new predicate sub (eq_sql_bind) to DBIC::SqlMakerTest (SQL::Abstract::Test has these, so made them available here, too).

r5508@rkinyon-lt-osx (orig r5507):  caelum | 2009-02-17 16:58:34 -0500
minor Oracle changes
r5509@rkinyon-lt-osx (orig r5508):  caelum | 2009-02-17 17:39:59 -0500
changed WriteAll; to WriteAll(); in Makefile.PL because of some sort of installation weirdness in some places
r5510@rkinyon-lt-osx (orig r5509):  caelum | 2009-02-17 17:52:28 -0500
changed DBD::SQLite from a test_requires to a configure_requires
r5511@rkinyon-lt-osx (orig r5510):  norbi | 2009-02-17 18:09:49 -0500
 r5635@vger:  mendel | 2009-02-18 00:09:36 +0100
  * Added new TODO tests for bind attributes (for ->select, ->update, ->delete).

Makefile.PL
lib/DBIx/Class/Manual/Cookbook.pod
lib/DBIx/Class/Manual/FAQ.pod
lib/DBIx/Class/Storage/DBI.pm
lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
t/72pg.t
t/73oracle.t
t/bindtype_columns.t
t/lib/DBIC/SqlMakerTest.pm

index 1f044f6..e116792 100644 (file)
@@ -29,7 +29,8 @@ requires 'Sub::Name'                 => 0.04;
 # Perl 5.8.0 doesn't have utf8::is_utf8()
 requires 'Encode'                    => 0 if ($] <= 5.008000);  
 
-test_requires 'DBD::SQLite'         => 1.14;
+configure_requires 'DBD::SQLite'         => 1.14;
+
 test_requires 'Test::Builder'       => 0.33;
 test_requires 'Test::Warn'          => 0.11;
 test_requires 'Test::Exception'     => 0;
@@ -145,7 +146,7 @@ EOE
 }
 
 
-WriteAll;
+WriteAll();
 
 
 if ($Module::Install::AUTHOR) {
index b7fc30a..9838759 100644 (file)
@@ -1379,12 +1379,16 @@ passing them as bind values:
 
   $resultset->search(
     {
-      numbers => \[ '= ?', [1, 2, 3] ]
+      numbers => \[ '= ?', [numbers => [1, 2, 3]] ]
     }
   );
 
 See L<SQL::Abstract/array_datatypes> and L<SQL::Abstract/Literal SQL with
-placeholders and bind values (subqueries)> for more explanation.
+placeholders and bind values (subqueries)> for more explanation. Note that
+L<DBIx::Class> sets L<SQL::Abstract/bindtype> to C<columns>, so you must pass
+the bind values (the C<[1, 2, 3]> arrayref in the above example) wrapped in
+arrayrefs together with the column name, like this: C<< [column_name => value]
+>>.
 
 =head1 BOOTSTRAPPING/MIGRATING 
 
index 9c19d31..273397a 100644 (file)
@@ -68,6 +68,24 @@ connection does not happen until you actually request data, so don't
 be alarmed if the error from incorrect connection details happens a
 lot later.
 
+=item .. use DBIx::Class across multiple databases?
+
+If your database server allows you to run querys across multiple
+databases at once, then so can DBIx::Class. All you need to do is make
+sure you write the database name as part of the
+L<DBIx::Class::ResultSource/table> call. Eg:
+
+  __PACKAGE__->table('mydb.mytablename');
+
+And load all the Result classes for both / all databases using one
+L<DBIx::Class::Schema/load_namespaces> call.
+
+=item .. use DBIx::Class across PostgreSQL/DB2/Oracle schemas?
+
+Add the name of the schema to the L<DBIx::Class::ResultSource/table>
+as part of the name, and make sure you give the one user you are going
+to connect with rights to read/write all the schemas/tables as
+necessary.
 
 =back 
 
index 5865916..00a6c19 100644 (file)
@@ -1377,7 +1377,7 @@ sub _select_args {
 
   my $for = delete $attrs->{for};
   my $sql_maker = $self->sql_maker;
-  local $sql_maker->{for} = $for;
+  $sql_maker->{for} = $for;
 
   if (exists $attrs->{group_by} || $attrs->{having}) {
     $order = {
index d12e3ff..2e9a8c1 100644 (file)
@@ -84,6 +84,17 @@ sub _sequence_fetch {
   return $id;
 }
 
+=head2 connected
+
+Returns true if we have an open (and working) database connection, false if it is not (yet)
+open (or does not work). (Executes a simple SELECT to make sure it works.)
+
+The reason this is needed is that L<DBD::Oracle>'s ping() does not do a real
+OCIPing but just gets the server version, which doesn't help if someone killed
+your session.
+
+=cut
+
 sub connected {
   my $self = shift;
 
@@ -113,24 +124,26 @@ sub _dbh_execute {
 
   my (@res, $exception, $retried);
 
-  do {
-    eval {
-      if ($wantarray) {
-        @res    = $self->SUPER::_dbh_execute(@_);
+  RETRY: {
+    do {
+      eval {
+        if ($wantarray) {
+          @res    = $self->SUPER::_dbh_execute(@_);
+        } else {
+          $res[0] = $self->SUPER::_dbh_execute(@_);
+        }
+      };
+      $exception = $@;
+      if ($exception =~ /ORA-01003/) {
+        # ORA-01003: no statement parsed (someone changed the table somehow,
+        # invalidating your cursor.)
+        my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
+        delete $dbh->{CachedKids}{$sql};
       } else {
-        $res[0] = $self->SUPER::_dbh_execute(@_);
+        last RETRY;
       }
-    };
-    $exception = $@;
-    if ($exception =~ /ORA-01003/) {
-      # ORA-01003: no statement parsed (someone changed the table somehow,
-      # invalidating your cursor.)
-      my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
-      delete $dbh->{CachedKids}{$sql};
-    } else {
-      last;
-    }
-  } while (not $retried++);
+    } while (not $retried++);
+  }
 
   $self->throw_exception($exception) if $exception;
 
index fa67d1c..45e614f 100644 (file)
--- a/t/72pg.t
+++ b/t/72pg.t
@@ -153,7 +153,7 @@ SKIP: {
   my $count;
   lives_ok {
     $count = $schema->resultset('ArrayTest')->search({
-      arrayfield => \[ '= ?' => [3, 4] ],   #TODO anything less ugly than this?
+      arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #TODO anything less ugly than this?
     })->count;
   } 'comparing arrayref to pg array data does not blow up';
   is($count, 1, 'comparing arrayref to pg array data gives correct result');
index ee00ed5..51cc932 100644 (file)
@@ -149,7 +149,7 @@ is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manual
 
 # clean up our mess
 END {
-    if($dbh = $schema->storage->dbh) {
+    if($schema && ($dbh = $schema->storage->dbh)) {
         $dbh->do("DROP SEQUENCE artist_seq");
         $dbh->do("DROP SEQUENCE pkid1_seq");
         $dbh->do("DROP SEQUENCE pkid2_seq");
index 05b514c..1462d9b 100644 (file)
@@ -10,7 +10,7 @@ my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}
 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
   unless ($dsn && $dbuser);
   
-plan tests => 3;
+plan tests => 6;
 
 my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
 
@@ -18,7 +18,7 @@ my $dbh = $schema->storage->dbh;
 
 {
     local $SIG{__WARN__} = sub {};
-    $dbh->do('DROP TABLE IF EXISTS artist');
+    $dbh->do('DROP TABLE IF EXISTS bindtype_test');
 
     # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
     $dbh->do(qq[
@@ -32,19 +32,57 @@ my $dbh = $schema->storage->dbh;
     ],{ RaiseError => 1, PrintError => 1 });
 }
 
-# test primary key handling
-my $big_long_string    = 'abcd' x 250000;
+my $big_long_string    = "\x00\x01\x02 abcd" x 125000;
 
-my $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
+my $new;
+# test inserting a row
+{
+  $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
 
-ok($new->id, "Created a bytea row");
-is($new->bytea,        $big_long_string, "Set the blob correctly.");
+  ok($new->id, "Created a bytea row");
+  is($new->bytea,      $big_long_string, "Set the blob correctly.");
+}
 
-my $rs = $schema->resultset('BindType')->find({ id => $new->id });
+# test retrieval of the bytea column
+{
+  my $row = $schema->resultset('BindType')->find({ id => $new->id });
+  is($row->get_column('bytea'), $big_long_string, "Created the blob correctly.");
+}
 
-is($rs->get_column('bytea'), $big_long_string, "Created the blob correctly.");
+TODO: {
+  local $TODO =
+    'Passing bind attributes to $sth->bind_param() should be implemented (it only works in $storage->insert ATM)';
+
+  my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
+
+  # search on the bytea column (select)
+  {
+    my $row = $rs->first;
+    is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
+  }
+
+  # search on the bytea column (update)
+  {
+    my $new_big_long_string = $big_long_string . "2";
+    $schema->txn_do(sub {
+      $rs->update({ bytea => $new_big_long_string });
+      my $row = $schema->resultset('BindType')->find({ id => $new->id });
+      is($row ? $row->get_column('bytea') : undef, $new_big_long_string,
+        "Updated the row correctly (searching on the bytea column)."
+      );
+      $schema->txn_rollback;
+    });
+  }
+
+  # search on the bytea column (delete)
+  {
+    $schema->txn_do(sub {
+      $rs->delete;
+      my $row = $schema->resultset('BindType')->find({ id => $new->id });
+      is($row, undef, "Deleted the row correctly (searching on the bytea column).");
+      $schema->txn_rollback;
+    });
+  }
+}
 
 $dbh->do("DROP TABLE bindtype_test");
-
-
-
index 8c2406c..cf33fd9 100644 (file)
@@ -7,8 +7,11 @@ use base qw/Test::Builder::Module Exporter/;
 
 our @EXPORT = qw/
   &is_same_sql_bind
+  &is_same_sql
+  &is_same_bind
   &eq_sql
   &eq_bind
+  &eq_sql_bind
 /;
 
 
@@ -39,19 +42,59 @@ our @EXPORT = qw/
     $tb->ok($same_sql && $same_bind, $msg);
 
     if (!$same_sql) {
-      $tb->diag("SQL expressions differ\n"
-        . "     got: $sql1\n"
-        . "expected: $sql2\n"
-      );
+      _sql_differ_diag($sql1, $sql2);
     }
     if (!$same_bind) {
-      $tb->diag("BIND values differ\n"
-        . "     got: " . Dumper($bind_ref1)
-        . "expected: " . Dumper($bind_ref2)
-      );
+      _bind_differ_diag($bind_ref1, $bind_ref2);
     }
   }
 
+  sub is_same_sql
+  {
+    my ($sql1, $sql2, $msg) = @_;
+
+    my $same_sql = eq_sql($sql1, $sql2);
+
+    $tb->ok($same_sql, $msg);
+
+    if (!$same_sql) {
+      _sql_differ_diag($sql1, $sql2);
+    }
+  }
+
+  sub is_same_bind
+  {
+    my ($bind_ref1, $bind_ref2, $msg) = @_;
+
+    my $same_bind = eq_bind($bind_ref1, $bind_ref2);
+
+    $tb->ok($same_bind, $msg);
+
+    if (!$same_bind) {
+      _bind_differ_diag($bind_ref1, $bind_ref2);
+    }
+  }
+
+  sub _sql_differ_diag
+  {
+    my ($sql1, $sql2) = @_;
+
+    $tb->diag("SQL expressions differ\n"
+      . "     got: $sql1\n"
+      . "expected: $sql2\n"
+    );
+  }
+
+  sub _bind_differ_diag
+  {
+    my ($bind_ref1, $bind_ref2) = @_;
+
+    $tb->diag("BIND values differ\n"
+      . "     got: " . Dumper($bind_ref1)
+      . "expected: " . Dumper($bind_ref2)
+    );
+  }
+
   sub eq_sql
   {
     my ($left, $right) = @_;
@@ -68,6 +111,13 @@ our @EXPORT = qw/
 
     return eq_deeply($bind_ref1, $bind_ref2);
   }
+
+  sub eq_sql_bind
+  {
+    my ($sql1, $bind_ref1, $sql2, $bind_ref2) = @_;
+
+    return eq_sql($sql1, $sql2) && eq_bind($bind_ref1, $bind_ref2);
+  }
 }
 
 eval "use SQL::Abstract::Test;";
@@ -75,14 +125,20 @@ if ($@ eq '') {
   # SQL::Abstract::Test available
 
   *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
+  *is_same_sql = \&SQL::Abstract::Test::is_same_sql;
+  *is_same_bind = \&SQL::Abstract::Test::is_same_bind;
   *eq_sql = \&SQL::Abstract::Test::eq_sql;
   *eq_bind = \&SQL::Abstract::Test::eq_bind;
+  *eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;
 } else {
   # old SQL::Abstract
 
   *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
+  *is_same_sql = \&DBIC::SqlMakerTest::SQLATest::is_same_sql;
+  *is_same_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_bind;
   *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
   *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
+  *eq_sql_bind = \&DBIC::SqlMakerTest::SQLATest::eq_sql_bind;
 }
 
 
@@ -131,6 +187,28 @@ comparison of bind values.
 Compares given and expected pairs of C<($sql, \@bind)>, and calls
 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
 
+=head2 is_same_sql
+
+  is_same_sql(
+    $given_sql,
+    $expected_sql,
+    $test_msg
+  );
+
+Compares given and expected SQL statement, and calls L<Test::Builder/ok> on the
+result, with C<$test_msg> as message.
+
+=head2 is_same_bind
+
+  is_same_bind(
+    \@given_bind, 
+    \@expected_bind,
+    $test_msg
+  );
+
+Compares given and expected bind value lists, and calls L<Test::Builder/ok> on
+the result, with C<$test_msg> as message.
+
 =head2 eq_sql
 
   my $is_same = eq_sql($given_sql, $expected_sql);
@@ -143,6 +221,16 @@ Compares the two SQL statements. Returns true IFF they are equivalent.
 
 Compares two lists of bind values. Returns true IFF their values are the same.
 
+=head2 eq_sql_bind
+
+  my $is_same = eq_sql_bind(
+    $given_sql, \@given_bind,
+    $expected_sql, \@expected_bind
+  );
+
+Compares the two SQL statements and the two lists of bind values. Returns true
+IFF they are equivalent and the bind values are the same.
+
 
 =head1 SEE ALSO