feat(sql-to-rest): friendly 'not supported yet' errors

This commit is contained in:
Greg Richardson committed 2024-05-06 17:00:28 -06:00
1 parent 238fdc6308
commit afebb83f01
2 files changed
+37 -14

No files matched your search

+17 -13
View File
@@ -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)
+20 -1
View File
@@ -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 = {