From 173a1b387d584abaffe16bf4ea6d487c6ec88c5e Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Thu, 3 Aug 2023 23:13:55 +0300 Subject: [PATCH] * adopt sql to support postgresql --- src/includes/api/TagsController.php | 14 ++++++++------ src/includes/api/TasksController.php | 12 ++++++++++-- src/includes/class.dbconnection.php | 5 +++++ src/includes/class.dbcore.php | 10 +++++++++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/includes/api/TagsController.php b/src/includes/api/TagsController.php index 814c1ae..8158ea8 100644 --- a/src/includes/api/TagsController.php +++ b/src/includes/api/TagsController.php @@ -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]; diff --git a/src/includes/api/TasksController.php b/src/includes/api/TasksController.php index 05c3218..9a0cbe8 100644 --- a/src/includes/api/TasksController.php +++ b/src/includes/api/TasksController.php @@ -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()) diff --git a/src/includes/class.dbconnection.php b/src/includes/class.dbconnection.php index 606a79e..389510c 100644 --- a/src/includes/class.dbconnection.php +++ b/src/includes/class.dbconnection.php @@ -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 */ diff --git a/src/includes/class.dbcore.php b/src/includes/class.dbcore.php index d1c5f63..7774310 100644 --- a/src/includes/class.dbcore.php +++ b/src/includes/class.dbcore.php @@ -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; }