Skip to content

Commit 49ec271

Browse files
committed
JOIN table are a-working, working, working.
1 parent 42c47ab commit 49ec271

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

graphql.sql

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ RETURNS text AS $$
143143
DECLARE
144144
q text = '';
145145
tab regclass = selector; -- For clarity
146-
cols name[] = ARRAY[]::name[];
146+
cols text[] = ARRAY[]::text[];
147147
col name;
148148
sub record;
149149
pk text = NULL;
@@ -179,36 +179,42 @@ BEGIN
179179
|| format(E'SELECT to_json(%1$I) AS %4$I FROM %1$I\n'
180180
' WHERE %1$I.%2$I = %3$I.%4$I',
181181
fks[1].other, fks[1].refs[1], tab, col);
182-
cols := cols
183-
|| name(format('%I.%I', 'sub/'||cardinality(subselects), col));
182+
cols := cols || format('%I.%I', 'sub/'||cardinality(subselects), col);
184183
ELSE
185-
cols := cols || name(format('%I', col));
184+
cols := cols || format('%I', col);
186185
END IF;
187186
WHEN FOUND AND sub.body IS NOT NULL THEN -- Index into a column
188187
subselects := subselects || graphql.to_sql(sub.selector,
189188
sub.predicate,
190189
sub.body,
191190
tab);
192-
cols := cols
193-
|| name(format('%I.%I', 'sub/'||cardinality(subselects), col));
191+
cols := cols || format('%I.%I', 'sub/'||cardinality(subselects), col);
194192
WHEN NOT FOUND THEN -- It might be a reference to another table
195193
subselects := subselects || graphql.to_sql(regclass(sub.selector),
196194
sub.predicate,
197195
sub.body,
198196
tab);
199197
cols := cols
200-
|| name(format('%I.%I',
201-
'sub/'||cardinality(subselects),
202-
sub.selector));
198+
|| format('%I.%I',
199+
'sub/'||cardinality(subselects),
200+
sub.selector);
203201
ELSE
204202
RAISE EXCEPTION 'Not able to interpret this selector: %', sub.selector;
205203
END CASE;
206204
END LOOP;
207205
DECLARE
208206
column_expression text;
209207
BEGIN
210-
IF cols > ARRAY[]::name[] THEN
211-
column_expression := array_to_string(cols, ', ');
208+
IF cols > ARRAY[]::text[] THEN
209+
--- We want a temporary record type to to pass to json_agg or to_json as
210+
--- a single parameter so that column names are preserved. So we select
211+
--- all the columns into a subselect with LATERAL and then reference the
212+
--- subselect. This subselect should always be the last one in the
213+
--- sequence, since it needs to reference "columns" created in the other
214+
--- subselects.
215+
subselects := subselects
216+
|| format('SELECT %s', array_to_string(cols, ', '));
217+
column_expression := format('%I', 'sub/'||cardinality(subselects));
212218
ELSE
213219
column_expression := format('%I', tab);
214220
END IF;

0 commit comments

Comments
 (0)