Skip to content

Commit c0b9526

Browse files
authored
Merge pull request mstaack#116 from chrudosvorlicek/create_geometry_outside_public_schema
Fix creation of geometry outside public schema
2 parents b29f5e2 + ff4ff60 commit c0b9526

File tree

1 file changed

+40
-62
lines changed

1 file changed

+40
-62
lines changed

src/Schema/Grammars/PostgisGrammar.php

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ class PostgisGrammar extends PostgresGrammar
1818
*/
1919
public function typePoint(Fluent $column)
2020
{
21-
$type = strtoupper($column->geomtype);
22-
if ($this->isValid($column)) {
23-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
24-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
25-
}
26-
return $type . '(POINT, ' . $column->srid . ')';
27-
} else {
28-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
29-
}
21+
return $this->createTypeDefinition($column, 'POINT');
3022
}
3123

3224
/**
@@ -37,15 +29,7 @@ public function typePoint(Fluent $column)
3729
*/
3830
public function typeMultipoint(Fluent $column)
3931
{
40-
$type = strtoupper($column->geomtype);
41-
if ($this->isValid($column)) {
42-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
43-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
44-
}
45-
return strtoupper($column->geomtype) . '(MULTIPOINT, ' . $column->srid . ')';
46-
} else {
47-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
48-
}
32+
return $this->createTypeDefinition($column, 'MULTIPOINT');
4933
}
5034

5135
/**
@@ -56,15 +40,7 @@ public function typeMultipoint(Fluent $column)
5640
*/
5741
public function typePolygon(Fluent $column)
5842
{
59-
$type = strtoupper($column->geomtype);
60-
if ($this->isValid($column)) {
61-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
62-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
63-
}
64-
return strtoupper($column->geomtype) . '(POLYGON, ' . $column->srid . ')';
65-
} else {
66-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
67-
}
43+
return $this->createTypeDefinition($column, 'POLYGON');
6844
}
6945

7046
/**
@@ -75,15 +51,7 @@ public function typePolygon(Fluent $column)
7551
*/
7652
public function typeMultipolygon(Fluent $column)
7753
{
78-
$type = strtoupper($column->geomtype);
79-
if ($this->isValid($column)) {
80-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
81-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
82-
}
83-
return strtoupper($column->geomtype) . '(MULTIPOLYGON, ' . $column->srid . ')';
84-
} else {
85-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
86-
}
54+
return $this->createTypeDefinition($column, 'MULTIPOLYGON');
8755
}
8856

8957
/**
@@ -94,15 +62,7 @@ public function typeMultipolygon(Fluent $column)
9462
*/
9563
public function typeLinestring(Fluent $column)
9664
{
97-
$type = strtoupper($column->geomtype);
98-
if ($this->isValid($column)) {
99-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
100-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
101-
}
102-
return strtoupper($column->geomtype) . '(LINESTRING, ' . $column->srid . ')';
103-
} else {
104-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
105-
}
65+
return $this->createTypeDefinition($column, 'LINESTRING');
10666
}
10767

10868
/**
@@ -113,15 +73,7 @@ public function typeLinestring(Fluent $column)
11373
*/
11474
public function typeMultilinestring(Fluent $column)
11575
{
116-
$type = strtoupper($column->geomtype);
117-
if ($this->isValid($column)) {
118-
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
119-
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
120-
}
121-
return strtoupper($column->geomtype) . '(MULTILINESTRING, ' . $column->srid . ')';
122-
} else {
123-
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
124-
}
76+
return $this->createTypeDefinition($column, 'MULTILINESTRING');
12577
}
12678

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

197150
return sprintf(
198-
"SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d, %s)",
199-
$blueprint->getTable(),
200-
$command->column,
201-
$srid,
202-
strtoupper($command->type),
203-
$dimensions,
204-
$typmod
151+
"SELECT %s.AddGeometryColumn('%s', '%s', %d, '%s.%s', %d, %s)",
152+
$schema,
153+
$blueprint->getTable(),
154+
$command->column,
155+
$srid,
156+
$schema,
157+
strtoupper($command->type),
158+
$dimensions,
159+
$typmod
205160
);
206161
}
207162

@@ -211,7 +166,30 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
211166
* @param \Illuminate\Support\Fluent $column
212167
* @return boolean
213168
*/
214-
protected function isValid($column) {
169+
protected function isValid($column)
170+
{
215171
return in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types) && is_int((int) $column->srid);
216172
}
173+
174+
/**
175+
* Create definition for geometry types.
176+
* @param Fluent $column
177+
* @param string $geometryType
178+
* @return string
179+
* @throws UnsupportedGeomtypeException
180+
*/
181+
private function createTypeDefinition(Fluent $column, $geometryType)
182+
{
183+
$schema = function_exists('config') ? config('postgis.schema') : 'public';
184+
$type = strtoupper($column->geomtype);
185+
if ($this->isValid($column)) {
186+
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
187+
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
188+
}
189+
return $schema . '.' . $type . '(' . $geometryType . ', ' . $column->srid . ')';
190+
} else {
191+
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
192+
}
193+
}
194+
217195
}

0 commit comments

Comments
 (0)