From ef0a0147c1690d84469dd7470956413f0c4b7738 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Tue, 31 Oct 2023 10:58:24 -0700 Subject: [PATCH] graphql: Get views, triggers, events, procedures for non-dolt dbs --- .../src/schemas/schema.queries.ts | 10 ++++ .../src/schemas/schema.resolver.ts | 50 +++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/packages/graphql-server/src/schemas/schema.queries.ts b/packages/graphql-server/src/schemas/schema.queries.ts index 0de3a8f6..29353b8d 100644 --- a/packages/graphql-server/src/schemas/schema.queries.ts +++ b/packages/graphql-server/src/schemas/schema.queries.ts @@ -6,3 +6,13 @@ export const getDoltSchemasQuery = (hasWhereCause = false): string => }`; export const doltProceduresQuery = `SELECT * FROM ${DoltSystemTable.PROCEDURES}`; + +export const getViewsQuery = `SELECT TABLE_SCHEMA, TABLE_NAME +FROM information_schema.tables +WHERE TABLE_TYPE = 'VIEW' AND TABLE_SCHEMA = ?`; + +export const getTriggersQuery = `SHOW TRIGGERS`; + +export const getEventsQuery = `SHOW EVENTS`; + +export const getProceduresQuery = `SHOW PROCEDURE STATUS WHERE type = "PROCEDURE" AND db = ?`; diff --git a/packages/graphql-server/src/schemas/schema.resolver.ts b/packages/graphql-server/src/schemas/schema.resolver.ts index de7c50be..257e8aca 100644 --- a/packages/graphql-server/src/schemas/schema.resolver.ts +++ b/packages/graphql-server/src/schemas/schema.resolver.ts @@ -1,9 +1,16 @@ import { Args, Query, Resolver } from "@nestjs/graphql"; -import { handleTableNotFound } from "src/tables/table.resolver"; -import { DataSourceService } from "../dataSources/dataSource.service"; +import { DataSourceService, ParQuery } from "../dataSources/dataSource.service"; +import { handleTableNotFound } from "../tables/table.resolver"; import { RefArgs } from "../utils/commonTypes"; import { SchemaItem } from "./schema.model"; -import { doltProceduresQuery, getDoltSchemasQuery } from "./schema.queries"; +import { + doltProceduresQuery, + getDoltSchemasQuery, + getEventsQuery, + getProceduresQuery, + getTriggersQuery, + getViewsQuery, +} from "./schema.queries"; @Resolver(_of => SchemaItem) export class SchemaResolver { @@ -16,6 +23,10 @@ export class SchemaResolver { ): Promise { return this.dss.queryMaybeDolt( async (query, isDolt) => { + if (!isDolt) { + return getSchemasForNonDolt(query, args.databaseName, type); + } + const res = await handleTableNotFound(async () => query(getDoltSchemasQuery(!!type), [type]), ); @@ -38,6 +49,13 @@ export class SchemaResolver { async doltProcedures(@Args() args: RefArgs): Promise<[SchemaItem]> { return this.dss.queryMaybeDolt( async (query, isDolt) => { + if (!isDolt) { + const res = await query(getProceduresQuery, [args.databaseName]); + return res.map(r => { + return { name: r.Name, type: "procedure" }; + }); + } + const res = await handleTableNotFound(async () => query(doltProceduresQuery), ); @@ -51,3 +69,29 @@ export class SchemaResolver { ); } } + +async function getSchemasForNonDolt( + query: ParQuery, + dbName: string, + type?: string, +): Promise { + const vRes = await query(getViewsQuery, [dbName]); + const views = vRes.map(v => { + return { name: v.TABLE_NAME, type: "view" }; + }); + if (type === "view") { + return views; + } + + const tRes = await query(getTriggersQuery); + const triggers = tRes.map(t => { + return { name: t.Trigger, type: "trigger" }; + }); + + const eRes = await query(getEventsQuery); + const events = eRes.map(e => { + return { name: e.Name, type: "event" }; + }); + + return [...views, ...triggers, ...events]; +}