summaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-11-06 17:36:54 +0100
committerBernhard Posselt <dev@bernhard-posselt.com>2014-11-06 17:36:54 +0100
commit23dcd8e5bff6a2e366b085ac373638206652f055 (patch)
tree0bafb8ac92d7bfa3948369d4c0772758fda3c738 /db
parent31975bfab7d8f94ce3d2f438caa50ac21547d655 (diff)
fix mysql
Diffstat (limited to 'db')
-rw-r--r--db/mapperfactory.php40
-rw-r--r--db/mysql/itemmapper.php66
2 files changed, 106 insertions, 0 deletions
diff --git a/db/mapperfactory.php b/db/mapperfactory.php
new file mode 100644
index 000000000..50dfa1d9b
--- /dev/null
+++ b/db/mapperfactory.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Alessandro Cosentino 2012
+ * @copyright Bernhard Posselt 2012, 2014
+ */
+
+namespace OCA\News\Db;
+
+use \OCP\IDb;
+use \OCA\News\Db\Mysql\ItemMapper as MysqlItemMapper;
+
+class MapperFactory {
+
+ private $dbType;
+ private $db;
+
+ public function __construct($dbType, IDb $db) {
+ $this->dbType = $dbType;
+ $this->db = $db;
+ }
+
+
+ public function getItemMapper() {
+ switch($this->dbType) {
+ case 'mysql':
+ return new MysqlItemMapper($this->db);
+ default:
+ return new ItemMapper($this->db);
+ }
+ }
+
+
+}
diff --git a/db/mysql/itemmapper.php b/db/mysql/itemmapper.php
new file mode 100644
index 000000000..c58036350
--- /dev/null
+++ b/db/mysql/itemmapper.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Alessandro Cosentino 2012
+ * @copyright Bernhard Posselt 2012, 2014
+ */
+
+namespace OCA\News\Db\Mysql;
+
+use \OCP\IDb;
+
+use \OCA\News\Db\StatusFlag;
+
+
+class ItemMapper extends \OCA\News\Db\ItemMapper {
+
+ public function __construct(IDb $db){
+ parent::__construct($db);
+ }
+
+
+ /**
+ * Delete all items for feeds that have over $threshold unread and not
+ * starred items
+ */
+ public function deleteReadOlderThanThreshold($threshold){
+ $status = StatusFlag::STARRED | StatusFlag::UNREAD;
+ $sql = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`, ' .
+ '`feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' .
+ 'FROM `*PREFIX*news_items` `items` ' .
+ 'JOIN `*PREFIX*news_feeds` `feeds` ' .
+ 'ON `feeds`.`id` = `items`.`feed_id` ' .
+ 'AND NOT ((`items`.`status` & ?) > 0) ' .
+ 'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' .
+ 'HAVING COUNT(*) > ?';
+ $params = [$status, $threshold];
+ $result = $this->execute($sql, $params);
+
+ while($row = $result->fetch()) {
+
+ $size = (int) $row['size'];
+ $limit = $size - $threshold;
+
+ if($limit > 0) {
+ $params = [$status, $row['feed_id'], $limit];
+
+ $sql = 'DELETE FROM `*PREFIX*news_items` ' .
+ 'WHERE NOT ((`status` & ?) > 0) ' .
+ 'AND `feed_id` = ? ' .
+ 'ORDER BY `id` ASC ' .
+ 'LIMIT ?';
+
+ $this->execute($sql, $params);
+ }
+ }
+
+ }
+
+
+}