Repository navigation
Add opt-in psql parsing #143
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { | |
| WindowDefinition, | ||
| } from "./Select"; | ||
| import { Default } from "./Insert"; | ||
| import { PsqlIdentifier, PsqlVariable } from "./Psql"; | ||
| import { | ||
| PostgresqlOperator, | ||
| PostgresqlOperatorExpr, | ||
|
|
@@ -36,6 +37,7 @@ export type AllExprNodes = | |
| | ArraySliceSpecifier; | ||
|
|
||
| export type Expr = | ||
| | PsqlVariable | ||
| | ListExpr | ||
| | ParenExpr | ||
| | BinaryExpr | ||
|
|
@@ -436,7 +438,9 @@ export interface ArraySliceSpecifier extends BaseNode { | |
| to?: Expr; | ||
| } | ||
|
|
||
| export interface Identifier extends BaseNode { | ||
| export type Identifier = PlainIdentifier | PsqlIdentifier; | ||
|
|
||
| export interface PlainIdentifier extends BaseNode { | ||
| type: "identifier"; | ||
|
Comment on lines
-439
to
444
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't good. We should keep the In here, we likely don't want to change the I'm unsure about the best direction where we should go. I guess the main intention in here is to allow Psql identifiers in all places where previously only SQL identifiers were allowed. But frankly, I'm not really even sure if we should allow that in the first place. |
||
| text: string; | ||
| name: string; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { BaseNode } from "./Base"; | ||
| import { Statement } from "./Statement"; | ||
|
|
||
| export interface PsqlCommand extends BaseNode { | ||
| type: "psql_command"; | ||
| text: string; | ||
| } | ||
|
|
||
| export interface PsqlQuery extends BaseNode { | ||
| type: "psql_query"; | ||
| query: Statement; | ||
| terminator: string; | ||
| } | ||
|
|
||
| export interface PsqlVariable extends BaseNode { | ||
| type: "psql_variable"; | ||
| text: string; | ||
| } | ||
|
|
||
| export interface PsqlIdentifier extends BaseNode { | ||
| type: "psql_identifier"; | ||
| name: string; | ||
| text: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { alterActionMap } from "./alter_action"; | ||
| import { show } from "../show"; | ||
| import { alterTableMap } from "./alter_table"; | ||
| import { analyzeMap } from "./analyze"; | ||
| import { baseMap } from "./base"; | ||
|
|
@@ -48,6 +49,8 @@ import { sqliteMap } from "./dialects/sqlite"; | |
| import { mysqlMap } from "./dialects/mysql"; | ||
|
|
||
| export const transformMap: FullTransformMap<string> = { | ||
| psql_command: (node) => node.text, | ||
| psql_query: (node) => show(node.query) + node.terminator, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not good. This file should only import various |
||
| ...baseMap, | ||
|
|
||
| // SELECT, INSERT, UPDATE, DELETE, TRUNCATE, MERGE | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { parse, show } from "../src/main"; | ||
| import { includeAll } from "./test_utils"; | ||
|
|
||
| describe("psql scripts", () => { | ||
| it.each([ | ||
| "\\set id 1\nSELECT :id, :'name' FROM :\"table\";\n\\quit\n", | ||
| "\\if :enabled\nSELECT 1\n\\gset result_\n\\else\nSELECT 2;\n\\endif\n", | ||
| "\\restrict abc123\nSELECT 1;\n\\unrestrict abc123\n", | ||
| "-- heading\nSELECT 1; -- trailing\n\\echo 'done'\n", | ||
| "SELECT 1\n\\gexec\nSELECT 2;", | ||
| "SELECT 1", | ||
| "\\echo 'two words with ''quotes'''\n", | ||
| '\\echo "two words"\n', | ||
| "\\echo `printf hi`\n", | ||
| "\\unknown value\n", | ||
| "SELECT :id, :'name', :\"table\", :{?enabled};", | ||
| ])("round-trips %s", (sql) => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test looks kinda horrendous. All these escape codes make my head hurt. Plus, it looks like we're testing all kinds of different things in this single I think we should split this up to several separate tests, each with a clear description of what exactly we're testing. Maybe we could use |
||
| const tree = parse(sql, { dialect: "postgresql", psql: true, ...includeAll }); | ||
| expect(show(tree)).toBe(sql); | ||
| expect(tree.psql).toBe(true); | ||
| }); | ||
|
|
||
| it.each(["SELECT\n\\echo partial\n1;", "SELECT 1 \\gset", "\\echo hi \\\\ SELECT 1;"])( | ||
| "rejects unsupported command placement or syntax: %s", | ||
| (sql) => { | ||
| expect(() => parse(sql, { dialect: "postgresql", psql: true })).toThrow(); | ||
| } | ||
| ); | ||
|
|
||
| it("requires opt-in and the PostgreSQL dialect", () => { | ||
| expect(() => parse("\\quit", { dialect: "postgresql" })).toThrow(); | ||
| expect(() => parse("\\quit", { dialect: "sqlite", psql: true })).toThrow(); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better approach would be to treat
psqlas a separate SQL dialect, not as a flag that's only applicable to PostgreSQL.