Commit 7aabee5e authored by jwage's avatar jwage

[2.0] Fixing empty insert sql statements (closes #2481)

parent 7c56bfa1
......@@ -1580,4 +1580,16 @@ abstract class AbstractPlatform
{
return $schemaElementName;
}
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($tableName, $identifierColumnName)
{
return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (null)';
}
}
\ No newline at end of file
......@@ -506,4 +506,16 @@ class MsSqlPlatform extends AbstractPlatform
return $query;
}
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($quotedTableName, $quotedIdentifierColumnName)
{
return 'INSERT INTO ' . $quotedTableName . ' DEFAULT VALUES';
}
}
\ No newline at end of file
......@@ -770,4 +770,16 @@ class PostgreSqlPlatform extends AbstractPlatform
{
return 'Y-m-d H:i:sO';
}
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($quotedTableName, $quotedIdentifierColumnName)
{
return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
}
}
\ No newline at end of file
......@@ -369,10 +369,17 @@ class ClassMetadataFactory
$class->resultColumnNames[$this->_targetPlatform->getSqlResultCasing($columnName)] = $columnName;
}
$class->insertSql = 'INSERT INTO ' .
$class->getQuotedTableName($this->_targetPlatform)
. ' (' . implode(', ', $columns) . ') '
. 'VALUES (' . implode(', ', $values) . ')';
if (empty($columns)) {
$class->insertSql = $this->_targetPlatform->getEmptyIdentityInsertSql(
$class->getQuotedTableName($this->_targetPlatform),
$class->getQuotedColumnName($class->identifier[0], $this->_targetPlatform)
);
} else {
$class->insertSql = 'INSERT INTO ' .
$class->getQuotedTableName($this->_targetPlatform)
. ' (' . implode(', ', $columns) . ') '
. 'VALUES (' . implode(', ', $values) . ')';
}
}
/**
......
......@@ -139,17 +139,19 @@ class StandardEntityPersister
$insertData = array();
$this->_prepareData($entity, $insertData, true);
$paramIndex = 1;
if ($sqlLogger) {
$params = array();
foreach ($insertData[$primaryTableName] as $value) {
$params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value);
}
$sqlLogger->logSql($this->_class->insertSql, $params);
} else {
foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value);
if (isset($insertData[$primaryTableName])) {
$paramIndex = 1;
if ($sqlLogger) {
$params = array();
foreach ($insertData[$primaryTableName] as $value) {
$params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value);
}
$sqlLogger->logSql($this->_class->insertSql, $params);
} else {
foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value);
}
}
}
......
......@@ -43,6 +43,7 @@ class AllTests
$suite->addTest(Locking\AllTests::suite());
$suite->addTest(SchemaTool\AllTests::suite());
$suite->addTest(Ticket\AllTests::suite());
return $suite;
}
......
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Functional_Ticket_AllTests::main');
}
require_once __DIR__ . '/../../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\OrmFunctionalTestSuite('Doctrine Orm Ticket Tests');
$tests = glob(__DIR__ . '/Ticket*Test.php');
foreach ($tests as $test) {
$info = pathinfo($test);
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\Ticket\\' . $info['filename']);
}
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Functional_Ticket_AllTests::main') {
AllTests::main();
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
class Ticket2481Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Ticket2481Product')
));
} catch (\Exception $e) {
// Swallow all exceptions. We do not test the schema tool here.
}
$this->_conn = $this->_em->getConnection();
}
public function testEmptyInsert()
{
$test = new Ticket2481Product();
$this->_em->persist($test);
$this->_em->flush();
$this->assertTrue($test->id > 0);
}
}
/**
* @Entity
* @Table(name="ticket_2481_products")
*/
class Ticket2481Product
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment