From 040a2a500ca7bdc5155403df02701f708937b122 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Wed, 13 Dec 2023 12:34:10 -0800 Subject: [PATCH] graphql: Postgres procedures, events, triggers --- graphql-server/src/queryFactory/dolt/index.ts | 16 +++++++++--- graphql-server/src/queryFactory/index.ts | 5 ++-- .../src/queryFactory/mysql/index.ts | 8 +++--- .../src/queryFactory/postgres/index.ts | 25 ++++++++++++++----- .../src/queryFactory/postgres/queries.ts | 18 ++++++++++--- graphql-server/src/schemas/schema.resolver.ts | 13 ++-------- 6 files changed, 56 insertions(+), 29 deletions(-) diff --git a/graphql-server/src/queryFactory/dolt/index.ts b/graphql-server/src/queryFactory/dolt/index.ts index ed30da4d..0b9fa80b 100644 --- a/graphql-server/src/queryFactory/dolt/index.ts +++ b/graphql-server/src/queryFactory/dolt/index.ts @@ -6,6 +6,7 @@ import * as foreignKey from "../../indexes/foreignKey.model"; import * as index from "../../indexes/index.model"; import { convertToStringForQuery } from "../../rowDiffs/rowDiff.enums"; import { SchemaType } from "../../schemas/schema.enums"; +import { SchemaItem } from "../../schemas/schema.model"; import { DoltSystemTable, systemTableValues, @@ -91,7 +92,7 @@ export class DoltQueryFactory return res.filter(c => c.Key === "PRI").map(c => c.Field); } - async getSchemas(args: t.RefArgs, type?: SchemaType): t.UPR { + async getSchemas(args: t.RefArgs, type?: SchemaType): Promise { return this.queryForBuilder( async em => { let sel = em @@ -103,21 +104,28 @@ export class DoltQueryFactory type, }); } - return handleTableNotFound(async () => sel.getRawMany()); + const res = await handleTableNotFound(async () => sel.getRawMany()); + if (!res) return []; + return res.map(r => ({ name: r.name, type: r.type })); }, args.databaseName, args.refName, ); } - async getProcedures(args: t.RefArgs): t.UPR { + async getProcedures(args: t.RefArgs): Promise { return this.queryForBuilder( async em => { const sel = em .createQueryBuilder() .select("*") .from(DoltSystemTable.PROCEDURES, ""); - return handleTableNotFound(async () => sel.getRawMany()); + const res = await handleTableNotFound(async () => sel.getRawMany()); + if (!res) return []; + return res.map(r => ({ + name: r.name, + type: SchemaType.Procedure, + })); }, args.databaseName, args.refName, diff --git a/graphql-server/src/queryFactory/index.ts b/graphql-server/src/queryFactory/index.ts index 4d21c328..e7c1dda4 100644 --- a/graphql-server/src/queryFactory/index.ts +++ b/graphql-server/src/queryFactory/index.ts @@ -2,6 +2,7 @@ import { DataSource, EntityManager, QueryRunner } from "typeorm"; import { SortBranchesBy } from "../branches/branch.enum"; import { CommitDiffType } from "../diffSummaries/diffSummary.enums"; import { SchemaType } from "../schemas/schema.enums"; +import { SchemaItem } from "../schemas/schema.model"; import { TableDetails } from "../tables/table.model"; import * as t from "./types"; @@ -68,9 +69,9 @@ export declare class QueryFactory { getSqlSelect(args: t.RefArgs & { queryString: string }): t.PR; - getSchemas(args: t.RefArgs, type?: SchemaType): t.UPR; + getSchemas(args: t.RefArgs, type?: SchemaType): Promise; - getProcedures(args: t.RefArgs): t.UPR; + getProcedures(args: t.RefArgs): Promise; // DOLT-SPECIFIC QUERIES diff --git a/graphql-server/src/queryFactory/mysql/index.ts b/graphql-server/src/queryFactory/mysql/index.ts index 2c7328cd..f753f9a8 100644 --- a/graphql-server/src/queryFactory/mysql/index.ts +++ b/graphql-server/src/queryFactory/mysql/index.ts @@ -3,6 +3,7 @@ import { EntityManager, QueryRunner } from "typeorm"; import { QueryFactory } from ".."; import { SchemaType } from "../../schemas/schema.enums"; +import { SchemaItem } from "../../schemas/schema.model"; import { TableDetails } from "../../tables/table.model"; import { ROW_LIMIT } from "../../utils"; import { BaseQueryFactory } from "../base"; @@ -146,7 +147,7 @@ export class MySQLQueryFactory return this.query(args.queryString, [], args.databaseName, args.refName); } - async getSchemas(args: t.DBArgs, type?: SchemaType): t.UPR { + async getSchemas(args: t.DBArgs, type?: SchemaType): Promise { return this.queryMultiple(async query => { const vRes = await query(qh.getViewsQuery, [args.databaseName]); const views = vRes.map(v => { @@ -170,12 +171,13 @@ export class MySQLQueryFactory }, args.databaseName); } - async getProcedures(args: t.DBArgs): t.UPR { - return this.query( + async getProcedures(args: t.DBArgs): Promise { + const res: t.RawRows = await this.query( qh.proceduresQuery, [args.databaseName], args.databaseName, ); + return res.map(r => ({ name: r.Name, type: SchemaType.Procedure })); } // DOLT QUERIES NOT IMPLEMENTED FOR MYSQL diff --git a/graphql-server/src/queryFactory/postgres/index.ts b/graphql-server/src/queryFactory/postgres/index.ts index 7362cb05..3883de6d 100644 --- a/graphql-server/src/queryFactory/postgres/index.ts +++ b/graphql-server/src/queryFactory/postgres/index.ts @@ -1,6 +1,7 @@ import { QueryRunner } from "typeorm"; import { QueryFactory } from ".."; import { SchemaType } from "../../schemas/schema.enums"; +import { SchemaItem } from "../../schemas/schema.model"; import { TableDetails } from "../../tables/table.model"; import { MySQLQueryFactory } from "../mysql"; import { convertToTableDetails } from "../mysql/utils"; @@ -47,7 +48,7 @@ export class PostgresQueryFactory }, args.databaseName); } - async getSchemas(args: t.DBArgs, type?: SchemaType): t.UPR { + async getSchemas(args: t.DBArgs, type?: SchemaType): Promise { return this.queryMultiple(async query => { const vRes = await query(qh.getViewsQuery, [args.databaseName]); const views = vRes.map(v => { @@ -56,13 +57,25 @@ export class PostgresQueryFactory if (type === SchemaType.View) { return views; } - // TODO: events, triggers - return views; + const tRes = await query(qh.getTriggersQuery, [args.databaseName]); + const triggers = tRes.map(tr => { + return { name: tr.trigger_name, type: SchemaType.Trigger }; + }); + + const eRes = await query(qh.getEventsQuery); + const events = eRes.map(e => { + return { name: e.evtname, type: SchemaType.Event }; + }); + return [...views, ...triggers, ...events]; }, args.databaseName); } - async getProcedures(args: t.DBArgs): t.UPR { - // TODO: procedures - return this.query("", [args.databaseName], args.databaseName); + async getProcedures(args: t.DBArgs): Promise { + const res: t.RawRows = await this.query( + qh.getProceduresQuery, + [args.databaseName], + args.databaseName, + ); + return res.map(r => ({ name: r.proname, type: SchemaType.Procedure })); } } diff --git a/graphql-server/src/queryFactory/postgres/queries.ts b/graphql-server/src/queryFactory/postgres/queries.ts index 40f56ac6..7e1d5cc6 100644 --- a/graphql-server/src/queryFactory/postgres/queries.ts +++ b/graphql-server/src/queryFactory/postgres/queries.ts @@ -3,7 +3,19 @@ export const setSearchPath = (dbName: string, _isDolt = false) => export const listTablesQuery = `SELECT * FROM pg_catalog.pg_tables where schemaname=$1;`; -export const databasesQuery = `select schema_name - from information_schema.schemata;`; +export const databasesQuery = `SELECT schema_name FROM information_schema.schemata;`; -export const getViewsQuery = `select table_name from INFORMATION_SCHEMA.views WHERE table_schema = $1`; +export const getViewsQuery = `SELECT table_name FROM INFORMATION_SCHEMA.views WHERE table_schema = $1`; + +export const getTriggersQuery = `SELECT trigger_name +FROM information_schema.triggers +where trigger_schema = $1`; + +export const getEventsQuery = `select evtname from pg_event_trigger`; + +export const getProceduresQuery = `SELECT proname +FROM pg_catalog.pg_namespace +JOIN pg_catalog.pg_proc +ON pronamespace = pg_namespace.oid +WHERE nspname = $1 +`; diff --git a/graphql-server/src/schemas/schema.resolver.ts b/graphql-server/src/schemas/schema.resolver.ts index 50f8c97a..ba3d75e7 100644 --- a/graphql-server/src/schemas/schema.resolver.ts +++ b/graphql-server/src/schemas/schema.resolver.ts @@ -15,10 +15,7 @@ export class SchemaResolver { ): Promise { const conn = this.conn.connection(); const res = await conn.getSchemas(args, type); - if (!res) return []; - return res.map(r => { - return { name: r.name, type: r.type }; - }); + return res; } @Query(_returns => [SchemaItem]) @@ -30,12 +27,6 @@ export class SchemaResolver { async doltProcedures(@Args() args: RefArgs): Promise { const conn = this.conn.connection(); const res = await conn.getProcedures(args); - if (!res) return []; - return res.map(r => { - return { - name: conn.isDolt ? r.name : r.Name, - type: SchemaType.Procedure, - }; - }); + return res; } }