diff --git a/src/db/dbdataevent.ts b/src/db/dbdataevent.ts new file mode 100644 index 0000000000000000000000000000000000000000..3b7e92e7eb99f1ac9608f413af5aafcf72c158e8 --- /dev/null +++ b/src/db/dbdataevent.ts @@ -0,0 +1,61 @@ +import { DiscordStore } from "../store"; +import { IDbData } from "./dbdatainterface"; +import * as log from "npmlog"; + +export class DbEvent implements IDbData { + public MatrixId: string; + public DiscordId: string; + public Result: boolean; + + public async RunQuery(store: DiscordStore, params: any): Promise<null> { + log.silly("DiscordStore", "_get_schema_version"); + let row = null; + if (params.matrix_id) { + row = await store.db.getAsync(` + SELECT * + FROM event_store + WHERE matrix_id = $id`, { + $id: params.matrix_id, + }); + } else if (params.discord_id) { + row = await store.db.getAsync(` + SELECT * + FROM event_store + WHERE discord_id = $id`, { + $id: params.discord_id, + }); + } else { + throw new Error("Unknown row given as a param"); + } + this.Result = row !== undefined; + if (this.Result) { + this.MatrixId = row.matrix_id; + this.DiscordId = row.discord_id; + } + return null; + } + + public Insert(store: DiscordStore): Promise<null> { + return store.db.runAsync(` + INSERT INTO event_store + (matrix_id,discord_id) + VALUES ($matrix_id,$discord_id);`, { + $matrix_id: this.MatrixId, + $discord_id: this.DiscordId, + }); + } + + public Update(store: DiscordStore): Promise<null> { + throw new Error("Update is not implemented"); + } + + public Delete(store: DiscordStore): Promise<null> { + return store.db.runAsync(` + DELETE FROM event_store + WHERE matrix_id = $matrix_id + AND discord_id = $discord_id;`, { + $matrix_id: this.MatrixId, + $discord_id: this.DiscordId, + }); + } +} diff --git a/src/db/schema/v5.ts b/src/db/schema/v5.ts new file mode 100644 index 0000000000000000000000000000000000000000..433eb996c5d1f890661e737045a356423fda5dbc --- /dev/null +++ b/src/db/schema/v5.ts @@ -0,0 +1,23 @@ +import {IDbSchema} from "./dbschema"; +import {DiscordStore} from "../../store"; +import {DiscordClientFactory} from "../../clientfactory"; +import * as log from "npmlog"; +import * as Bluebird from "bluebird"; + +export class Schema implements IDbSchema { + public description = "create event_store table"; + public run(store: DiscordStore): Promise<null> { + return store.create_table(` + CREATE TABLE event_store ( + matrix_id TEXT NOT NULL, + discord_id TEXT NOT NULL, + PRIMARY KEY(matrix_id, discord_id) + );`, "event_store"); + } + + public rollBack(store: DiscordStore): Promise <null> { + return store.db.execAsync( + `DROP TABLE IF EXISTS guild_emoji;`, + ); + } +}