Просмотр исходного кода

Preparation #3 for SQLite

https://github.com/marienfressinaud/FreshRSS/issues/100
Alexandre Alapetite 11 лет назад
Родитель
Сommit
b34f59e85a

+ 3 - 3
app/Controllers/configureController.php

@@ -14,7 +14,7 @@ class FreshRSS_configure_Controller extends Minz_ActionController {
 	}
 
 	public function categorizeAction () {
-		$feedDAO = new FreshRSS_FeedDAO ();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$catDAO = new FreshRSS_CategoryDAO ();
 		$defaultCategory = $catDAO->getDefault ();
 		$defaultId = $defaultCategory->id ();
@@ -70,7 +70,7 @@ class FreshRSS_configure_Controller extends Minz_ActionController {
 		$catDAO = new FreshRSS_CategoryDAO ();
 		$this->view->categories = $catDAO->listCategories (false);
 
-		$feedDAO = new FreshRSS_FeedDAO ();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$this->view->feeds = $feedDAO->listFeeds ();
 
 		$id = Minz_Request::param ('id');
@@ -336,7 +336,7 @@ class FreshRSS_configure_Controller extends Minz_ActionController {
 					);
 					break;
 				case 'f':
-					$dao = new FreshRSS_FeedDAO();
+					$dao = FreshRSS_Factory::createFeedDao();
 					$feed = $dao->searchById(substr($query['get'], 2));
 					$this->view->query_get[$key] = array(
 						'type' => 'feed',

+ 2 - 2
app/Controllers/entryController.php

@@ -100,7 +100,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController {
 			$entryDAO = FreshRSS_Factory::createEntryDao();
 			$entryDAO->optimizeTable();
 
-			$feedDAO = new FreshRSS_FeedDAO();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			$feedDAO->updateCachedValues();
 
 			invalidateHttpCache();
@@ -124,7 +124,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController {
 		$nb_month_old = max($this->view->conf->old_entries, 1);
 		$date_min = time() - (3600 * 24 * 30 * $nb_month_old);
 
-		$feedDAO = new FreshRSS_FeedDAO();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$feeds = $feedDAO->listFeedsOrderUpdate();
 		$nbTotal = 0;
 

+ 4 - 4
app/Controllers/feedController.php

@@ -31,7 +31,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 			), true);
 		}
 
-		$feedDAO = new FreshRSS_FeedDAO ();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$this->catDAO = new FreshRSS_CategoryDAO ();
 		$this->catDAO->checkDefault ();
 
@@ -201,7 +201,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 	public function truncateAction () {
 		if (Minz_Request::isPost ()) {
 			$id = Minz_Request::param ('id');
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			$n = $feedDAO->truncate($id);
 			$notif = array(
 				'type' => $n === false ? 'bad' : 'good',
@@ -216,7 +216,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 	public function actualizeAction () {
 		@set_time_limit(300);
 
-		$feedDAO = new FreshRSS_FeedDAO ();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$entryDAO = FreshRSS_Factory::createEntryDao();
 
 		Minz_Session::_param('actualize_feeds', false);
@@ -375,7 +375,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 			$type = Minz_Request::param ('type', 'feed');
 			$id = Minz_Request::param ('id');
 
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			if ($type == 'category') {
 				if ($feedDAO->deleteFeedByCategory ($id)) {
 					$notif = array (

+ 1 - 1
app/Controllers/importExportController.php

@@ -13,7 +13,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 
 		$this->catDAO = new FreshRSS_CategoryDAO();
 		$this->entryDAO = FreshRSS_Factory::createEntryDao();
-		$this->feedDAO = new FreshRSS_FeedDAO();
+		$this->feedDAO = FreshRSS_Factory::createFeedDao();
 	}
 
 	public function indexAction() {

+ 2 - 2
app/Controllers/indexController.php

@@ -126,7 +126,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 			// on essaye de récupérer tous les articles
 			if ($state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null)) {
 				Minz_Log::record ('Conflicting information about nbNotRead!', Minz_Log::DEBUG);
-				$feedDAO = new FreshRSS_FeedDAO();
+				$feedDAO = FreshRSS_Factory::createFeedDao();
 				try {
 					$feedDAO->updateCachedValues();
 				} catch (Exception $ex) {
@@ -187,7 +187,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 			case 'f':
 				$feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
 				if (empty($feed)) {
-					$feedDAO = new FreshRSS_FeedDAO();
+					$feedDAO = FreshRSS_Factory::createFeedDao();
 					$feed = $feedDAO->searchById($getId);
 				}
 				if ($feed) {

+ 1 - 1
app/Controllers/javascriptController.php

@@ -7,7 +7,7 @@ class FreshRSS_javascript_Controller extends Minz_ActionController {
 
 	public function actualizeAction () {
 		header('Content-Type: text/javascript; charset=UTF-8');
-		$feedDAO = new FreshRSS_FeedDAO ();
+		$feedDAO = FreshRSS_Factory::createFeedDao();
 		$this->view->feeds = $feedDAO->listFeedsOrderUpdate();
 	}
 

+ 1 - 1
app/Models/Category.php

@@ -44,7 +44,7 @@ class FreshRSS_Category extends Minz_Model {
 	}
 	public function feeds () {
 		if ($this->feeds === null) {
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			$this->feeds = $feedDAO->listByCategory ($this->id ());
 			$this->nbFeed = 0;
 			$this->nbNotRead = 0;

+ 1 - 1
app/Models/Entry.php

@@ -74,7 +74,7 @@ class FreshRSS_Entry extends Minz_Model {
 	}
 	public function feed ($object = false) {
 		if ($object) {
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			return $feedDAO->searchById ($this->feed);
 		} else {
 			return $this->feed;

+ 5 - 5
app/Models/EntryDAOSQLite.php

@@ -13,24 +13,24 @@ class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
 			}
 		} else {
 			$this->bd->beginTransaction();
-			$sql = 'UPDATE `' . $this->prefix . 'entry` e SET e.is_read = ? WHERE e.id=? AND e.is_read<>?';
+			$sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=? WHERE id=? AND is_read<>?';
 			$values = array($is_read ? 1 : 0, $ids, $is_read ? 1 : 0);
 			$stm = $this->bd->prepare($sql);
 			if (!($stm && $stm->execute ($values))) {
 				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
-				Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
+				Minz_Log::record('SQL error markRead 1: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack ();
 				return false;
 			}
 			$affected = $stm->rowCount();
 			if ($affected > 0) {
-				$sql = 'UPDATE `' . $this->prefix . 'feed` f SET f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
-				 . 'WHERE f.id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
+				$sql = 'UPDATE `' . $this->prefix . 'feed` SET cache_nbUnreads=cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
+				 . 'WHERE id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
 				$values = array($ids);
 				$stm = $this->bd->prepare($sql);
 				if (!($stm && $stm->execute ($values))) {
 					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
-					Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
+					Minz_Log::record('SQL error markRead 2: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack ();
 					return false;
 				}

+ 9 - 0
app/Models/Factory.php

@@ -2,6 +2,15 @@
 
 class FreshRSS_Factory {
 
+	public static function createFeedDao() {
+		$db = Minz_Configuration::dataBase();
+		if ($db['type'] === 'sqlite') {
+			return new FreshRSS_FeedDAOSQLite();
+		} else {
+			return new FreshRSS_FeedDAO();
+		}
+	}
+
 	public static function createEntryDao() {
 		$db = Minz_Configuration::dataBase();
 		if ($db['type'] === 'sqlite') {

+ 2 - 2
app/Models/Feed.php

@@ -87,7 +87,7 @@ class FreshRSS_Feed extends Minz_Model {
 	}
 	public function nbEntries () {
 		if ($this->nbEntries < 0) {
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			$this->nbEntries = $feedDAO->countEntries ($this->id ());
 		}
 
@@ -95,7 +95,7 @@ class FreshRSS_Feed extends Minz_Model {
 	}
 	public function nbNotRead () {
 		if ($this->nbNotRead < 0) {
-			$feedDAO = new FreshRSS_FeedDAO ();
+			$feedDAO = FreshRSS_Factory::createFeedDao();
 			$this->nbNotRead = $feedDAO->countNotRead ($this->id ());
 		}
 

+ 8 - 8
app/Models/FeedDAO.php

@@ -84,15 +84,15 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 
 	public function updateLastUpdate ($id, $inError = 0, $updateCache = true) {
 		if ($updateCache) {
-			$sql = 'UPDATE `' . $this->prefix . 'feed` f '	//2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
-			     . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'
-			     . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
+			$sql = 'UPDATE `' . $this->prefix . 'feed` '	//2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
+			     . 'SET cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=feed.id),'
+			     . 'cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=feed.id AND e2.is_read=0),'
 			     . 'lastUpdate=?, error=? '
-			     . 'WHERE f.id=?';
+			     . 'WHERE id=?';
 		} else {
-			$sql = 'UPDATE `' . $this->prefix . 'feed` f '
+			$sql = 'UPDATE `' . $this->prefix . 'feed` '
 			     . 'SET lastUpdate=?, error=? '
-			     . 'WHERE f.id=?';
+			     . 'WHERE id=?';
 		}
 
 		$values = array (
@@ -320,8 +320,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		}
 		$affected = $stm->rowCount();
 
-		$sql = 'UPDATE `' . $this->prefix . 'feed` f '
-			 . 'SET f.cache_nbEntries=0, f.cache_nbUnreads=0 WHERE f.id=?';
+		$sql = 'UPDATE `' . $this->prefix . 'feed` '
+			 . 'SET cache_nbEntries=0, cache_nbUnreads=0 WHERE id=?';
 		$values = array ($id);
 		$stm = $this->bd->prepare ($sql);
 		if (!($stm && $stm->execute ($values))) {

+ 5 - 0
app/Models/FeedDAOSQLite.php

@@ -0,0 +1,5 @@
+<?php
+
+class FreshRSS_FeedDAOSQLite extends FreshRSS_FeedDAO {
+
+}

+ 1 - 1
p/api/greader.php

@@ -327,7 +327,7 @@ function streamContents($path, $include_target, $start_time, $count, $order, $ex
 	logMe("streamContents($path, $include_target, $start_time, $count, $order, $exclude_target, $continuation)\n");
 	header('Content-Type: application/json; charset=UTF-8');
 
-	$feedDAO = new FreshRSS_FeedDAO();
+	$feedDAO = FreshRSS_Factory::createFeedDao();
 	$arrayFeedCategoryNames = $feedDAO->arrayFeedCategoryNames();
 
 	switch ($path) {