Skip to content

Commit

Permalink
PostgreSQL draft
Browse files Browse the repository at this point in the history
FreshRSS#416
Based on @Damstre work FreshRSS#1071
Not tested
  • Loading branch information
Alkarex committed Aug 2, 2016
1 parent 8c2db93 commit 17b8434
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 58 deletions.
7 changes: 4 additions & 3 deletions app/Models/CategoryDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function addCategory($valuesTmp) {
);

if ($stm && $stm->execute($values)) {
return $this->bd->lastInsertId();
return $this->bd->lastInsertId('"' . parent::prefix . 'category_id_seq"');
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
Minz_Log::error('SQL error addCategory: ' . $info[2]);
Expand Down Expand Up @@ -207,12 +207,13 @@ public static function daoToCategoryPrepopulated($listDAO) {

$previousLine = null;
$feedsDao = array();
$feedDao = FreshRSS_Factory::createFeedDAO();
foreach ($listDAO as $line) {
if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
// End of the current category, we add it to the $list
$cat = new FreshRSS_Category(
$previousLine['c_name'],
FreshRSS_FeedDAO::daoToFeed($feedsDao, $previousLine['c_id'])
$feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
);
$cat->_id($previousLine['c_id']);
$list[$previousLine['c_id']] = $cat;
Expand All @@ -228,7 +229,7 @@ public static function daoToCategoryPrepopulated($listDAO) {
if ($previousLine != null) {
$cat = new FreshRSS_Category(
$previousLine['c_name'],
FreshRSS_FeedDAO::daoToFeed($feedsDao, $previousLine['c_id'])
$feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
);
$cat->_id($previousLine['c_id']);
$list[$previousLine['c_id']] = $cat;
Expand Down
1 change: 1 addition & 0 deletions app/Models/ConfigurationSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ private function _db(&$data, $value) {

switch ($value['type']) {
case 'mysql':
case 'pgsql':
if (empty($value['host']) ||
empty($value['user']) ||
empty($value['base']) ||
Expand Down
2 changes: 1 addition & 1 deletion app/Models/EntryDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {

public function isCompressed() {
return parent::$sharedDbType !== 'sqlite';
return parent::$sharedDbType === 'mysql';
}

public function hasNativeHex() {
Expand Down
92 changes: 92 additions & 0 deletions app/Models/EntryDAOPGSQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAO {

protected function addColumn($name) {
return false;
}

protected function updateCacheUnreads($catId = false, $feedId = false) {
return true; //done via triggers
}

/**
* Mark all the articles in a feed as read.
* There is a fail safe to prevent to mark as read articles that are
* loaded during the mark as read action. Then the cache is updated.
*
* If $idMax equals 0, a deprecated debug message is logged
*
* @param integer $id_feed feed ID
* @param integer $idMax fail safe article ID
* @return integer affected rows
*/
public function markReadFeed($id_feed, $idMax = 0) {
if ($idMax == 0) {
$idMax = time() . '000000';
Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
}
$this->bd->beginTransaction();

$sql = 'UPDATE "' . $this->prefix . 'entry" '
. 'SET is_read=:is_read '
. 'WHERE id_feed=:id_feed AND NOT is_read AND id <= :idmax';
$values = array($id_feed, $idMax);
$stm = $this->bd->prepare($sql);
$stm->bindValue(':is_read', true, PDO::PARAM_BOOL);
$stm->bindValue(':id_feed', $id_feed);
$stm->bindValue(':idmax', $idMax);

if (!($stm && $stm->execute())) {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
Minz_Log::error('SQL error markReadFeed: ' . $info[2]);
$this->bd->rollBack();
return false;
}
$affected = $stm->rowCount();

$this->bd->commit();
return $affected;
}

public function listHashForFeedGuids($id_feed, $guids) {
if (count($guids) < 1) {
return array();
}
$sql = 'SELECT guid, hash AS hexHash FROM "' . $this->prefix . 'entry" WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
$stm = $this->bd->prepare($sql);
$values = array($id_feed);
$values = array_merge($values, $guids);
if ($stm && $stm->execute($values)) {
$result = array();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$result[$row['guid']] = $row['hexHash'];
}
return $result;
} else {
$info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
if ($this->autoAddColumn($info)) {
return $this->listHashForFeedGuids($id_feed, $guids);
}
Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
. ' while querying feed ' . $id_feed);
return false;
}
}

public function optimizeTable() {
return null;
}

public function size($all = true) {
$db = FreshRSS_Context::$system_conf->db;
$sql = 'SELECT pg_size_pretty(pg_database_size(?))';
$values = array($db['base']);
$stm = $this->bd->prepare($sql);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
}

}
42 changes: 26 additions & 16 deletions app/Models/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,47 @@ class FreshRSS_Factory {

public static function createFeedDao($username = null) {
$conf = Minz_Configuration::get('system');
if ($conf->db['type'] === 'sqlite') {
return new FreshRSS_FeedDAOSQLite($username);
} else {
return new FreshRSS_FeedDAO($username);
switch ($conf->db['type']) {
case 'sqlite':
return new FreshRSS_FeedDAOSQLite($username);
default:
return new FreshRSS_FeedDAO($username);
}
}

public static function createEntryDao($username = null) {
$conf = Minz_Configuration::get('system');
if ($conf->db['type'] === 'sqlite') {
return new FreshRSS_EntryDAOSQLite($username);
} else {
return new FreshRSS_EntryDAO($username);
switch ($conf->db['type']) {
case 'sqlite':
return new FreshRSS_EntryDAOSQLite($username);
case 'pgsql':
return new FreshRSS_EntryDAOPGSQL($username);
default:
return new FreshRSS_EntryDAO($username);
}
}

public static function createStatsDAO($username = null) {
$conf = Minz_Configuration::get('system');
if ($conf->db['type'] === 'sqlite') {
return new FreshRSS_StatsDAOSQLite($username);
} else {
return new FreshRSS_StatsDAO($username);
switch ($conf->db['type']) {
case 'sqlite':
return new FreshRSS_StatsDAOSQLite($username);
case 'pgsql':
return new FreshRSS_StatsDAOPGSQL($username);
default:
return new FreshRSS_StatsDAO($username);
}
}

public static function createDatabaseDAO($username = null) {
$conf = Minz_Configuration::get('system');
if ($conf->db['type'] === 'sqlite') {
return new FreshRSS_DatabaseDAOSQLite($username);
} else {
return new FreshRSS_DatabaseDAO($username);
switch ($conf->db['type']) {
case 'sqlite':
return new FreshRSS_DatabaseDAOSQLite($username);
case 'pgsql':
return new FreshRSS_DatabaseDAOPGSQL($username);
default:
return new FreshRSS_DatabaseDAO($username);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/FeedDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function addFeed($valuesTmp) {
);

if ($stm && $stm->execute($values)) {
return $this->bd->lastInsertId();
return $this->bd->lastInsertId('"' . parent::prefix . 'feed_id_seq"');
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
Minz_Log::error('SQL error addFeed: ' . $info[2]);
Expand Down
8 changes: 4 additions & 4 deletions app/Models/StatsDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function calculateEntryRepartitionPerFeed($feed = null, $only_main = fals
$filter .= "AND e.id_feed = {$feed}";
}
$sql = <<<SQL
SELECT COUNT(1) AS `total`,
COUNT(1) - SUM(e.is_read) AS `unread`,
SUM(e.is_read) AS `read`,
SUM(e.is_favorite) AS `favorite`
SELECT COUNT(1) AS total,
COUNT(1) - SUM(e.is_read) AS unread,
SUM(e.is_read) AS read,
SUM(e.is_favorite) AS favorite
FROM {$this->prefix}entry AS e
, {$this->prefix}feed AS f
WHERE e.id_feed = f.id
Expand Down
Loading

0 comments on commit 17b8434

Please sign in to comment.