@@ -110,141 +110,8 @@ impl SchemaCacheItem for Function {
110110 type Item = Function ;
111111
112112 async fn load ( pool : & PgPool ) -> Result < Vec < Function > , sqlx:: Error > {
113- sqlx:: query_as!(
114- Function ,
115- r#"
116- with functions as (
117- select
118- oid,
119- proname,
120- prosrc,
121- prorettype,
122- proretset,
123- provolatile,
124- prosecdef,
125- prolang,
126- pronamespace,
127- proconfig,
128-
129- -- proargmodes is null when all arg modes are IN
130- coalesce(
131- p.proargmodes,
132- array_fill(
133- 'i' :: text,
134- array [cardinality(coalesce(p.proallargtypes, p.proargtypes))]
135- )
136- ) as arg_modes,
137- -- proargnames is null when all args are unnamed
138- coalesce(
139- p.proargnames,
140- array_fill(
141- '' :: text,
142- array [cardinality(coalesce(p.proallargtypes, p.proargtypes))]
143- )
144- ) as arg_names,
145- -- proallargtypes is null when all arg modes are IN
146- coalesce(p.proallargtypes, p.proargtypes) as arg_types,
147- array_cat(
148- array_fill(false, array [pronargs - pronargdefaults]),
149- array_fill(true, array [pronargdefaults])
150- ) as arg_has_defaults
151- from
152- pg_proc as p
153- where
154- p.prokind = 'f'
155- )
156- select
157- f.oid :: int8 as "id!",
158- n.nspname as "schema!",
159- f.proname as "name!",
160- l.lanname as "language!",
161- case
162- when l.lanname = 'internal' then ''
163- else f.prosrc
164- end as body,
165- case
166- when l.lanname = 'internal' then ''
167- else pg_get_functiondef(f.oid)
168- end as definition,
169- coalesce(f_args.args, '[]') as args,
170- nullif(pg_get_function_arguments(f.oid), '') as argument_types,
171- nullif(pg_get_function_identity_arguments(f.oid), '') as identity_argument_types,
172- f.prorettype :: int8 as "return_type_id!",
173- pg_get_function_result(f.oid) as "return_type!",
174- nullif(rt.typrelid :: int8, 0) as return_type_relation_id,
175- f.proretset as is_set_returning_function,
176- case
177- when f.provolatile = 'i' then 'IMMUTABLE'
178- when f.provolatile = 's' then 'STABLE'
179- when f.provolatile = 'v' then 'VOLATILE'
180- end as behavior,
181- f.prosecdef as security_definer
182- from
183- functions f
184- left join pg_namespace n on f.pronamespace = n.oid
185- left join pg_language l on f.prolang = l.oid
186- left join pg_type rt on rt.oid = f.prorettype
187- left join (
188- select
189- oid,
190- jsonb_object_agg(param, value) filter (
191- where
192- param is not null
193- ) as config_params
194- from
195- (
196- select
197- oid,
198- (string_to_array(unnest(proconfig), '=')) [1] as param,
199- (string_to_array(unnest(proconfig), '=')) [2] as value
200- from
201- functions
202- ) as t
203- group by
204- oid
205- ) f_config on f_config.oid = f.oid
206- left join (
207- select
208- oid,
209- jsonb_agg(
210- jsonb_build_object(
211- 'mode',
212- t2.mode,
213- 'name',
214- name,
215- 'type_id',
216- type_id,
217- 'has_default',
218- has_default
219- )
220- ) as args
221- from
222- (
223- select
224- oid,
225- unnest(arg_modes) as mode,
226- unnest(arg_names) as name,
227- unnest(arg_types) :: int8 as type_id,
228- unnest(arg_has_defaults) as has_default
229- from
230- functions
231- ) as t1,
232- lateral (
233- select
234- case
235- when t1.mode = 'i' then 'in'
236- when t1.mode = 'o' then 'out'
237- when t1.mode = 'b' then 'inout'
238- when t1.mode = 'v' then 'variadic'
239- else 'table'
240- end as mode
241- ) as t2
242- group by
243- t1.oid
244- ) f_args on f_args.oid = f.oid;
245- "#
246- )
247- . fetch_all ( pool)
248- . await
113+ sqlx:: query_file_as!( Function , "src/queries/functions.sql" )
114+ . fetch_all ( pool)
115+ . await
249116 }
250117}
0 commit comments