Commit f7b9970f authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge remote branch 'origin/2.0.x' into 2.0.x

parents bb9de7bd 22f88e0b
...@@ -620,7 +620,20 @@ class Connection implements DriverConnection ...@@ -620,7 +620,20 @@ class Connection implements DriverConnection
{ {
$this->connect(); $this->connect();
return call_user_func_array(array($this->_conn, 'query'), func_get_args()); $args = func_get_args();
$logger = $this->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
$statement = call_user_func_array(array($this->_conn, 'query'), $args);
if ($logger) {
$logger->stopQuery();
}
return $statement;
} }
/** /**
......
...@@ -960,18 +960,33 @@ abstract class AbstractPlatform ...@@ -960,18 +960,33 @@ abstract class AbstractPlatform
throw new \InvalidArgumentException("Incomplete definition. 'columns' required."); throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
} }
if ($index->isPrimary()) {
return $this->getCreatePrimaryKeySQL($index, $table);
} else {
$type = ''; $type = '';
if ($index->isUnique()) { if ($index->isUnique()) {
$type = 'UNIQUE '; $type = 'UNIQUE ';
} }
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table; $query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')'; $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
}
return $query; return $query;
} }
/**
* Get SQL to create an unnamed primary key constraint.
*
* @param Index $index
* @param string|Table $table
* @return string
*/
public function getCreatePrimaryKeySQL(Index $index, $table)
{
return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getColumns()) . ')';
}
/** /**
* Quotes a string so that it can be safely used as a table or column name, * Quotes a string so that it can be safely used as a table or column name,
* even if it is a reserved word of the platform. * even if it is a reserved word of the platform.
......
...@@ -607,14 +607,12 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -607,14 +607,12 @@ class MsSqlPlatform extends AbstractPlatform
// Remove ORDER BY clause from $query // Remove ORDER BY clause from $query
$query = preg_replace('/\s+ORDER BY(.*)/', '', $query); $query = preg_replace('/\s+ORDER BY(.*)/', '', $query);
$query = preg_replace('/^SELECT\s/', '', $query);
// Add ORDER BY clause as an argument for ROW_NUMBER()
$query = "SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", * FROM ($query) AS inner_tbl";
$start = $offset + 1; $start = $offset + 1;
$end = $offset + $count; $end = $offset + $count;
$query = "WITH outer_tbl AS ($query) SELECT * FROM outer_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end"; $query = "SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", $query) AS doctrine_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end";
} }
} }
......
...@@ -20,7 +20,9 @@ ...@@ -20,7 +20,9 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException, use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\TableDiff; Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table;
/** /**
* The MySqlPlatform provides the behavior, features and SQL dialect of the * The MySqlPlatform provides the behavior, features and SQL dialect of the
...@@ -524,19 +526,36 @@ class MySqlPlatform extends AbstractPlatform ...@@ -524,19 +526,36 @@ class MySqlPlatform extends AbstractPlatform
*/ */
public function getDropIndexSQL($index, $table=null) public function getDropIndexSQL($index, $table=null)
{ {
if($index instanceof \Doctrine\DBAL\Schema\Index) { if($index instanceof Index) {
$index = $index->getQuotedName($this); $indexName = $index->getQuotedName($this);
} else if(!is_string($index)) { } else if(is_string($index)) {
$indexName = $index;
} else {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
} }
if($table instanceof \Doctrine\DBAL\Schema\Table) { if($table instanceof Table) {
$table = $table->getQuotedName($this); $table = $table->getQuotedName($this);
} else if(!is_string($table)) { } else if(!is_string($table)) {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
} }
return 'DROP INDEX ' . $index . ' ON ' . $table; if ($index instanceof Index && $index->isPrimary()) {
// mysql primary keys are always named "PRIMARY",
// so we cannot use them in statements because of them being keyword.
return $this->getDropPrimaryKeySQL($table);
}
return 'DROP INDEX ' . $indexName . ' ON ' . $table;
}
/**
* @param Index $index
* @param Table $table
*/
protected function getDropPrimaryKeySQL($table)
{
return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
} }
/** /**
......
...@@ -288,12 +288,13 @@ class Table extends AbstractAsset ...@@ -288,12 +288,13 @@ class Table extends AbstractAsset
* @param array $localColumns * @param array $localColumns
* @param array $foreignColumns * @param array $foreignColumns
* @param array $options * @param array $options
* @param string $constraintName
* @return Table * @return Table
*/ */
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array()) public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null)
{ {
$name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength()); $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options); return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
} }
/** /**
...@@ -301,6 +302,7 @@ class Table extends AbstractAsset ...@@ -301,6 +302,7 @@ class Table extends AbstractAsset
* *
* Name is to be generated by the database itsself. * Name is to be generated by the database itsself.
* *
* @deprecated Use {@link addForeignKeyConstraint}
* @param Table $foreignTable * @param Table $foreignTable
* @param array $localColumns * @param array $localColumns
* @param array $foreignColumns * @param array $foreignColumns
...@@ -309,12 +311,13 @@ class Table extends AbstractAsset ...@@ -309,12 +311,13 @@ class Table extends AbstractAsset
*/ */
public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array()) public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
{ {
return $this->addNamedForeignKeyConstraint(null, $foreignTable, $localColumnNames, $foreignColumnNames, $options); return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
} }
/** /**
* Add a foreign key constraint with a given name * Add a foreign key constraint with a given name
* *
* @deprecated Use {@link addForeignKeyConstraint}
* @param string $name * @param string $name
* @param Table $foreignTable * @param Table $foreignTable
* @param array $localColumns * @param array $localColumns
......
...@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms; ...@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function testGenerateMixedCaseTableCreate() public function testGenerateMixedCaseTableCreate()
{ {
$table = new \Doctrine\DBAL\Schema\Table("Foo"); $table = new Table("Foo");
$table->addColumn("Bar", "integer"); $table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table); $sql = $this->_platform->getCreateTableSQL($table);
...@@ -145,6 +147,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -145,6 +147,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)'; return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
} }
/**
* @group DBAL-126
*/
public function testUniquePrimaryKey()
{
$keyTable = new Table("foo");
$keyTable->addColumn("bar", "integer");
$keyTable->addColumn("baz", "string");
$keyTable->setPrimaryKey(array("bar"));
$keyTable->addUniqueIndex(array("baz"));
$oldTable = new Table("foo");
$oldTable->addColumn("bar", "integer");
$oldTable->addColumn("baz", "string");
$c = new \Doctrine\DBAL\Schema\Comparator;
$diff = $c->diffTable($oldTable, $keyTable);
$sql = $this->_platform->getAlterTableSQL($diff);
$this->assertEquals(array(
"ALTER TABLE foo ADD PRIMARY KEY (bar)",
"CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
), $sql);
}
public function testModifyLimitQuery() public function testModifyLimitQuery()
{ {
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0); $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
......
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