Skip to content

Commit adc9044

Browse files
committed
Fix escaping issue.
1 parent 2460220 commit adc9044

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

demo.sql

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1+
\set QUIET true
2+
\set ON_ERROR_ROLLBACK 1
3+
\set ON_ERROR_STOP true
4+
15
\include_relative graphql.sql
26
\include_relative fb/schema.sql
37
\include_relative fb/data.sql
48

59
\x auto
610

7-
\C Consumers:
8-
SELECT * FROM consumer;
11+
\C Users:
12+
SELECT * FROM "user";
913

1014
\C Friendships:
1115
SELECT friendship.*, l._||' -> '||r._ AS who FROM friendship,
12-
LATERAL (SELECT full_name FROM consumer WHERE first = id) AS l(_),
13-
LATERAL (SELECT full_name FROM consumer WHERE second = id) AS r(_);
16+
LATERAL (SELECT full_name FROM "user" WHERE first = id) AS l(_),
17+
LATERAL (SELECT full_name FROM "user" WHERE second = id) AS r(_);
1418

1519
DO $$
1620
DECLARE
17-
--graphql_q text = E'consumer("f3411edc-e1d0-452a-bc19-b42c0d5a0e36") {\n'
18-
-- ' full_name,\n'
19-
-- ' friendship\n'
20-
-- '}';
21-
graphql_q text = E'consumer("f3411edc-e1d0-452a-bc19-b42c0d5a0e36") {\n'
21+
graphql_q text = E'user("f3411edc-e1d0-452a-bc19-b42c0d5a0e36") {\n'
2222
' full_name,\n'
23-
' friendship { full_name }\n'
23+
' friendship\n'
2424
'}';
25+
--graphql_q text = E'user("f3411edc-e1d0-452a-bc19-b42c0d5a0e36") {\n'
26+
-- ' full_name,\n'
27+
-- ' friendship { full_name }\n'
28+
-- '}';
2529
sql_q text;
2630
result json;
2731
msg text;

fb/data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BEGIN;
22

3-
INSERT INTO consumer (id, full_name) VALUES
3+
INSERT INTO "user" (id, full_name) VALUES
44
('606fa027-a577-4018-952e-3c8469372829', 'Curly'),
55
('f3411edc-e1d0-452a-bc19-b42c0d5a0e36', 'Larry'),
66
('05ed1b59-090e-40af-8f0e-68a1129b55b4', 'Mo');

fb/schema.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SET search_path TO fb,"$user",public;
66
CREATE EXTENSION "uuid-ossp";
77

88

