From d56d77e64b9a4dd28072b37264ab3c97b8a5f15b Mon Sep 17 00:00:00 2001 From: Will Hunt <half-shot@molrams.com> Date: Sun, 10 Sep 2017 15:31:30 +0100 Subject: [PATCH] Add missing schema and data item. --- src/db/dbdataevent.ts | 61 +++++++++++++++++++++++++++++++++++++++++++ src/db/schema/v5.ts | 23 ++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/db/dbdataevent.ts create mode 100644 src/db/schema/v5.ts diff --git a/src/db/dbdataevent.ts b/src/db/dbdataevent.ts new file mode 100644 index 0000000..3b7e92e --- /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 0000000..433eb99 --- /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;`, + ); + } +} -- GitLab