EntryDAOPGSQL.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
  3. public function sqlHexDecode($x) {
  4. return 'decode(' . $x . ", 'hex')";
  5. }
  6. public function sqlHexEncode($x) {
  7. return 'encode(' . $x . ", 'hex')";
  8. }
  9. protected function autoUpdateDb($errorInfo) {
  10. if (isset($errorInfo[0])) {
  11. if ($errorInfo[0] === '42P01' && stripos($errorInfo[2], 'entrytmp') !== false) { //undefined_table
  12. return $this->createEntryTempTable();
  13. }
  14. }
  15. return false;
  16. }
  17. protected function addColumn($name) {
  18. return false;
  19. }
  20. public function commitNewEntries() {
  21. $sql = 'DO $$
  22. DECLARE
  23. maxrank bigint := (SELECT MAX(id) FROM `' . $this->prefix . 'entrytmp`);
  24. rank bigint := (SELECT maxrank - COUNT(*) FROM `' . $this->prefix . 'entrytmp`);
  25. BEGIN
  26. INSERT INTO `' . $this->prefix . 'entry` (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
  27. (SELECT rank + row_number() OVER(ORDER BY date) AS id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags FROM `' . $this->prefix . 'entrytmp` ORDER BY date);
  28. DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= maxrank;
  29. END $$;';
  30. $hadTransaction = $this->bd->inTransaction();
  31. if (!$hadTransaction) {
  32. $this->bd->beginTransaction();
  33. }
  34. $result = $this->bd->exec($sql) !== false;
  35. if (!$hadTransaction) {
  36. $this->bd->commit();
  37. }
  38. return $result;
  39. }
  40. public function size($all = true) {
  41. $db = FreshRSS_Context::$system_conf->db;
  42. $sql = 'SELECT pg_size_pretty(pg_database_size(?))';
  43. $values = array($db['base']);
  44. $stm = $this->bd->prepare($sql);
  45. $stm->execute($values);
  46. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  47. return $res[0];
  48. }
  49. }