@@ -158,20 +158,22 @@ BEGIN
158
158
ELSE
159
159
column_expression := format(' %I' , tab);
160
160
END IF;
161
- IF pk IS NOT NULL THEN -- Implies single result
162
- q := ' SELECT to_json(' || column_expression || E ' ) \n ' || q;
161
+ IF pk IS NOT NULL THEN -- Implies single result
162
+ q := ' SELECT to_json(' || column_expression || ' ) ' || q;
163
163
ELSE
164
- q := ' SELECT json_agg(' || column_expression || E ' ) \n ' || q;
164
+ q := ' SELECT json_agg(' || column_expression || ' ) ' || q;
165
165
END IF;
166
166
END;
167
167
q := q || format(E' \n FROM %I' , tab);
168
168
FOR n IN 1 ..cardinality(subselects) LOOP
169
- q := q || E' ,\n '
170
- || E' LATERAL (\n '
171
- || graphql .indent (subselects[i])
172
- || E' \n ) AS ' || format(' %I' , ' sub/' || i);
173
- -- - TODO: Switch to abstract representation of subqueries so we don't end
174
- -- - up reindenting the same lines multiple times.
169
+ q := q || array_to_string(ARRAY[
170
+ ' ,' ,
171
+ graphql .indent (7 , ' LATERAL (' ), -- 7 to line up with SELECT ...
172
+ graphql .indent (9 , subselects[i]), -- 9 to be 2 under LATERAL
173
+ graphql .indent (7 , ' ) AS ' || format(' %I' , ' sub/' || i))
174
+ ], E' \n ' );
175
+ -- - TODO: Find an "indented text" abstraction so we don't split and
176
+ -- - recombine the same lines so many times.
175
177
END LOOP;
176
178
FOR n IN 1 ..cardinality(predicates) LOOP
177
179
IF n = 1 THEN
@@ -268,11 +270,11 @@ RETURNS text AS $$
268
270
SELECT substr(regexp_replace(str, ' [ \n\t ]+' , ' ' , ' g' ), start, length);
269
271
$$ LANGUAGE sql IMMUTABLE STRICT;
270
272
271
- CREATE FUNCTION indent (str text )
273
+ CREATE FUNCTION indent (level integer , str text )
272
274
RETURNS text AS $$
273
275
SELECT array_to_string(array_agg(s), E' \n ' )
274
276
FROM unnest(string_to_array(str, E' \n ' )) AS _(ln),
275
- LATERAL (SELECT CASE ln WHEN ' ' THEN ln ELSE ' ' || ln)
277
+ LATERAL (SELECT CASE ln WHEN ' ' THEN ln ELSE repeat( ' ' , level) || ln)
276
278
AS indented(s)
277
279
$$ LANGUAGE sql IMMUTABLE STRICT;
278
280
0 commit comments