Repository navigation
Expand file tree
/
Copy pathPostgresBuilder.php
More file actions
293 lines (239 loc) · 7.24 KB
/
Copy pathPostgresBuilder.php
File metadata and controls
293 lines (239 loc) · 7.24 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\PgSQL\Schema;
use Hyperf\Database\PgSQL\Query\Processors\PostgresProcessor;
use Hyperf\Database\Schema\Builder;
use function Hyperf\Collection\head;
class PostgresBuilder extends Builder
{
/**
* Create a database in the schema.
*
* @param string $name
*/
public function createDatabase($name): bool
{
return $this->connection->statement(
$this->grammar->compileCreateDatabase($name, $this->connection)
);
}
/**
* Drop a database from the schema if the database exists.
*
* @param string $name
*/
public function dropDatabaseIfExists($name): bool
{
return $this->connection->statement(
$this->grammar->compileDropDatabaseIfExists($name)
);
}
/**
* Determine if the given table exists.
*
* @param string $table
*/
public function hasTable($table): bool
{
[$schema, $table] = $this->parseSchemaAndTable($table);
$table = $this->connection->getTablePrefix() . $table;
return count($this->connection->select(
$this->grammar->compileTableExists(),
[$schema, $table]
)) > 0;
}
/**
* Get the tables that belong to the database.
*/
public function getTables(): array
{
return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
);
}
/**
* Drop all tables from the database.
*/
public function dropAllTables(): void
{
$tables = [];
$excludedTables = $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'];
foreach ($this->getAllTables() as $row) {
$row = (array) $row;
$table = reset($row);
if (! in_array($table, $excludedTables)) {
$tables[] = $table;
}
}
if (empty($tables)) {
return;
}
$this->connection->statement(
$this->grammar->compileDropAllTables($tables)
);
}
/**
* Drop all views from the database.
*/
public function dropAllViews(): void
{
$views = [];
foreach ($this->getAllViews() as $row) {
$row = (array) $row;
$views[] = reset($row);
}
if (empty($views)) {
return;
}
$this->connection->statement(
$this->grammar->compileDropAllViews($views)
);
}
/**
* Drop all types from the database.
*/
public function dropAllTypes()
{
$types = [];
foreach ($this->getAllTypes() as $row) {
$row = (array) $row;
$types[] = reset($row);
}
if (empty($types)) {
return;
}
$this->connection->statement(
$this->grammar->compileDropAllTypes($types)
);
}
/**
* Get all of the table names for the database.
*/
public function getAllTables(): array
{
return $this->connection->select(
$this->grammar->compileGetAllTables($this->getSchemas())
);
}
/**
* Get all of the view names for the database.
*/
public function getAllViews(): array
{
return $this->connection->select(
$this->grammar->compileGetAllViews($this->getSchemas())
);
}
/**
* Determine if the given view exists.
*/
public function hasView(string $view): bool
{
[$schema, $view] = $this->parseSchemaAndTable($view);
$view = $this->connection->getTablePrefix() . $view;
foreach ($this->getViews() as $value) {
if (strtolower($view) === strtolower($value['name'])
&& strtolower($schema) === strtolower($value['schema'])) {
return true;
}
}
return false;
}
/**
* Get all of the type names for the database.
*
* @return array
*/
public function getAllTypes()
{
return $this->connection->select(
$this->grammar->compileGetAllTypes()
);
}
/**
* Get the column listing for a given table.
*
* @param string $table
*/
public function getColumnListing($table): array
{
[$schema, $table] = $this->parseSchemaAndTable($table);
$databaseName = $this->connection->getDatabaseName();
$table = $this->connection->getTablePrefix() . $table;
$results = $this->connection->select(
$this->grammar->compileColumnListing(),
[$databaseName, $schema, $table]
);
return $this->connection->getPostProcessor()->processColumnListing($results);
}
/**
* Get the foreign keys for a given table.
*/
public function getForeignKeys(string $table): array
{
[$schema, $table] = $this->parseSchemaAndTable($table);
$table = $this->connection->getTablePrefix() . $table;
return $this->connection->getPostProcessor()->processForeignKeys(
$this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($schema, $table))
);
}
/**
* Get the column type listing for a given table.
*/
public function getColumnTypeListing(string $table, ?string $database = null): array
{
[$schema, $table] = $this->parseSchemaAndTable($table);
$table = $this->connection->getTablePrefix() . $table;
$results = $this->connection->select(
$this->grammar->compileColumnListing(),
[$database ?? $this->connection->getDatabaseName(), $schema, $table]
);
/** @var PostgresProcessor $processor */
$processor = $this->connection->getPostProcessor();
return $processor->processListing($results);
}
/**
* Get the indexes for a given table.
*/
public function getIndexes(string $table): array
{
[$schema, $table] = $this->parseSchemaAndTable($table);
$table = $this->connection->getTablePrefix() . $table;
return $this->connection->getPostProcessor()->processIndexes(
$this->connection->selectFromWriteConnection($this->grammar->compileIndexes($schema, $table))
);
}
/**
* Get the schemas for the connection.
*/
protected function getSchemas(): array
{
$schemas = (array) $this->connection->getConfig('schema');
return empty($schemas) ? ['public'] : $schemas;
}
/**
* Parse the table name and extract the schema and table.
*
* @param string $table
* @return array
*/
protected function parseSchemaAndTable($table)
{
$table = explode('.', $table);
if (is_array($schema = $this->connection->getConfig('schema'))) {
if (in_array($table[0], $schema)) {
return [array_shift($table), implode('.', $table)];
}
$schema = head($schema);
}
return [$schema ?: 'public', implode('.', $table)];
}
}