Skip to content

Commit

Permalink
web: Bump node-sql-parser, update types
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Dec 18, 2023
1 parent 0a366c2 commit f3e0c6e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
39 changes: 16 additions & 23 deletions web/hooks/useSqlBuilder/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ColumnForDataTableFragment, SchemaType } from "@gen/graphql-types";
import useSqlParser from "@hooks/useSqlParser";
import { Delete, Insert_Replace, Select, Update } from "node-sql-parser";
import { Alter, Delete, Insert_Replace, Select, Update } from "node-sql-parser";
import {
ColumnValue,
Conditions,
addToExistingWhereFromPKCols,
escapeSingleQuotes,
escapeSingleQuotesInWhereObj,
getOrderByArr,
Expand Down Expand Up @@ -31,9 +32,9 @@ export default function useSqlBuilder() {
return sqlify(getSqlDelete(del));
}

// function convertToSqlAlter(alt: Partial<Alter>): string {
// return sqlify(getSqlAlter(alt));
// }
function convertToSqlAlter(alt: Partial<Alter>): string {
return sqlify(getSqlAlter(alt));
}

function convertToSqlUpdate(upd: Partial<Update>): string {
return sqlify(getSqlUpdate(upd));
Expand All @@ -43,15 +44,6 @@ export default function useSqlBuilder() {
return sqlify(getSqlSelect(select));
}

// TODO: Remove these when node-sql-parser types are updated
function convertToSqlAlterAny(alt: any): string {
return sqlify(getSqlAlter(alt));
}

function convertToSqlSelectAny(select: any): string {
return sqlify(getSqlSelect(select));
}

// Converts query string to sql with new table name and columns
function convertToSqlWithNewCols(
q: string,
Expand Down Expand Up @@ -146,7 +138,7 @@ export default function useSqlBuilder() {
sel = parsed;
}
}
sel.where = getWhereFromPKCols(conditions, isPostgres, sel.where);
sel.where = addToExistingWhereFromPKCols(conditions, isPostgres, sel.where);
return convertToSqlSelect(sel);
}

Expand Down Expand Up @@ -192,26 +184,27 @@ where schemaname='${dbName}';`;
}

function hideRowQuery(tableName: string, conditions: Conditions): string {
return convertToSqlSelectAny({
const whereVals = getWhereFromPKCols(conditions, isPostgres);
const sel: Partial<Select> = {
columns: [getSqlColumn("*")],
type: "select",
from: [{ table: tableName }],
where: {
};
if (whereVals) {
sel.where = {
name: "NOT",
type: "function",
args: {
type: "expr_list",
value: [getWhereFromPKCols(conditions, isPostgres)],
value: [whereVals],
},
},
});
};
}
return convertToSqlSelect(sel);
}

function alterTableDropColQuery(tableName: string, column: string): string {
const alt = {
// type: "alter",
// TODO: Alter type is wrong
// table: { table: tableName, db: null, as: null },
table: [{ table: tableName, db: null, as: null }],
expr: [
{
Expand All @@ -223,7 +216,7 @@ where schemaname='${dbName}';`;
},
],
};
return convertToSqlAlterAny(alt);
return convertToSqlAlter(alt);
}

function updateTableQuery(
Expand Down
43 changes: 29 additions & 14 deletions web/hooks/useSqlBuilder/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Expr,
Insert_Replace,
OrderBy,
Function as ParserFunction,
Select,
Update,
} from "node-sql-parser";
Expand All @@ -26,7 +27,7 @@ export function getSqlSelect(sel: Partial<Select>): Select {
distinct: null,
columns: [getSqlColumn("*")],
from: null,
where: null as any,
where: null,
groupby: null,
having: null,
orderby: null,
Expand All @@ -51,15 +52,15 @@ export function getSqlDelete(del: Partial<Delete>): Delete {
type: "delete",
from: [],
table: null,
where: null as any,
where: null,
...del,
};
}

export function getSqlAlter(alt: Partial<Alter>): Alter {
return {
type: "alter",
table: { db: null, table: "", as: null },
table: [],
expr: {},
...alt,
};
Expand All @@ -71,7 +72,7 @@ export function getSqlUpdate(upd: Partial<Update>): Update {
db: null,
table: [],
set: [],
where: null as any,
where: null,
...upd,
};
}
Expand Down Expand Up @@ -107,12 +108,11 @@ export function mapColsToColumnRef(
});
}

