From afebb83f01363eb74b7f05399a6ea69dcb7de77a Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Mon, 6 May 2024 17:00:28 -0600 Subject: [PATCH] feat(sql-to-rest): friendly 'not supported yet' errors --- packages/sql-to-rest/src/processor.ts | 30 +++++++++++-------- packages/sql-to-rest/src/types/libpg-query.ts | 21 ++++++++++++- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/sql-to-rest/src/processor.ts b/packages/sql-to-rest/src/processor.ts index 11faddf264c..1c09709708f 100644 --- a/packages/sql-to-rest/src/processor.ts +++ b/packages/sql-to-rest/src/processor.ts @@ -170,19 +170,19 @@ export async function processSql(sql: string) { * Converts a pg-query `Stmt` into a PostgREST-compatible `Statement`. */ function processStmt({ stmt }: Stmt): Statement { - const keys = Object.keys(stmt) as unknown as (keyof typeof stmt)[] - - if (keys.length > 1) { - throw new Error(`stmt contains multiple statements: ${JSON.stringify(keys)}`) - } - - const [stmtType] = keys - - switch (stmtType) { - case 'SelectStmt': - return processSelectStmt(stmt) - default: - throw new Error(`Unsupported stmt type '${stmtType}'`) + if ('SelectStmt' in stmt) { + return processSelectStmt(stmt) + } else if ('InsertStmt' in stmt) { + throw new Error(`Insert statements are not yet supported by the translator`) + } else if ('UpdateStmt' in stmt) { + throw new Error(`Update statements are not yet supported by the translator`) + } else if ('DeleteStmt' in stmt) { + throw new Error(`Delete statements are not yet supported by the translator`) + } else if ('ExplainStmt' in stmt) { + throw new Error(`Explain statements are not yet supported by the translator`) + } else { + const [stmtType] = Object.keys(stmt) + throw new Error(`Unsupported stmt type '${stmtType}'`) } } @@ -195,6 +195,10 @@ function processSelectStmt(stmt: SelectStmt): Select { throw new Error('Only one FROM source is supported') } + if (stmt.SelectStmt.withClause) { + throw new Error('WITH clauses are not supported') + } + const [fromClause] = stmt.SelectStmt.fromClause const { from, alias, embeddedTargets } = processFromClause(fromClause) diff --git a/packages/sql-to-rest/src/types/libpg-query.ts b/packages/sql-to-rest/src/types/libpg-query.ts index f89499b529a..ec45ec7ae6b 100644 --- a/packages/sql-to-rest/src/types/libpg-query.ts +++ b/packages/sql-to-rest/src/types/libpg-query.ts @@ -187,6 +187,7 @@ export type SelectStmt = { groupClause?: any[] groupDistinct?: boolean havingClause?: any[] + withClause?: any sortClause?: SortBy[] limitOption: string limitCount?: A_Const @@ -195,8 +196,26 @@ export type SelectStmt = { } } +export type InsertStmt = { + InsertStmt: {} +} + +export type UpdateStmt = { + UpdateStmt: {} +} + +export type DeleteStmt = { + DeleteStmt: {} +} + +export type ExplainStmt = { + ExplainStmt: { + query: SelectStmt | InsertStmt | UpdateStmt | DeleteStmt + } +} + export type Stmt = { - stmt: SelectStmt + stmt: SelectStmt | InsertStmt | UpdateStmt | DeleteStmt | ExplainStmt } export type ParsedQuery = {