Fix deprecated OCP\DB calls for Nextcloud 14 compatibility (#65)

* Fix deprecated OCP\DB calls
master
e-alfred 2018-09-28 13:51:45 +02:00 committed by Loïc Blot
parent c5c07d8dae
commit c26cc3f7ba
2 changed files with 74 additions and 83 deletions

View File

@ -7,6 +7,7 @@
* *
* @author Loic Blot <loic.blot@unix-experience.fr> * @author Loic Blot <loic.blot@unix-experience.fr>
* @copyright Loic Blot 2015 * @copyright Loic Blot 2015
* @copyright e-alfred 2018
*/ */
namespace OCA\Weather\Db; namespace OCA\Weather\Db;
@ -16,62 +17,57 @@ use \OCP\IDBConnection;
use \OCP\AppFramework\Db\Mapper; use \OCP\AppFramework\Db\Mapper;
class CityMapper extends Mapper { class CityMapper extends Mapper {
public function __construct (IDBConnection $db) { public function __construct (IDBConnection $db) {
parent::__construct($db, 'weather_city'); parent::__construct($db, 'weather_city');
} }
public function load ($id) { public function load ($id) {
$sql = 'SELECT id, name, user_id FROM ' . $sql = "SELECT id, name, user_id FROM *PREFIX*weather_city WHERE `id` ='" . $id . "'";
'*PREFIX*weather_city WHERE id = ?'; $result = $this->db->executequery($sql);
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($id));
if ($row = $result->fetchRow()) { if ($row = $result->fetch()) {
return $row; return $row;
} }
return null; return null;
} }
public function count() { public function count() {
$sql = 'SELECT count(*) AS ct FROM *PREFIX*weather_city WHERE user_id = ?'; $sql = "SELECT count(*) AS ct FROM *PREFIX*weather_city WHERE `user_id` ='" . $userId . "'";
$query = \OCP\DB::prepare($sql); $result = $this->db->executequery($sql);
$result = $query->execute(array($userId)); if ($row = $result->fetch()) {
if ($row = $result->fetchRow()) { return $row['ct'];
return $row['ct']; }
} return 0;
return 0; }
}
public function exists ($id) { public function exists ($id) {
return ($this->load($id)); return ($this->load($id));
} }
public function getAll ($userId) { public function getAll ($userId) {
$sql = 'SELECT id, name FROM *PREFIX*weather_city WHERE user_id = ?'; $sql = "SELECT id, name FROM *PREFIX*weather_city WHERE `user_id` = '" . $userId . "'";
$query = \OCP\DB::prepare($sql); $result = $this->db->executequery($sql);
$result = $query->execute(array($userId));
$cities = array(); $cities = array();
while ($row = $result->fetchRow()) { while ($row = $result->fetch()) {
$cities[] = $row; $cities[] = $row;
} }
return $cities; return $cities;
} }
public function create ($userId, $name) { public function create ($userId, $name) {
$this->db->beginTransaction(); $this->db->beginTransaction();
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city(user_id, name) VALUES (?,?)'); $sql = "INSERT INTO *PREFIX*weather_city(`user_id`, `name`) VALUES ('" . $userId . "','" . $name . "')";
$query->execute(array($userId, $name)); $this->db->executequery($sql);
$this->db->commit(); $this->db->commit();
$sql = 'SELECT max(id) as maxid FROM *PREFIX*weather_city WHERE user_id = ? and name = ?'; $sql = "SELECT max(id) as maxid FROM *PREFIX*weather_city WHERE `user_id` = '" . $userId . "' and `name` = '" . $name . "'";
$query = \OCP\DB::prepare($sql); $result = $this->db->executequery($sql);
$result = $query->execute(array($userId, $name));
if ($row = $result->fetchRow()) { if ($row = $result->fetch()) {
return $row['maxid']; return $row['maxid'];
} }
return null; return null;
} }
}; };
?> ?>

View File

@ -7,6 +7,7 @@
* *
* @author Loic Blot <loic.blot@unix-experience.fr> * @author Loic Blot <loic.blot@unix-experience.fr>
* @copyright Loic Blot 2015 * @copyright Loic Blot 2015
* @copyright e-alfred 2018
*/ */
namespace OCA\Weather\Db; namespace OCA\Weather\Db;
@ -16,49 +17,43 @@ use \OCP\IDBConnection;
use \OCP\AppFramework\Db\Mapper; use \OCP\AppFramework\Db\Mapper;
class SettingsMapper extends Mapper { class SettingsMapper extends Mapper {
public function __construct (IDBConnection $db) { public function __construct (IDBConnection $db) {
parent::__construct($db, 'weather_city'); parent::__construct($db, 'weather_config');
} }
public function setHome ($userId, $cityId) { public function setHome ($userId, $cityId) {
$this->setSetting("home", $userId, $cityId); $this->setSetting("home", $userId, $cityId);
} }
public function getHome ($userId) { public function getHome ($userId) {
return $this->getSetting($userId, "home"); return $this->getSetting($userId, "home");
} }
public function setMetric ($userId, $metric) { public function setMetric ($userId, $metric) {
$this->setSetting("metric", $userId, $metric); $this->setSetting("metric", $userId, $metric);
} }
public function getMetric ($userId) { public function getMetric ($userId) {
return $this->getSetting($userId, "metric"); return $this->getSetting($userId, "metric");
} }
public function setSetting ($settingName, $userId, $settingValue) { public function setSetting ($settingName, $userId, $settingValue) {
$this->db->beginTransaction(); $sql = "DELETE FROM *PREFIX*weather_config WHERE `user` = '" . $userId . "' and `key` = '" . $settingName . "'";
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_config ' . $this->db->executequery($sql);
'WHERE `user` = ? and `key` = ?');
$query->execute(array($userId, $settingName));
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_config ' . $sql = "INSERT INTO *PREFIX*weather_config (`user`,`key`,`value`) VALUES ('" . $userId . "','" . $settingName . "','" . $settingValue . "')";
'(`user`,`key`,`value`) VALUES (?,?,?)'); $this->db->executequery($sql);
$query->execute(array($userId, $settingName, $settingValue)); }
$this->db->commit();
}
public function getSetting ($userId, $settingName) { public function getSetting ($userId, $settingName) {
$sql = 'SELECT value FROM ' . $sql = "SELECT value FROM *PREFIX*weather_config WHERE `user` ='" . $userId . "' and `key` ='" . $settingName . "'";
'*PREFIX*weather_config WHERE `user` = ? and `key` = ?'; $result = $this->db->executeQuery($sql);
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($userId, $settingName));
if ($row = $result->fetchRow()) { if ($row = $result->fetch()) {
return $row["value"]; return $row["value"];
} }
return 0; return 0;
} }
}; };
?> ?>