From b6575631b366845b736e2b8160c4cb54e9ccea03 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 25 Jan 2007 22:27:20 +0000 Subject: [PATCH] added tests for mssql import driver --- lib/Doctrine/Export.php | 56 ++++++++++------------------------ lib/Doctrine/Import/Mssql.php | 28 ++++++++++++----- tests/Import/MssqlTestCase.php | 40 +++++++++++++++++++++++- 3 files changed, 75 insertions(+), 49 deletions(-) diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 4c9e20fb8..af96a5547 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -145,7 +145,7 @@ class Doctrine_Export extends Doctrine_Connection_Module $queryFields = $this->getFieldDeclarationList($fields); 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); @@ -191,11 +191,11 @@ class Doctrine_Export extends Doctrine_Connection_Module { $table = $this->conn->quoteIdentifier($table, 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'])) { - $query.= ' PRIMARY KEY'; + $query .= ' PRIMARY KEY'; } elseif (!empty($definition['unique'])) { - $query.= ' UNIQUE'; + $query .= ' UNIQUE'; } $fields = array(); 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 * - * * @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 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) { 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 * - * * @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 array $definition associative array that defines properties of the index to be created. - * Currently, only one property named FIELDS is supported. This property - * 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() - * ) - * ) + * @see Doctrine_Export::createIndex() * @return string */ public function createIndexSql($table, $name, array $definition) @@ -296,6 +261,17 @@ class Doctrine_Export extends Doctrine_Connection_Module $query .= ' (' . implode(', ', $fields) . ')'; 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 diff --git a/lib/Doctrine/Import/Mssql.php b/lib/Doctrine/Import/Mssql.php index c3db03023..c2206d8c1 100644 --- a/lib/Doctrine/Import/Mssql.php +++ b/lib/Doctrine/Import/Mssql.php @@ -54,7 +54,7 @@ class Doctrine_Import_Mssql extends Doctrine_Import */ 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); $columns = array(); @@ -102,7 +102,20 @@ class Doctrine_Import_Mssql extends Doctrine_Import { $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 @@ -113,10 +126,7 @@ class Doctrine_Import_Mssql extends Doctrine_Import public function listTableTriggers($table) { $table = $this->conn->quote($table, 'text'); - $query = "SELECT name FROM sysobjects WHERE xtype = 'TR'"; - if (!is_null($table)) { - $query .= "AND object_name(parent_obj) = $table"; - } + $query = "SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = " . $table; $result = $this->conn->fetchColumn($query); @@ -143,11 +153,13 @@ class Doctrine_Import_Mssql extends Doctrine_Import } $table = $this->conn->quote($table, 'text'); $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; - $pkAll = $this->conn->queryCol($query, 'text', $pkName); + $pkAll = $this->conn->fetchColumn($query, $pkName); + $result = array(); + foreach ($indexes as $index) { if (!in_array($index, $pkAll) && $index != null) { $result[] = $this->_fixIndexName($index); diff --git a/tests/Import/MssqlTestCase.php b/tests/Import/MssqlTestCase.php index 17cc6a27c..903e9abff 100644 --- a/tests/Import/MssqlTestCase.php +++ b/tests/Import/MssqlTestCase.php @@ -30,5 +30,43 @@ * @since 1.0 * @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'"); + } }