Skip to content

Commit

Permalink
graphql: Get views, triggers, events, procedures for non-dolt dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Oct 31, 2023
1 parent 69606f0 commit ef0a014
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/graphql-server/src/schemas/schema.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?`;
50 changes: 47 additions & 3 deletions packages/graphql-server/src/schemas/schema.resolver.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,6 +23,10 @@ export class SchemaResolver {
): Promise<SchemaItem[]> {
return this.dss.queryMaybeDolt(
async (query, isDolt) => {
if (!isDolt) {
return getSchemasForNonDolt(query, args.databaseName, type);
}

const res = await handleTableNotFound(async () =>
query(getDoltSchemasQuery(!!type), [type]),
);
Expand All @@ -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),
);
Expand All @@ -51,3 +69,29 @@ export class SchemaResolver {
);
}
}

async function getSchemasForNonDolt(
query: ParQuery,
dbName: string,
type?: string,
): Promise<SchemaItem[]> {
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];
}

0 comments on commit ef0a014

Please sign in to comment.