Skip to content

Commit d95817b

Browse files
committed
Push PK check into FK lookups.
This was missing before.
1 parent 96d8f29 commit d95817b

File tree

1 file changed

+11
-55
lines changed

1 file changed

+11
-55
lines changed

graphql.sql

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,10 @@ BEGIN
193193
subselects := subselects || graphql.to_sql(regclass(sub.selector),
194194
sub.predicate,
195195
sub.body,
196-
tab);
196+
tab,
197+
pk);
197198
cols := cols
198-
|| format('%I.%I',
199-
'sub/'||cardinality(subselects),
200-
sub.selector);
199+
|| format('%I.%I', 'sub/'||cardinality(subselects), sub.selector);
201200
ELSE
202201
RAISE EXCEPTION 'Not able to interpret this selector: %', sub.selector;
203202
END CASE;
@@ -299,7 +298,8 @@ $$ LANGUAGE plpgsql STABLE;
299298
CREATE FUNCTION to_sql(selector regclass,
300299
predicate text,
301300
body text,
302-
tab regclass)
301+
tab regclass,
302+
keys text[])
303303
RETURNS text AS $$
304304
DECLARE
305305
q text = '';
@@ -329,18 +329,13 @@ BEGIN
329329
IF NOT FOUND AND (SELECT count(1) FROM graphql.fk(selector)) = 2 THEN
330330
SELECT * INTO STRICT okey FROM graphql.fk(selector) WHERE fk != ikey;
331331
q := graphql.to_sql(okey.other, NULL, body, name(selector))
332-
|| E'\n ' || graphql.format_join_table_lookup(tab,
333-
ikey.refs,
334-
ikey.cols,
335-
selector,
336-
okey.cols,
337-
okey.refs,
338-
okey.other);
339-
ELSE
340-
q := graphql.to_sql(selector, NULL, body, name(selector))
341332
|| E'\n '
342-
|| graphql.format_join(selector, ikey.cols, tab, ikey.refs, 'join/1');
333+
|| graphql.format_join(okey.other, okey.refs, selector, okey.cols);
334+
ELSE
335+
q := graphql.to_sql(selector, NULL, body, name(selector));
343336
END IF;
337+
q := q || E'\n WHERE '
338+
|| graphql.format_comparison(selector, ikey.cols, keys);
344339
RETURN q;
345340
END
346341
$$ LANGUAGE plpgsql STABLE;
@@ -484,24 +479,6 @@ RETURNS text AS $$
484479
array_to_string((SELECT array_agg(val) FROM casted), ', '))
485480
$$ LANGUAGE sql STABLE STRICT;
486481

487-
CREATE FUNCTION format_join(tab name, -- When tab is an alias given with AS
488-
cols name[],
489-
other regclass,
490-
refs name[],
491-
label name DEFAULT NULL)
492-
RETURNS text AS $$
493-
SELECT CASE WHEN label IS NULL THEN
494-
format('JOIN %I ON (%s)',
495-
other,
496-
graphql.format_comparison(tab, cols, other, refs))
497-
ELSE
498-
format('JOIN %I AS %I ON (%s)',
499-
other,
500-
label,
501-
graphql.format_comparison(tab, cols, label, refs))
502-
END
503-
$$ LANGUAGE sql STABLE STRICT;
504-
505482
CREATE FUNCTION format_join(tab regclass,
506483
cols name[],
507484
other regclass,
@@ -518,27 +495,6 @@ RETURNS text AS $$
518495
label,
519496
graphql.format_comparison(tab, cols, label, refs))
520497
END
521-
$$ LANGUAGE sql STABLE STRICT;
522-
523-
CREATE FUNCTION format_join_table_lookup(main_table regclass,
524-
main_refs name[],
525-
main_cols name[],
526-
join_table regclass,
527-
data_cols name[],
528-
data_refs name[],
529-
data_table regclass)
530-
RETURNS text AS $$
531-
SELECT graphql.format_join(data_table,
532-
data_refs,
533-
join_table,
534-
data_cols,
535-
'join/1')
536-
|| E'\n '
537-
|| graphql.format_join('join/1',
538-
main_cols,
539-
main_table,
540-
main_refs,
541-
'join/2')
542-
$$ LANGUAGE sql STABLE STRICT;
498+
$$ LANGUAGE sql STABLE;
543499

544500
END;

0 commit comments

Comments
 (0)