|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Umbrellio\Postgres\Schema; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Database\Schema\PostgresBuilder as BasePostgresBuilder; |
|
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
10
|
|
|
|
|
11
|
|
|
class Builder extends BasePostgresBuilder |
|
12
|
|
|
{ |
|
13
|
1 |
|
use Macroable; |
|
14
|
|
|
|
|
15
|
|
|
public $name; |
|
16
|
|
|
|
|
17
|
1 |
|
public function createView(string $view, string $select, $materialize = false): void |
|
18
|
|
|
{ |
|
19
|
1 |
|
$blueprint = $this->createBlueprint($view); |
|
20
|
1 |
|
$blueprint->createView($view, $select, $materialize); |
|
21
|
1 |
|
$this->build($blueprint); |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function dropView(string $view): void |
|
25
|
|
|
{ |
|
26
|
1 |
|
$blueprint = $this->createBlueprint($view); |
|
27
|
1 |
|
$blueprint->dropView($view); |
|
28
|
1 |
|
$this->build($blueprint); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
public function hasView(string $view): bool |
|
32
|
|
|
{ |
|
33
|
2 |
|
return count($this->connection->selectFromWriteConnection($this->grammar->compileViewExists(), [ |
|
34
|
2 |
|
$this->connection->getConfig()['schema'], |
|
35
|
2 |
|
$this->connection->getTablePrefix() . $view, |
|
36
|
2 |
|
])) > 0; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function getForeignKeys(string $tableName): array |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this->connection->selectFromWriteConnection( |
|
42
|
1 |
|
$this->grammar->compileForeignKeysListing($tableName) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function getViewDefinition($view): string |
|
47
|
|
|
{ |
|
48
|
2 |
|
$results = $this->connection->selectFromWriteConnection($this->grammar->compileViewDefinition(), [ |
|
49
|
2 |
|
$this->connection->getConfig()['schema'], |
|
50
|
2 |
|
$this->connection->getTablePrefix() . $view, |
|
51
|
|
|
]); |
|
52
|
2 |
|
return count($results) > 0 ? $results[0]->view_definition : ''; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $table |
|
57
|
|
|
* @return Blueprint|\Illuminate\Database\Schema\Blueprint |
|
58
|
|
|
*/ |
|
59
|
34 |
|
protected function createBlueprint($table, Closure $callback = null) |
|
60
|
|
|
{ |
|
61
|
34 |
|
return new Blueprint($table, $callback); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|