-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
pgQuery "github.com/pganalyze/pg_query_go/v5" | ||
) | ||
|
||
type ParserShow struct { | ||
config *Config | ||
} | ||
|
||
func NewParserShow(config *Config) *ParserShow { | ||
return &ParserShow{config: config} | ||
} | ||
|
||
func (parser *ParserShow) VariableName(stmt *pgQuery.RawStmt) string { | ||
return stmt.Stmt.GetVariableShowStmt().Name | ||
} | ||
|
||
// SHOW var -> SELECT value AS var FROM duckdb_settings() WHERE LOWER(name) = 'var'; | ||
func (parser *ParserShow) MakeSelectFromDuckdbSettings(variableName string) *pgQuery.RawStmt { | ||
return &pgQuery.RawStmt{ | ||
Stmt: &pgQuery.Node{ | ||
Node: &pgQuery.Node_SelectStmt{ | ||
SelectStmt: &pgQuery.SelectStmt{ | ||
TargetList: []*pgQuery.Node{ | ||
pgQuery.MakeResTargetNodeWithNameAndVal( | ||
variableName, | ||
pgQuery.MakeColumnRefNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("value")}, | ||
0, | ||
), | ||
0, | ||
), | ||
}, | ||
FromClause: []*pgQuery.Node{ | ||
pgQuery.MakeSimpleRangeFunctionNode( | ||
[]*pgQuery.Node{ | ||
pgQuery.MakeListNode( | ||
[]*pgQuery.Node{ | ||
pgQuery.MakeFuncCallNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("duckdb_settings")}, | ||
nil, | ||
0, | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
}, | ||
WhereClause: pgQuery.MakeAExprNode( | ||
pgQuery.A_Expr_Kind_AEXPR_OP, | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("=")}, | ||
pgQuery.MakeFuncCallNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("lower")}, | ||
[]*pgQuery.Node{ | ||
pgQuery.MakeColumnRefNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("name")}, | ||
0, | ||
), | ||
}, | ||
0, | ||
), | ||
pgQuery.MakeAConstStrNode(variableName, 0), | ||
0, | ||
), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// SHOW search_path -> SELECT CONCAT('"$user", ', value) AS search_path FROM duckdb_settings() WHERE name = 'search_path' | ||
func (parser *ParserShow) SetTargetListForSearchPath(stmt *pgQuery.RawStmt) { | ||
stmt.Stmt.GetSelectStmt().TargetList = []*pgQuery.Node{ | ||
pgQuery.MakeResTargetNodeWithNameAndVal( | ||
"search_path", | ||
pgQuery.MakeFuncCallNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("concat")}, | ||
[]*pgQuery.Node{ | ||
pgQuery.MakeAConstStrNode(`"$user", `, 0), | ||
pgQuery.MakeColumnRefNode( | ||
[]*pgQuery.Node{pgQuery.MakeStrNode("value")}, | ||
0, | ||
), | ||
}, | ||
0, | ||
), | ||
0, | ||
), | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
pgQuery "github.com/pganalyze/pg_query_go/v5" | ||
) | ||
|
||
type QueryRemapperShow struct { | ||
config *Config | ||
parserShow *ParserShow | ||
} | ||
|
||
func NewQueryRemapperShow(config *Config) *QueryRemapperShow { | ||
return &QueryRemapperShow{ | ||
config: config, | ||
parserShow: NewParserShow(config), | ||
} | ||
} | ||
|
||
func (remapper *QueryRemapperShow) RemapShowStatement(stmt *pgQuery.RawStmt) *pgQuery.RawStmt { | ||
parser := remapper.parserShow | ||
variableName := parser.VariableName(stmt) | ||
|
||
// SHOW var -> SELECT value AS variable FROM duckdb_settings() WHERE LOWER(name) = 'variable'; | ||
newStmt := parser.MakeSelectFromDuckdbSettings(variableName) | ||
|
||
// SHOW search_path -> SELECT CONCAT('"$user", ', value) AS search_path FROM duckdb_settings() WHERE name = 'search_path' | ||
if variableName == "search_path" { | ||
parser.SetTargetListForSearchPath(newStmt) | ||
} | ||
|
||
return newStmt | ||
} |