-
Notifications
You must be signed in to change notification settings - Fork 20
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
Update pg-query-emscripten to 5.1.0 #567
Merged
kristiandupont
merged 1 commit into
kristiandupont:main
from
valentin-nemcev:vn/update-pg-query-emscripten
Nov 16, 2024
+42
−29
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import jp from "jsonpath"; | ||
import pgQuery from "pg-query-emscripten"; | ||
import { last } from "ramda"; | ||
|
||
let pgQueryInstance: any; | ||
const getPgQuery = async () => { | ||
if (!pgQueryInstance) { | ||
const { default: pgQueryModule } = await import( | ||
"pg-query-emscripten/pg_query_wasm" | ||
); | ||
pgQueryInstance = await new pgQueryModule(); | ||
} | ||
return pgQueryInstance; | ||
}; | ||
|
||
export type ViewReference = { | ||
viewColumn: string; | ||
source: | ||
|
@@ -21,8 +31,8 @@ function parseSelectStmt( | |
if (selectAst.larg && selectAst.rarg) { | ||
// This is a UNION, INTERSECT, or EXCEPT operation | ||
return [ | ||
...parseSelectStmt(selectAst.larg.SelectStmt, defaultSchema, aliases), | ||
...parseSelectStmt(selectAst.rarg.SelectStmt, defaultSchema, aliases), | ||
...parseSelectStmt(selectAst.larg, defaultSchema, aliases), | ||
...parseSelectStmt(selectAst.rarg, defaultSchema, aliases), | ||
]; | ||
} | ||
|
||
|
@@ -34,7 +44,7 @@ function parseSelectStmt( | |
const selectTargets = jp.query(selectAst, "$.targetList[*].ResTarget"); | ||
|
||
selectTargets.forEach((selectTarget: any) => { | ||
const fields = jp.query(selectTarget, "$.val[*].fields[*].String.str"); | ||
const fields = jp.query(selectTarget, "$.val[*].fields[*].String.sval"); | ||
let sourceTable = fromTable?.relname; | ||
let sourceSchema = fromTable?.schemaname; | ||
if (fields.length === 2) { | ||
|
@@ -65,12 +75,13 @@ function parseSelectStmt( | |
|
||
return viewReferences; | ||
} | ||
function parseViewDefinition( | ||
async function parseViewDefinition( | ||
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. It's called from |
||
selectStatement: string, | ||
defaultSchema: string, | ||
): ViewReference[] { | ||
const ast = pgQuery.parse(selectStatement).parse_tree[0]; | ||
const selectAst = ast.RawStmt?.stmt?.SelectStmt; | ||
): Promise<ViewReference[]> { | ||
const pgQuery = await getPgQuery(); | ||
const ast = pgQuery.parse(selectStatement).parse_tree.stmts[0]; | ||
const selectAst = ast?.stmt?.SelectStmt; | ||
|
||
if (!selectAst) { | ||
throw new Error( | ||
|
@@ -80,17 +91,17 @@ function parseViewDefinition( | |
|
||
const aliasDefinitions = jp.query( | ||
ast, | ||
"$.RawStmt.stmt.SelectStmt.fromClause..[?(@.alias)]", | ||
"$.stmt.SelectStmt.fromClause..[?(@.alias)]", | ||
); | ||
|
||
const aliases = Object.fromEntries( | ||
aliasDefinitions.map(({ schemaname, relname, alias }) => [ | ||
alias.Alias.aliasname, | ||
alias.aliasname, | ||
{ schema: schemaname, table: relname }, | ||
]), | ||
); | ||
|
||
const withClauses = selectAst.withClause?.WithClause?.ctes || []; | ||
const withClauses = selectAst.withClause?.ctes || []; | ||
|
||
const cteAliases: Record<string, ViewReference[]> = {}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
declare module "pg-query-emscripten"; | ||
declare module "pg-query-emscripten/pg_query_wasm"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
New emscripten version requires async initialisation. Since project target didn't support top-level await, and because view parsing is not always required, I made both importing and initialisation happen on-demand