@@ -204,3 +204,193 @@ export const parseQueryDetailed = awaitInit(async (query) => {
204204 wasmModule . _free ( queryPtr ) ;
205205 }
206206} ) ;
207+
208+ export function parseQuerySync ( query ) {
209+ if ( ! wasmModule ) {
210+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
211+ }
212+ const queryPtr = stringToPtr ( query ) ;
213+ let resultPtr ;
214+ let protobufPtr ;
215+
216+ try {
217+ resultPtr = wasmModule . _wasm_parse_query ( queryPtr ) ;
218+ const resultStr = ptrToString ( resultPtr ) ;
219+
220+ if ( resultStr . startsWith ( 'syntax error' ) || resultStr . startsWith ( 'deparse error' ) || resultStr . includes ( 'ERROR' ) ) {
221+ throw new Error ( resultStr ) ;
222+ }
223+
224+ const parseResult = JSON . parse ( resultStr ) ;
225+
226+ const protobufLen = wasmModule . _wasm_get_protobuf_len ( queryPtr ) ;
227+ if ( protobufLen > 0 ) {
228+ const lenPtr = wasmModule . _malloc ( 4 ) ;
229+ wasmModule . HEAPU32 [ lenPtr >> 2 ] = 0 ;
230+ protobufPtr = wasmModule . _wasm_parse_query_protobuf ( queryPtr , lenPtr ) ;
231+ const actualLen = wasmModule . HEAPU32 [ lenPtr >> 2 ] ;
232+ wasmModule . _free ( lenPtr ) ;
233+
234+ if ( actualLen > 0 ) {
235+ const protobufData = new Uint8Array ( wasmModule . HEAPU8 . buffer , protobufPtr , actualLen ) ;
236+ const protobufCopy = new Uint8Array ( protobufData ) ;
237+ protobufCache . set ( parseResult , protobufCopy ) ;
238+ }
239+ }
240+
241+ return parseResult ;
242+ } finally {
243+ wasmModule . _free ( queryPtr ) ;
244+ if ( resultPtr ) {
245+ wasmModule . _wasm_free_string ( resultPtr ) ;
246+ }
247+ if ( protobufPtr ) {
248+ wasmModule . _wasm_free_string ( protobufPtr ) ;
249+ }
250+ }
251+ }
252+
253+ export function deparseSync ( parseTree ) {
254+ if ( ! wasmModule ) {
255+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
256+ }
257+ const protobufData = protobufCache . get ( parseTree ) ;
258+
259+ if ( ! protobufData ) {
260+ throw new Error ( 'deparse error: No protobuf data found for parse tree. Make sure to use the result from parseQuery directly.' ) ;
261+ }
262+
263+ const dataPtr = wasmModule . _malloc ( protobufData . length ) ;
264+ let resultPtr ;
265+
266+ try {
267+ wasmModule . HEAPU8 . set ( protobufData , dataPtr ) ;
268+ resultPtr = wasmModule . _wasm_deparse_protobuf ( dataPtr , protobufData . length ) ;
269+ const resultStr = ptrToString ( resultPtr ) ;
270+
271+ if ( resultStr . startsWith ( 'syntax error' ) || resultStr . startsWith ( 'deparse error' ) || resultStr . includes ( 'ERROR' ) ) {
272+ throw new Error ( resultStr ) ;
273+ }
274+
275+ return resultStr ;
276+ } finally {
277+ wasmModule . _free ( dataPtr ) ;
278+ if ( resultPtr ) {
279+ wasmModule . _wasm_free_string ( resultPtr ) ;
280+ }
281+ }
282+ }
283+
284+ export function parsePlPgSQLSync ( query ) {
285+ if ( ! wasmModule ) {
286+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
287+ }
288+ const queryPtr = stringToPtr ( query ) ;
289+ let resultPtr ;
290+
291+ try {
292+ resultPtr = wasmModule . _wasm_parse_plpgsql ( queryPtr ) ;
293+ const resultStr = ptrToString ( resultPtr ) ;
294+
295+ if ( resultStr . startsWith ( 'syntax error' ) || resultStr . startsWith ( 'deparse error' ) || resultStr . includes ( 'ERROR' ) ) {
296+ throw new Error ( resultStr ) ;
297+ }
298+
299+ return JSON . parse ( resultStr ) ;
300+ } finally {
301+ wasmModule . _free ( queryPtr ) ;
302+ if ( resultPtr ) {
303+ wasmModule . _wasm_free_string ( resultPtr ) ;
304+ }
305+ }
306+ }
307+
308+ export function fingerprintSync ( query ) {
309+ if ( ! wasmModule ) {
310+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
311+ }
312+ const queryPtr = stringToPtr ( query ) ;
313+ let resultPtr ;
314+
315+ try {
316+ resultPtr = wasmModule . _wasm_fingerprint ( queryPtr ) ;
317+ const resultStr = ptrToString ( resultPtr ) ;
318+
319+ if ( resultStr . startsWith ( 'syntax error' ) || resultStr . startsWith ( 'deparse error' ) || resultStr . includes ( 'ERROR' ) ) {
320+ throw new Error ( resultStr ) ;
321+ }
322+
323+ return resultStr ;
324+ } finally {
325+ wasmModule . _free ( queryPtr ) ;
326+ if ( resultPtr ) {
327+ wasmModule . _wasm_free_string ( resultPtr ) ;
328+ }
329+ }
330+ }
331+
332+ export function normalizeSync ( query ) {
333+ if ( ! wasmModule ) {
334+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
335+ }
336+ const queryPtr = stringToPtr ( query ) ;
337+ let resultPtr ;
338+
339+ try {
340+ resultPtr = wasmModule . _wasm_normalize_query ( queryPtr ) ;
341+ const resultStr = ptrToString ( resultPtr ) ;
342+
343+ if ( resultStr . startsWith ( 'syntax error' ) || resultStr . startsWith ( 'deparse error' ) || resultStr . includes ( 'ERROR' ) ) {
344+ throw new Error ( resultStr ) ;
345+ }
346+
347+ return resultStr ;
348+ } finally {
349+ wasmModule . _free ( queryPtr ) ;
350+ if ( resultPtr ) {
351+ wasmModule . _wasm_free_string ( resultPtr ) ;
352+ }
353+ }
354+ }
355+
356+ export function parseQueryDetailedSync ( query ) {
357+ if ( ! wasmModule ) {
358+ throw new Error ( 'WASM module not initialized. Call an async method first to initialize.' ) ;
359+ }
360+ const queryPtr = stringToPtr ( query ) ;
361+ let resultPtr ;
362+
363+ try {
364+ resultPtr = wasmModule . _wasm_parse_query_detailed ( queryPtr ) ;
365+
366+ const hasError = wasmModule . HEAPU32 [ resultPtr >> 2 ] ;
367+
368+ if ( hasError ) {
369+ const messagePtr = wasmModule . HEAPU32 [ ( resultPtr + 4 ) >> 2 ] ;
370+ const funcnamePtr = wasmModule . HEAPU32 [ ( resultPtr + 8 ) >> 2 ] ;
371+ const filenamePtr = wasmModule . HEAPU32 [ ( resultPtr + 12 ) >> 2 ] ;
372+ const lineno = wasmModule . HEAPU32 [ ( resultPtr + 16 ) >> 2 ] ;
373+ const cursorpos = wasmModule . HEAPU32 [ ( resultPtr + 20 ) >> 2 ] ;
374+ const contextPtr = wasmModule . HEAPU32 [ ( resultPtr + 24 ) >> 2 ] ;
375+
376+ const error = {
377+ message : messagePtr ? ptrToString ( messagePtr ) : '' ,
378+ funcname : funcnamePtr ? ptrToString ( funcnamePtr ) : null ,
379+ filename : filenamePtr ? ptrToString ( filenamePtr ) : null ,
380+ lineno : lineno ,
381+ cursorpos : cursorpos ,
382+ context : contextPtr ? ptrToString ( contextPtr ) : null
383+ } ;
384+
385+ wasmModule . _wasm_free_detailed_result ( resultPtr ) ;
386+ throw new Error ( error . message ) ;
387+ } else {
388+ const dataPtr = wasmModule . HEAPU32 [ ( resultPtr + 28 ) >> 2 ] ;
389+ const result = JSON . parse ( ptrToString ( dataPtr ) ) ;
390+ wasmModule . _wasm_free_detailed_result ( resultPtr ) ;
391+ return result ;
392+ }
393+ } finally {
394+ wasmModule . _free ( queryPtr ) ;
395+ }
396+ }
0 commit comments