Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement scanSync #6

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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