// TODO: Return Expr
// The where object is a binary tree with 'left' and 'right' nodes
export function escapeSingleQuotesInWhereObj(
where: any | null,
isPostgres: boolean,
): any | null {
): Expr | null {
if (!where) return null;

if (where.args) {
Expand Down Expand Up @@ -155,12 +155,11 @@ export function escapeSingleQuotes(value: string, isPostgres: boolean): string {
return value.replace(/'/g, "\\'");
}

// TODO: Return Expr
function getNewWhereCondition(
column: string,
value: string,
isPostgres: boolean,
): any {
): Expr {
const valIsNull = isNullValue(value);
const escapedVal = escapeSingleQuotes(value, isPostgres);
return {
Expand All @@ -182,7 +181,7 @@ function getNewWhereCondition(
export function getWhereObj(
column: string,
value: string,
where: Expr | null,
where: Expr | ParserFunction | null,
isPostgres: boolean,
): Expr {
const newCondition = getNewWhereCondition(column, value, isPostgres);
Expand All @@ -191,10 +190,15 @@ export function getWhereObj(
return newCondition;
}

const left = escapeSingleQuotesInWhereObj(where, isPostgres);
if (!left) {
return newCondition;
}

return {
type: "binary_expr",
operator: "AND",
left: { ...escapeSingleQuotesInWhereObj(where, isPostgres) },
left,
right: newCondition,
};
}
Expand Down Expand Up @@ -231,13 +235,24 @@ export function getOrderByArr(
export function getWhereFromPKCols(
conditions: Conditions,
isPostgres: boolean,
where?: Expr | null,
): Expr | undefined {
let cond: Expr | null = where || null;
): Expr | null {
let cond: Expr | null = null;
conditions.forEach(c => {
cond = getWhereObj(c.col, c.val, cond, isPostgres);
});
return cond;
}

export function addToExistingWhereFromPKCols(
conditions: Conditions,
isPostgres: boolean,
where?: Expr | ParserFunction | null,
): Expr | ParserFunction | null {
let cond: Expr | ParserFunction | null = where || null;
conditions.forEach(c => {
cond = getWhereObj(c.col, c.val, cond, isPostgres);
});
return cond || undefined;
return cond;
}

export function getPostgresSchemaDefQuery(
Expand Down
9 changes: 1 addition & 8 deletions web/hooks/useSqlParser/tests/testData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AggrFunc, ColumnRef } from "node-sql-parser";
import { Column } from "node-sql-parser";

export const invalidQuery = `this is not a valid query`;

Expand Down Expand Up @@ -117,13 +117,6 @@ export const mutationExamples = [
"FLUSH PRIVILEGES",
];

// TODO: Replace with node-sql-parser column when fixed
type Column = {
expr: ColumnRef | AggrFunc;
as: string | null;
type?: string;
};

export function getParserCol(
name: string,
includeType?: boolean,
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"lodash": "^4.17.21",
"next": "^14.0.1",
"next-useragent": "^2.8.0",
"node-sql-parser": "^4.15.0",
"node-sql-parser": "^4.17.0",
"react": "^18.2.0",
"react-ace": "^10.1.0",
"react-copy-to-clipboard": "^5.1.0",
Expand Down
10 changes: 5 additions & 5 deletions web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ __metadata:
lodash: ^4.17.21
next: ^14.0.1
next-useragent: ^2.8.0
node-sql-parser: ^4.15.0
node-sql-parser: ^4.17.0
postcss: ^8.4.31
postcss-preset-env: ^9.1.4
prettier: ^3.1.0
Expand Down Expand Up @@ -10012,12 +10012,12 @@ __metadata:
languageName: node
linkType: hard

"node-sql-parser@npm:^4.15.0":
version: 4.15.0
resolution: "node-sql-parser@npm:4.15.0"
"node-sql-parser@npm:^4.17.0":
version: 4.17.0
resolution: "node-sql-parser@npm:4.17.0"
dependencies:
big-integer: ^1.6.48
checksum: 0e8bb210e8e6b10a135c81fe7d26f37f085e0e909dd4a87afc1266b915abd036084bb4a5d97a263384781bfcef44d33bfb0b0d6448e78fa8ebbc1c6da05cb0ea
checksum: 6b05834ae2039d2b716d7054e927c03c47a41f8361ae0426fd690dd1b789af1b40446b81408d50023bbb5b1d49b7b342b3e9b28eccd57176d4912f728fffad01
languageName: node
linkType: hard

Expand Down

0 comments on commit f3e0c6e

Please sign in to comment.