Skip to content

Commit

Permalink
Fix handling of table and column names using reserved words
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Nov 25, 2016
1 parent 0fbe9d9 commit a2ae238
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public function listSpatialIndexes($table)
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
ORDER BY i.relname";

$tableIndexes = $this->connection->fetchAll($sql, array($table));
$tableIndexes = $this->connection->fetchAll(
$sql,
array(
$this->trimQuotes($table)
)
);

$indexes = array();
foreach ($tableIndexes as $row) {
Expand Down Expand Up @@ -80,7 +85,12 @@ public function listSpatialGeometryColumns($table)
FROM geometry_columns
WHERE f_table_name = ?';

$tableColumns = $this->connection->fetchAll($sql, array($table));
$tableColumns = $this->connection->fetchAll(
$sql,
array(
$this->trimQuotes($table)
)
);

$columns = array();
foreach ($tableColumns as $row) {
Expand All @@ -101,7 +111,13 @@ public function getGeometrySpatialColumnInfo($table, $column)
WHERE f_table_name = ?
AND f_geometry_column = ?';

$row = $this->connection->fetchAssoc($sql, array($table, $column));
$row = $this->connection->fetchAssoc(
$sql,
array(
$this->trimQuotes($table),
$this->trimQuotes($column)
)
);

if (!$row) {
return null;
Expand All @@ -121,7 +137,13 @@ public function getGeographySpatialColumnInfo($table, $column)
WHERE f_table_name = ?
AND f_geography_column = ?';

$row = $this->connection->fetchAssoc($sql, array($table, $column));
$row = $this->connection->fetchAssoc(
$sql,
array(
$this->trimQuotes($table),
$this->trimQuotes($column)
)
);

if (!$row) {
return null;
Expand Down Expand Up @@ -149,4 +171,13 @@ protected function buildSpatialColumnInfo($row)
'srid' => max((int) $row['srid'], 0),
);
}

/**
* Copied from Doctrine\DBAL\Schema\AbstractAsset::trimQuotes,
* check on updates!
*/
protected function trimQuotes($identifier)
{
return str_replace(array('`', '"', '[', ']'), '', $identifier);
}
}
24 changes: 24 additions & 0 deletions tests/Event/ORMSchemaEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Jsor\Doctrine\PostGIS\AbstractFunctionalTestCase;
use Jsor\Doctrine\PostGIS\PointsEntity;
use Jsor\Doctrine\PostGIS\ReservedWordsEntity;

class ORMSchemaEventSubscriberTest extends AbstractFunctionalTestCase
{
Expand Down Expand Up @@ -51,4 +52,27 @@ public function testEntity()
$this->assertEquals('SRID=4326;POINT(1 1)', $entity->getPointGeography2d());
$this->assertEquals('SRID=4326;POINT(1 1)', $entity->getPointGeography2dSrid());
}

public function testEntityWithReservedWords()
{
$this->_setUpEntitySchema(array(
'Jsor\Doctrine\PostGIS\ReservedWordsEntity',
));

$em = $this->_getEntityManager();

$entity = new ReservedWordsEntity(array(
'user' => 'POINT(1 1)',
'primary' => 'SRID=4326;POINT(1 1)',
));

$em->persist($entity);
$em->flush();
$em->clear();

$entity = $em->find('Jsor\Doctrine\PostGIS\ReservedWordsEntity', 1);

$this->assertEquals('POINT(1 1)', $entity->getUser());
$this->assertEquals('SRID=4326;POINT(1 1)', $entity->getPrimary());
}
}
38 changes: 38 additions & 0 deletions tests/Schema/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ protected function setUp()

$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_points_drop.sql');
$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_points_create.sql');

$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_reserved-words_drop.sql');
$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_reserved-words_create.sql');
}

protected function tearDown()
{
parent::tearDown();

$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_points_drop.sql');

$this->_execFile('postgis-' . getenv('POSTGIS_VERSION') . '_reserved-words_drop.sql');
}

