2015-07-08 21:40:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - weather
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
* @copyright Loic Blot 2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Weather\Db;
|
|
|
|
|
2017-05-23 06:44:54 +00:00
|
|
|
use \OCP\IDBConnection;
|
2015-07-08 21:40:17 +00:00
|
|
|
|
|
|
|
use \OCP\AppFramework\Db\Mapper;
|
|
|
|
|
|
|
|
class CityMapper extends Mapper {
|
2017-05-23 06:44:54 +00:00
|
|
|
public function __construct (IDBConnection $db) {
|
2015-07-08 21:40:17 +00:00
|
|
|
parent::__construct($db, 'weather_city');
|
|
|
|
}
|
2015-07-08 22:05:57 +00:00
|
|
|
|
2015-07-09 19:05:43 +00:00
|
|
|
public function load ($id) {
|
|
|
|
$sql = 'SELECT id, name, user_id FROM ' .
|
|
|
|
'*PREFIX*weather_city WHERE id = ?';
|
|
|
|
$query = \OCP\DB::prepare($sql);
|
|
|
|
$result = $query->execute(array($id));
|
|
|
|
|
|
|
|
if ($row = $result->fetchRow()) {
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-02 10:52:18 +00:00
|
|
|
public function count() {
|
|
|
|
$sql = 'SELECT count(*) AS ct FROM *PREFIX*weather_city WHERE user_id = ?';
|
|
|
|
$query = \OCP\DB::prepare($sql);
|
|
|
|
$result = $query->execute(array($userId));
|
|
|
|
if ($row = $result->fetchRow()) {
|
|
|
|
return $row['ct'];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-18 13:58:51 +00:00
|
|
|
public function exists ($id) {
|
|
|
|
return ($this->load($id));
|
|
|
|
}
|
|
|
|
|
2015-07-09 19:05:43 +00:00
|
|
|
public function getAll ($userId) {
|
2017-05-02 10:52:18 +00:00
|
|
|
$sql = 'SELECT id, name FROM *PREFIX*weather_city WHERE user_id = ?';
|
2015-07-09 19:05:43 +00:00
|
|
|
$query = \OCP\DB::prepare($sql);
|
|
|
|
$result = $query->execute(array($userId));
|
|
|
|
|
|
|
|
$cities = array();
|
|
|
|
while ($row = $result->fetchRow()) {
|
|
|
|
$cities[] = $row;
|
|
|
|
}
|
|
|
|
return $cities;
|
|
|
|
}
|
|
|
|
|
2015-07-08 22:05:57 +00:00
|
|
|
public function create ($userId, $name) {
|
2018-03-21 11:21:18 +00:00
|
|
|
$this->db->beginTransaction();
|
2017-05-02 10:52:18 +00:00
|
|
|
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city(user_id, name) VALUES (?,?)');
|
2015-07-08 22:05:57 +00:00
|
|
|
$query->execute(array($userId, $name));
|
2018-03-21 11:21:18 +00:00
|
|
|
$this->db->commit();
|
2015-07-08 22:05:57 +00:00
|
|
|
|
2017-05-02 10:52:18 +00:00
|
|
|
$sql = 'SELECT max(id) as maxid FROM *PREFIX*weather_city WHERE user_id = ? and name = ?';
|
2015-07-08 22:05:57 +00:00
|
|
|
$query = \OCP\DB::prepare($sql);
|
|
|
|
$result = $query->execute(array($userId, $name));
|
|
|
|
|
|
|
|
if ($row = $result->fetchRow()) {
|
2015-11-21 17:01:06 +00:00
|
|
|
return $row['maxid'];
|
2015-07-08 22:05:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-08 21:40:17 +00:00
|
|
|
};
|
|
|
|
?>
|