Skip to content

Commit 2b58bbd

Browse files
committed
Fix edgedbsql.to_regclass() properly with current_schemas()
1 parent 63f56e9 commit 2b58bbd

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

edb/pgsql/metaschema.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7949,39 +7949,50 @@ def construct_pg_view(
79497949
END;
79507950
""",
79517951
),
7952-
# WARNING: this `edgedbsql.to_regclass()` function is currently not
7953-
# accurately implemented to take application-level `search_path`
7954-
# into consideration. It is currently here to support the following
7955-
# `has_*privilege` functions (which are less sensitive to such issue).
7956-
# SO DO NOT USE `edgedbsql.to_regclass()` FOR ANYTHING ELSE.
79577952
trampoline.VersionedFunction(
79587953
name=('edgedbsql', 'to_regclass'),
79597954
args=(
7960-
('name', 'text',),
7955+
('name_or_oid', 'text',),
79617956
),
79627957
returns=('regclass',),
7958+
language="plpgsql",
79637959
text="""
7964-
SELECT
7965-
CASE
7966-
WHEN array_length(parts, 1) = 1 THEN
7967-
(
7968-
SELECT oid::regclass
7969-
FROM edgedbsql_VER.pg_class
7970-
WHERE relname = parts[1]
7971-
LIMIT 1 -- HACK: see comments above
7972-
)
7973-
WHEN array_length(parts, 1) = 2 THEN
7974-
(
7975-
SELECT pc.oid::regclass
7976-
FROM edgedbsql_VER.pg_class pc
7977-
JOIN edgedbsql_VER.pg_namespace pn
7978-
ON pn.oid = pc.relnamespace
7979-
WHERE relname = parts[2] AND nspname = parts[1]
7980-
)
7981-
ELSE
7982-
NULL::regclass
7983-
END
7984-
FROM parse_ident(name) parts
7960+
DECLARE
7961+
parts text[];
7962+
result regclass;
7963+
BEGIN
7964+
IF name_or_oid = '-' THEN
7965+
RETURN 0::regclass;
7966+
END IF;
7967+
7968+
IF name_or_oid ~ '^[0-9]+$' THEN
7969+
RETURN name_or_oid::oid::regclass;
7970+
END IF;
7971+
7972+
parts := parse_ident(name_or_oid);
7973+
IF array_length(parts, 1) = 1 THEN
7974+
SELECT pc.oid::regclass INTO result
7975+
FROM unnest(edgedbsql_VER.current_schemas(true))
7976+
WITH ORDINALITY AS ns(nspname, ord)
7977+
JOIN edgedbsql_VER.pg_namespace pn
7978+
USING (nspname)
7979+
JOIN edgedbsql_VER.pg_class pc
7980+
ON pn.oid = pc.relnamespace
7981+
WHERE pc.relname = parts[1]
7982+
ORDER BY ns.ord
7983+
LIMIT 1;
7984+
ELSEIF array_length(parts, 1) = 2 THEN
7985+
SELECT pc.oid::regclass INTO result
7986+
FROM edgedbsql_VER.pg_class pc
7987+
JOIN edgedbsql_VER.pg_namespace pn
7988+
ON pn.oid = pc.relnamespace
7989+
WHERE relname = parts[2] AND nspname = parts[1];
7990+
ELSE
7991+
RAISE EXCEPTION
7992+
'improper relation name (too many dotted names): %', name_or_oid;
7993+
END IF;
7994+
RETURN result;
7995+
END;
79857996
"""
79867997
),
79877998
trampoline.VersionedFunction(

0 commit comments

Comments
 (0)