sqlparser-rs 0.60.0-rc2 → 0.60.0-rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +209 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# sqlparser-rs
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/sqlparser-rs)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](https://webassembly.org/)
|
|
8
|
+
[](https://github.com/apache/datafusion-sqlparser-rs)
|
|
9
|
+
|
|
10
|
+
A SQL parser for JavaScript and TypeScript, powered by [datafusion-sqlparser-rs](https://github.com/apache/datafusion-sqlparser-rs) via WebAssembly.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Parse SQL into a detailed Abstract Syntax Tree (AST)
|
|
15
|
+
- Support for 13+ SQL dialects (PostgreSQL, MySQL, SQLite, BigQuery, etc.)
|
|
16
|
+
- Full TypeScript type definitions
|
|
17
|
+
- Works in Node.js and browsers
|
|
18
|
+
- Fast and accurate parsing using the battle-tested Rust implementation
|
|
19
|
+
- Zero native dependencies
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install sqlparser-rs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { Parser, GenericDialect, PostgreSqlDialect } from 'sqlparser-rs';
|
|
31
|
+
|
|
32
|
+
// Simple parsing
|
|
33
|
+
const statements = await Parser.parse('SELECT * FROM users', new GenericDialect());
|
|
34
|
+
console.log(statements);
|
|
35
|
+
|
|
36
|
+
// With specific dialect
|
|
37
|
+
const pgStatements = await Parser.parse(
|
|
38
|
+
'SELECT * FROM users WHERE id = $1',
|
|
39
|
+
new PostgreSqlDialect()
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Format SQL
|
|
43
|
+
const formatted = await Parser.format('select * from users', new GenericDialect());
|
|
44
|
+
console.log(formatted); // "SELECT * FROM users"
|
|
45
|
+
|
|
46
|
+
// Validate SQL
|
|
47
|
+
try {
|
|
48
|
+
await Parser.validate('SELEC * FROM users', new GenericDialect());
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.log('Invalid SQL:', error.message);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### Parser
|
|
57
|
+
|
|
58
|
+
The main class for parsing SQL.
|
|
59
|
+
|
|
60
|
+
#### Static Methods
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// Parse SQL into statements
|
|
64
|
+
const statements = await Parser.parse(sql: string, dialect: Dialect): Promise<Statement[]>;
|
|
65
|
+
|
|
66
|
+
// Parse and return JSON string
|
|
67
|
+
const json = await Parser.parseToJson(sql: string, dialect: Dialect): Promise<string>;
|
|
68
|
+
|
|
69
|
+
// Parse and return formatted SQL string
|
|
70
|
+
const formatted = await Parser.parseToString(sql: string, dialect: Dialect): Promise<string>;
|
|
71
|
+
|
|
72
|
+
// Format SQL (round-trip through parser)
|
|
73
|
+
const formatted = await Parser.format(sql: string, dialect: Dialect): Promise<string>;
|
|
74
|
+
|
|
75
|
+
// Validate SQL syntax
|
|
76
|
+
const isValid = await Parser.validate(sql: string, dialect: Dialect): Promise<boolean>;
|
|
77
|
+
|
|
78
|
+
// Get list of supported dialects
|
|
79
|
+
const dialects = await Parser.getSupportedDialects(): Promise<string[]>;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Instance Methods (Builder Pattern)
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { Parser, PostgreSqlDialect } from 'sqlparser-rs';
|
|
86
|
+
|
|
87
|
+
const parser = new Parser(new PostgreSqlDialect())
|
|
88
|
+
.withRecursionLimit(50) // Set max recursion depth
|
|
89
|
+
.withOptions({ // Set parser options
|
|
90
|
+
trailingCommas: true
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Parse asynchronously
|
|
94
|
+
const statements = await parser.parseAsync('SELECT * FROM users');
|
|
95
|
+
|
|
96
|
+
// Parse synchronously (requires initWasm() first)
|
|
97
|
+
import { initWasm } from 'sqlparser-rs';
|
|
98
|
+
await initWasm();
|
|
99
|
+
const statements = parser.parseSync('SELECT * FROM users');
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Dialects
|
|
103
|
+
|
|
104
|
+
All dialects from the upstream Rust crate are supported:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import {
|
|
108
|
+
GenericDialect, // Permissive, accepts most SQL syntax
|
|
109
|
+
AnsiDialect, // ANSI SQL standard
|
|
110
|
+
MySqlDialect, // MySQL
|
|
111
|
+
PostgreSqlDialect, // PostgreSQL
|
|
112
|
+
SQLiteDialect, // SQLite
|
|
113
|
+
SnowflakeDialect, // Snowflake
|
|
114
|
+
RedshiftDialect, // Amazon Redshift
|
|
115
|
+
MsSqlDialect, // Microsoft SQL Server
|
|
116
|
+
ClickHouseDialect, // ClickHouse
|
|
117
|
+
BigQueryDialect, // Google BigQuery
|
|
118
|
+
DuckDbDialect, // DuckDB
|
|
119
|
+
DatabricksDialect, // Databricks
|
|
120
|
+
HiveDialect, // Apache Hive
|
|
121
|
+
} from 'sqlparser-rs';
|
|
122
|
+
|
|
123
|
+
// Create dialect from string
|
|
124
|
+
import { dialectFromString } from 'sqlparser-rs';
|
|
125
|
+
const dialect = dialectFromString('postgresql'); // Returns PostgreSqlDialect instance
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Error Handling
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { Parser, GenericDialect, ParserError } from 'sqlparser-rs';
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
await Parser.parse('SELEC * FROM users', new GenericDialect());
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (error instanceof ParserError) {
|
|
137
|
+
console.log('Parse error:', error.message);
|
|
138
|
+
if (error.location) {
|
|
139
|
+
console.log(`At line ${error.location.line}, column ${error.location.column}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### AST Types
|
|
146
|
+
|
|
147
|
+
Full TypeScript types are provided for the AST:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import type { Statement, Query, Expr, Ident, ObjectName } from 'sqlparser-rs';
|
|
151
|
+
|
|
152
|
+
const statements: Statement[] = await Parser.parse('SELECT 1', new GenericDialect());
|
|
153
|
+
|
|
154
|
+
// Statement is a discriminated union type
|
|
155
|
+
for (const stmt of statements) {
|
|
156
|
+
if ('Query' in stmt) {
|
|
157
|
+
const query: Query = stmt.Query;
|
|
158
|
+
console.log('Found SELECT query');
|
|
159
|
+
} else if ('Insert' in stmt) {
|
|
160
|
+
console.log('Found INSERT statement');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Building from Source
|
|
166
|
+
|
|
167
|
+
### Prerequisites
|
|
168
|
+
|
|
169
|
+
- Rust toolchain (1.70+)
|
|
170
|
+
- wasm-pack (`cargo install wasm-pack`)
|
|
171
|
+
- Node.js (16+)
|
|
172
|
+
|
|
173
|
+
### Build
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
# Build everything
|
|
177
|
+
./scripts/build.sh
|
|
178
|
+
|
|
179
|
+
# Or step by step:
|
|
180
|
+
# 1. Build WASM
|
|
181
|
+
wasm-pack build --target nodejs --out-dir ts/wasm
|
|
182
|
+
|
|
183
|
+
# 2. Build TypeScript
|
|
184
|
+
cd ts
|
|
185
|
+
npm install
|
|
186
|
+
npm run build
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Run Tests
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
cd ts
|
|
193
|
+
npm test
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Version
|
|
197
|
+
|
|
198
|
+
| This package | sqlparser-rs |
|
|
199
|
+
|--------------|--------------|
|
|
200
|
+
| 0.60.0-x | 0.60.0 |
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
Apache-2.0, matching the upstream Rust crate.
|
|
205
|
+
|
|
206
|
+
## Related Projects
|
|
207
|
+
|
|
208
|
+
- [datafusion-sqlparser-rs](https://github.com/apache/datafusion-sqlparser-rs) - The Rust SQL parser this package wraps
|
|
209
|
+
- [Apache DataFusion](https://github.com/apache/datafusion) - Query execution framework using sqlparser-rs
|
package/package.json
CHANGED