Skip to content

Commit

Permalink
graphql: Postgres procedures, events, triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Dec 13, 2023
1 parent a0c41ae commit 040a2a5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
16 changes: 12 additions & 4 deletions graphql-server/src/queryFactory/dolt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<SchemaItem[]> {
return this.queryForBuilder(
async em => {
let sel = em
Expand All @@ -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 }));

Check failure on line 109 in graphql-server/src/queryFactory/dolt/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected block statement surrounding arrow body
},
args.databaseName,
args.refName,
);
}

async getProcedures(args: t.RefArgs): t.UPR {
async getProcedures(args: t.RefArgs): Promise<SchemaItem[]> {
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 => ({

Check failure on line 125 in graphql-server/src/queryFactory/dolt/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected block statement surrounding arrow body
name: r.name,
type: SchemaType.Procedure,
}));
},
args.databaseName,
args.refName,
Expand Down
5 changes: 3 additions & 2 deletions graphql-server/src/queryFactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<SchemaItem[]>;

getProcedures(args: t.RefArgs): t.UPR;
getProcedures(args: t.RefArgs): Promise<SchemaItem[]>;

// DOLT-SPECIFIC QUERIES

Expand Down
8 changes: 5 additions & 3 deletions graphql-server/src/queryFactory/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<SchemaItem[]> {
return this.queryMultiple(async query => {
const vRes = await query(qh.getViewsQuery, [args.databaseName]);
const views = vRes.map(v => {
Expand All @@ -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<SchemaItem[]> {
const res: t.RawRows = await this.query(
qh.proceduresQuery,
[args.databaseName],
args.databaseName,
);
return res.map(r => ({ name: r.Name, type: SchemaType.Procedure }));

Check failure on line 180 in graphql-server/src/queryFactory/mysql/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected block statement surrounding arrow body
}

// DOLT QUERIES NOT IMPLEMENTED FOR MYSQL
Expand Down
25 changes: 19 additions & 6 deletions graphql-server/src/queryFactory/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<SchemaItem[]> {
return this.queryMultiple(async query => {
const vRes = await query(qh.getViewsQuery, [args.databaseName]);
const views = vRes.map(v => {
Expand All @@ -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<SchemaItem[]> {
const res: t.RawRows = await this.query(
qh.getProceduresQuery,
[args.databaseName],
args.databaseName,
);
return res.map(r => ({ name: r.proname, type: SchemaType.Procedure }));

Check failure on line 79 in graphql-server/src/queryFactory/postgres/index.ts

View workflow job for this annotation

GitHub Actions / ci

Expected block statement surrounding arrow body
}
}
18 changes: 15 additions & 3 deletions graphql-server/src/queryFactory/postgres/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
`;
13 changes: 2 additions & 11 deletions graphql-server/src/schemas/schema.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export class SchemaResolver {
): Promise<SchemaItem[]> {
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])
Expand All @@ -30,12 +27,6 @@ export class SchemaResolver {
async doltProcedures(@Args() args: RefArgs): Promise<SchemaItem[]> {
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;
}
}

0 comments on commit 040a2a5

Please sign in to comment.