From af1b0f878d719fbbca12f449c7dd238e9b77108e Mon Sep 17 00:00:00 2001 From: Cameron Junge Date: Fri, 29 Nov 2013 12:26:47 +1300 Subject: [PATCH 1/2] Fixed driver to use correct classes; removed unneeded methods from SchemaManager --- Driver/PDODblib/Driver.php | 3 +- Schema/DblibSchemaManager.php | 159 +--------------------------------- 2 files changed, 5 insertions(+), 157 deletions(-) diff --git a/Driver/PDODblib/Driver.php b/Driver/PDODblib/Driver.php index 2bf2425..2085ffb 100644 --- a/Driver/PDODblib/Driver.php +++ b/Driver/PDODblib/Driver.php @@ -12,6 +12,7 @@ namespace Realestate\MssqlBundle\Driver\PDODblib; use Realestate\MssqlBundle\Platforms\DblibPlatform; +use Realestate\MssqlBundle\Schema\DblibSchemaManager; class Driver implements \Doctrine\DBAL\Driver { @@ -97,7 +98,7 @@ public function getDatabasePlatform() public function getSchemaManager(\Doctrine\DBAL\Connection $conn) { - return new \Doctrine\DBAL\Schema\DblibSchemaManager($conn); + return new DblibSchemaManager($conn); } public function getName() diff --git a/Schema/DblibSchemaManager.php b/Schema/DblibSchemaManager.php index c31e975..cf932d5 100644 --- a/Schema/DblibSchemaManager.php +++ b/Schema/DblibSchemaManager.php @@ -2,7 +2,7 @@ namespace Realestate\MssqlBundle\Schema; -use Doctrine\DBAL\Schema\MsSqlSchemaManager; +use Doctrine\DBAL\Schema\SQLServerSchemaManager; /** * Schema manager for the MsSql/Dblib RDBMS. @@ -17,7 +17,7 @@ * @since 2.0 */ -class DblibSchemaManager extends MsSqlSchemaManager +class DblibSchemaManager extends SQLServerSchemaManager { @@ -75,160 +75,7 @@ public function createDatabase($name) } return $this->_conn->standaloneQuery($query, null, true); } - /** - * alter an existing table - * - * @param string $name name of the table that is intended to be changed. - * @param array $changes associative array that contains the details of each type - * of change that is intended to be performed. The types of - * changes that are currently supported are defined as follows: - * - * name - * - * New name for the table. - * - * add - * - * Associative array with the names of fields to be added as - * indexes of the array. The value of each entry of the array - * should be set to another associative array with the properties - * of the fields to be added. The properties of the fields should - * be the same as defined by the Metabase parser. - * - * - * remove - * - * Associative array with the names of fields to be removed as indexes - * of the array. Currently the values assigned to each entry are ignored. - * An empty array should be used for future compatibility. - * - * rename - * - * Associative array with the names of fields to be renamed as indexes - * of the array. The value of each entry of the array should be set to - * another associative array with the entry named name with the new - * field name and the entry named Declaration that is expected to contain - * the portion of the field declaration already in DBMS specific SQL code - * as it is used in the CREATE TABLE statement. - * - * change - * - * Associative array with the names of the fields to be changed as indexes - * of the array. Keep in mind that if it is intended to change either the - * name of a field and any other properties, the change array entries - * should have the new names of the fields as array indexes. - * - * The value of each entry of the array should be set to another associative - * array with the properties of the fields to that are meant to be changed as - * array entries. These entries should be assigned to the new values of the - * respective properties. The properties of the fields should be the same - * as defined by the Metabase parser. - * - * Example - * array( - * 'name' => 'userlist', - * 'add' => array( - * 'quota' => array( - * 'type' => 'integer', - * 'unsigned' => 1 - * ) - * ), - * 'remove' => array( - * 'file_limit' => array(), - * 'time_limit' => array() - * ), - * 'change' => array( - * 'name' => array( - * 'length' => '20', - * 'definition' => array( - * 'type' => 'text', - * 'length' => 20, - * ), - * ) - * ), - * 'rename' => array( - * 'sex' => array( - * 'name' => 'gender', - * 'definition' => array( - * 'type' => 'text', - * 'length' => 1, - * 'default' => 'M', - * ), - * ) - * ) - * ) - * - * @param boolean $check indicates whether the function should just check if the DBMS driver - * can perform the requested table alterations if the value is true or - * actually perform them otherwise. - * @return void - */ - public function alterTable($name, array $changes, $check = false) - { - foreach ($changes as $changeName => $change) { - switch ($changeName) { - case 'add': - break; - case 'remove': - break; - case 'name': - case 'rename': - case 'change': - default: - throw SchemaException::alterTableChangeNotSupported($changeName); - } - } - - $query = ''; - if ( ! empty($changes['add']) && is_array($changes['add'])) { - foreach ($changes['add'] as $fieldName => $field) { - if ($query) { - $query .= ', '; - } - $query .= 'ADD ' . $this->getDeclaration($fieldName, $field); - } - } - - if ( ! empty($changes['remove']) && is_array($changes['remove'])) { - foreach ($changes['remove'] as $fieldName => $field) { - if ($query) { - $query .= ', '; - } - $query .= 'DROP COLUMN ' . $fieldName; - } - } - - if ( ! $query) { - return false; - } - - return $this->_conn->exec('ALTER TABLE ' . $name . ' ' . $query); - } - - /** - * {@inheritdoc} - */ - public function createSequence($seqName, $start = 1, $allocationSize = 1) - { - $seqcolName = 'seq_col'; - $query = 'CREATE TABLE ' . $seqName . ' (' . $seqcolName . - ' INT PRIMARY KEY CLUSTERED IDENTITY(' . $start . ', 1) NOT NULL)'; - - $res = $this->_conn->exec($query); - - if ($start == 1) { - return true; - } - - try { - $query = 'SET IDENTITY_INSERT ' . $sequenceName . ' ON ' . - 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES ( ' . $start . ')'; - $res = $this->_conn->exec($query); - } catch (Exception $e) { - $result = $this->_conn->exec('DROP TABLE ' . $sequenceName); - } - return true; - } + /** * lists all database sequences * From 049779986dbb4a7c8100375a2bcf7bc3a387f428 Mon Sep 17 00:00:00 2001 From: Cameron Junge Date: Wed, 4 Dec 2013 11:45:57 +1300 Subject: [PATCH 2/2] More tweaks to the SQLSchemaManager and DlibPlatform; added geography type --- Platforms/DblibPlatform.php | 4 +++ RealestateMssqlBundle.php | 4 +++ Schema/DblibSchemaManager.php | 36 ----------------------- Types/PointType.php | 54 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 Types/PointType.php diff --git a/Platforms/DblibPlatform.php b/Platforms/DblibPlatform.php index 769a429..55e4303 100644 --- a/Platforms/DblibPlatform.php +++ b/Platforms/DblibPlatform.php @@ -136,6 +136,10 @@ protected function initializeDoctrineTypeMappings() // add uniqueidentifier $this->doctrineTypeMapping['uniqueidentifier'] = 'uniqueidentifier'; + // use the geography type + $this->doctrineTypeMapping['geography'] = 'geography'; + // define this column type as a string so it works properly for now + $this->doctrineTypeMapping['hierarchyid'] = 'string'; } /** diff --git a/RealestateMssqlBundle.php b/RealestateMssqlBundle.php index 7c1340c..37b2721 100644 --- a/RealestateMssqlBundle.php +++ b/RealestateMssqlBundle.php @@ -22,6 +22,10 @@ public function boot() if(!Type::hasType('uniqueidentifier')) { Type::addType('uniqueidentifier', 'Realestate\MssqlBundle\Types\UniqueidentifierType'); } + + if(!Type::hasType('geography')) { + Type::addType('geography', 'Realestate\MssqlBundle\Types\PointType'); + } Type::overrideType('date', 'Realestate\MssqlBundle\Types\DateType'); Type::overrideType('datetime', 'Realestate\MssqlBundle\Types\DateTimeType'); diff --git a/Schema/DblibSchemaManager.php b/Schema/DblibSchemaManager.php index cf932d5..7b5c37c 100644 --- a/Schema/DblibSchemaManager.php +++ b/Schema/DblibSchemaManager.php @@ -29,42 +29,6 @@ protected function _getPortableSequenceDefinition($sequence) return end($sequence); } - - - protected function _getPortableTableForeignKeysList($tableForeignKeys) - { - $list = array(); - foreach ($tableForeignKeys as $key => $value) { - $value = \array_change_key_case($value, CASE_LOWER); - if (!isset($list[$value['constraint_name']])) { - if ($value['delete_rule'] == "NO ACTION") { - $value['delete_rule'] = null; - } - - $list[$value['pkconstraint_name']] = array( - 'name' => $value['pkconstraint_name'], - 'local' => array(), - 'foreign' => array(), - 'foreignTable' => $value['fktable_name'], - 'onDelete' => $value['delete_rule'], - ); - } - $list[$value['pkconstraint_name']]['local'][$value['deferrability']] = $value['pkcolumn_name']; - $list[$value['pkconstraint_name']]['foreign'][$value['deferrability']] = $value['fkcolumn_name']; - } - - $result = array(); - foreach($list AS $constraint) { - $result[] = new ForeignKeyConstraint( - array_values($constraint['local']), $constraint['foreignTable'], - array_values($constraint['foreign']), $constraint['name'], - array('onDelete' => $constraint['onDelete']) - ); - } - - return $result; - } - public function createDatabase($name) { $query = "CREATE DATABASE $name"; diff --git a/Types/PointType.php b/Types/PointType.php new file mode 100644 index 0000000..8e9389b --- /dev/null +++ b/Types/PointType.php @@ -0,0 +1,54 @@ +$latitude, 'longitude'=>$longitude); + } + + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if (is_array($value)) { + $value = sprintf('POINT(%F %F)', $value['longitude'], $value['latitude']); + } + + return $value; + } + + public function canRequireSQLConversion() + { + return true; + } + + public function convertToPHPValueSQL($sqlExpr, $platform) + { + var_dump($sqlExpr);die; + return sprintf('%s.STAsText()', $sqlExpr); + } + + public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) + { + return sprintf('geography::STPointFromText(\'%s\', 4326)', $sqlExpr); + } +} \ No newline at end of file