-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathPostgresGrammar.php
More file actions
359 lines (324 loc) · 8.4 KB
/
Copy pathPostgresGrammar.php
File metadata and controls
359 lines (324 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace Bosnadev\Database\Schema\Grammars;
use Illuminate\Support\Fluent;
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Bosnadev\Database\Schema\Blueprint;
/**
* Class PostgresGrammar
* @package Bosnadev\Database\Schema\Grammars
*/
class PostgresGrammar extends \Illuminate\Database\Schema\Grammars\PostgresGrammar
{
/**
* Check if the type is uuid, use internal guid
*
* @param string $type
* @return \Doctrine\DBAL\Types\Type
*/
protected function getDoctrineColumnType($type)
{
if($type === 'uuid') {
$type = 'guid';
}
return parent::getDoctrineColumnType($type);
}
/**
* Create the column definition for a character type.
*
* @param Fluent $column
* @return string
*/
protected function typeCharacter(Fluent $column)
{
return "character({$column->length})";
}
/**
* Create the column definition for a hstore type.
*
* @param Fluent $column
* @return string
*/
protected function typeHstore(Fluent $column)
{
return "hstore";
}
/**
* Create the column definition for a uuid type.
*
* @param Fluent $column
* @return string
*/
protected function typeUuid(Fluent $column)
{
return "uuid";
}
/**
* Create the column definition for a jsonb type.
*
* @param Fluent $column
* @return string
*/
protected function typeJsonb(Fluent $column)
{
return "jsonb";
}
/**
* Create the column definition for an IPv4 or IPv6 address.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeIpAddress(Fluent $column)
{
return 'inet';
}
/**
* Create the column definition for a CIDR notation-style netmask.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeNetmask(Fluent $column)
{
return 'cidr';
}
/**
* Create the column definition for a MAC address.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMacAddress(Fluent $column)
{
return 'macaddr';
}
/**
* Create the column definition for a 2D geometric point (x, y), x and y are floating-point numbers.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePoint(Fluent $column)
{
return 'point';
}
/**
* Create the column definition for a line represented as a standard linear equation Ax + By + C = 0.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeLine(Fluent $column)
{
return 'line';
}
/**
* Create the column definition for a line segment represented by two points (x1, y1), (x2, y2).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeLineSegment(Fluent $column)
{
return 'lseg';
}
/**
* Create the column definition for a path represented as a list of points (x1, y1), (x2, y2), ..., (xn, yn).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePath(Fluent $column)
{
return 'path';
}
/**
* Create the column definition for a box represented by opposite corners of the box as points (x1, y1), (x2, y2).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeBox(Fluent $column)
{
return 'box';
}
/**
* Create the column definition for a polygon represented by a list of points (vertices of the polygon).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePolygon(Fluent $column)
{
return 'polygon';
}
/**
* Create the column definition for a circle represented by a center point and a radius <(x, y), r>
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeCircle(Fluent $column)
{
return 'circle';
}
/**
* Create the column definition for storing amounts of currency with a fixed fractional precision.
*
* This will store values in the range of: -92233720368547758.08 to +92233720368547758.07. (92 quadrillion).
* Output is locale-sensitive, see lc_monetary setting of PostgreSQL instance/s.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMoney(Fluent $column)
{
return 'money';
}
/**
* Create the column definition for an int4range type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeInt4range(Fluent $column)
{
return "int4range";
}
/**
* Create the column definition for an int8range type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeInt8range(Fluent $column)
{
return "int8range";
}
/**
* Create the column definition for an numrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeNumrange(Fluent $column)
{
return "numrange";
}
/**
* Create the column definition for an tsrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTsrange(Fluent $column)
{
return "tsrange";
}
/**
* Create the column definition for an tstzrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTstzrange(Fluent $column)
{
return "tstzrange";
}
/**
* Create the column definition for an daterange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeDaterange(Fluent $column)
{
return "daterange";
}
/**
* Create the column definition for a Text Search Vector type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTsvector(Fluent $column)
{
return "tsvector";
}
/**
* Create the column definition for a Case Insensitive Text type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeCitext(Fluent $column)
{
return "citext";
}
/**
* @param mixed $value
* @return mixed|string
*/
protected function getDefaultValue($value)
{
if($this->isUuid($value)) return strval($value);
return parent::getDefaultValue($value);
}
/**
* Check if string matches on of uuid_generate functions
*
* @param $value
* @return int
*/
protected function isUuid($value)
{
return preg_match('/^uuid_generate_v/', $value);
}
/**
* Compile a gin index key command.
*
* @param \Bosnadev\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileGin(Blueprint $blueprint, Fluent $command)
{
$columns = $this->columnize($command->columns);
return sprintf('CREATE INDEX %s ON %s USING GIN(%s)', $command->index, $this->wrapTable($blueprint), $columns);
}
/**
* Compile a gist index key command.
*
* @param \Bosnadev\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileGist(Blueprint $blueprint, Fluent $command)
{
$columns = $this->columnize($command->columns);
return sprintf('CREATE INDEX %s ON %s USING GIST(%s)', $command->index, $this->wrapTable($blueprint), $columns);
}
/**
* Compile create table query.
*
* @param Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileCreate(BaseBlueprint $blueprint, Fluent $command)
{
$sql = parent::compileCreate($blueprint, $command);
if (isset($blueprint->inherits)) {
$sql .= ' INHERITS ("'.$blueprint->inherits.'")';
}
return $sql;
}
}