diff --git a/src/Sql/SqlPgsql.php b/src/Sql/SqlPgsql.php index 6355ca77ce..e17d9d67d0 100644 --- a/src/Sql/SqlPgsql.php +++ b/src/Sql/SqlPgsql.php @@ -6,8 +6,6 @@ use Drush\Drush; -define('PSQL_SHOW_TABLES', "SELECT tablename FROM pg_tables WHERE schemaname='public';"); - class SqlPgsql extends SqlBase { public string $queryExtra = "--no-align --field-separator=\"\t\" --pset tuples_only=on"; @@ -130,14 +128,14 @@ public function dbExists(): bool public function queryFormat($query) { if (strtolower($query) === 'show tables;') { - return PSQL_SHOW_TABLES; + return $this->showTablesSql(); } return $query; } public function listTables(): array { - $this->alwaysQuery(PSQL_SHOW_TABLES); + $this->alwaysQuery($this->showTablesSql()); $tables = explode(PHP_EOL, trim($this->getProcess()->getOutput())); return array_filter($tables); } @@ -199,4 +197,22 @@ public function getPasswordFile(): ?string { return $this->password_file; } + + private function schemaName(): string + { + $dbSpec = $this->getDbSpec(); + + return $dbSpec['schema'] + ?? $dbSpec['database'] + ?? 'public'; + } + + private function showTablesSql(): string + { + $schema = str_replace("'", "''", $this->schemaName()); + + return "SELECT quote_ident(schemaname) || '.' || quote_ident(tablename) + FROM pg_tables + WHERE schemaname='{$schema}';"; + } }