diff options
| author | 2023-04-28 14:01:11 +0200 | |
|---|---|---|
| committer | 2023-04-28 14:01:11 +0200 | |
| commit | c72914bba2363e436574204b3d6093a6f3cfce89 (patch) | |
| tree | 377008a7393e4d80e4c8659f27dd42c0ccbab382 /app/Models/DatabaseDAOPGSQL.php | |
| parent | 26e2a703125ffe1d0d2746b0e5ea3491b627832c (diff) | |
PHPStan Level 7 for more DAO PDO (#5328)
* PHPStan Level 7 for more DAO PDO
With new function to address common type and check problems
* A bit more
* PHPStan Level 7 for FreshRSS_Entry
Diffstat (limited to 'app/Models/DatabaseDAOPGSQL.php')
| -rw-r--r-- | app/Models/DatabaseDAOPGSQL.php | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/app/Models/DatabaseDAOPGSQL.php b/app/Models/DatabaseDAOPGSQL.php index b1db0f347..29b16e3dc 100644 --- a/app/Models/DatabaseDAOPGSQL.php +++ b/app/Models/DatabaseDAOPGSQL.php @@ -11,56 +11,54 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite { public function tablesAreCorrect(): bool { $db = FreshRSS_Context::$system_conf->db; - $dbowner = $db['user']; - $sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?'; - $stm = $this->pdo->prepare($sql); - $values = array($dbowner); - $stm->execute($values); - $res = $stm->fetchAll(PDO::FETCH_ASSOC); + $sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=:tableowner'; + $res = $this->fetchAssoc($sql, [':tableowner' => $db['user']]); + if ($res == null) { + return false; + } - $tables = array( + $tables = [ $this->pdo->prefix() . 'category' => false, $this->pdo->prefix() . 'feed' => false, $this->pdo->prefix() . 'entry' => false, $this->pdo->prefix() . 'entrytmp' => false, $this->pdo->prefix() . 'tag' => false, $this->pdo->prefix() . 'entrytag' => false, - ); + ]; foreach ($res as $value) { $tables[array_pop($value)] = true; } - return count(array_keys($tables, true, true)) == count($tables); + return count(array_keys($tables, true, true)) === count($tables); } - /** @return array<array<string,string|bool>> */ + /** @return array<array<string,string|int|bool|null>> */ public function getSchema(string $table): array { - $sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?'; - $stm = $this->pdo->prepare($sql); - $stm->execute(array($this->pdo->prefix() . $table)); - return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC)); + $sql = <<<'SQL' +SELECT column_name AS field, data_type AS type, column_default AS default, is_nullable AS null +FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table_name +SQL; + $res = $this->fetchAssoc($sql, [':table_name' => $this->pdo->prefix() . $table]); + return $res == null ? [] : $this->listDaoToSchema($res); } /** - * @param array<string,string> $dao - * @return array<string,string|bool> + * @param array<string,string|int|bool|null> $dao + * @return array{'name':string,'type':string,'notnull':bool,'default':mixed} */ public function daoToSchema(array $dao): array { - return array( - 'name' => $dao['field'], - 'type' => strtolower($dao['type']), + return [ + 'name' => (string)($dao['field']), + 'type' => strtolower((string)($dao['type'])), 'notnull' => (bool)$dao['null'], 'default' => $dao['default'], - ); + ]; } public function size(bool $all = false): int { if ($all) { $db = FreshRSS_Context::$system_conf->db; - $sql = 'SELECT pg_database_size(:base)'; - $stm = $this->pdo->prepare($sql); - $stm->bindParam(':base', $db['base']); - $stm->execute(); + $res = $this->fetchColumn('SELECT pg_database_size(:base)', 0, [':base' => $db['base']]); } else { $sql = <<<SQL SELECT @@ -71,13 +69,9 @@ pg_total_relation_size('`{$this->pdo->prefix()}entrytmp`') + pg_total_relation_size('`{$this->pdo->prefix()}tag`') + pg_total_relation_size('`{$this->pdo->prefix()}entrytag`') SQL; - $stm = $this->pdo->query($sql); - } - if ($stm == false) { - return 0; + $res = $this->fetchColumn($sql, 0); } - $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0); - return intval($res[0]); + return intval($res[0] ?? -1); } |
