@@ -42,7 +42,6 @@ import NeatInterpolation (trimming)
4242import PostgREST.Config (AppConfig (.. ))
4343import PostgREST.Config.Database (TimezoneNames ,
4444 toIsolationLevel )
45- import PostgREST.Query.SqlFragment (escapeIdent )
4645import PostgREST.SchemaCache.Identifiers (FieldName ,
4746 QualifiedIdentifier (.. ),
4847 RelIdentifier (.. ),
@@ -357,7 +356,7 @@ allFunctions :: Bool -> SQL.Statement AppConfig RoutineMap
357356allFunctions = SQL. Statement funcsSqlQuery params decodeFuncs
358357 where
359358 params =
360- (map escapeIdent . toList . configDbSchemas >$< arrayParam HE. text) <>
359+ (toList . configDbSchemas >$< arrayParam HE. text) <>
361360 (configDbHoistedTxSettings >$< arrayParam HE. text)
362361
363362baseTypesCte :: Text
@@ -458,7 +457,7 @@ funcsSqlQuery = encodeUtf8 [trimming|
458457 ) func_settings ON TRUE
459458 WHERE t.oid <> 'trigger'::regtype AND COALESCE(a.callable, true)
460459 AND prokind = 'f'
461- AND p.pronamespace = ANY($$1::regnamespace[] ) |]
460+ AND pn.nspname = ANY($$1) |]
462461{-
463462Adds M2O and O2O relationships for views to tables, tables to views, and views to views. The example below is taken from the test fixtures, but the views names/colnames were modified.
464463
@@ -569,7 +568,7 @@ addViewPrimaryKeys tabs keyDeps =
569568allTables :: Bool -> SQL. Statement AppConfig TablesMap
570569allTables = SQL. Statement tablesSqlQuery params decodeTables
571570 where
572- params = map escapeIdent . toList . configDbSchemas >$< arrayParam HE. text
571+ params = toList . configDbSchemas >$< arrayParam HE. text
573572
574573-- | Gets tables with their PK cols
575574tablesSqlQuery :: SqlQuery
@@ -621,6 +620,8 @@ tablesSqlQuery =
621620 ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
622621 JOIN pg_class c
623622 ON a.attrelid = c.oid
623+ JOIN pg_namespace nc
624+ ON c.relnamespace = nc.oid
624625 JOIN pg_type t
625626 ON a.atttypid = t.oid
626627 LEFT JOIN base_types bt
@@ -632,7 +633,7 @@ tablesSqlQuery =
632633 AND a.attnum > 0
633634 AND NOT a.attisdropped
634635 AND c.relkind in ('r', 'v', 'f', 'm', 'p')
635- AND c.relnamespace = ANY($$1::regnamespace[] )
636+ AND nc.nspname = ANY($$1)
636637 ),
637638 columns_agg AS (
638639 SELECT
@@ -812,8 +813,8 @@ allViewsKeyDependencies =
812813 -- * json transformation: https://gist.github.com/wolfgangwalther/3a8939da680c24ad767e93ad2c183089
813814 where
814815 params =
815- (map escapeIdent . toList . configDbSchemas >$< arrayParam HE. text) <>
816- (map escapeIdent . toList . configDbExtraSearchPath >$< arrayParam HE. text)
816+ (toList . configDbSchemas >$< arrayParam HE. text) <>
817+ (configDbExtraSearchPath >$< arrayParam HE. text)
817818 sql = encodeUtf8 [trimming |
818819 with recursive
819820 pks_fks as (
@@ -844,18 +845,17 @@ allViewsKeyDependencies =
844845 views as (
845846 select
846847 c.oid as view_id,
847- c.relnamespace as view_schema_id,
848848 n.nspname as view_schema,
849849 c.relname as view_name,
850850 r.ev_action as view_definition
851851 from pg_class c
852852 join pg_namespace n on n.oid = c.relnamespace
853853 join pg_rewrite r on r.ev_class = c.oid
854- where c.relkind in ('v', 'm') and c.relnamespace = ANY($$1::regnamespace[] || $$2::regnamespace[] )
854+ where c.relkind in ('v', 'm') and n.nspname = ANY($$1 || $$2)
855855 ),
856856 transform_json as (
857857 select
858- view_id, view_schema_id, view_schema, view_name,
858+ view_id, view_schema, view_name,
859859 -- the following formatting is without indentation on purpose
860860 -- to allow simple diffs, with less whitespace noise
861861 replace(
@@ -935,31 +935,30 @@ allViewsKeyDependencies =
935935 ),
936936 target_entries as(
937937 select
938- view_id, view_schema_id, view_schema, view_name,
938+ view_id, view_schema, view_name,
939939 json_array_elements(view_definition->0->'targetList') as entry
940940 from transform_json
941941 ),
942942 results as(
943943 select
944- view_id, view_schema_id, view_schema, view_name,
944+ view_id, view_schema, view_name,
945945 (entry->>'resno')::int as view_column,
946946 (entry->>'resorigtbl')::oid as resorigtbl,
947947 (entry->>'resorigcol')::int as resorigcol
948948 from target_entries
949949 ),
950950 -- CYCLE detection according to PG docs: https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CYCLE
951951 -- Can be replaced with CYCLE clause once PG v13 is EOL.
952- recursion(view_id, view_schema_id, view_schema, view_name, view_column, resorigtbl, resorigcol, is_cycle, path) as(
952+ recursion(view_id, view_schema, view_name, view_column, resorigtbl, resorigcol, is_cycle, path) as(
953953 select
954954 r.*,
955955 false,
956956 ARRAY[resorigtbl]
957957 from results r
958- where view_schema_id = ANY ($$1::regnamespace[] )
958+ where view_schema = ANY ($$1)
959959 union all
960960 select
961961 view.view_id,
962- view.view_schema_id,
963962 view.view_schema,
964963 view.view_name,
965964 view.view_column,
@@ -1018,7 +1017,7 @@ mediaHandlers :: Bool -> SQL.Statement AppConfig MediaHandlerMap
10181017mediaHandlers =
10191018 SQL. Statement sql params decodeMediaHandlers
10201019 where
1021- params = map escapeIdent . toList . configDbSchemas >$< arrayParam HE. text
1020+ params = toList . configDbSchemas >$< arrayParam HE. text
10221021 sql = encodeUtf8 [trimming |
10231022 with
10241023 all_relations as (
@@ -1059,7 +1058,7 @@ mediaHandlers =
10591058 join pg_type arg_name on arg_name.oid = proc.proargtypes[0]
10601059 join pg_namespace arg_schema on arg_schema.oid = arg_name.typnamespace
10611060 where
1062- proc.pronamespace = ANY($$1::regnamespace[] ) and
1061+ proc_schema.nspname = ANY($$1) and
10631062 proc.pronargs = 1 and
10641063 arg_name.oid in (select reltype from all_relations)
10651064 union
@@ -1075,7 +1074,7 @@ mediaHandlers =
10751074 join media_types mtype on proc.prorettype = mtype.oid
10761075 join pg_namespace typ_sch on typ_sch.oid = mtype.typnamespace
10771076 where
1078- proc.pronamespace = ANY($$1::regnamespace[] ) and NOT proretset
1077+ pro_sch.nspname = ANY($$1) and NOT proretset
10791078 and prokind = 'f'|]
10801079
10811080decodeMediaHandlers :: HD. Result MediaHandlerMap
0 commit comments