Răsfoiți Sursa

Preparation for SQLite

https://github.com/marienfressinaud/FreshRSS/issues/100
Alexandre Alapetite 11 ani în urmă
părinte
comite
d6f4141086

+ 1 - 1
app/Controllers/configureController.php

@@ -291,7 +291,7 @@ class FreshRSS_configure_Controller extends Minz_ActionController {
 
 		Minz_View::prependTitle(Minz_Translate::t('archiving_configuration') . ' · ');
 
-		$entryDAO = new FreshRSS_EntryDAO();
+		$entryDAO = FreshRSS_Factory::createEntryDao();
 		$this->view->nb_total = $entryDAO->count();
 		$this->view->size_user = $entryDAO->size();
 

+ 3 - 3
app/Controllers/entryController.php

@@ -43,7 +43,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController {
 		$nextGet = Minz_Request::param ('nextGet', $get); 
 		$idMax = Minz_Request::param ('idMax', 0);
 
-		$entryDAO = new FreshRSS_EntryDAO ();
+		$entryDAO = FreshRSS_Factory::createEntryDao();
 		if ($id == false) {
 			if (!$get) {
 				$entryDAO->markReadEntries ($idMax);
@@ -85,7 +85,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController {
 
 		$id = Minz_Request::param ('id');
 		if ($id) {
-			$entryDAO = new FreshRSS_EntryDAO ();
+			$entryDAO = FreshRSS_Factory::createEntryDao();
 			$entryDAO->markFavorite ($id, (bool)(Minz_Request::param ('is_favorite', true)));
 		}
 	}
@@ -97,7 +97,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController {
 			// La table des entrées a tendance à grossir énormément
 			// Cette action permet d'optimiser cette table permettant de grapiller un peu de place
 			// Cette fonctionnalité n'est à appeler qu'occasionnellement
-			$entryDAO = new FreshRSS_EntryDAO();
+			$entryDAO = FreshRSS_Factory::createEntryDao();
 			$entryDAO->optimizeTable();
 
 			$feedDAO = new FreshRSS_FeedDAO();

+ 2 - 2
app/Controllers/feedController.php

@@ -102,7 +102,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 
 						$is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
 
-						$entryDAO = new FreshRSS_EntryDAO ();
+						$entryDAO = FreshRSS_Factory::createEntryDao();
 						$entries = array_reverse($feed->entries());	//We want chronological order and SimplePie uses reverse order
 
 						// on calcule la date des articles les plus anciens qu'on accepte
@@ -217,7 +217,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 		@set_time_limit(300);
 
 		$feedDAO = new FreshRSS_FeedDAO ();
-		$entryDAO = new FreshRSS_EntryDAO ();
+		$entryDAO = FreshRSS_Factory::createEntryDao();
 
 		Minz_Session::_param('actualize_feeds', false);
 		$id = Minz_Request::param ('id');

+ 1 - 1
app/Controllers/importExportController.php

@@ -12,7 +12,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		require_once(LIB_PATH . '/lib_opml.php');
 
 		$this->catDAO = new FreshRSS_CategoryDAO();
-		$this->entryDAO = new FreshRSS_EntryDAO();
+		$this->entryDAO = FreshRSS_Factory::createEntryDao();
 		$this->feedDAO = new FreshRSS_FeedDAO();
 	}
 

+ 1 - 1
app/Controllers/indexController.php

@@ -45,7 +45,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 		}
 
 		$catDAO = new FreshRSS_CategoryDAO();
-		$entryDAO = new FreshRSS_EntryDAO();
+		$entryDAO = FreshRSS_Factory::createEntryDao();
 
 		$this->view->cat_aside = $catDAO->listCategories ();
 		$this->view->nb_favorites = $entryDAO->countUnreadReadFavorites ();

+ 4 - 2
app/Controllers/usersController.php

@@ -99,7 +99,8 @@ class FreshRSS_users_Controller extends Minz_ActionController {
 
 	public function createAction() {
 		if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
-			require_once(APP_PATH . '/sql.php');
+			$db = Minz_Configuration::dataBase();
+			require_once(APP_PATH . '/SQL/sql.' . $db['type'] . '.php');
 
 			$new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
 			if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
@@ -170,7 +171,8 @@ class FreshRSS_users_Controller extends Minz_ActionController {
 
 	public function deleteAction() {
 		if (Minz_Request::isPost() && Minz_Configuration::isAdmin(Minz_Session::param('currentUser', '_'))) {
-			require_once(APP_PATH . '/sql.php');
+			$db = Minz_Configuration::dataBase();
+			require_once(APP_PATH . '/SQL/sql.' . $db['type'] . '.php');
 
 			$username = Minz_Request::param('username');
 			$ok = ctype_alnum($username);

+ 6 - 6
app/Models/CategoryDAO.php

@@ -12,8 +12,8 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $this->bd->lastInsertId();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error addCategory: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -43,8 +43,8 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error updateCategory: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -58,8 +58,8 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error deleteCategory: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}

+ 1 - 1
app/Models/Entry.php

@@ -154,7 +154,7 @@ class FreshRSS_Entry extends Minz_Model {
 		// Gestion du contenu
 		// On cherche à récupérer les articles en entier... même si le flux ne le propose pas
 		if ($pathEntries) {
-			$entryDAO = new FreshRSS_EntryDAO();
+			$entryDAO = FreshRSS_Factory::createEntryDao();
 			$entry = $entryDAO->searchByGuid($this->feed, $this->guid);
 
 			if($entry) {

+ 58 - 43
app/Models/EntryDAO.php

@@ -1,9 +1,18 @@
 <?php
 
 class FreshRSS_EntryDAO extends Minz_ModelPdo {
+
+	public function isCompressed() {
+		return parent::$sharedDbType !== 'sqlite';
+	}
+
 	public function addEntry ($valuesTmp) {
-		$sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
-		     . 'VALUES(?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
+		$sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, '
+		     . ($this->isCompressed() ? 'content_bin' : 'content')
+		     . ', link, date, is_read, is_favorite, id_feed, tags) '
+		     . 'VALUES(?, ?, ?, ?, '
+		     . ($this->isCompressed() ? 'COMPRESS(?)' : '?')
+		     . ', ?, ?, ?, ?, ?, ?)';
 		$stm = $this->bd->prepare ($sql);
 
 		$values = array (
@@ -23,9 +32,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $this->bd->lastInsertId();
 		} else {
-			$info = $stm->errorInfo();
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
 			if ((int)($info[0] / 1000) !== 23) {	//Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
-				Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
+				Minz_Log::record('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
 				. ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
 			} /*else {
 				Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
@@ -78,14 +87,14 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error markFavorite: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
 
 	public function markRead($ids, $is_read = true) {
-		if (is_array($ids)) {
+		if (is_array($ids)) {	//Many IDs at once
 			if (count($ids) < 6) {	//Speed heuristics
 				$affected = 0;
 				foreach ($ids as $id) {
@@ -102,8 +111,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$values = array_merge($values, $ids);
 			$stm = $this->bd->prepare($sql);
 			if (!($stm && $stm->execute($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack();
 				return false;
 			}
@@ -121,8 +130,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 				     . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
 				$stm = $this->bd->prepare($sql);
 				if (!($stm && $stm->execute())) {
-					$info = $stm->errorInfo();
-					Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+					Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack();
 					return false;
 				}
@@ -134,14 +143,14 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
 				 . 'SET e.is_read = ?,'
 				 . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
-				 . 'WHERE e.id=?';
-			$values = array($is_read ? 1 : 0, $ids);
+				 . 'WHERE e.id=? AND e.is_read<>?';
+			$values = array($is_read ? 1 : 0, $ids, $is_read ? 1 : 0);
 			$stm = $this->bd->prepare($sql);
 			if ($stm && $stm->execute($values)) {
 				return $stm->rowCount();
 			} else {
-				$info = $stm->errorInfo();
-				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markRead: ' . $info[2], Minz_Log::ERROR);
 				return false;
 			}
 		}
@@ -161,8 +170,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			if ($stm && $stm->execute ()) {
 				return $stm->rowCount();
 			} else {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadEntries: ' . $info[2], Minz_Log::ERROR);
 				return false;
 			}
 		} else {
@@ -179,8 +188,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$values = array ($idMax);
 			$stm = $this->bd->prepare ($sql);
 			if (!($stm && $stm->execute ($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadEntries: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack ();
 				return false;
 			}
@@ -198,8 +207,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			     . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
 				$stm = $this->bd->prepare ($sql);
 				if (!($stm && $stm->execute ())) {
-					$info = $stm->errorInfo();
-					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+					Minz_Log::record('SQL error markReadEntries: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack ();
 					return false;
 				}
@@ -220,8 +229,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			if ($stm && $stm->execute ($values)) {
 				return $stm->rowCount();
 			} else {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadCat: ' . $info[2], Minz_Log::ERROR);
 				return false;
 			}
 		} else {
@@ -233,8 +242,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$values = array ($id, $idMax);
 			$stm = $this->bd->prepare ($sql);
 			if (!($stm && $stm->execute ($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadCat: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack ();
 				return false;
 			}
@@ -254,8 +263,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 				$values = array ($id);
 				$stm = $this->bd->prepare ($sql);
 				if (!($stm && $stm->execute ($values))) {
-					$info = $stm->errorInfo();
-					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+					Minz_Log::record('SQL error markReadCat: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack ();
 					return false;
 				}
@@ -278,8 +287,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			if ($stm && $stm->execute($values)) {
 				return $stm->rowCount();
 			} else {
-				$info = $stm->errorInfo();
-				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadCatName: ' . $info[2], Minz_Log::ERROR);
 				return false;
 			}
 		} else {
@@ -293,8 +302,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$values = array($name, $idMax);
 			$stm = $this->bd->prepare($sql);
 			if (!($stm && $stm->execute($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadCatName: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack();
 				return false;
 			}
@@ -315,8 +324,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 				$values = array($name);
 				$stm = $this->bd->prepare($sql);
 				if (!($stm && $stm->execute($values))) {
-					$info = $stm->errorInfo();
-					Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
+					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+					Minz_Log::record('SQL error markReadCatName: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack();
 					return false;
 				}
@@ -337,8 +346,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			if ($stm && $stm->execute ($values)) {
 				return $stm->rowCount();
 			} else {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadFeed: ' . $info[2], Minz_Log::ERROR);
 				return false;
 			}
 		} else {
@@ -350,8 +359,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 			$values = array ($id, $idMax);
 			$stm = $this->bd->prepare ($sql);
 			if (!($stm && $stm->execute ($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::record('SQL error markReadFeed: ' . $info[2], Minz_Log::ERROR);
 				$this->bd->rollBack ();
 				return false;
 			}
@@ -364,8 +373,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 				$values = array ($id);
 				$stm = $this->bd->prepare ($sql);
 				if (!($stm && $stm->execute ($values))) {
-					$info = $stm->errorInfo();
-					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+					$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+					Minz_Log::record('SQL error markReadFeed: ' . $info[2], Minz_Log::ERROR);
 					$this->bd->rollBack ();
 					return false;
 				}
@@ -378,7 +387,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 
 	public function searchByGuid ($feed_id, $id) {
 		// un guid est unique pour un flux donné
-		$sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
+		$sql = 'SELECT id, guid, title, author, '
+		     . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
+		     . ', link, date, is_read, is_favorite, id_feed, tags '
 		     . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
 		$stm = $this->bd->prepare ($sql);
 
@@ -394,7 +405,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 	}
 
 	public function searchById ($id) {
-		$sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
+		$sql = 'SELECT id, guid, title, author, '
+		     . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
+		     . ', link, date, is_read, is_favorite, id_feed, tags '
 		     . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
 		$stm = $this->bd->prepare ($sql);
 
@@ -520,7 +533,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 						$search .= 'AND e1.tags LIKE ? ';
 						$values[] = '%' . $word .'%';
 					} else {
-						$search .= 'AND CONCAT(e1.title, UNCOMPRESS(e1.content_bin)) LIKE ? ';
+						$search .= 'AND CONCAT(e1.title, ' . ($this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ') LIKE ? ';
 						$values[] = '%' . $word .'%';
 					}
 				}
@@ -539,7 +552,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
 	public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
 		list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);
 
-		$sql = 'SELECT e.id, e.guid, e.title, e.author, UNCOMPRESS(e.content_bin) AS content, e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
+		$sql = 'SELECT e.id, e.guid, e.title, e.author, '
+		     . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
+		     . ', e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
 		     . 'FROM `' . $this->prefix . 'entry` e '
 		     . 'INNER JOIN ('
 		     . $sql

+ 42 - 0
app/Models/EntryDAO_SQLite.php

@@ -0,0 +1,42 @@
+<?php
+
+class FreshRSS_EntryDAO_SQLite extends FreshRSS_EntryDAO {
+
+	public function markRead($ids, $is_read = true) {
+		if (is_array($ids)) {	//Many IDs at once
+			if (true) {	//Not supported yet in SQLite, so always call IDs one by one
+				$affected = 0;
+				foreach ($ids as $id) {
+					$affected += $this->markRead($id, $is_read);
+				}
+				return $affected;
+			}
+		} else {
+			$this->bd->beginTransaction();
+			$sql = 'UPDATE `' . $this->prefix . 'entry` e SET e.is_read = ? WHERE e.id=? AND e.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);
+				$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=?)';
+				$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);
+					$this->bd->rollBack ();
+					return false;
+				}
+			}
+			$this->bd->commit();
+			return $affected;
+		}
+	}
+}

+ 13 - 0
app/Models/Factory.php

@@ -0,0 +1,13 @@
+<?php
+
+class FreshRSS_Factory {
+
+	public static function createEntryDao() {
+		$db = Minz_Configuration::dataBase();
+		if ($db['type'] === 'sqlite') {
+			return new FreshRSS_EntryDAO_SQLite();
+		} else {
+			return new FreshRSS_EntryDAO();
+		}
+	}
+}

+ 23 - 23
app/Models/FeedDAO.php

@@ -18,8 +18,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $this->bd->lastInsertId();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error addFeed: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -76,8 +76,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error updateFeed: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -106,8 +106,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error updateLastUpdate: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -130,8 +130,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error changeCategory: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -155,8 +155,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error deleteFeed: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -181,8 +181,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error deleteFeedByCategory: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -301,8 +301,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute()) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error updateCachedValues: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
@@ -313,11 +313,11 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		$values = array($id);
 		$this->bd->beginTransaction ();
 		if (!($stm && $stm->execute ($values))) {
-				$info = $stm->errorInfo();
-				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
-				$this->bd->rollBack ();
-				return false;
-			}
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error truncate: ' . $info[2], Minz_Log::ERROR);
+			$this->bd->rollBack ();
+			return false;
+		}
 		$affected = $stm->rowCount();
 
 		$sql = 'UPDATE `' . $this->prefix . 'feed` f '
@@ -325,8 +325,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		$values = array ($id);
 		$stm = $this->bd->prepare ($sql);
 		if (!($stm && $stm->execute ($values))) {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error truncate: ' . $info[2], Minz_Log::ERROR);
 			$this->bd->rollBack ();
 			return false;
 		}
@@ -350,8 +350,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		if ($stm && $stm->execute ()) {
 			return $stm->rowCount();
 		} else {
-			$info = $stm->errorInfo();
-			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+			Minz_Log::record('SQL error cleanOldEntries: ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}

+ 21 - 10
app/Models/UserDAO.php

@@ -2,33 +2,44 @@
 
 class FreshRSS_UserDAO extends Minz_ModelPdo {
 	public function createUser($username) {
-		require_once(APP_PATH . '/sql.php');
 		$db = Minz_Configuration::dataBase();
+		require_once(APP_PATH . '/SQL/sql.' . $db['type'] . '.php');
+		
+		if (defined('SQL_CREATE_TABLES')) {
+			$sql = sprintf(SQL_CREATE_TABLES, $db['prefix'] . $username . '_', Minz_Translate::t('default_category'));
+			$stm = $c->prepare($sql);
+			$ok = $stm && $stm->execute();
+		} else {
+			global $SQL_CREATE_TABLES;
+			if (is_array($SQL_CREATE_TABLES)) {
+				$ok = true;
+				foreach ($SQL_CREATE_TABLES as $instruction) {
+					$sql = sprintf($instruction, '', Minz_Translate::t('default_category'));
+					$stm = $c->prepare($sql);
+					$ok &= ($stm && $stm->execute());
+				}
+			}
+		}
 
-		$sql = sprintf(SQL_CREATE_TABLES, $db['prefix'] . $username . '_');
-		$stm = $this->bd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
-		$values = array(
-			'catName' => Minz_Translate::t('default_category'),
-		);
-		if ($stm && $stm->execute($values)) {
+		if ($ok) {
 			return true;
 		} else {
-			$info = $stm->errorInfo();
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
 			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}
 	}
 
 	public function deleteUser($username) {
-		require_once(APP_PATH . '/sql.php');
 		$db = Minz_Configuration::dataBase();
+		require_once(APP_PATH . '/SQL/sql.' . $db['type'] . '.php');
 
 		$sql = sprintf(SQL_DROP_TABLES, $db['prefix'] . $username . '_');
 		$stm = $this->bd->prepare($sql);
 		if ($stm && $stm->execute()) {
 			return true;
 		} else {
-			$info = $stm->errorInfo();
+			$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
 			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
 			return false;
 		}

+ 3 - 1
app/sql.php → app/SQL/install.sql.mysql.php

@@ -52,7 +52,9 @@ CREATE TABLE IF NOT EXISTS `%1$sentry` (
 ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
 ENGINE = INNODB;
 
-INSERT IGNORE INTO `%1$scategory` (id, name) VALUES(1, :catName);
+INSERT IGNORE INTO `%1$scategory` (id, name) VALUES(1, "%2$s");
 ');
 
 define('SQL_DROP_TABLES', 'DROP TABLES %1$sentry, %1$sfeed, %1$scategory');
+
+define('SQL_SHOW_TABLES', 'SHOW tables;');

+ 57 - 0
app/SQL/install.sql.sqlite.php

@@ -0,0 +1,57 @@
+<?php
+$SQL_CREATE_TABLES = array(
+'CREATE TABLE IF NOT EXISTS `%1$scategory` (
+	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+	`name` varchar(255) NOT NULL,
+	UNIQUE (`name`)
+);',
+
+'CREATE TABLE IF NOT EXISTS `%1$sfeed` (
+	`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+	`url` varchar(511) NOT NULL,
+	`%1$scategory` SMALLINT DEFAULT 0,
+	`name` varchar(255) NOT NULL,
+	`website` varchar(255),
+	`description` text,
+	`lastUpdate` int(11) DEFAULT 0,
+	`priority` tinyint(2) NOT NULL DEFAULT 10,
+	`pathEntries` varchar(511) DEFAULT NULL,
+	`httpAuth` varchar(511) DEFAULT NULL,
+	`error` boolean DEFAULT 0,
+	`keep_history` MEDIUMINT NOT NULL DEFAULT -2,
+	`cache_nbEntries` int DEFAULT 0,
+	`cache_nbUnreads` int DEFAULT 0,
+	FOREIGN KEY (`%1$scategory`) REFERENCES `%1$scategory`(`id`) ON DELETE SET NULL ON UPDATE CASCADE,
+	UNIQUE (`url`)
+);',
+
+'CREATE INDEX IF NOT EXISTS feed_name_index ON `%1$sfeed`(`name`);',
+'CREATE INDEX IF NOT EXISTS feed_priority_index ON `%1$sfeed`(`priority`);',
+'CREATE INDEX IF NOT EXISTS feed_keep_history_index ON `%1$sfeed`(`keep_history`);',
+
+'CREATE TABLE IF NOT EXISTS `%1$sentry` (
+	`id` bigint NOT NULL,
+	`guid` varchar(760) NOT NULL,
+	`title` varchar(255) NOT NULL,
+	`author` varchar(255),
+	`content` text,
+	`link` varchar(1023) NOT NULL,
+	`date` int(11),
+	`is_read` boolean NOT NULL DEFAULT 0,
+	`is_favorite` boolean NOT NULL DEFAULT 0,
+	`id_feed` SMALLINT,
+	`tags` varchar(1023),
+	PRIMARY KEY (`id`),
+	FOREIGN KEY (`id_feed`) REFERENCES `%1$sfeed`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
+	UNIQUE (`id_feed`,`guid`)
+);',
+
+'CREATE INDEX IF NOT EXISTS entry_is_favorite_index ON `%1$sentry`(`is_favorite`);',
+'CREATE INDEX IF NOT EXISTS entry_is_read_index ON `%1$sentry`(`is_read`);',
+
+'INSERT OR IGNORE INTO `%1$scategory` (id, name) VALUES(1, "%1$s");',
+);
+
+define('SQL_DROP_TABLES', 'DROP TABLES %1$sentry, %1$sfeed, %1$scategory');
+
+define('SQL_SHOW_TABLES', 'SELECT name FROM sqlite_master WHERE type="table"');

+ 50 - 30
lib/Minz/Configuration.php

@@ -297,41 +297,61 @@ class Minz_Configuration {
 		// Base de données
 		if (isset ($ini_array['db'])) {
 			$db = $ini_array['db'];
-			if (empty($db['host'])) {
+			if (empty($db['type'])) {
 				throw new Minz_BadConfigurationException (
-					'host',
+					'type',
 					Minz_Exception::ERROR
 				);
 			}
-			if (empty($db['user'])) {
-				throw new Minz_BadConfigurationException (
-					'user',
-					Minz_Exception::ERROR
-				);
-			}
-			if (!isset ($db['password'])) {
-				throw new Minz_BadConfigurationException (
-					'password',
-					Minz_Exception::ERROR
-				);
-			}
-			if (empty($db['base'])) {
-				throw new Minz_BadConfigurationException (
-					'base',
-					Minz_Exception::ERROR
-				);
-			}
-
-			if (!empty($db['type'])) {
-				self::$db['type'] = $db['type'];
-			}
-			self::$db['host'] = $db['host'];
-			self::$db['user'] = $db['user'];
-			self::$db['password'] = $db['password'];
-			self::$db['base'] = $db['base'];
-			if (isset($db['prefix'])) {
-				self::$db['prefix'] = $db['prefix'];
+			switch ($db['type']) {
+				case 'mysql':
+					if (empty($db['host'])) {
+						throw new Minz_BadConfigurationException (
+							'host',
+							Minz_Exception::ERROR
+						);
+					}
+					if (empty($db['user'])) {
+						throw new Minz_BadConfigurationException (
+							'user',
+							Minz_Exception::ERROR
+						);
+					}
+					if (!isset($db['password'])) {
+						throw new Minz_BadConfigurationException (
+							'password',
+							Minz_Exception::ERROR
+						);
+					}
+					if (empty($db['base'])) {
+						throw new Minz_BadConfigurationException (
+							'base',
+							Minz_Exception::ERROR
+						);
+					}
+					self::$db['host'] = $db['host'];
+					self::$db['user'] = $db['user'];
+					self::$db['password'] = $db['password'];
+					self::$db['base'] = $db['base'];
+					if (isset($db['prefix'])) {
+						self::$db['prefix'] = $db['prefix'];
+					}
+					break;
+				case 'sqlite':
+					self::$db['host'] = '';
+					self::$db['user'] = '';
+					self::$db['password'] = '';
+					self::$db['base'] = '';
+					self::$db['prefix'] = '';
+					break;
+				default:
+					throw new Minz_BadConfigurationException (
+						'type',
+						Minz_Exception::ERROR
+					);
+					break;
 			}
+			self::$db['type'] = $db['type'];
 		}
 	}
 

+ 32 - 20
lib/Minz/ModelPdo.php

@@ -16,6 +16,7 @@ class Minz_ModelPdo {
 	public static $useSharedBd = true;
 	private static $sharedBd = null;
 	private static $sharedPrefix;
+	protected static $sharedDbType;
 
 	/**
 	 * $bd variable représentant la base de données
@@ -24,46 +25,57 @@ class Minz_ModelPdo {
 
 	protected $prefix;
 
+	public function dbType() {
+		return self::$sharedDbType;
+	}
+
 	/**
 	 * Créé la connexion à la base de données à l'aide des variables
 	 * HOST, BASE, USER et PASS définies dans le fichier de configuration
 	 */
-	public function __construct () {
+	public function __construct() {
 		if (self::$useSharedBd && self::$sharedBd != null) {
 			$this->bd = self::$sharedBd;
 			$this->prefix = self::$sharedPrefix;
 			return;
 		}
 
-		$db = Minz_Configuration::dataBase ();
-		$driver_options = null;
+		$db = Minz_Configuration::dataBase();
 
 		try {
 			$type = $db['type'];
-			if($type == 'mysql') {
-				$string = $type
-				        . ':host=' . $db['host']
+			if ($type === 'mysql') {
+				$string = 'mysql:host=' . $db['host']
 				        . ';dbname=' . $db['base']
 				        . ';charset=utf8';
 				$driver_options = array(
-					PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
+					PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
+				);
+				$this->prefix = $db['prefix'] . Minz_Session::param('currentUser', '_') . '_';
+			} elseif ($type === 'sqlite') {
+				$string = 'sqlite:' . DATA_PATH . '/' . Minz_Session::param('currentUser', '_') . '.sqlite';
+				$driver_options = array(
+					//PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+				);
+				$this->prefix = '';
+			} else {
+				throw new Minz_PDOConnectionException(
+					'Invalid database type!',
+					$db['user'], Minz_Exception::ERROR
 				);
-			} elseif($type == 'sqlite') {
-				$string = $type . ':/' . DATA_PATH . $db['base'] . '.sqlite';	//TODO: DEBUG UTF-8 http://www.siteduzero.com/forum/sujet/sqlite-connexion-utf-8-18797
 			}
+			self::$sharedDbType = $type;
+			self::$sharedPrefix = $this->prefix;
 
-			$this->bd = new FreshPDO (
+			$this->bd = new FreshPDO(
 				$string,
 				$db['user'],
 				$db['password'],
 				$driver_options
 			);
 			self::$sharedBd = $this->bd;
-
-			$this->prefix = $db['prefix'] . Minz_Session::param('currentUser', '_') . '_';
-			self::$sharedPrefix = $this->prefix;
 		} catch (Exception $e) {
-			throw new Minz_PDOConnectionException (
+			throw new Minz_PDOConnectionException(
 				$string,
 				$db['user'], Minz_Exception::ERROR
 			);
@@ -81,15 +93,15 @@ class Minz_ModelPdo {
 	}
 
 	public function size($all = false) {
-		$db = Minz_Configuration::dataBase ();
+		$db = Minz_Configuration::dataBase();
 		$sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?';
-		$values = array ($db['base']);
+		$values = array($db['base']);
 		if (!$all) {
 			$sql .= ' AND table_name LIKE ?';
 			$values[] = $this->prefix . '%';
 		}
-		$stm = $this->bd->prepare ($sql);
-		$stm->execute ($values);
+		$stm = $this->bd->prepare($sql);
+		$stm->execute($values);
 		$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
 		return $res[0];
 	}
@@ -107,12 +119,12 @@ class FreshPDO extends PDO {
 		}
 	}
 
-	public function prepare ($statement, $driver_options = array()) {
+	public function prepare($statement, $driver_options = array()) {
 		FreshPDO::check($statement);
 		return parent::prepare($statement, $driver_options);
 	}
 
-	public function exec ($statement) {
+	public function exec($statement) {
 		FreshPDO::check($statement);
 		return parent::exec($statement);
 	}