@@ -176,12 +176,12 @@ BEGIN
176
176
tab, col;
177
177
END IF;
178
178
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' ,
181
181
fks[1 ].other, fks[1 ].refs[1 ], tab, col);
182
182
cols := cols || format(' %I.%I' , ' sub/' || cardinality(subselects), col);
183
183
ELSE
184
- cols := cols || format(' %I .%I' , tab, col);
184
+ cols := cols || format(' %s .%I' , tab, col);
185
185
END IF;
186
186
WHEN FOUND AND sub .body IS NOT NULL THEN -- Index into a column
187
187
subselects := subselects || graphql .to_sql (sub .selector ,
@@ -215,16 +215,20 @@ BEGIN
215
215
|| format(' SELECT %s' , array_to_string(cols, ' , ' ));
216
216
column_expression := format(' %I' , ' sub/' || cardinality(subselects));
217
217
ELSE
218
- column_expression := format(' %I ' , tab);
218
+ column_expression := format(' %s ' , tab);
219
219
END IF;
220
220
IF pk IS NOT NULL THEN -- Implies single result
221
221
q := ' SELECT to_json(' || column_expression || ' )' || q;
222
222
ELSE
223
223
q := ' SELECT json_agg(' || column_expression || ' )' || q;
224
224
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;
226
230
END;
227
- q := q || format(E' \n FROM %I ' , tab);
231
+ q := q || format(E' \n FROM %s ' , tab);
228
232
FOR i IN 1 ..cardinality(subselects) LOOP
229
233
q := q || array_to_string(ARRAY[
230
234
' ,' ,
@@ -447,8 +451,8 @@ $$ LANGUAGE sql IMMUTABLE STRICT;
447
451
448
452
CREATE FUNCTION format_comparison (x regclass, xs name[], y regclass, ys name[])
449
453
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))
452
456
SELECT format(' (%s) = (%s)' ,
453
457
array_to_string((SELECT array_agg(col) FROM xs), ' , ' ),
454
458
array_to_string((SELECT array_agg(col) FROM ys), ' , ' ))
@@ -457,15 +461,15 @@ $$ LANGUAGE sql STABLE STRICT;
457
461
CREATE FUNCTION format_comparison (x name, xs name[], y regclass, ys name[])
458
462
RETURNS text AS $$
459
463
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))
461
465
SELECT format(' (%s) = (%s)' ,
462
466
array_to_string((SELECT array_agg(col) FROM xs), ' , ' ),
463
467
array_to_string((SELECT array_agg(col) FROM ys), ' , ' ))
464
468
$$ LANGUAGE sql STABLE STRICT;
465
469
466
470
CREATE FUNCTION format_comparison (x regclass, xs name[], y name, ys name[])
467
471
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)),
469
473
ys(col) AS (SELECT format(' %I.%I' , y, col) FROM unnest(ys) AS _(col))
470
474
SELECT format(' (%s) = (%s)' ,
471
475
array_to_string((SELECT array_agg(col) FROM xs), ' , ' ),
@@ -483,7 +487,7 @@ $$ LANGUAGE sql STABLE STRICT;
483
487
484
488
CREATE FUNCTION format_comparison (x regclass, xs name[], ys text [])
485
489
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)),
487
491
named(col, txt) AS (SELECT * FROM unnest(xs, ys)),
488
492
casted(val) AS (SELECT format(' %L::%I' , txt, typ)
489
493
FROM named JOIN graphql .cols (x) USING (col))
@@ -499,11 +503,11 @@ CREATE FUNCTION format_join(tab regclass,
499
503
label name DEFAULT NULL )
500
504
RETURNS text AS $$
501
505
SELECT CASE WHEN label IS NULL THEN
502
- format(' JOIN %I ON (%s)' ,
506
+ format(' JOIN %s ON (%s)' ,
503
507
other,
504
508
graphql .format_comparison (tab, cols, other, refs))
505
509
ELSE
506
- format(' JOIN %I AS %I ON (%s)' ,
510
+ format(' JOIN %s AS %I ON (%s)' ,
507
511
other,
508
512
label,
509
513
graphql .format_comparison (tab, cols, label, refs))
0 commit comments