@@ -18,15 +18,7 @@ class PostgisGrammar extends PostgresGrammar
18
18
*/
19
19
public function typePoint (Fluent $ column )
20
20
{
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 ' );
30
22
}
31
23
32
24
/**
@@ -37,15 +29,7 @@ public function typePoint(Fluent $column)
37
29
*/
38
30
public function typeMultipoint (Fluent $ column )
39
31
{
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 ' );
49
33
}
50
34
51
35
/**
@@ -56,15 +40,7 @@ public function typeMultipoint(Fluent $column)
56
40
*/
57
41
public function typePolygon (Fluent $ column )
58
42
{
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 ' );
68
44
}
69
45
70
46
/**
@@ -75,15 +51,7 @@ public function typePolygon(Fluent $column)
75
51
*/
76
52
public function typeMultipolygon (Fluent $ column )
77
53
{
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 ' );
87
55
}
88
56
89
57
/**
@@ -94,15 +62,7 @@ public function typeMultipolygon(Fluent $column)
94
62
*/
95
63
public function typeLinestring (Fluent $ column )
96
64
{
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 ' );
106
66
}
107
67
108
68
/**
@@ -113,15 +73,7 @@ public function typeLinestring(Fluent $column)
113
73
*/
114
74
public function typeMultilinestring (Fluent $ column )
115
75
{
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 ' );
125
77
}
126
78
127
79
/**
@@ -193,15 +145,18 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
193
145
$ dimensions = $ command ->dimensions ?: 2 ;
194
146
$ typmod = $ command ->typmod ? 'true ' : 'false ' ;
195
147
$ srid = $ command ->srid ?: 4326 ;
148
+ $ schema = function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' ;
196
149
197
150
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
205
160
);
206
161
}
207
162
@@ -211,7 +166,30 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
211
166
* @param \Illuminate\Support\Fluent $column
212
167
* @return boolean
213
168
*/
214
- protected function isValid ($ column ) {
169
+ protected function isValid ($ column )
170
+ {
215
171
return in_array (strtoupper ($ column ->geomtype ), PostgisGrammar::$ allowed_geom_types ) && is_int ((int ) $ column ->srid );
216
172
}
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
+
217
195
}
0 commit comments