Merge 'trunk' into 'DBIx-Class-current'
Brian Cassidy [Thu, 16 Nov 2006 15:47:06 +0000 (15:47 +0000)]
r14845@Brian (orig r2878):  ash | 2006-11-13 15:48:13 -0400
Seperated out quote tests so that matt can use them for S::A at some point (old
test t/19quotes.t) Also includes two failing tests (quoted order by and
{ select => ['me.*']} attrs).

r14870@Brian (orig r2882):  blblack | 2006-11-15 10:13:44 -0400
fix for rt.cpan.org #22740 (use $^X instead of hardcoded "perl")
r14875@Brian (orig r2887):  castaway | 2006-11-15 10:26:47 -0400
Fix foreign/self example

r14885@Brian (orig r2897):  blblack | 2006-11-15 16:16:14 -0400

Don't blow up if columns_info_for returns useless results

r14893@Brian (orig r2898):  bricas | 2006-11-16 11:42:33 -0400
moving it to -current branch

lib/DBIx/Class/Relationship.pm
lib/DBIx/Class/ResultSource.pm
t/89dbicadmin.t
t/94pk_mutation.t [deleted file]
t/95sql_maker_quote.t [new file with mode: 0644]

index 6a5a046..3b381a0 100644 (file)
@@ -125,7 +125,7 @@ The above belongs_to relationship could also have been specified as,
 
   My::DBIC::Schema::Book->belongs_to( author,
                                       'My::DBIC::Schema::Author',
-                                      { 'self.author' => 'foreign.author' } );
+                                      { 'foreign.author' => 'self.author' } );
 
 Creates a relationship where the calling class stores the foreign class's
 primary key in one (or more) of its columns. This relationship defaults to
index c585e19..aeab10c 100644 (file)
@@ -211,8 +211,8 @@ sub column_info {
        and $self->schema and $self->storage )
   {
     $self->{_columns_info_loaded}++;
-    my $info;
-    my $lc_info;
+    my $info = {};
+    my $lc_info = {};
     # eval for the case of storage without table
     eval { $info = $self->storage->columns_info_for( $self->from ) };
     unless ($@) {
@@ -220,7 +220,10 @@ sub column_info {
         $lc_info->{lc $realcol} = $info->{$realcol};
       }
       foreach my $col ( keys %{$self->_columns} ) {
-        $self->_columns->{$col} = { %{ $self->_columns->{$col}}, %{$info->{$col} || $lc_info->{lc $col}} };
+        $self->_columns->{$col} = {
+          %{ $self->_columns->{$col} },
+          %{ $info->{$col} || $lc_info->{lc $col} || {} }
+        };
       }
     }
   }
index 7307c6f..b62d622 100644 (file)
@@ -23,7 +23,7 @@ plan tests => 5;
 # tests run on windows as well
 
 my $employees = $schema->resultset('Employee');
-my $cmd = qq|perl script/dbicadmin --schema=DBICTest::Schema --class=Employee --tlibs --connect="['dbi:SQLite:dbname=t/var/DBIxClass.db','','']" --force --tlibs|;
+my $cmd = qq|$^X script/dbicadmin --schema=DBICTest::Schema --class=Employee --tlibs --connect="['dbi:SQLite:dbname=t/var/DBIxClass.db','','']" --force --tlibs|;
 
 `$cmd --op=insert --set="{name:'Matt'}"`;
 ok( ($employees->count()==1), 'insert count' );
