Skip to content

Commit 5f27caf

Browse files
authored
Merge pull request #117 from launchql/fix/cleanup
Fix/cleanup
2 parents 865f337 + fdee77d commit 5f27caf

32 files changed

+139
-394
lines changed

β€ŽBUILD_SIZE_REPORT.mdβ€Ž

Lines changed: 0 additions & 53 deletions
This file was deleted.

β€ŽREADME.mdβ€Ž

Lines changed: 25 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ Built to power [pgsql-parser](https://github.com/pyramation/pgsql-parser), this
3838
npm install libpg-query
3939
```
4040

41+
## Example
42+
43+
```typescript
44+
import { parse } from 'libpg-query';
45+
46+
const result = await parse('SELECT * FROM users WHERE active = true');
47+
// {"version":170004,"stmts":[{"stmt":{"SelectStmt":{"targetList":[{"ResTarget" ... "op":"SETOP_NONE"}}}]}
48+
```
49+
50+
## Versions
51+
52+
Our latest is built with `17-latest` branch from libpg_query
53+
54+
| PG Major Version | libpg_query | npm dist-tag
55+
|--------------------------|-------------|---------|
56+
| 17 | 17-6.1.0 | [`pg17`](https://www.npmjs.com/package/libpg-query/v/latest)
57+
| 16 | 16-5.2.0 | [`pg16`](https://www.npmjs.com/package/libpg-query/v/pg16)
58+
| 15 | 15-4.2.4 | [`pg15`](https://www.npmjs.com/package/libpg-query/v/pg15)
59+
| 14 | 14-3.0.0 | [`pg14`](https://www.npmjs.com/package/libpg-query/v/pg14)
60+
| 13 | 13-2.2.0 | [`pg13`](https://www.npmjs.com/package/libpg-query/v/pg13)
61+
4162
## Usage
4263

4364
### `parse(query: string): Promise<ParseResult>`
@@ -62,124 +83,8 @@ const result = parseSync('SELECT * FROM users WHERE active = true');
6283
// Returns: ParseResult - parsed query object
6384
```
6485

65-
### `parsePlPgSQL(funcsSql: string): Promise<ParseResult>`
66-
67-
Parses the contents of a PL/pgSQL function from a `CREATE FUNCTION` declaration. Returns a Promise for the parse tree.
68-
69-
```typescript
70-
import { parsePlPgSQL } from 'libpg-query';
71-
72-
const functionSql = `
73-
CREATE FUNCTION get_user_count() RETURNS integer AS $$
74-
BEGIN
75-
RETURN (SELECT COUNT(*) FROM users);
76-
END;
77-
$$ LANGUAGE plpgsql;
78-
`;
79-
80-
const result = await parsePlPgSQL(functionSql);
81-
```
82-
83-
### `parsePlPgSQLSync(funcsSql: string): ParseResult`
84-
85-
Synchronous version of PL/pgSQL parsing.
86-
87-
```typescript
88-
import { parsePlPgSQLSync } from 'libpg-query';
89-
90-
const result = parsePlPgSQLSync(functionSql);
91-
```
92-
93-
### `deparse(parseTree: ParseResult): Promise<string>`
94-
95-
Converts a parse tree back to SQL string. Returns a Promise for the SQL string.
96-
97-
```typescript
98-
import { parse, deparse } from 'libpg-query';
99-
100-
const parseTree = await parse('SELECT * FROM users WHERE active = true');
101-
const sql = await deparse(parseTree);
102-
// Returns: string - reconstructed SQL query
103-
```
104-
105-
### `deparseSync(parseTree: ParseResult): string`
106-
107-
Synchronous version that converts a parse tree back to SQL string directly.
108-
109-
```typescript
110-
import { parseSync, deparseSync } from 'libpg-query';
111-
112-
const parseTree = parseSync('SELECT * FROM users WHERE active = true');
113-
const sql = deparseSync(parseTree);
114-
// Returns: string - reconstructed SQL query
115-
```
116-
117-
### `fingerprint(sql: string): Promise<string>`
118-
119-
Generates a unique fingerprint for a SQL query that can be used for query identification and caching. Returns a Promise for a 16-character fingerprint string.
120-
121-
```typescript
122-
import { fingerprint } from 'libpg-query';
123-
124-
const fp = await fingerprint('SELECT * FROM users WHERE active = $1');
125-
// Returns: string - unique 16-character fingerprint (e.g., "50fde20626009aba")
126-
```
127-
128-
### `fingerprintSync(sql: string): string`
12986

130-
Synchronous version that generates a unique fingerprint for a SQL query directly.
131-
132-
```typescript
133-
import { fingerprintSync } from 'libpg-query';
134-
135-
const fp = fingerprintSync('SELECT * FROM users WHERE active = $1');
136-
// Returns: string - unique 16-character fingerprint
137-
```
138-
139-
### `normalize(sql: string): Promise<string>`
140-
141-
Normalizes a SQL query by removing comments, standardizing whitespace, and converting to a canonical form. Returns a Promise for the normalized SQL string.
142-
143-
```typescript
144-
import { normalize } from 'libpg-query';
145-
146-
const normalized = await normalize('SELECT * FROM users WHERE active = true');
147-
// Returns: string - normalized SQL query
148-
```
149-
150-
### `normalizeSync(sql: string): string`
151-
152-
Synchronous version that normalizes a SQL query directly.
153-
154-
```typescript
155-
import { normalizeSync } from 'libpg-query';
156-
157-
const normalized = normalizeSync('SELECT * FROM users WHERE active = true');
158-
// Returns: string - normalized SQL query
159-
```
160-
161-
### `scan(sql: string): Promise<ScanResult>`
162-
163-
Scans (tokenizes) a SQL query and returns detailed information about each token. Returns a Promise for a ScanResult containing all tokens with their positions, types, and classifications.
164-
165-
```typescript
166-
import { scan } from 'libpg-query';
167-
168-
const result = await scan('SELECT * FROM users WHERE id = $1');
169-
// Returns: ScanResult - detailed tokenization information
170-
console.log(result.tokens[0]); // { start: 0, end: 6, text: "SELECT", tokenType: 651, tokenName: "UNKNOWN", keywordKind: 4, keywordName: "RESERVED_KEYWORD" }
171-
```
172-
173-
### `scanSync(sql: string): ScanResult`
174-
175-
Synchronous version that scans (tokenizes) a SQL query directly.
176-
177-
```typescript
178-
import { scanSync } from 'libpg-query';
179-
180-
const result = scanSync('SELECT * FROM users WHERE id = $1');
181-
// Returns: ScanResult - detailed tokenization information
182-
```
87+
**Note:** If you need additional functionality like `fingerprint`, `scan`, `deparse`, or `normalize`, check out the full package (`@pgsql/parser`) in the [./full](https://github.com/launchql/libpg-query-node/tree/17-latest/libpg-query) folder of the repo.
18388

18489
### Initialization
18590

@@ -190,40 +95,36 @@ The library provides both async and sync methods. Async methods handle initializ
19095
Async methods handle initialization automatically and are always safe to use:
19196

19297
```typescript
193-
import { parse, deparse, scan } from 'libpg-query';
98+
import { parse } from 'libpg-query';
19499

195100
// These handle initialization automatically
196101
const result = await parse('SELECT * FROM users');
197-
const sql = await deparse(result);
198-
const tokens = await scan('SELECT * FROM users');
199102
```
200103

201104
#### Sync Methods
202105

203106
Sync methods require explicit initialization using `loadModule()`:
204107

205108
```typescript
206-
import { loadModule, parseSync, scanSync } from 'libpg-query';
109+
import { loadModule, parseSync } from 'libpg-query';
207110

208111
// Initialize first
209112
await loadModule();
210113

211114
// Now safe to use sync methods
212115
const result = parseSync('SELECT * FROM users');
213-
const tokens = scanSync('SELECT * FROM users');
214116
```
215117

216118
### `loadModule(): Promise<void>`
217119

218120
Explicitly initializes the WASM module. Required before using any sync methods.
219121

220122
```typescript
221-
import { loadModule, parseSync, scanSync } from 'libpg-query';
123+
import { loadModule, parseSync } from 'libpg-query';
222124

223125
// Initialize before using sync methods
224126
await loadModule();
225127
const result = parseSync('SELECT * FROM users');
226-
const tokens = scanSync('SELECT * FROM users');
227128
```
228129

229130
Note: We recommend using async methods as they handle initialization automatically. Use sync methods only when necessary, and always call `loadModule()` first.
@@ -243,20 +144,6 @@ interface Statement {
243144
query: string;
244145
}
245146

246-
interface ScanResult {
247-
version: number;
248-
tokens: ScanToken[];
249-
}
250-
251-
interface ScanToken {
252-
start: number; // Starting position in the SQL string
253-
end: number; // Ending position in the SQL string
254-
text: string; // The actual token text
255-
tokenType: number; // Numeric token type identifier
256-
tokenName: string; // Human-readable token type name
257-
keywordKind: number; // Numeric keyword classification
258-
keywordName: string; // Human-readable keyword classification
259-
}
260147
```
261148

262149
**Note:** The return value is an array, as multiple queries may be provided in a single string (semicolon-delimited, as PostgreSQL expects).
@@ -316,25 +203,6 @@ pnpm run test
316203
pnpm run clean && pnpm run build && pnpm run test
317204
```
318205

319-
320-
321-
## Versions
322-
323-
Our latest is built with `17-latest` branch from libpg_query
324-
325-
326-
| PG Major Version | libpg_query | npm dist-tag
327-
|--------------------------|-------------|---------|
328-
| 17 | 17-6.1.0 | [`pg17`](https://www.npmjs.com/package/libpg-query/v/latest)
329-
| 16 | 16-5.2.0 | [`pg16`](https://www.npmjs.com/package/libpg-query/v/pg16)
330-
| 15 | 15-4.2.4 | [`pg15`](https://www.npmjs.com/package/libpg-query/v/pg15)
331-
| 14 | 14-3.0.0 | [`pg14`](https://www.npmjs.com/package/libpg-query/v/pg14)
332-
| 13 | 13-2.2.0 | [`pg13`](https://www.npmjs.com/package/libpg-query/v/pg13)
333-
| 12 | (n/a) | |
334-
| 11 | (n/a) | |
335-
| 10 | 10-latest | | `@1.3.1` ([tree](https://github.com/pyramation/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
336-
337-
338206
## Troubleshooting
339207

340208
### Common Issues
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

β€Žlibpg-query/package.jsonβ€Ž renamed to β€Žfull/package.jsonβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "libpg-query",
3-
"version": "17.3.3",
2+
"name": "@pgsql/parser",
3+
"version": "17.5.0",
44
"description": "The real PostgreSQL query parser",
55
"homepage": "https://github.com/launchql/libpg-query-node",
66
"main": "./wasm/index.cjs",

0 commit comments

Comments
Β (0)