9-
CREATE TABLE consumer (
9+
CREATE TABLE "user" (
1010
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
1111
created timestamptz NOT NULL DEFAULT now(),
1212
full_name text NOT NULL DEFAULT ''
@@ -17,13 +17,13 @@ CREATE TABLE post (
1717
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
1818
created timestamptz NOT NULL DEFAULT now(),
1919
content text NOT NULL DEFAULT '',
20-
consumer uuid REFERENCES consumer NOT NULL
20+
"user" uuid REFERENCES "user" NOT NULL
2121
);
2222

2323

2424
CREATE TABLE friendship (
25-
first uuid REFERENCES consumer NOT NULL,
26-
second uuid REFERENCES consumer NOT NULL,
25+
first uuid REFERENCES "user" NOT NULL,
26+
second uuid REFERENCES "user" NOT NULL,
2727
created timestamptz NOT NULL DEFAULT now(),
2828
UNIQUE (first, second)
2929
);

graphql.sql

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ BEGIN
176176
tab, col;
177177
END IF;
178178
subselects := subselects
179-
|| format(E'SELECT to_json(%1$I) AS %4$I FROM %1$I\n'
180-
' WHERE %1$I.%2$I = %3$I.%4$I',
179+
|| format(E'SELECT to_json(%1$s) AS %4$I FROM %1$s\n'
180+
' WHERE %1$s.%2$I = %3$s.%4$I',
181181
fks[1].other, fks[1].refs[1], tab, col);
182182
cols := cols || format('%I.%I', 'sub/'||cardinality(subselects), col);
183183
ELSE
184-
cols := cols || format('%I.%I', tab, col);
184+
cols := cols || format('%s.%I', tab, col);
185185
END IF;
186186
WHEN FOUND AND sub.body IS NOT NULL THEN -- Index into a column
187187
subselects := subselects || graphql.to_sql(sub.selector,
@@ -215,16 +215,20 @@ BEGIN
215215
|| format('SELECT %s', array_to_string(cols, ', '));
216216
column_expression := format('%I', 'sub/'||cardinality(subselects));
217217
ELSE
218-
column_expression := format('%I', tab);
218+
column_expression := format('%s', tab);
219219
END IF;
220220
IF pk IS NOT NULL THEN -- Implies single result
221221
q := 'SELECT to_json(' || column_expression || ')' || q;
222222
ELSE
223223
q := 'SELECT json_agg(' || column_expression || ')' || q;
224224
END IF;
225-
q := q || format(' AS %I', COALESCE(label, name(tab)));
225+
IF label IS NOT NULL THEN
226+
q := q || format(' AS %I', label);
227+
ELSE
228+
q := q || format(' AS %s', tab);
229+
END IF;
226230
END;
227-
q := q || format(E'\n FROM %I', tab);
231+
q := q || format(E'\n FROM %s', tab);
228232
FOR i IN 1..cardinality(subselects) LOOP
229233
q := q || array_to_string(ARRAY[
230234
',',
@@ -447,8 +451,8 @@ $$ LANGUAGE sql IMMUTABLE STRICT;
447451

448452
CREATE FUNCTION format_comparison(x regclass, xs name[], y regclass, ys name[])
449453
RETURNS text AS $$
450-
WITH xs(col) AS (SELECT format('%I.%I', x, col) FROM unnest(xs) AS _(col)),
451-
ys(col) AS (SELECT format('%I.%I', y, col) FROM unnest(ys) AS _(col))
454+
WITH xs(col) AS (SELECT format('%s.%I', x, col) FROM unnest(xs) AS _(col)),
455+
ys(col) AS (SELECT format('%s.%I', y, col) FROM unnest(ys) AS _(col))
452456
SELECT format('(%s) = (%s)',
453457
array_to_string((SELECT array_agg(col) FROM xs), ', '),
454458
array_to_string((SELECT array_agg(col) FROM ys), ', '))
@@ -457,15 +461,15 @@ $$ LANGUAGE sql STABLE STRICT;
457461
CREATE FUNCTION format_comparison(x name, xs name[], y regclass, ys name[])
458462
RETURNS text AS $$
459463
WITH xs(col) AS (SELECT format('%I.%I', x, col) FROM unnest(xs) AS _(col)),
460-
ys(col) AS (SELECT format('%I.%I', y, col) FROM unnest(ys) AS _(col))
464+
ys(col) AS (SELECT format('%s.%I', y, col) FROM unnest(ys) AS _(col))
461465
SELECT format('(%s) = (%s)',
462466
array_to_string((SELECT array_agg(col) FROM xs), ', '),
463467
array_to_string((SELECT array_agg(col) FROM ys), ', '))
464468
$$ LANGUAGE sql STABLE STRICT;
465469

466470
CREATE FUNCTION format_comparison(x regclass, xs name[], y name, ys name[])
467471
RETURNS text AS $$
468-
WITH xs(col) AS (SELECT format('%I.%I', x, col) FROM unnest(xs) AS _(col)),
472+
WITH xs(col) AS (SELECT format('%s.%I', x, col) FROM unnest(xs) AS _(col)),
469473
ys(col) AS (SELECT format('%I.%I', y, col) FROM unnest(ys) AS _(col))
470474
SELECT format('(%s) = (%s)',
471475
array_to_string((SELECT array_agg(col) FROM xs), ', '),
@@ -483,7 +487,7 @@ $$ LANGUAGE sql STABLE STRICT;
483487

484488
CREATE FUNCTION format_comparison(x regclass, xs name[], ys text[])
485489
RETURNS text AS $$
486-
WITH xs(col) AS (SELECT format('%I.%I', x, col) FROM unnest(xs) AS _(col)),
490+
WITH xs(col) AS (SELECT format('%s.%I', x, col) FROM unnest(xs) AS _(col)),
487491
named(col, txt) AS (SELECT * FROM unnest(xs, ys)),
488492
casted(val) AS (SELECT format('%L::%I', txt, typ)
489493
FROM named JOIN graphql.cols(x) USING (col))
@@ -499,11 +503,11 @@ CREATE FUNCTION format_join(tab regclass,
499503
label name DEFAULT NULL)
500504
RETURNS text AS $$
501505
SELECT CASE WHEN label IS NULL THEN
502-
format('JOIN %I ON (%s)',
506+
format('JOIN %s ON (%s)',
503507
other,
504508
graphql.format_comparison(tab, cols, other, refs))
505509
ELSE
506-
format('JOIN %I AS %I ON (%s)',
510+
format('JOIN %s AS %I ON (%s)',
507511
other,
508512
label,
509513
graphql.format_comparison(tab, cols, label, refs))

0 commit comments

Comments
 (0)