Skip to content

Commit

Permalink
Merge pull request #116 from chrudosvorlicek/create_geometry_outside_…
Browse files Browse the repository at this point in the history
…public_schema

Fix creation of geometry outside public schema
  • Loading branch information
njbarrett committed Jul 17, 2019
2 parents b29f5e2 + ff4ff60 commit c0b9526
Showing 1 changed file with 40 additions and 62 deletions.
102 changes: 40 additions & 62 deletions src/Schema/Grammars/PostgisGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ class PostgisGrammar extends PostgresGrammar
*/
public function typePoint(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return $type . '(POINT, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'POINT');
}

/**
Expand All @@ -37,15 +29,7 @@ public function typePoint(Fluent $column)
*/
public function typeMultipoint(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTIPOINT, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'MULTIPOINT');
}

/**
Expand All @@ -56,15 +40,7 @@ public function typeMultipoint(Fluent $column)
*/
public function typePolygon(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(POLYGON, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'POLYGON');
}

/**
Expand All @@ -75,15 +51,7 @@ public function typePolygon(Fluent $column)
*/
public function typeMultipolygon(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTIPOLYGON, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'MULTIPOLYGON');
}

/**
Expand All @@ -94,15 +62,7 @@ public function typeMultipolygon(Fluent $column)
*/
public function typeLinestring(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(LINESTRING, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'LINESTRING');
}

/**
Expand All @@ -113,15 +73,7 @@ public function typeLinestring(Fluent $column)
*/
public function typeMultilinestring(Fluent $column)
{
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTILINESTRING, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
return $this->createTypeDefinition($column, 'MULTILINESTRING');
}

/**
Expand Down Expand Up @@ -193,15 +145,18 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
$dimensions = $command->dimensions ?: 2;
$typmod = $command->typmod ? 'true' : 'false';
$srid = $command->srid ?: 4326;
$schema = function_exists('config') ? config('postgis.schema') : 'public';

return sprintf(
"SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d, %s)",
$blueprint->getTable(),
$command->column,
$srid,
strtoupper($command->type),
$dimensions,
$typmod
"SELECT %s.AddGeometryColumn('%s', '%s', %d, '%s.%s', %d, %s)",
$schema,
$blueprint->getTable(),
$command->column,
$srid,
$schema,
strtoupper($command->type),
$dimensions,
$typmod
);
}

Expand All @@ -211,7 +166,30 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
* @param \Illuminate\Support\Fluent $column
* @return boolean
*/
protected function isValid($column) {
protected function isValid($column)
{
return in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types) && is_int((int) $column->srid);
}

/**
* Create definition for geometry types.
* @param Fluent $column
* @param string $geometryType
* @return string
* @throws UnsupportedGeomtypeException
*/
private function createTypeDefinition(Fluent $column, $geometryType)
{
$schema = function_exists('config') ? config('postgis.schema') : 'public';
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return $schema . '.' . $type . '(' . $geometryType . ', ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

}

0 comments on commit c0b9526

Please sign in to comment.