mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
* adopt sql to support postgresql
This commit is contained in:
parent
772305dbed
commit
173a1b387d
4 changed files with 32 additions and 9 deletions
|
|
@ -19,11 +19,11 @@ class TagsController extends ApiController {
|
|||
checkReadAccess($listId);
|
||||
$db = DBConnection::instance();
|
||||
|
||||
$sqlWhere = ($listId == -1) ? "" : "WHERE list_id=$listId";
|
||||
$q = $db->dq("SELECT name,tag_id,COUNT(tag_id) AS tags_count
|
||||
FROM {$db->prefix}tag2task INNER JOIN {$db->prefix}tags ON tag_id=id
|
||||
$sqlWhere = ($listId == -1) ? "" : "WHERE list_id = $listId";
|
||||
$q = $db->dq("SELECT name, tag_id, COUNT(tag_id) AS tags_count
|
||||
FROM {$db->prefix}tag2task INNER JOIN {$db->prefix}tags ON tag_id = id
|
||||
$sqlWhere
|
||||
GROUP BY (tag_id)
|
||||
GROUP BY tag_id, name
|
||||
ORDER BY tags_count DESC");
|
||||
$at = array();
|
||||
$ac = array();
|
||||
|
|
@ -72,10 +72,12 @@ class TagsController extends ApiController {
|
|||
$db = DBConnection::instance();
|
||||
$begin = trim(_get('q'));
|
||||
$limit = 8;
|
||||
$q = $db->dq("SELECT name,id FROM {$db->prefix}tags
|
||||
$q = $db->dq("SELECT name, tag_id AS id FROM {$db->prefix}tags
|
||||
INNER JOIN {$db->prefix}tag2task ON id=tag_id
|
||||
WHERE list_id=$listId AND ". $db->like('name', '%s%%', $begin). "
|
||||
GROUP BY tag_id ORDER BY name LIMIT $limit");
|
||||
GROUP BY tag_id, name
|
||||
ORDER BY name
|
||||
LIMIT $limit");
|
||||
$t = array();
|
||||
while ($r = $q->fetchRow()) {
|
||||
$t[] = $r[0];
|
||||
|
|
|
|||
|
|
@ -98,13 +98,21 @@ class TasksController extends ApiController {
|
|||
$t['total'] = 0;
|
||||
$t['list'] = array();
|
||||
|
||||
$groupConcat = '';
|
||||
if ($db::DBTYPE == DBConnection::DBTYPE_POSTGRES) {
|
||||
$groupConcat = "array_to_string(array_agg(tags.id), ',') AS tags_ids, string_agg(tags.name, ',') AS tags";
|
||||
}
|
||||
else {
|
||||
$groupConcat = "GROUP_CONCAT(tags.id) AS tags_ids, GROUP_CONCAT(tags.name) AS tags";
|
||||
}
|
||||
$q = $db->dq("
|
||||
SELECT todo.*, todo.duedate IS NULL AS ddn, GROUP_CONCAT(tags.id) AS tags_ids, GROUP_CONCAT(tags.name) AS tags
|
||||
SELECT todo.*, todo.duedate IS NULL AS ddn, $groupConcat
|
||||
FROM {$db->prefix}todolist AS todo
|
||||
LEFT JOIN {$db->prefix}tag2task AS t2t ON todo.id = t2t.task_id
|
||||
LEFT JOIN {$db->prefix}tags AS tags ON t2t.tag_id = tags.id
|
||||
WHERE $sqlWhereListId $sqlWhere
|
||||
GROUP BY todo.id $sqlSort
|
||||
GROUP BY todo.id
|
||||
$sqlSort
|
||||
");
|
||||
|
||||
while ($r = $q->fetchAssoc())
|
||||
|
|
|
|||
|
|
@ -8,6 +8,10 @@
|
|||
|
||||
class DBConnection
|
||||
{
|
||||
const DBTYPE_SQLITE = "sqlite";
|
||||
const DBTYPE_MYSQL = "mysql";
|
||||
const DBTYPE_POSTGRES = "postgres";
|
||||
|
||||
protected static $instance;
|
||||
|
||||
public static function init(Database_Abstract $instance) : Database_Abstract
|
||||
|
|
@ -33,6 +37,7 @@ class DBConnection
|
|||
|
||||
abstract class Database_Abstract
|
||||
{
|
||||
const DBTYPE = '';
|
||||
protected static $readonlyProps = ['prefix', 'lastQuery'];
|
||||
|
||||
/** @var string */
|
||||
|
|
|
|||
|
|
@ -86,12 +86,20 @@ class DBCore
|
|||
public function getTaskById(int $id): ?array
|
||||
{
|
||||
$db = $this->db;
|
||||
$groupConcat = '';
|
||||
if ($db::DBTYPE == DBConnection::DBTYPE_POSTGRES) {
|
||||
$groupConcat = "array_to_string(array_agg(tags.id), ',') AS tags_ids, string_agg(tags.name, ',') AS tags";
|
||||
}
|
||||
else {
|
||||
$groupConcat = "GROUP_CONCAT(tags.id) AS tags_ids, GROUP_CONCAT(tags.name) AS tags";
|
||||
}
|
||||
$r = $db->sqa("
|
||||
SELECT todo.*, GROUP_CONCAT(tags.id) AS tags_ids, GROUP_CONCAT(tags.name) AS tags
|
||||
SELECT todo.*, $groupConcat
|
||||
FROM {$db->prefix}todolist AS todo
|
||||
LEFT JOIN {$db->prefix}tag2task AS t2t ON todo.id = t2t.task_id
|
||||
LEFT JOIN {$db->prefix}tags AS tags ON t2t.tag_id = tags.id
|
||||
WHERE todo.id = $id
|
||||
GROUP BY todo.id
|
||||
");
|
||||
return $r;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue