Skip to content

Instantly share code, notes, and snippets.

@jsor
Created December 5, 2011 15:40
Show Gist options
  • Save jsor/1433998 to your computer and use it in GitHub Desktop.
Save jsor/1433998 to your computer and use it in GitHub Desktop.
DoctrineGISSchemaHandler
<?php
namespace DoctrineGIS\Schema;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\CustomSchemaHandler;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Table;
class DoctrineGISSchemaHandler implements CustomSchemaHandler
{
public function getPortableTableColumnDefinition($table, $database, $tableColumn, Connection $conn)
{
switch ($conn->getDatabasePlatform()->getName()) {
case 'postgresql':
return $this->getPortablePostgresTableColumnDefinition($table, $database, $tableColumn, $conn);
default:
return false;
}
}
protected function getPortablePostgresTableColumnDefinition($table, $database, $tableColumn, Connection $conn)
{
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
if ($tableColumn['type'] === 'geometry') {
$sql = "SELECT coord_dimension, srid, type FROM geometry_columns WHERE f_table_name=? AND f_geometry_column=?";
$stmt = $conn->prepare($sql);
$stmt->execute(array($table, $tableColumn['field']));
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
$type = strtolower($row['type']);
$options = array(
'length' => null,
'notnull' => (bool) $tableColumn['isnotnull'],
'default' => $tableColumn['default'],
'primary' => (bool) ($tableColumn['pri'] == 't'),
'precision' => null,
'scale' => null,
'fixed' => null,
'unsigned' => false,
'autoincrement' => false,
'comment' => $tableColumn['comment'],
'platformOptions' => array(
'srid' => $row['srid'],
'coord_dimension' => $row['coord_dimension']
)
);
return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
}
return false;
}
public function getColumnDeclarationSQL($name, array $field, AbstractPlatform $platform)
{
switch ($platform->getName()) {
case 'postgresql':
switch (strtolower($field['type']->getName())) {
case 'point':
case 'linestring':
case 'polygon':
case 'multipoint':
case 'multilinestring':
case 'multipolygon':
case 'geometrycollection':
return null; // Skip
}
return false;
default:
return false;
}
}
public function getCreateTableSQL(Table $table, AbstractPlatform $platform)
{
$query = array();
switch ($platform->getName()) {
case 'postgresql':
foreach ($table->getColumns() as $column) {
$type = $column->getType()->getName();
switch (strtolower($type)) {
case 'point':
case 'linestring':
case 'polygon':
case 'multipoint':
case 'multilinestring':
case 'multipolygon':
case 'geometrycollection':
$query[] = sprintf(
"SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d)",
strtolower($table->getQuotedName($platform)), // Table name
$column->getQuotedName($platform), // Column name
-1, // SRID
strtoupper($type), // Geometry type
1 // Dimension
);
}
}
break;
default:
break;
}
return $query;
}
public function getDropTableSQL(Table $table, AbstractPlatform $platform)
{
$query = array();
switch ($platform->getName()) {
// We us DropGeometryColumn() to also drop entries from the geometry_columns table
case 'postgresql':
foreach ($table->getColumns() as $column) {
$type = $column->getType()->getName();
switch (strtolower($type)) {
case 'point':
case 'linestring':
case 'polygon':
case 'multipoint':
case 'multilinestring':
case 'multipolygon':
case 'geometrycollection':
$query[] = sprintf(
"SELECT DropGeometryColumn ('%s', '%s')",
strtolower($table->getQuotedName($platform)), // Table name
$column->getQuotedName($platform) // Column name
);
}
}
break;
default:
break;
}
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment