@@ -152,6 +152,7 @@ querySchemaCache conf@AppConfig{..} = do
152152 reps <- SQL. statement conf $ dataRepresentations prepared
153153 mHdlers <- SQL. statement conf $ mediaHandlers prepared
154154 tzones <- SQL. statement mempty $ timezones prepared
155+ hasPgis <- SQL. statement conf $ postgisFunc prepared
155156 _ <-
156157 let sleepCall = SQL. Statement " select pg_sleep($1 / 1000.0)" (param HE. int4) HD. noResult prepared in
157158 whenJust configInternalSCSleep (`SQL.statement` sleepCall) -- only used for testing
@@ -164,7 +165,7 @@ querySchemaCache conf@AppConfig{..} = do
164165 , dbRelationships = getOverrideRelationshipsMap rels cRels
165166 , dbRoutines = funcs
166167 , dbRepresentations = reps
167- , dbMediaHandlers = HM. union mHdlers initialMediaHandlers -- the custom handlers will override the initial ones
168+ , dbMediaHandlers = HM. union mHdlers $ initialMediaHandlers hasPgis -- the custom handlers will override the initial ones
168169 , dbTimezones = tzones
169170 }
170171 where
@@ -1048,12 +1049,14 @@ allViewsKeyDependencies =
10481049 having ncol = array_length(array_agg(row(col.attname, view_columns) order by pks_fks.ord), 1)
10491050 |]
10501051
1051- initialMediaHandlers :: MediaHandlerMap
1052- initialMediaHandlers =
1052+ initialMediaHandlers :: Bool -> MediaHandlerMap
1053+ initialMediaHandlers hasPostgisFunc =
10531054 HM. insert (RelAnyElement , MediaType. MTAny ) (BuiltinOvAggJson , MediaType. MTApplicationJSON ) $
10541055 HM. insert (RelAnyElement , MediaType. MTApplicationJSON ) (BuiltinOvAggJson , MediaType. MTApplicationJSON ) $
10551056 HM. insert (RelAnyElement , MediaType. MTTextCSV ) (BuiltinOvAggCsv , MediaType. MTTextCSV ) $
1056- HM. insert (RelAnyElement , MediaType. MTGeoJSON ) (BuiltinOvAggGeoJson , MediaType. MTGeoJSON )
1057+ (if hasPostgisFunc
1058+ then HM. insert (RelAnyElement , MediaType. MTGeoJSON ) (BuiltinOvAggGeoJson , MediaType. MTGeoJSON )
1059+ else mempty )
10571060 HM. empty
10581061
10591062mediaHandlers :: Bool -> SQL. Statement AppConfig MediaHandlerMap
@@ -1139,6 +1142,35 @@ timezones = SQL.Statement sql HE.noParams decodeTimezones
11391142 decodeTimezones :: HD. Result TimezoneNames
11401143 decodeTimezones = S. fromList <$> HD. rowList (column HD. text)
11411144
1145+
1146+ -- Find the postgis function that has the signature:
1147+ -- st_asgeojson(record,...) returns text
1148+ postgisFunc :: Bool -> SQL. Statement AppConfig Bool
1149+ postgisFunc = SQL. Statement sql params decoder
1150+ where
1151+ params =
1152+ (map escapeIdent . toList . configDbSchemas >$< arrayParam HE. text) <>
1153+ (map escapeIdent . toList . configDbExtraSearchPath >$< arrayParam HE. text)
1154+ decoder = HD. singleRow (column HD. bool)
1155+ sql = encodeUtf8 [trimming |
1156+ SELECT
1157+ exists(
1158+ SELECT
1159+ 1
1160+ FROM pg_catalog.pg_proc AS p
1161+ JOIN pg_catalog.pg_depend AS d
1162+ ON d.objid = p.oid
1163+ AND d.deptype = 'e'
1164+ JOIN pg_catalog.pg_extension AS e
1165+ ON e.oid = d.refobjid
1166+ WHERE p.pronamespace = ANY($$1::regnamespace[] || $$2::regnamespace[])
1167+ AND p.proname = 'st_asgeojson'
1168+ AND e.extname = 'postgis'
1169+ AND p.proargtypes[0] = 'record'::regtype
1170+ AND pg_get_function_result(p.oid) = 'text'
1171+ );
1172+ |]
1173+
11421174param :: HE. Value a -> HE. Params a
11431175param = HE. param . HE. nonNullable
11441176
0 commit comments