Skip to content

Commit

Permalink
Add support for pg_catalog.array_to_string function
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunlol committed Jan 3, 2025
1 parent db5ddd4 commit e2b6869
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

const VERSION = "0.29.0"
const VERSION = "0.29.1"

func main() {
config := LoadConfig()
Expand Down
15 changes: 15 additions & 0 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ func TestHandleQuery(t *testing.T) {
"types": {Uint32ToString(pgtype.TextOID)},
"values": {""},
},
"SELECT main.array_to_string('[1, 2, 3]', '') as str": {
"description": {"str"},
"types": {Uint32ToString(pgtype.TextOID)},
"values": {"123"},
},
"SELECT pg_catalog.array_to_string('[1, 2, 3]', '') as str": {
"description": {"str"},
"types": {Uint32ToString(pgtype.TextOID)},
"values": {"123"},
},
"SELECT array_to_string('[1, 2, 3]', '') as str": {
"description": {"str"},
"types": {Uint32ToString(pgtype.TextOID)},
"values": {"123"},
},
// PG system tables
"SELECT oid, typname AS typename FROM pg_type WHERE typname='geometry' OR typname='geography'": {
"description": {"oid", "typename"},
Expand Down
23 changes: 19 additions & 4 deletions src/query_parser_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

const (
PG_FUNCTION_QUOTE_INDENT = "quote_ident"
PG_FUNCTION_PG_GET_EXPR = "pg_get_expr"
PG_FUNCTION_SET_CONFIG = "set_config"
PG_FUNCTION_ROW_TO_JSON = "row_to_json"
PG_FUNCTION_QUOTE_INDENT = "quote_ident"
PG_FUNCTION_PG_GET_EXPR = "pg_get_expr"
PG_FUNCTION_SET_CONFIG = "set_config"
PG_FUNCTION_ROW_TO_JSON = "row_to_json"
PG_FUNCTION_ARRAY_TO_STRING = "array_to_string"
)

type QueryParserSelect struct {
Expand Down Expand Up @@ -96,6 +97,20 @@ func (parser *QueryParserSelect) RemapRowToJson(functionCall *pgQuery.FuncCall)
return functionCall
}

// array_to_string()
func (parser *QueryParserSelect) IsArrayToStringFunction(functionName string) bool {
return functionName == PG_FUNCTION_ARRAY_TO_STRING
}

// array_to_string() -> main.array_to_string()
func (parser *QueryParserSelect) RemapArrayToString(functionCall *pgQuery.FuncCall) *pgQuery.FuncCall {
functionCall.Funcname = []*pgQuery.Node{
pgQuery.MakeStrNode("main"),
pgQuery.MakeStrNode("array_to_string"),
}
return functionCall
}

func (parser *QueryParserSelect) OverrideFunctionCallArg(functionCall *pgQuery.FuncCall, index int, node *pgQuery.Node) {
functionCall.Args[index] = node
}
Expand Down
4 changes: 4 additions & 0 deletions src/select_remapper_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (remapper *SelectRemapperSelect) SubselectStatement(targetNode *pgQuery.Nod
func (remapper *SelectRemapperSelect) remappedFunctionName(functionCall *pgQuery.FuncCall) *pgQuery.FuncCall {
functionName := remapper.parserSelect.FunctionName(functionCall)

if remapper.parserSelect.IsArrayToStringFunction(functionName) {
return remapper.parserSelect.RemapArrayToString(functionCall)
}

if remapper.parserSelect.IsRowToJsonFunction(functionName) {
return remapper.parserSelect.RemapRowToJson(functionCall)
}
Expand Down

0 comments on commit e2b6869

Please sign in to comment.