Skip to content
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
12 changes: 7 additions & 5 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DefaultSqlTooltipRenders,
defaultSqlHoverTheme,
NodeSqlParser,
type SupportedDialects,
sqlExtension,
} from "../src/index.js";
import { type Schema, tableTooltipRenderer } from "./custom-renderers.js";
Expand Down Expand Up @@ -146,9 +147,9 @@ const getKeywordDocs = async () => {
};
};

const setDatabase = StateEffect.define<string>();
const setDatabase = StateEffect.define<SupportedDialects>();

const databaseField = StateField.define({
const databaseField = StateField.define<SupportedDialects>({
create: () => "PostgreSQL",
update: (prevValue, transaction) => {
for (const effect of transaction.effects) {
Expand All @@ -162,7 +163,7 @@ const databaseField = StateField.define({

// Initialize the SQL editor
function initializeEditor() {
// Use the same parser for linter and gutter
// Use the same parser
const parser = new NodeSqlParser({
getParserOptions: (state: EditorState) => {
return {
Expand Down Expand Up @@ -233,6 +234,7 @@ function initializeEditor() {
table: tableTooltipRenderer,
},
theme: defaultSqlHoverTheme("light"),
parser,
},
}),
dialect.language.data.of({
Expand Down Expand Up @@ -337,15 +339,15 @@ function setupExampleButtons() {
});
}

function getDialect(state: EditorState): string {
function getDialect(state: EditorState): SupportedDialects {
return state.field(databaseField);
}

function setupDialectSelect() {
const select = document.querySelector("#dialect-select");
if (select) {
select.addEventListener("change", (e) => {
const value = (e.target as HTMLSelectElement).value;
const value = (e.target as HTMLSelectElement).value as SupportedDialects;
editor.dispatch({
effects: [setDatabase.of(value)],
});
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"default": "./dist/index.js"
}
},
"./dialects": {
"import": {
"types": "./dist/dialects/index.d.ts",
"default": "./dist/dialects/index.js"
}
},
"./data/common-keywords.json": "./src/data/common-keywords.json",
"./data/duckdb-keywords.json": "./src/data/duckdb-keywords.json"
},
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import * as dialects from "../dialects";
import * as exports from "../index";

describe("index.ts exports", () => {
Expand Down Expand Up @@ -34,3 +35,14 @@ describe("keywords", async () => {
}
});
});

describe("dialects.ts exports", () => {
it("should not change unexpectedly", () => {
const sortedExports = Object.keys(dialects).sort();
expect(sortedExports).toMatchInlineSnapshot(`
[
"DuckDBDialect",
]
`);
});
});
1 change: 1 addition & 0 deletions src/dialects/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DuckDBDialect } from "./duckdb/duckdb.js";
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type {
SqlKeywordInfo,
} from "./sql/hover.js";
export { DefaultSqlTooltipRenders, defaultSqlHoverTheme, sqlHover } from "./sql/hover.js";
export { NodeSqlParser } from "./sql/parser.js";
export { NodeSqlParser, type SupportedDialects } from "./sql/parser.js";
export type { SqlStatement } from "./sql/structure-analyzer.js";
export { SqlStructureAnalyzer } from "./sql/structure-analyzer.js";
export type { SqlGutterConfig } from "./sql/structure-extension.js";
Expand Down
28 changes: 26 additions & 2 deletions src/sql/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { debug } from "../debug.js";
import { lazy } from "../utils.js";
import type { SqlParseError, SqlParseResult, SqlParser } from "./types.js";

interface ParserOption extends Option {
database: SupportedDialects;
}

interface NodeSqlParserOptions {
getParserOptions?: (state: EditorState) => Option;
getParserOptions?: (state: EditorState) => ParserOption;
}

interface NodeSqlParserResult extends SqlParseResult {
Expand Down Expand Up @@ -97,7 +101,7 @@ export class NodeSqlParser implements SqlParser {

// Otherwise, try standard parsing with PostgreSQL dialect
try {
const postgresOptions = { ...parserOptions, database: "PostgresQL" };
const postgresOptions = { ...parserOptions, database: "PostgreSQL" };
const ast = parser.astify(sql, postgresOptions);
return {
success: true,
Expand Down Expand Up @@ -202,3 +206,23 @@ export class NodeSqlParser implements SqlParser {
}
}
}

/**
* https://github.com/taozhi8833998/node-sql-parser?tab=readme-ov-file#supported-database-sql-syntax
* While DuckDB is not supported in the library, we perform some special handling for it and treat it as PostgreSQL.
*/
export type SupportedDialects =
| "Athena"
| "BigQuery"
| "DB2"
| "Hive"
| "MariaDB"
| "MySQL"
| "PostgreSQL"
| "DuckDB"
| "Redshift"
| "Sqlite"
| "TransactSQL"
| "FlinkSQL"
| "Snowflake"
| "Noql";