Skip to content

Commit

Permalink
feat: implement scanSync (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
markandrus authored Nov 7, 2024
1 parent c19b906 commit adc8277
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 1 deletion.
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")",
"<!@(pwd)/libpg_query"
"<!@(pwd)/libpg_query",
"<!@(pwd)/libpg_query/vendor",
],
"cflags!": [
"-fno-exceptions"
Expand Down
5 changes: 5 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function::New(env, FingerprintAsync)
);

exports.Set(
Napi::String::New(env, "scanSync"),
Napi::Function::New(env, ScanSync)
);

exports.Set(
Napi::String::New(env, "splitWithScannerSync"),
Napi::Function::New(env, SplitWithScannerSync)
Expand Down
15 changes: 15 additions & 0 deletions src/lib/binding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bindings from 'bindings'
import type { Node } from './ast'
import type { TokenKind, KeywordKind } from './tokens'

const loadAddon = () => bindings('queryparser')

Expand All @@ -19,6 +20,7 @@ let PgQuery: {
callback: (err: Error | null, result: string) => void,
) => void
fingerprintSync: (query: string) => string
scanSync: (query: string) => ScanResult
splitWithScannerSync: (query: string) => {
location: number
length: number
Expand All @@ -30,6 +32,15 @@ export type ParseResult = {
version: number
}

export interface Token {
kind: TokenKind
start: number
end: number
keyword: KeywordKind
}

export type ScanResult = Token[]

export function parseQuery(query: string) {
return new Promise<ParseResult>((resolve, reject) => {
PgQuery ??= loadAddon()
Expand Down Expand Up @@ -69,6 +80,10 @@ export function fingerprintSync(query: string): string {
return (PgQuery ??= loadAddon()).fingerprintSync(query)
}

export function scanSync(query: string) {
return (PgQuery ??= loadAddon()).scanSync(query)
}

export function splitWithScannerSync(query: string) {
return (PgQuery ??= loadAddon()).splitWithScannerSync(query)
}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './ast.js'
export * from './binding.js'
export * from './node.js'
export * from './select.js'
export * from './tokens.js'
export * from './walk.js'
Loading

0 comments on commit adc8277

Please sign in to comment.