Skip to content

Commit

Permalink
Fix ::regclass type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Feb 11, 2025
1 parent f75b58e commit 345bcb1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ func TestHandleQuery(t *testing.T) {
"types": {Uint32ToString(pgtype.OIDOID)},
"values": {"1270"},
},
"SELECT attrelid FROM pg_attribute WHERE attrelid = '\"public\".\"test_table\"'::regclass": {
"description": {"attrelid"},
"types": {Uint32ToString(pgtype.Int8OID)},
"values": {},
},
"SELECT objoid, classoid, objsubid, description FROM pg_description WHERE classoid = 'pg_class'::regclass": {
"description": {"objoid", "classoid", "objsubid", "description"},
"types": {Uint32ToString(pgtype.OIDOID), Uint32ToString(pgtype.TextOID), Uint32ToString(pgtype.Int4OID), Uint32ToString(pgtype.TextOID)},
Expand Down
10 changes: 5 additions & 5 deletions src/query_remapper_type_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func (remapper *QueryRemapperTypeCast) RemapTypeCast(node *pgQuery.Node) *pgQuer

typeName := remapper.parserTypeCast.TypeName(typeCast)
switch typeName {
case "regclass":
// 'schema.table'::regclass -> 'schema.table'
return typeCast.Arg
case "text":
// '{a,b,c}'::text[] -> ARRAY['a', 'b', 'c']
return remapper.parserTypeCast.MakeListValueFromArray(typeCast.Arg)
case "regproc":
// 'schema.function_name'::regproc -> 'function_name'
functionNameParts := strings.Split(remapper.parserTypeCast.ArgStringValue(typeCast), ".")
return pgQuery.MakeAConstStrNode(functionNameParts[len(functionNameParts)-1], 0)
nameParts := strings.Split(remapper.parserTypeCast.ArgStringValue(typeCast), ".")
return pgQuery.MakeAConstStrNode(nameParts[len(nameParts)-1], 0)
case "regclass":
// 'schema.table'::regclass -> SELECT c.oid FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'schema' AND c.relname = 'table'
return remapper.parserTypeCast.MakeSubselectOidBySchemaTable(typeCast.Arg)
case "oid":
// 'schema.table'::regclass::oid -> SELECT c.oid FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'schema' AND c.relname = 'table'
nestedNode := typeCast.Arg
Expand Down

0 comments on commit 345bcb1

Please sign in to comment.