added tests for mssql import driver
This commit is contained in:
parent
cdddf75112
commit
b6575631b3
@ -145,7 +145,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
$queryFields = $this->getFieldDeclarationList($fields);
|
$queryFields = $this->getFieldDeclarationList($fields);
|
||||||
|
|
||||||
if (isset($options['primary']) && ! empty($options['primary'])) {
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
||||||
$queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')';
|
$queryFields.= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = $this->conn->quoteIdentifier($name, true);
|
$name = $this->conn->quoteIdentifier($name, true);
|
||||||
@ -191,11 +191,11 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
{
|
{
|
||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table, true);
|
||||||
$name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true);
|
$name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true);
|
||||||
$query = "ALTER TABLE $table ADD CONSTRAINT $name";
|
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
|
||||||
if (!empty($definition['primary'])) {
|
if (!empty($definition['primary'])) {
|
||||||
$query.= ' PRIMARY KEY';
|
$query .= ' PRIMARY KEY';
|
||||||
} elseif (!empty($definition['unique'])) {
|
} elseif (!empty($definition['unique'])) {
|
||||||
$query.= ' UNIQUE';
|
$query .= ' UNIQUE';
|
||||||
}
|
}
|
||||||
$fields = array();
|
$fields = array();
|
||||||
foreach (array_keys($definition['fields']) as $field) {
|
foreach (array_keys($definition['fields']) as $field) {
|
||||||
@ -207,7 +207,6 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
/**
|
/**
|
||||||
* Get the stucture of a field into an array
|
* Get the stucture of a field into an array
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param string $table name of the table on which the index is to be created
|
* @param string $table name of the table on which the index is to be created
|
||||||
* @param string $name name of the index to be created
|
* @param string $name name of the index to be created
|
||||||
* @param array $definition associative array that defines properties of the index to be created.
|
* @param array $definition associative array that defines properties of the index to be created.
|
||||||
@ -239,48 +238,14 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
public function createIndex($table, $name, array $definition)
|
public function createIndex($table, $name, array $definition)
|
||||||
{
|
{
|
||||||
return $this->conn->execute($this->createIndexSql($table, $name, $definition));
|
return $this->conn->execute($this->createIndexSql($table, $name, $definition));
|
||||||
}
|
|
||||||
/**
|
|
||||||
* createForeignKey
|
|
||||||
*
|
|
||||||
* @param string $table name of the table on which the index is to be created
|
|
||||||
* @param string $name name of the foreign key to be created
|
|
||||||
* @param array $definition associative array that defines properties of the foreign key to be created.
|
|
||||||
*/
|
|
||||||
public function createForeignKey($table, $name, array $definition)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Get the stucture of a field into an array
|
* Get the stucture of a field into an array
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param string $table name of the table on which the index is to be created
|
* @param string $table name of the table on which the index is to be created
|
||||||
* @param string $name name of the index to be created
|
* @param string $name name of the index to be created
|
||||||
* @param array $definition associative array that defines properties of the index to be created.
|
* @param array $definition associative array that defines properties of the index to be created.
|
||||||
* Currently, only one property named FIELDS is supported. This property
|
* @see Doctrine_Export::createIndex()
|
||||||
* is also an associative with the names of the index fields as array
|
|
||||||
* indexes. Each entry of this array is set to another type of associative
|
|
||||||
* array that specifies properties of the index that are specific to
|
|
||||||
* each field.
|
|
||||||
*
|
|
||||||
* Currently, only the sorting property is supported. It should be used
|
|
||||||
* to define the sorting direction of the index. It may be set to either
|
|
||||||
* ascending or descending.
|
|
||||||
*
|
|
||||||
* Not all DBMS support index sorting direction configuration. The DBMS
|
|
||||||
* drivers of those that do not support it ignore this property. Use the
|
|
||||||
* function supports() to determine whether the DBMS driver can manage indexes.
|
|
||||||
*
|
|
||||||
* Example
|
|
||||||
* array(
|
|
||||||
* 'fields' => array(
|
|
||||||
* 'user_name' => array(
|
|
||||||
* 'sorting' => 'ascending'
|
|
||||||
* ),
|
|
||||||
* 'last_login' => array()
|
|
||||||
* )
|
|
||||||
* )
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function createIndexSql($table, $name, array $definition)
|
public function createIndexSql($table, $name, array $definition)
|
||||||
@ -296,6 +261,17 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
$query .= ' (' . implode(', ', $fields) . ')';
|
$query .= ' (' . implode(', ', $fields) . ')';
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* createForeignKey
|
||||||
|
*
|
||||||
|
* @param string $table name of the table on which the index is to be created
|
||||||
|
* @param string $name name of the foreign key to be created
|
||||||
|
* @param array $definition associative array that defines properties of the foreign key to be created.
|
||||||
|
*/
|
||||||
|
public function createForeignKey($table, $name, array $definition)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* alter an existing table
|
* alter an existing table
|
||||||
|
@ -54,7 +54,7 @@ class Doctrine_Import_Mssql extends Doctrine_Import
|
|||||||
*/
|
*/
|
||||||
public function listTableColumns($table)
|
public function listTableColumns($table)
|
||||||
{
|
{
|
||||||
$sql = 'EXEC sp_columns @table_name = ' . $this->quoteIdentifier($table);
|
$sql = 'EXEC sp_columns @table_name = ' . $this->conn->quoteIdentifier($table, true);
|
||||||
$result = $this->conn->fetchAssoc($sql);
|
$result = $this->conn->fetchAssoc($sql);
|
||||||
$columns = array();
|
$columns = array();
|
||||||
|
|
||||||
@ -102,7 +102,20 @@ class Doctrine_Import_Mssql extends Doctrine_Import
|
|||||||
{
|
{
|
||||||
$sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
|
$sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
|
||||||
|
|
||||||
return $this->dbh->fetchColumn($sql);
|
return $this->conn->fetchColumn($sql);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* lists all triggers
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function listTriggers()
|
||||||
|
{
|
||||||
|
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
|
||||||
|
|
||||||
|
$result = $this->conn->fetchColumn($query);
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* lists table triggers
|
* lists table triggers
|
||||||
@ -113,10 +126,7 @@ class Doctrine_Import_Mssql extends Doctrine_Import
|
|||||||
public function listTableTriggers($table)
|
public function listTableTriggers($table)
|
||||||
{
|
{
|
||||||
$table = $this->conn->quote($table, 'text');
|
$table = $this->conn->quote($table, 'text');
|
||||||
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR'";
|
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table;
|
||||||
if (!is_null($table)) {
|
|
||||||
$query .= "AND object_name(parent_obj) = $table";
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = $this->conn->fetchColumn($query);
|
$result = $this->conn->fetchColumn($query);
|
||||||
|
|
||||||
@ -143,11 +153,13 @@ class Doctrine_Import_Mssql extends Doctrine_Import
|
|||||||
}
|
}
|
||||||
$table = $this->conn->quote($table, 'text');
|
$table = $this->conn->quote($table, 'text');
|
||||||
$query = 'EXEC sp_statistics @table_name = ' . $table;
|
$query = 'EXEC sp_statistics @table_name = ' . $table;
|
||||||
$indexes = $this->conn->queryCol($query, 'text', $keyName);
|
$indexes = $this->conn->fetchColumn($query, $keyName);
|
||||||
|
|
||||||
$query = 'EXEC sp_pkeys @table_name = ' . $table;
|
$query = 'EXEC sp_pkeys @table_name = ' . $table;
|
||||||
$pkAll = $this->conn->queryCol($query, 'text', $pkName);
|
$pkAll = $this->conn->fetchColumn($query, $pkName);
|
||||||
|
|
||||||
$result = array();
|
$result = array();
|
||||||
|
|
||||||
foreach ($indexes as $index) {
|
foreach ($indexes as $index) {
|
||||||
if (!in_array($index, $pkAll) && $index != null) {
|
if (!in_array($index, $pkAll) && $index != null) {
|
||||||
$result[] = $this->_fixIndexName($index);
|
$result[] = $this->_fixIndexName($index);
|
||||||
|
@ -30,5 +30,43 @@
|
|||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
class Doctrine_Import_Mssql_TestCase extends Doctrine_UnitTestCase {
|
class Doctrine_Import_Mssql_TestCase extends Doctrine_UnitTestCase
|
||||||
|
{
|
||||||
|
public function testListSequencesExecutesSql()
|
||||||
|
{
|
||||||
|
$this->import->listSequences('table');
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "SELECT name FROM sysobjects WHERE xtype = 'U'");
|
||||||
|
}
|
||||||
|
public function testListTableColumnsExecutesSql()
|
||||||
|
{
|
||||||
|
$this->conn->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, false);
|
||||||
|
$this->import->listTableColumns('table');
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "EXEC sp_columns @table_name = table");
|
||||||
|
}
|
||||||
|
public function testListTablesExecutesSql()
|
||||||
|
{
|
||||||
|
$this->import->listTables();
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name");
|
||||||
|
}
|
||||||
|
public function testListTriggersExecutesSql()
|
||||||
|
{
|
||||||
|
$this->import->listTriggers();
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "SELECT name FROM sysobjects WHERE xtype = 'TR'");
|
||||||
|
}
|
||||||
|
public function testListTableTriggersExecutesSql()
|
||||||
|
{
|
||||||
|
$this->import->listTableTriggers('table');
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = 'table'");
|
||||||
|
}
|
||||||
|
public function testListViewsExecutesSql()
|
||||||
|
{
|
||||||
|
$this->import->listViews();
|
||||||
|
|
||||||
|
$this->assertEqual($this->adapter->pop(), "SELECT name FROM sysobjects WHERE xtype = 'V'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user