From 30968bcd5b62efca1b134ccc0fcc550cfff20db0 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 1 Nov 2024 17:44:35 +0100 Subject: [PATCH] split sql --- src/events/client/ready.ts | 3 +- src/sql/reminder/add.sql | 14 +++++ src/sql/reminder/find.sql | 13 +++++ src/sql/reminder/findById.sql | 6 ++ src/sql/reminder/ownership_check.sql | 14 +++++ src/sql/reminder/remove.sql | 4 ++ src/sql/reminder/select.sql | 4 ++ src/sql/reminder/update.sql | 13 +++++ src/utils/commands/reminder.ts | 82 +++++++++------------------- 9 files changed, 96 insertions(+), 57 deletions(-) create mode 100644 src/sql/reminder/add.sql create mode 100644 src/sql/reminder/find.sql create mode 100644 src/sql/reminder/findById.sql create mode 100644 src/sql/reminder/ownership_check.sql create mode 100644 src/sql/reminder/remove.sql create mode 100644 src/sql/reminder/select.sql create mode 100644 src/sql/reminder/update.sql diff --git a/src/events/client/ready.ts b/src/events/client/ready.ts index 2c8aa4e..d27bc42 100644 --- a/src/events/client/ready.ts +++ b/src/events/client/ready.ts @@ -9,6 +9,7 @@ import { setTimeoutReminder, updateReminder, } from "../../utils/commands/reminder"; +import { readSQL } from "../../utils/db"; export const once = true; @@ -19,7 +20,7 @@ export default async (client: Client) => { // Restart all the timeout about reminders here new Promise((ok, ko) => { // Fetch all reminders - client.db.all("SELECT * FROM reminder", [], (err, row) => { + client.db.all(readSQL("reminder/select"), [], (err, row) => { if (err) { ko(err); } diff --git a/src/sql/reminder/add.sql b/src/sql/reminder/add.sql new file mode 100644 index 0000000..d703484 --- /dev/null +++ b/src/sql/reminder/add.sql @@ -0,0 +1,14 @@ +INSERT INTO + reminder ( + data, + expiration_date, + option_id, + channel_id, + creation_date, + user_id, + guild_id, + locale, + timeout_id + ) +VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?); diff --git a/src/sql/reminder/find.sql b/src/sql/reminder/find.sql new file mode 100644 index 0000000..f7ad878 --- /dev/null +++ b/src/sql/reminder/find.sql @@ -0,0 +1,13 @@ +SELECT + data, + creation_date, + expiration_date, + id +FROM + reminder +WHERE + user_id = ? + AND ( + guild_id = ? + OR guild_id = 0 + ) diff --git a/src/sql/reminder/findById.sql b/src/sql/reminder/findById.sql new file mode 100644 index 0000000..8592f27 --- /dev/null +++ b/src/sql/reminder/findById.sql @@ -0,0 +1,6 @@ +SELECT + * +FROM + reminder +WHERE + id = ? diff --git a/src/sql/reminder/ownership_check.sql b/src/sql/reminder/ownership_check.sql new file mode 100644 index 0000000..17e7eca --- /dev/null +++ b/src/sql/reminder/ownership_check.sql @@ -0,0 +1,14 @@ +SELECT + EXISTS ( + SELECT + 1 + FROM + reminder + WHERE + id = ? + AND user_id = ? + AND ( + guild_id = ? + OR guild_id = 0 + ) + ) diff --git a/src/sql/reminder/remove.sql b/src/sql/reminder/remove.sql new file mode 100644 index 0000000..c9bca24 --- /dev/null +++ b/src/sql/reminder/remove.sql @@ -0,0 +1,4 @@ +DELETE FROM reminder +WHERE + creation_date = ? + AND user_id = ? diff --git a/src/sql/reminder/select.sql b/src/sql/reminder/select.sql new file mode 100644 index 0000000..8095263 --- /dev/null +++ b/src/sql/reminder/select.sql @@ -0,0 +1,4 @@ +SELECT + * +FROM + reminder diff --git a/src/sql/reminder/update.sql b/src/sql/reminder/update.sql new file mode 100644 index 0000000..d1a499b --- /dev/null +++ b/src/sql/reminder/update.sql @@ -0,0 +1,13 @@ +UPDATE reminder +SET + data = ?, + expiration_date = ?, + option_id = ?, + channel_id = ?, + creation_date = ?, + user_id = ?, + guild_id = ?, + locale = ?, + timeout_id = ? +WHERE + ID = ? diff --git a/src/utils/commands/reminder.ts b/src/utils/commands/reminder.ts index 1005b8c..19627fc 100644 --- a/src/utils/commands/reminder.ts +++ b/src/utils/commands/reminder.ts @@ -3,6 +3,7 @@ import { getLocale } from "../locales"; import { blank, cleanCodeBlock } from "../misc"; import { showDate, strToSeconds, timeDeltaToString } from "../time"; import { RegexC, RegExpFlags } from "../regex"; +import { readSQL } from "../db"; /** * Option possible for reminders @@ -92,9 +93,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind // Add the remind to the db client.db.run( - "INSERT INTO reminder ( \ - data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id, locale, timeout_id \ - ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );", + readSQL("reminder/add"), [ info.message, `${expiration_date}`, @@ -127,19 +126,15 @@ export const newReminder = async (client: Client, time: string, info: infoRemind export const deleteReminder = (client: Client, createdAt: string, userId: string) => { // Delete the reminder for the database return new Promise((ok, ko) => { - // Add the remind to the db - client.db.run( - "DELETE FROM reminder WHERE creation_date = ? AND user_id = ?", - [createdAt, userId], - (err) => { - if (err) { - ko(err); - } + // Remove the remind to the db + client.db.run(readSQL("reminder/remove"), [createdAt, userId], (err) => { + if (err) { + ko(err); + } - // Send confirmation to user - ok(true); - }, - ); + // Send confirmation to user + ok(true); + }); }); }; @@ -280,12 +275,7 @@ export const checkOwnershipReminder = async ( const data = (await new Promise((ok, ko) => { // Check the ownership client.db.all( - "SELECT EXISTS ( \ - SELECT 1 FROM reminder \ - WHERE id = ? \ - AND user_id = ? \ - AND (guild_id = ? OR guild_id = 0) \ - )", + readSQL("reminder/ownership_check"), [id, userId, guildId], (err, row) => { if (err) { @@ -308,19 +298,14 @@ export const checkOwnershipReminder = async ( export const getReminderInfo = async (client: Client, id: number) => { return (await new Promise((ok, ko) => { // Check the ownership - client.db.all( - "SELECT * FROM reminder \ - WHERE id = ?", - [id], - (err, row) => { - if (err) { - ko(err); - } + client.db.all(readSQL("reminder/findById"), [id], (err, row) => { + if (err) { + ko(err); + } - // Send all the current reminders - ok(row[0]); - }, - ); + // Send all the current reminders + ok(row[0]); + }); })) as dbReminder; }; @@ -334,17 +319,7 @@ export const updateReminder = (client: Client, data: dbReminder) => { return new Promise((ok, ko) => { // Update the db client.db.run( - "UPDATE reminder \ - SET data = ?, \ - expiration_date = ?, \ - option_id = ?, \ - channel_id = ?, \ - creation_date = ?, \ - user_id = ?, \ - guild_id = ?, \ - locale = ?, \ - timeout_id = ? \ - WHERE ID = ?", + readSQL("reminder/update"), [ data.data, data.expiration_date, @@ -378,19 +353,14 @@ export const updateReminder = (client: Client, data: dbReminder) => { const listReminders = async (client: Client, userId: string, guildId: string | null) => { return (await new Promise((ok, ko) => { // Check the ownership - client.db.all( - "SELECT data, creation_date, expiration_date, id FROM reminder \ - WHERE user_id = ? AND (guild_id = ? OR guild_id = 0)", - [userId, guildId ?? 0], - (err, row) => { - if (err) { - ko(err); - } + client.db.all(readSQL("reminder/find"), [userId, guildId ?? 0], (err, row) => { + if (err) { + ko(err); + } - // Send all the current reminders - ok(row); - }, - ); + // Send all the current reminders + ok(row); + }); })) as dbReminder[]; };