11import { FieldInfo , Queryable , nameQuery , sql } from '@pgkit/client'
2- import PGQueryEmscripten , { PgQueryEmscriptenParseResult } from 'pg-query-emscripten'
2+ import PGQueryEmscripten from 'pg-query-emscripten'
33import { type ServerContext } from './context.js'
44
5- export type PgsqlParserParseResult = {
6- RawStmt ?: {
7- stmt ?: { }
8- stmt_len ?: number
9- stmt_location ?: number
10- }
11- }
12-
135export const runQuery = async ( query : string , { connection} : ServerContext ) : Promise < QueryResult [ ] > => {
14- console . log ( 'query' , query )
156 if ( query . startsWith ( '--split-semicolons\n' ) ) {
167 const queries = query . split ( / ; \s * \n / )
178 const results = [ ]
@@ -26,52 +17,32 @@ export const runQuery = async (query: string, {connection}: ServerContext): Prom
2617
2718 const results = [ ] as QueryResult [ ]
2819
29- let nativeParsed : PgQueryEmscriptenParseResult
30- try {
31- const pgsqlParser = await new PGQueryEmscripten ( )
32- nativeParsed = pgsqlParser . parse ( query )
33- if ( nativeParsed . error ) {
34- const message = [
35- nativeParsed . error . message ,
36- '' ,
37- `If you think the query is actually valid, it's possible the parsing library has a bug.` ,
38- `Try adding --no-parse at the top of your query to disable statement-level query parsing and send it to the DB as-is.` ,
39- ] . join ( '\n' )
40- const err = new Error ( message , { cause : nativeParsed . error } )
41- makeJsonable ( err )
42- // if (Math.random()) throw new Error(nativeParsed.error.message, {cause: nativeParsed.error})
43- return [
44- {
45- query : nameQuery ( [ query ] ) ,
46- original : query ,
47- error : err ,
48- result : null ,
49- fields : null ,
50- position : nativeParsed . error . cursorpos - 1 ,
51- } ,
52- ]
53- }
54- } catch ( err ) {
55- makeJsonable ( err )
56- err . message = [
57- err . message ,
20+ const [ parseError , nativeParsed ] = await Promise . resolve ( )
21+ . then ( ( ) => new PGQueryEmscripten ( ) )
22+ . then ( parser => [ null , parser . parse ( query ) ] as const )
23+ . catch ( ( error : Error ) => [ error , null ] as const )
24+
25+ if ( parseError || nativeParsed . error ) {
26+ const error = parseError || new Error ( nativeParsed . error ! . message , { cause : nativeParsed . error } )
27+ error . message = [
28+ error . message ,
5829 '' ,
5930 `If you think the query is actually valid, it's possible the parsing library has a bug.` ,
6031 `Try adding --no-parse at the top of your query to disable statement-level query parsing and send it to the DB as-is.` ,
6132 ] . join ( '\n' )
33+ makeJsonable ( error )
6234 return [
6335 {
6436 query : nameQuery ( [ query ] ) ,
6537 original : query ,
66- error : err ,
38+ error,
6739 result : null ,
6840 fields : null ,
69- position : ( err as { cursorPosition ?: number } ) ?. cursorPosition ,
41+ position : nativeParsed ?. error ?. cursorpos ,
7042 } ,
7143 ]
7244 }
7345
74- console . dir ( { nativeParsed} , { depth : null } )
7546 const slices = nativeParsed . parse_tree . stmts . map ( s => {
7647 // if (typeof s?.stmt_location !== 'number') return undefined
7748
0 commit comments