Skip to content

Commit 83488d9

Browse files
committed
add types for dialect
1 parent 9bbff70 commit 83488d9

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

demo/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DefaultSqlTooltipRenders,
99
defaultSqlHoverTheme,
1010
NodeSqlParser,
11+
type SupportedDialects,
1112
sqlExtension,
1213
} from "../src/index.js";
1314
import { type Schema, tableTooltipRenderer } from "./custom-renderers.js";
@@ -146,9 +147,9 @@ const getKeywordDocs = async () => {
146147
};
147148
};
148149

149-
const setDatabase = StateEffect.define<string>();
150+
const setDatabase = StateEffect.define<SupportedDialects>();
150151

151-
const databaseField = StateField.define({
152+
const databaseField = StateField.define<SupportedDialects>({
152153
create: () => "PostgreSQL",
153154
update: (prevValue, transaction) => {
154155
for (const effect of transaction.effects) {
@@ -162,7 +163,7 @@ const databaseField = StateField.define({
162163

163164
// Initialize the SQL editor
164165
function initializeEditor() {
165-
// Use the same parser for linter and gutter
166+
// Use the same parser
166167
const parser = new NodeSqlParser({
167168
getParserOptions: (state: EditorState) => {
168169
return {
@@ -233,6 +234,7 @@ function initializeEditor() {
233234
table: tableTooltipRenderer,
234235
},
235236
theme: defaultSqlHoverTheme("light"),
237+
parser,
236238
},
237239
}),
238240
dialect.language.data.of({
@@ -337,15 +339,15 @@ function setupExampleButtons() {
337339
});
338340
}
339341

340-
function getDialect(state: EditorState): string {
342+
function getDialect(state: EditorState): SupportedDialects {
341343
return state.field(databaseField);
342344
}
343345

344346
function setupDialectSelect() {
345347
const select = document.querySelector("#dialect-select");
346348
if (select) {
347349
select.addEventListener("change", (e) => {
348-
const value = (e.target as HTMLSelectElement).value;
350+
const value = (e.target as HTMLSelectElement).value as SupportedDialects;
349351
editor.dispatch({
350352
effects: [setDatabase.of(value)],
351353
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type {
88
SqlKeywordInfo,
99
} from "./sql/hover.js";
1010
export { DefaultSqlTooltipRenders, defaultSqlHoverTheme, sqlHover } from "./sql/hover.js";
11-
export { NodeSqlParser } from "./sql/parser.js";
11+
export { NodeSqlParser, type SupportedDialects } from "./sql/parser.js";
1212
export type { SqlStatement } from "./sql/structure-analyzer.js";
1313
export { SqlStructureAnalyzer } from "./sql/structure-analyzer.js";
1414
export type { SqlGutterConfig } from "./sql/structure-extension.js";

src/sql/parser.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { debug } from "../debug.js";
44
import { lazy } from "../utils.js";
55
import type { SqlParseError, SqlParseResult, SqlParser } from "./types.js";
66

7+
interface ParserOption extends Option {
8+
database: SupportedDialects;
9+
}
10+
711
interface NodeSqlParserOptions {
8-
getParserOptions?: (state: EditorState) => Option;
12+
getParserOptions?: (state: EditorState) => ParserOption;
913
}
1014

1115
interface NodeSqlParserResult extends SqlParseResult {
@@ -97,7 +101,7 @@ export class NodeSqlParser implements SqlParser {
97101

98102
// Otherwise, try standard parsing with PostgreSQL dialect
99103
try {
100-
const postgresOptions = { ...parserOptions, database: "PostgresQL" };
104+
const postgresOptions = { ...parserOptions, database: "PostgreSQL" };
101105
const ast = parser.astify(sql, postgresOptions);
102106
return {
103107
success: true,
@@ -202,3 +206,23 @@ export class NodeSqlParser implements SqlParser {
202206
}
203207
}
204208
}
209+
210+
/**
211+
* https://github.com/taozhi8833998/node-sql-parser?tab=readme-ov-file#supported-database-sql-syntax
212+
* While DuckDB is not supported in the library, we perform some special handling for it and treat it as PostgreSQL.
213+
*/
214+
export type SupportedDialects =
215+
| "Athena"
216+
| "BigQuery"
217+
| "DB2"
218+
| "Hive"
219+
| "MariaDB"
220+
| "MySQL"
221+
| "PostgreSQL"
222+
| "DuckDB"
223+
| "Redshift"
224+
| "Sqlite"
225+
| "TransactSQL"
226+
| "FlinkSQL"
227+
| "Snowflake"
228+
| "Noql";

0 commit comments

Comments
 (0)