public function testListSpatialIndexes()
Expand Down Expand Up @@ -76,6 +81,17 @@ public function testListSpatialGeometryColumns()
$this->assertEquals($expected, $schemaManager->listSpatialGeometryColumns('foo.points'));
}

public function testListSpatialGeometryColumnsWithReservedWords()
{
$schemaManager = new SchemaManager($this->_getConnection());

$expected = array(
'user'
);

$this->assertEquals($expected, $schemaManager->listSpatialGeometryColumns('"user"'));
}

public function testGetGeometrySpatialColumnInfo()
{
$schemaManager = new SchemaManager($this->_getConnection());
Expand Down Expand Up @@ -156,6 +172,28 @@ public function testGetGeographySpatialColumnInfo()
$this->assertEquals($expected, $schemaManager->getGeographySpatialColumnInfo('points', 'point_geography_2d_srid'));
}

public function testGetGeometrySpatialColumnInfoWithReservedWords()
{
$schemaManager = new SchemaManager($this->_getConnection());

$expected = array(
'type' => 'GEOMETRY',
'srid' => 0,
);
$this->assertEquals($expected, $schemaManager->getGeometrySpatialColumnInfo('"user"', '"user"'));
}

public function testGetGeographySpatialColumnInfoWithReservedWords()
{
$schemaManager = new SchemaManager($this->_getConnection());

$expected = array(
'type' => 'GEOMETRY',
'srid' => 4326,
);
$this->assertEquals($expected, $schemaManager->getGeographySpatialColumnInfo('"user"', '"primary"'));
}

/**
* @group postgis-1.5
*/
Expand Down
51 changes: 51 additions & 0 deletions tests/fixtures/ReservedWordsEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Jsor\Doctrine\PostGIS;

use Doctrine\ORM\Mapping as ORM;

/**
* Entity with reserved words as table and column names.
*
* @ORM\Entity
* @ORM\Table(
* name="`user`"
* )
*/
class ReservedWordsEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;


/**
* @ORM\Column(type="geometry", name="`user`")
*/
private $user;

/**
* @ORM\Column(type="geography", name="`primary`")
*/
private $primary;

public function __construct(array $points)
{
foreach ($points as $key => $val) {
$this->$key = $val;
}
}

public function getUser()
{
return $this->user;
}

public function getPrimary()
{
return $this->primary;
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/postgis-1.5_reserved-words_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE "user" (id INT NOT NULL, "primary" geography(GEOMETRY, 4326) NOT NULL, PRIMARY KEY(id));

SELECT AddGeometryColumn('user', 'user', -1, 'GEOMETRY', 2);
ALTER TABLE "user" ALTER "user" SET NOT NULL;
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-1.5_reserved-words_drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP SEQUENCE IF EXISTS user_id_seq CASCADE;
SELECT DropGeometryTable('user');
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.0_reserved-words_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE user (id INT NOT NULL, "user" geometry(GEOMETRY, 0) NOT NULL, "primary" geography(GEOMETRY, 4326) NOT NULL, PRIMARY KEY(id));
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.0_reserved-words_drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP SEQUENCE IF EXISTS user_id_seq CASCADE;
DROP TABLE IF EXISTS "user";
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.1_reserved-words_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE "user" (id INT NOT NULL, "user" geometry(GEOMETRY, 0) NOT NULL, "primary" geography(GEOMETRY, 4326) NOT NULL, PRIMARY KEY(id));
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.1_reserved-words_drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP SEQUENCE IF EXISTS user_id_seq CASCADE;
DROP TABLE IF EXISTS "user";
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.2_reserved-words_create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE "user" (id INT NOT NULL, "user" geometry(GEOMETRY, 0) NOT NULL, "primary" geography(GEOMETRY, 4326) NOT NULL, PRIMARY KEY(id));
2 changes: 2 additions & 0 deletions tests/fixtures/postgis-2.2_reserved-words_drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP SEQUENCE IF EXISTS user_id_seq CASCADE;
DROP TABLE IF EXISTS "user";

0 comments on commit a2ae238

Please sign in to comment.