From: Rafael Kitover Date: Wed, 24 Nov 2010 09:42:52 +0000 (-0500) Subject: Quote table name when inserting DEFAULT VALUES X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=20595c02ab4627497090c5081506c1fbac3dd57d;p=dbsrgits%2FDBIx-Class-Historic.git Quote table name when inserting DEFAULT VALUES --- diff --git a/Changes b/Changes index 4a7c676..a07cefe 100644 --- a/Changes +++ b/Changes @@ -26,6 +26,7 @@ Revision history for DBIx::Class - Fix infinite loops on old perls with a recent Try::Tiny - Improve "fork()" on Win32 by reimplementing a more robust DBIC thread support (still problematic, pending a DBI fix) + - Properly quote table name on INSERT with no values * Misc - Switch all serialization to use Storable::nfreeze for portable diff --git a/lib/DBIx/Class/SQLMaker.pm b/lib/DBIx/Class/SQLMaker.pm index fe701ca..6ea68ba 100644 --- a/lib/DBIx/Class/SQLMaker.pm +++ b/lib/DBIx/Class/SQLMaker.pm @@ -193,7 +193,9 @@ sub insert { # which is sadly understood only by MySQL. Change default behavior here, # until SQLA2 comes with proper dialect support if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) { - my $sql = "INSERT INTO $_[1] DEFAULT VALUES"; + my $sql = sprintf( + 'INSERT INTO %s DEFAULT VALUES', $_[0]->_quote($_[1]) + ); if (my $ret = ($_[3]||{})->{returning} ) { $sql .= $_[0]->_insert_returning ($ret); diff --git a/t/18insert_default.t b/t/18insert_default.t index c3f9369..cd49cec 100644 --- a/t/18insert_default.t +++ b/t/18insert_default.t @@ -2,30 +2,48 @@ use strict; use warnings; use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; - -my $tests = 3; -plan tests => $tests; +use DBIC::DebugObj; +use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); +$schema->storage->sql_maker->quote_char('"'); + my $rs = $schema->resultset ('Artist'); my $last_obj = $rs->search ({}, { order_by => { -desc => 'artistid' }, rows => 1})->single; my $last_id = $last_obj ? $last_obj->artistid : 0; + +my ($sql, @bind); +my $orig_debugobj = $schema->storage->debugobj; +my $orig_debug = $schema->storage->debug; + +$schema->storage->debugobj (DBIC::DebugObj->new (\$sql, \@bind) ); +$schema->storage->debug (1); + my $obj; -eval { $obj = $rs->create ({}) }; -my $err = $@; +lives_ok { $obj = $rs->create ({}) } 'Default insert successful'; + +$schema->storage->debugobj ($orig_debugobj); +$schema->storage->debug ($orig_debug); + +is_same_sql_bind ( + $sql, + \@bind, + 'INSERT INTO "artist" DEFAULT VALUES', + [], + 'Default-value insert correct SQL', +); ok ($obj, 'Insert defaults ( $rs->create ({}) )' ); -SKIP: { - skip "Default insert failed: $err", $tests-1 if $err; - # this should be picked up without calling the DB again - is ($obj->artistid, $last_id + 1, 'Autoinc PK works'); +# this should be picked up without calling the DB again +is ($obj->artistid, $last_id + 1, 'Autoinc PK works'); - # for this we need to refresh - $obj->discard_changes; - is ($obj->rank, 13, 'Default value works'); -} +# for this we need to refresh +$obj->discard_changes; +is ($obj->rank, 13, 'Default value works'); +done_testing;