From 4ecf14a32c0345eebf21e11d89a73cde649201e0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 2 Mar 2021 17:16:43 +0100 Subject: [PATCH] Create a view to get conversations info --- docs/db_struct.sql | 37 +++++++++++++++++++++++++++++++++++++ docs/migration.sql | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/docs/db_struct.sql b/docs/db_struct.sql index 006cfa1..d1006ee 100644 --- a/docs/db_struct.sql +++ b/docs/db_struct.sql @@ -245,3 +245,40 @@ CREATE TABLE `comunic_custom_emojis` ( `shortcut` VARCHAR(45) NULL, `path` VARCHAR(255) NULL, PRIMARY KEY (`id`)); + + +CREATE VIEW comunic_conversations_info AS + SELECT + l.id, + m.user_id, + l.name, + l.color, + l.background, + l.creation_time, + l.group_id, + l.can_everyone_add_members, + m.is_admin, + m.added_on AS user_added_on, + m.following, + m.last_message_seen, + msg.id AS last_msg_id, + msg.time_sent AS last_msg_time_sent, + msg.user_id AS last_msg_user_id, + msg.message AS last_msg_message, + msg.filepath AS last_msg_filepath, + msg.file_size AS last_msg_file_size, + msg.file_name AS last_msg_file_name, + msg.file_thumbnail AS last_msg_file_thumbnail, + msg.file_type AS last_msg_file_type + FROM + comunic_conversations_list l + JOIN + comunic_conversations_members m ON l.id = m.conv_id + JOIN + (SELECT + MAX(id) AS max_msg_id, conv_id + FROM + comunic_conversations_messages + GROUP BY conv_id) msg_id ON msg_id.conv_id = l.id + JOIN + comunic_conversations_messages msg ON msg.id = msg_id.max_msg_id; \ No newline at end of file diff --git a/docs/migration.sql b/docs/migration.sql index 4c70c2a..0faabd4 100644 --- a/docs/migration.sql +++ b/docs/migration.sql @@ -24,4 +24,41 @@ alter table comunic_conversations_list drop column last_active; alter table comunic_conversations_list add column color varchar(6), add column background varchar(255), - add group_id int; \ No newline at end of file + add group_id int; + +-- Conversations view +CREATE VIEW comunic_conversations_info AS + SELECT + l.id, + m.user_id, + l.name, + l.color, + l.background, + l.creation_time, + l.group_id, + l.can_everyone_add_members, + m.is_admin, + m.added_on AS user_added_on, + m.following, + m.last_message_seen, + msg.id AS last_msg_id, + msg.time_sent AS last_msg_time_sent, + msg.user_id AS last_msg_user_id, + msg.message AS last_msg_message, + msg.filepath AS last_msg_filepath, + msg.file_size AS last_msg_file_size, + msg.file_name AS last_msg_file_name, + msg.file_thumbnail AS last_msg_file_thumbnail, + msg.file_type AS last_msg_file_type + FROM + comunic_conversations_list l + JOIN + comunic_conversations_members m ON l.id = m.conv_id + JOIN + (SELECT + MAX(id) AS max_msg_id, conv_id + FROM + comunic_conversations_messages + GROUP BY conv_id) msg_id ON msg_id.conv_id = l.id + JOIN + comunic_conversations_messages msg ON msg.id = msg_id.max_msg_id; \ No newline at end of file