Merge 'trunk' into 'table_name_ref'
Peter Rabbitson [Sat, 8 Aug 2009 15:51:23 +0000 (15:51 +0000)]
r7256@Thesaurus (orig r7253):  ribasushi | 2009-08-07 11:19:35 +0200
 r7232@Thesaurus (orig r7229):  jnapiorkowski | 2009-08-05 16:56:32 +0200
 added test for the new default force pool behavior in PK->discard_changes and cleaned up the related tests a bit to give more meaningful info
 r7233@Thesaurus (orig r7230):  jnapiorkowski | 2009-08-05 16:57:45 +0200
 opps typo in test status messages
 r7234@Thesaurus (orig r7231):  jnapiorkowski | 2009-08-05 17:03:46 +0200
 added the default attrs to solve the failing test recently commited
 r7235@Thesaurus (orig r7232):  jnapiorkowski | 2009-08-05 17:58:44 +0200
 added test to make sure you can override the default attributes to discard_changes
 r7241@Thesaurus (orig r7238):  jnapiorkowski | 2009-08-05 22:00:58 +0200
 added replication as an optional feature to make installing it easier
 r7253@Thesaurus (orig r7250):  ribasushi | 2009-08-07 11:06:41 +0200
 Streamline makefile dep handling
 r7254@Thesaurus (orig r7251):  ribasushi | 2009-08-07 11:07:14 +0200
 Switch to done_testing
 r7255@Thesaurus (orig r7252):  ribasushi | 2009-08-07 11:19:13 +0200
 Move discard_changes code to Row.pm, better docs

r7257@Thesaurus (orig r7254):  ribasushi | 2009-08-07 11:21:35 +0200
Remove merged branch
r7259@Thesaurus (orig r7256):  ribasushi | 2009-08-07 14:16:13 +0200
Fix bogus POD
r7261@Thesaurus (orig r7258):  ribasushi | 2009-08-07 17:22:58 +0200
per mst: no optional deps
r7262@Thesaurus (orig r7259):  ribasushi | 2009-08-08 17:02:39 +0200
Stop using discard_changes() in Ordered (if I knew it will be *that* complex I would not touch it)
r7265@Thesaurus (orig r7262):  ribasushi | 2009-08-08 17:49:19 +0200
 r7032@Thesaurus (orig r7031):  caelum | 2009-07-11 11:28:52 +0200
 new branch to reduce connected() calls
 r7033@Thesaurus (orig r7032):  caelum | 2009-07-11 13:07:41 +0200
 added failing test
 r7034@Thesaurus (orig r7033):  caelum | 2009-07-11 14:36:53 +0200
 minor optimization
 r7048@Thesaurus (orig r7047):  caelum | 2009-07-14 15:09:47 +0200
 substantially reduced ping count, dynamic cursors support for mssql through odbc
 r7050@Thesaurus (orig r7049):  caelum | 2009-07-14 16:06:39 +0200
 a couple more options for odbc/mssql
 r7052@Thesaurus (orig r7051):  caelum | 2009-07-15 00:14:09 +0200
 unfuck ensure_connected for odbc/mssql
 r7055@Thesaurus (orig r7054):  caelum | 2009-07-15 21:10:27 +0200
 rename _scope_identity to _identity for odbc/mssql
 r7056@Thesaurus (orig r7055):  caelum | 2009-07-16 00:41:45 +0200
 add IC::DT tests for odbc/mssql
 r7069@Thesaurus (orig r7068):  caelum | 2009-07-17 11:47:31 +0200
 don't run connection actions if ->_rebless does not connect
 r7108@Thesaurus (orig r7105):  caelum | 2009-07-24 07:26:13 +0200
 moving test to another branch
 r7110@Thesaurus (orig r7107):  caelum | 2009-07-24 07:52:33 +0200
 revert odbc/mssql code to trunk and move it to another branch
 r7111@Thesaurus (orig r7108):  caelum | 2009-07-24 08:13:35 +0200
 revert t/746mssql.t to trunk and move to another branch
 r7224@Thesaurus (orig r7221):  caelum | 2009-08-05 11:48:04 +0200
 update branch after pull
 r7225@Thesaurus (orig r7222):  ribasushi | 2009-08-05 12:09:07 +0200
 Rename last_dbh and turn it into a public method
 r7226@Thesaurus (orig r7223):  ribasushi | 2009-08-05 12:12:20 +0200
 Whoopsie - more renames
 r7227@Thesaurus (orig r7224):  ribasushi | 2009-08-05 12:32:09 +0200
 Changes and a deploy() fix
 r7228@Thesaurus (orig r7225):  ribasushi | 2009-08-05 12:36:01 +0200
 We do not count pings during deploy - they are expected
 r7229@Thesaurus (orig r7226):  ribasushi | 2009-08-05 12:49:06 +0200
 Clarify autocommit default
 r7238@Thesaurus (orig r7235):  caelum | 2009-08-05 20:39:47 +0200
 fix up txn_begin and the ping_count test
 r7263@Thesaurus (orig r7260):  ribasushi | 2009-08-08 17:40:19 +0200
 A more straightforward txn_begin fix, some more test fixes

