Skip to content

Commit 1b6860d

Browse files
committed
Rethink indenting.
1 parent 752659d commit 1b6860d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

graphql.sql

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,22 @@ BEGIN
158158
ELSE
159159
column_expression := format('%I', tab);
160160
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;
163163
ELSE
164-
q := 'SELECT json_agg(' || column_expression || E')\n ' || q;
164+
q := 'SELECT json_agg(' || column_expression || ')' || q;
165165
END IF;
166166
END;
167167
q := q || format(E'\n FROM %I', tab);
168168
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.
175177
END LOOP;
176178
FOR n IN 1..cardinality(predicates) LOOP
177179
IF n = 1 THEN
@@ -268,11 +270,11 @@ RETURNS text AS $$
268270
SELECT substr(regexp_replace(str, '[ \n\t]+', ' ', 'g'), start, length);
269271
$$ LANGUAGE sql IMMUTABLE STRICT;
270272

271-
CREATE FUNCTION indent(str text)
273+
CREATE FUNCTION indent(level integer, str text)
272274
RETURNS text AS $$
273275
SELECT array_to_string(array_agg(s), E'\n')
274276
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)
276278
AS indented(s)
277279
$$ LANGUAGE sql IMMUTABLE STRICT;
278280

0 commit comments

Comments
 (0)