]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/store/sqlite.class.php
a15bc095b4a2e8c248e9baa4b563017190f45f9b
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
11 class Sqlite
extends Store
{
13 public static $db_path = 'sqlite:./db/poche.sqlite';
16 function __construct() {
17 parent
::__construct();
19 $this->handle
= new PDO(self
::$db_path);
20 $this->handle
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
23 private function getHandle() {
27 public function isInstalled() {
28 $sql = "SELECT name FROM sqlite_sequence WHERE name=?";
29 $query = $this->executeQuery($sql, array('config'));
30 $hasConfig = $query->fetchAll();
32 if (count($hasConfig) == 0)
35 if (!$this->getLogin() || !$this->getPassword())
41 public function install($login, $password) {
42 $this->getHandle()->exec('CREATE TABLE IF NOT EXISTS "config" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "name" VARCHAR UNIQUE, "value" BLOB)');
44 $this->handle
->exec('CREATE TABLE IF NOT EXISTS "entries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "title" VARCHAR, "url" VARCHAR UNIQUE , "is_read" INTEGER DEFAULT 0, "is_fav" INTEGER DEFAULT 0, "content" BLOB)');
46 if (!$this->getLogin()) {
47 $sql_login = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
48 $params_login = array('login', $login);
49 $query = $this->executeQuery($sql_login, $params_login);
52 if (!$this->getPassword()) {
53 $sql_pass = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
54 $params_pass = array('password', $password);
55 $query = $this->executeQuery($sql_pass, $params_pass);
61 public function getLogin() {
62 $sql = "SELECT value FROM config WHERE name=?";
63 $query = $this->executeQuery($sql, array('login'));
64 $login = $query->fetchAll();
66 return isset($login[0]['value']) ? $login[0]['value'] : FALSE;
69 public function getPassword() {
70 $sql = "SELECT value FROM config WHERE name=?";
71 $query = $this->executeQuery($sql, array('password'));
72 $pass = $query->fetchAll();
74 return isset($pass[0]['value']) ? $pass[0]['value'] : FALSE;
77 public function updatePassword($password)
79 $sql_update = "UPDATE config SET value=? WHERE name='password'";
80 $params_update = array($password);
81 $query = $this->executeQuery($sql_update, $params_update);
84 private function executeQuery($sql, $params) {
87 $query = $this->getHandle()->prepare($sql);
88 $query->execute($params);
93 Tools
::logm('execute query error : '.$e->getMessage());
97 public function retrieveAll() {
98 $sql = "SELECT * FROM entries ORDER BY id";
99 $query = $this->executeQuery($sql, array());
100 $entries = $query->fetchAll();
105 public function retrieveOneById($id) {
106 parent
::__construct();
109 $sql = "SELECT * FROM entries WHERE id=?";
110 $params = array(intval($id));
111 $query = $this->executeQuery($sql, $params);
112 $entry = $query->fetchAll();
117 public function getEntriesByView($view) {
118 parent
::__construct();
120 switch ($_SESSION['sort'])
123 $order = 'ORDER BY id';
126 $order = 'ORDER BY id DESC';
129 $order = 'ORDER BY lower(title)';
132 $order = 'ORDER BY lower(title) DESC';
135 $order = 'ORDER BY id';
142 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
146 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
150 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
155 $query = $this->executeQuery($sql, $params);
156 $entries = $query->fetchAll();
161 public function add($url, $title, $content) {
162 parent
::__construct();
163 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
164 $params_action = array($url, $title, $content);
165 $query = $this->executeQuery($sql_action, $params_action);
169 public function deleteById($id) {
170 parent
::__construct();
171 $sql_action = "DELETE FROM entries WHERE id=?";
172 $params_action = array($id);
173 $query = $this->executeQuery($sql_action, $params_action);
177 public function favoriteById($id) {
178 parent
::__construct();
179 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
180 $params_action = array($id);
181 $query = $this->executeQuery($sql_action, $params_action);
184 public function archiveById($id) {
185 parent
::__construct();
186 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?";
187 $params_action = array($id);
188 $query = $this->executeQuery($sql_action, $params_action);
191 public function getLastId() {
192 parent
::__construct();
193 return $this->getHandle()->lastInsertId();
196 public function updateContentById($id) {
197 parent
::__construct();
198 $sql_update = "UPDATE entries SET content=? WHERE id=?";
199 $params_update = array($content, $id);
200 $query = $this->executeQuery($sql_update, $params_update);