13 files changed:
lib/DBIx/Class/Manual/Cookbook.pod
lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/ResultSourceProxy/Table.pm
lib/DBIx/Class/SQLAHacks.pm
lib/DBIx/Class/SQLAHacks/MySQL.pm
lib/DBIx/Class/Storage/DBI/Pg.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
t/19quotes.t
t/19quotes_newstyle.t
t/76select.t
t/99dbic_sqlt_parser.t
t/lib/DBICTest/Schema/CD.pm
t/lib/DBICTest/Schema/CustomSql.pm

index 8728eb5..0951c49 100644 (file)
@@ -141,6 +141,13 @@ L</delete>, ...  on it).
 
 Note that you cannot have bind parameters unless is_virtual is set to true.
 
+If you're using the old C<< $rsrc_instance->name(\'( SELECT ...') >> method for
+custom SQL, you are highly encouraged to update your code to use a virtual view
+as above. Otherwise add the following code so that on C<< ->deploy >> there is
+no attempt to create a table with that name:
+
+  sub sqlt_deploy_hook { $_[1]->schema->drop_table ($_[1]) }
+
 =head2 Using specific columns
 
 When you only want specific columns from a table, you can use
index 1b93f4e..d7f20b4 100644 (file)
@@ -584,7 +584,10 @@ optional constraint name.
 sub name_unique_constraint {
   my ($self, $cols) = @_;
 
-  return join '_', $self->name, @$cols;
+  my $name = $self->name;
+  $name = $$name if ref $name;
+
+  return join '_', $name, @$cols;
 }
 
 =head2 unique_constraints
index 929c300..db82b47 100644 (file)
@@ -5,6 +5,9 @@ use warnings;
 
 use base qw/DBIx::Class::ResultSourceProxy/;
 
+use DBIx::Class::ResultSource::Table;
+use Scalar::Util ();
+
 __PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
 
 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
