Grammars
3 weeks ago
Blueprint.php
3 weeks ago
Builder.php
3 weeks ago
ColumnDefinition.php
3 weeks ago
ForeignIdColumnDefinition.php
3 weeks ago
ForeignKeyDefinition.php
3 weeks ago
IndexDefinition.php
3 weeks ago
MySqlBuilder.php
3 weeks ago
MySqlSchemaState.php
3 weeks ago
PostgresBuilder.php
3 weeks ago
PostgresSchemaState.php
3 weeks ago
SQLiteBuilder.php
3 weeks ago
SchemaState.php
3 weeks ago
SqlServerBuilder.php
3 weeks ago
SqliteSchemaState.php
3 weeks ago
PostgresBuilder.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Concerns\ParsesSearchPath; |
| 6 | /** @internal */ |
| 7 | class PostgresBuilder extends Builder |
| 8 | { |
| 9 | use ParsesSearchPath { |
| 10 | parseSearchPath as baseParseSearchPath; |
| 11 | } |
| 12 | /** |
| 13 | * Create a database in the schema. |
| 14 | * |
| 15 | * @param string $name |
| 16 | * @return bool |
| 17 | */ |
| 18 | public function createDatabase($name) |
| 19 | { |
| 20 | return $this->connection->statement($this->grammar->compileCreateDatabase($name, $this->connection)); |
| 21 | } |
| 22 | /** |
| 23 | * Drop a database from the schema if the database exists. |
| 24 | * |
| 25 | * @param string $name |
| 26 | * @return bool |
| 27 | */ |
| 28 | public function dropDatabaseIfExists($name) |
| 29 | { |
| 30 | return $this->connection->statement($this->grammar->compileDropDatabaseIfExists($name)); |
| 31 | } |
| 32 | /** |
| 33 | * Determine if the given table exists. |
| 34 | * |
| 35 | * @param string $table |
| 36 | * @return bool |
| 37 | */ |
| 38 | public function hasTable($table) |
| 39 | { |
| 40 | [$database, $schema, $table] = $this->parseSchemaAndTable($table); |
| 41 | $table = $this->connection->getTablePrefix() . $table; |
| 42 | return \count($this->connection->selectFromWriteConnection($this->grammar->compileTableExists(), [$database, $schema, $table])) > 0; |
| 43 | } |
| 44 | /** |
| 45 | * Drop all tables from the database. |
| 46 | * |
| 47 | * @return void |
| 48 | */ |
| 49 | public function dropAllTables() |
| 50 | { |
| 51 | $tables = []; |
| 52 | $excludedTables = $this->grammar->escapeNames($this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys']); |
| 53 | foreach ($this->getAllTables() as $row) { |
| 54 | $row = (array) $row; |
| 55 | if (empty(\array_intersect($this->grammar->escapeNames($row), $excludedTables))) { |
| 56 | $tables[] = $row['qualifiedname'] ?? \reset($row); |
| 57 | } |
| 58 | } |
| 59 | if (empty($tables)) { |
| 60 | return; |
| 61 | } |
| 62 | $this->connection->statement($this->grammar->compileDropAllTables($tables)); |
| 63 | } |
| 64 | /** |
| 65 | * Drop all views from the database. |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function dropAllViews() |
| 70 | { |
| 71 | $views = []; |
| 72 | foreach ($this->getAllViews() as $row) { |
| 73 | $row = (array) $row; |
| 74 | $views[] = $row['qualifiedname'] ?? \reset($row); |
| 75 | } |
| 76 | if (empty($views)) { |
| 77 | return; |
| 78 | } |
| 79 | $this->connection->statement($this->grammar->compileDropAllViews($views)); |
| 80 | } |
| 81 | /** |
| 82 | * Drop all types from the database. |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function dropAllTypes() |
| 87 | { |
| 88 | $types = []; |
| 89 | foreach ($this->getAllTypes() as $row) { |
| 90 | $row = (array) $row; |
| 91 | $types[] = \reset($row); |
| 92 | } |
| 93 | if (empty($types)) { |
| 94 | return; |
| 95 | } |
| 96 | $this->connection->statement($this->grammar->compileDropAllTypes($types)); |
| 97 | } |
| 98 | /** |
| 99 | * Get all of the table names for the database. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | public function getAllTables() |
| 104 | { |
| 105 | return $this->connection->select($this->grammar->compileGetAllTables($this->parseSearchPath($this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema')))); |
| 106 | } |
| 107 | /** |
| 108 | * Get all of the view names for the database. |
| 109 | * |
| 110 | * @return array |
| 111 | */ |
| 112 | public function getAllViews() |
| 113 | { |
| 114 | return $this->connection->select($this->grammar->compileGetAllViews($this->parseSearchPath($this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema')))); |
| 115 | } |
| 116 | /** |
| 117 | * Get all of the type names for the database. |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | public function getAllTypes() |
| 122 | { |
| 123 | return $this->connection->select($this->grammar->compileGetAllTypes()); |
| 124 | } |
| 125 | /** |
| 126 | * Get the column listing for a given table. |
| 127 | * |
| 128 | * @param string $table |
| 129 | * @return array |
| 130 | */ |
| 131 | public function getColumnListing($table) |
| 132 | { |
| 133 | [$database, $schema, $table] = $this->parseSchemaAndTable($table); |
| 134 | $table = $this->connection->getTablePrefix() . $table; |
| 135 | $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(), [$database, $schema, $table]); |
| 136 | return $this->connection->getPostProcessor()->processColumnListing($results); |
| 137 | } |
| 138 | /** |
| 139 | * Parse the database object reference and extract the database, schema, and table. |
| 140 | * |
| 141 | * @param string $reference |
| 142 | * @return array |
| 143 | */ |
| 144 | protected function parseSchemaAndTable($reference) |
| 145 | { |
| 146 | $searchPath = $this->parseSearchPath(($this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema')) ?: 'public'); |
| 147 | $parts = \explode('.', $reference); |
| 148 | $database = $this->connection->getConfig('database'); |
| 149 | // If the reference contains a database name, we will use that instead of the |
| 150 | // default database name for the connection. This allows the database name |
| 151 | // to be specified in the query instead of at the full connection level. |
| 152 | if (\count($parts) === 3) { |
| 153 | $database = $parts[0]; |
| 154 | \array_shift($parts); |
| 155 | } |
| 156 | // We will use the default schema unless the schema has been specified in the |
| 157 | // query. If the schema has been specified in the query then we can use it |
| 158 | // instead of a default schema configured in the connection search path. |
| 159 | $schema = $searchPath[0]; |
| 160 | if (\count($parts) === 2) { |
| 161 | $schema = $parts[0]; |
| 162 | \array_shift($parts); |
| 163 | } |
| 164 | return [$database, $schema, $parts[0]]; |
| 165 | } |
| 166 | /** |
| 167 | * Parse the "search_path" configuration value into an array. |
| 168 | * |
| 169 | * @param string|array|null $searchPath |
| 170 | * @return array |
| 171 | */ |
| 172 | protected function parseSearchPath($searchPath) |
| 173 | { |
| 174 | return \array_map(function ($schema) { |
| 175 | return $schema === '$user' ? $this->connection->getConfig('username') : $schema; |
| 176 | }, $this->baseParseSearchPath($searchPath)); |
| 177 | } |
| 178 | } |
| 179 |