diff --git a/t/94pk_mutation.t b/t/94pk_mutation.t
deleted file mode 100644 (file)
index 4623332..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-use strict;
-use warnings;  
-
-use Test::More;
-use lib qw(t/lib);
-use DBICTest;
-
-my $schema = DBICTest->init_schema();
-
-plan tests => 5;
-
-my $old_artistid = 1;
-my $new_artistid = $schema->resultset("Artist")->get_column('artistid')->max + 1;
-
-# Update the PK
-{
-  my $artist = $schema->resultset("Artist")->find($old_artistid);
-  ok(defined $artist, 'found an artist with the new PK');
-
-  $artist->update({ artistid => $new_artistid });
-  is($artist->artistid, $new_artistid, 'artist ID matches');
-}
-
-# Look for the old PK
-{
-  my $artist = $schema->resultset("Artist")->find($old_artistid);
-  ok(!defined $artist, 'no artist found with the old PK');
-}
-
-# Look for the new PK
-{
-  my $artist = $schema->resultset("Artist")->find($new_artistid);
-  ok(defined $artist, 'found an artist with the new PK');
-  is($artist->artistid, $new_artistid, 'artist ID matches');
-}
diff --git a/t/95sql_maker_quote.t b/t/95sql_maker_quote.t
new file mode 100644 (file)
index 0000000..dc33199
--- /dev/null
@@ -0,0 +1,196 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 8 );
+}
+
+use lib qw(t/lib);
+
+use_ok('DBICTest');
+
+DBICTest->init_schema();
+
+my $sql_maker = DBICTest->schema->storage->sql_maker;
+
+$sql_maker->quote_char('`');
+$sql_maker->name_sep('.');
+
+my ($sql,) = $sql_maker->select(
+          [
+            {
+              'me' => 'cd'
+            },
+            [
+              {
+                'artist' => 'artist',
+                '-join_type' => ''
+              },
+              {
+                'artist.artistid' => 'me.artist'
+              }
+            ]
+          ],
+          [
+            {
+              'count' => '*'
+            }
+          ],
+          {
+            'artist.name' => 'Caterwauler McCrae',
+            'me.year' => 2001
+          },
+          [],
+          undef,
+          undef
+);
+
+is($sql, 
+   q/SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, 
+   'got correct SQL for count query with quoting');
+
+($sql,) = $sql_maker->select(
+          [
+            {
+              'me' => 'cd'
+            }
+          ],
+          [
+            'me.cdid',
+            'me.artist',
+            'me.title',
+            'me.year'
+          ],
+          undef,
+          [
+            'year DESC'
+          ],
+          undef,
+          undef
+);
+
+TODO: {
+    local $TODO = "order_by with quoting needs fixing (ash/castaway)";
+
+    is($sql, 
+       q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, 
+       'quoted ORDER BY with DESC okay');
+}
+
+TODO: {
+    local $TODO = "select attr with star needs fixing (mst/nate)";
+
+    ($sql,) = $sql_maker->select(
+          [
+            {
+              'me' => 'cd'
+            }
+          ],
+          [
+            'me.*'
+          ],
+          undef,
+          [],
+          undef,
+          undef    
+    );
+
+    is($sql, q/SELECT `me`.* FROM `cd` `me`/, 'select attr with me.* is right');
+}
+
+($sql,) = $sql_maker->select(
+          [
+            {
+              'me' => 'cd'
+            }
+          ],
+          [
+            'me.cdid',
+            'me.artist',
+            'me.title',
+            'me.year'
+          ],
+          undef,
+          [
+            \'year DESC'
+          ],
+          undef,
+          undef
+);
+
+is($sql, 
+   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/,
+   'did not quote ORDER BY with scalarref');
+
+my %data = ( 
+    name => 'Bill',
+    order => 12
+);
+
+my @binds;
+
+($sql,@binds) = $sql_maker->update(
+          'group',
+          {
+            'order' => '12',
+            'name' => 'Bill'
+          }
+);
+
+is($sql,
+   q/UPDATE `group` SET `name` = ?, `order` = ?/,
+   'quoted table names for UPDATE');
+
+$sql_maker->quote_char([qw/[ ]/]);
+
+($sql,) = $sql_maker->select(
+          [
+            {
+              'me' => 'cd'
+            },
+            [
+              {
+                'artist' => 'artist',
+                '-join_type' => ''
+              },
+              {
+                'artist.artistid' => 'me.artist'
+              }
+            ]
+          ],
+          [
+            {
+              'count' => '*'
+            }
+          ],
+          {
+            'artist.name' => 'Caterwauler McCrae',
+            'me.year' => 2001
+          },
+          [],
+          undef,
+          undef
+);
+
+is($sql,
+   q/SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/,
+   'got correct SQL for count query with bracket quoting');
+
+
+($sql,@binds) = $sql_maker->update(
+          'group',
+          {
+            'order' => '12',
+            'name' => 'Bill'
+          }
+);
+
+is($sql,
+   q/UPDATE [group] SET [name] = ?, [order] = ?/,
+   'bracket quoted table names for UPDATE');