@@ -76,7 +79,8 @@ Gets or sets the table name.
 sub table {
   my ($class, $table) = @_;
   return $class->result_source_instance->name unless $table;
-  unless (ref $table) {
+
+  unless (Scalar::Util::blessed($table) && $table->isa($class->table_class)) {
 
     my $table_class = $class->table_class;
     $class->ensure_class_loaded($table_class);
index c03adbb..61ff365 100644 (file)
@@ -329,12 +329,10 @@ sub select {
 
   $self->{"${_}_bind"} = [] for (qw/having from order/);
 
-  if (ref $table eq 'SCALAR') {
-    $table = $$table;
-  }
-  elsif (not ref $table) {
+  if (not ref($table) or ref($table) eq 'SCALAR') {
     $table = $self->_quote($table);
   }
+
   local $self->{rownum_hack_count} = 1
     if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
   @rest = (-1) unless defined $rest[0];
@@ -354,7 +352,7 @@ sub select {
 sub insert {
   my $self = shift;
   my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
+  $table = $self->_quote($table);
 
   # SQLA will emit INSERT INTO $table ( ) VALUES ( )
   # which is sadly understood only by MySQL. Change default behavior here,
@@ -370,7 +368,7 @@ sub insert {
 sub update {
   my $self = shift;
   my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
+  $table = $self->_quote($table);
   $self->SUPER::update($table, @_);
 }
 
@@ -378,7 +376,7 @@ sub update {
 sub delete {
   my $self = shift;
   my $table = shift;
-  $table = $self->_quote($table) unless ref($table);
+  $table = $self->_quote($table);
   $self->SUPER::delete($table, @_);
 }
 
@@ -584,6 +582,7 @@ sub _join_condition {
 sub _quote {
   my ($self, $label) = @_;
   return '' unless defined $label;
+  return $$label if ref($label) eq 'SCALAR';
   return "*" if $label eq '*';
   return $label unless $self->{quote_char};
   if(ref $self->{quote_char} eq "ARRAY"){
index 9b4d0be..687a793 100644 (file)
@@ -12,7 +12,7 @@ sub insert {
   my $self = shift;
 
   my $table = $_[0];
-  $table = $self->_quote($table) unless ref($table);
+  $table = $self->_quote($table);
 
   if (! $_[1] or (ref $_[1] eq 'HASH' and !keys %{$_[1]} ) ) {
     return "INSERT INTO ${table} () VALUES ()"
index 7723a21..91418a4 100644 (file)
@@ -52,8 +52,16 @@ sub get_autoinc_seq {
   my ($self,$source,$col) = @_;
 
   my @pri = $source->primary_columns;
-  my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
-    : (undef,$source->name);
+
+  my $schema;
+  my $table = $source->name;
+
+  if (ref $table eq 'SCALAR') {
+    $table = $$table;
+  }
+  elsif ($table =~ /^(.+)\.(.+)$/) {
+    ($schema, $table) = ($1, $2);
+  }
 
   $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
 }
index 9f45d1b..4e57a47 100644 (file)
@@ -82,8 +82,8 @@ sub parse {
         my $source = $dbicschema->source($moniker);
         my $table_name = $source->name;
 
-        # Skip custom query sources
-        next if ref $table_name;
+        # sqlt currently does not do quoting right anyway
+        $table_name = $$table_name if ref $table_name eq 'SCALAR';
 
         # Its possible to have multiple DBIC sources using the same table
         next if $tables{$table_name};
@@ -141,6 +141,7 @@ sub parse {
 
             my $othertable = $source->related_source($rel);
             my $rel_table = $othertable->name;
+            $rel_table = $$rel_table if ref $rel_table eq 'SCALAR';  #sqlt currently does not do quoting right anyway
 
             my $reverse_rels = $source->reverse_relationship_info($rel);
             my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
@@ -251,14 +252,27 @@ sub parse {
     ) {
       $schema->add_table ($tables{$table}{object});
       $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} );
-    }
 
+      if ($schema->get_table($table) && $table =~ /SELECT \s+/ix) {
+        warn <<'EOF';
+
+Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, see the "Arbitrary
+SQL" entry in:
+
+  perldoc DBIx::Class::Manual::Cookbook
+
+for the current method of doing this.
+
+EOF
+      }
+    }
 
     my %views;
     foreach my $moniker (sort @view_monikers)
     {
         my $source = $dbicschema->source($moniker);
         my $view_name = $source->name;
+        $view_name = $$view_name if ref $view_name eq 'SCALAR';  #sqlt currently does not do quoting right anyway
 
         # Skip custom query sources
         next if ref $view_name;
index c4768c8..8750d5a 100644 (file)
@@ -36,7 +36,7 @@ $rs = $schema->resultset('CD')->search(
 eval { $rs->count };
 is_same_sql_bind(
   $sql, \@bind,
-  "SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
+  "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
   'got correct SQL for count query with quoting'
 );
 
@@ -60,7 +60,7 @@ $rs = $schema->resultset('CD')->search(
 eval { $rs->count };
 is_same_sql_bind(
   $sql, \@bind,
-  "SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
+  "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
   'got correct SQL for count query with bracket quoting'
 );
 
index f0c34a9..3e7595a 100644 (file)
@@ -42,7 +42,7 @@ $rs = $schema->resultset('CD')->search(
 eval { $rs->count };
 is_same_sql_bind(
   $sql, \@bind,
-  "SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
+  "SELECT COUNT( * ) FROM cd `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
   'got correct SQL for count query with quoting'
 );
 
@@ -73,7 +73,7 @@ $rs = $schema->resultset('CD')->search(
 eval { $rs->count };
 is_same_sql_bind(
   $sql, \@bind,
-  "SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
+  "SELECT COUNT( * ) FROM cd [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
   'got correct SQL for count query with bracket quoting'
 );
 
index bee69e6..7560d2c 100644 (file)
@@ -64,6 +64,7 @@ my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # ma
 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
 
 my $table = $cds->result_source->name;
+$table = $$table if ref $table eq 'SCALAR';
 my $subsel = $cds->search ({}, {
     columns => [qw/cdid title/],
     from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
index bbae42d..5bbd302 100644 (file)
@@ -29,7 +29,7 @@ plan tests => ( @sources * 3);
        my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
 
        foreach my $source (@sources) {
-               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+               my $table = get_table($sqlt_schema, $schema, $source);
 
                my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
                my @indices = $table->get_indices;
@@ -43,7 +43,7 @@ plan tests => ( @sources * 3);
        my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
 
        foreach my $source (@sources) {
-               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+               my $table = get_table($sqlt_schema, $schema, $source);
 
                my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
                my @indices = $table->get_indices;
@@ -57,7 +57,7 @@ plan tests => ( @sources * 3);
        my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
 
        foreach my $source (@sources) {
-               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+               my $table = get_table($sqlt_schema, $schema, $source);
 
                my @indices = $table->get_indices;
                my $index_count = scalar(@indices);
@@ -83,3 +83,12 @@ sub create_schema {
        $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
        return $sqlt->translate({ data => $schema }) or die $sqlt->error;
 }
+
+sub get_table {
+    my ($sqlt_schema, $schema, $source) = @_;
+
+    my $table_name = $schema->source($source)->from;
+    $table_name    = $$table_name if ref $table_name;
+
+    return $sqlt_schema->get_table($table_name);
+}
index 80af1df..1463d00 100644 (file)
@@ -3,7 +3,10 @@ package # hide from PAUSE
 
 use base qw/DBICTest::BaseResult/;
 
-__PACKAGE__->table('cd');
+# this tests table name as scalar ref
+# DO NOT REMOVE THE \
+__PACKAGE__->table(\'cd');
+
 __PACKAGE__->add_columns(
   'cdid' => {
     data_type => 'integer',
index d169d72..bdad8b8 100644 (file)
@@ -12,4 +12,6 @@ __PACKAGE__->result_source_instance->name(\<<SQL);
   WHERE cd.year = ?)
 SQL
 
+sub sqlt_deploy_hook { $_[1]->schema->drop_table($_[1]) }
+
 1;