Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from cjunge-work/schema-tweaks
Browse files Browse the repository at this point in the history
SchemaManager and Dlibplatform fixes and added geography type
  • Loading branch information
markcollister committed Dec 8, 2013
2 parents e9ab4c6 + 0497799 commit def7ba7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 193 deletions.
3 changes: 2 additions & 1 deletion Driver/PDODblib/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions Platforms/DblibPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down
4 changes: 4 additions & 0 deletions RealestateMssqlBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
195 changes: 3 additions & 192 deletions Schema/DblibSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,7 +17,7 @@
* @since 2.0
*/

class DblibSchemaManager extends MsSqlSchemaManager
class DblibSchemaManager extends SQLServerSchemaManager
{


Expand All @@ -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";
Expand All @@ -75,160 +39,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
*
Expand Down
54 changes: 54 additions & 0 deletions Types/PointType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Realestate\MssqlBundle\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;


class PointType extends Type
{
const POINT = 'point';

public function getName()
{
return self::POINT;
}

public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'POINT';
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
list($longitude, $latitude) = sscanf($value, 'POINT(%f %f)');

return array('latitude'=>$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);
}
}

0 comments on commit def7ba7

Please sign in to comment.