summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/sql/sqljson.pgc
blob: a00550383447bf5cf9c123be869490901e5d06ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>

EXEC SQL INCLUDE sqlca;
exec sql include ../regression;

EXEC SQL WHENEVER SQLERROR sqlprint;

int
main ()
{
EXEC SQL BEGIN DECLARE SECTION;
  char json[1024];
  bool is_json[8];
EXEC SQL END DECLARE SECTION;

  ECPGdebug (1, stderr);

  EXEC SQL CONNECT TO REGRESSDB1;
  EXEC SQL SET AUTOCOMMIT = ON;

  EXEC SQL SELECT JSON_OBJECT(RETURNING text) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL SELECT JSON_OBJECT(RETURNING text FORMAT JSON) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL SELECT JSON_ARRAY(RETURNING jsonb) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL SELECT JSON_ARRAY(RETURNING jsonb FORMAT JSON) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL SELECT JSON_OBJECT(1: 1, '1': NULL WITH UNIQUE) INTO :json;
  // error

  EXEC SQL SELECT JSON_OBJECT(1: 1, '2': NULL, 1: '2' ABSENT ON NULL WITHOUT UNIQUE KEYS) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL SELECT JSON_OBJECT(1: 1, '2': NULL ABSENT ON NULL WITHOUT UNIQUE RETURNING jsonb) INTO :json;
  printf("Found json=%s\n", json);

  EXEC SQL WITH val (js) AS (VALUES ('{ "a": 1, "b": [{ "a": 1, "b": 0, "a": 2 }] }'))
	  SELECT
	  js IS JSON "IS JSON",
	  js IS NOT JSON "IS NOT JSON",
	  js IS JSON VALUE "IS VALUE",
	  js IS JSON OBJECT "IS OBJECT",
	  js IS JSON ARRAY "IS ARRAY",
	  js IS JSON SCALAR "IS SCALAR",
	  js IS JSON WITHOUT UNIQUE KEYS "WITHOUT UNIQUE",
	  js IS JSON WITH UNIQUE KEYS "WITH UNIQUE"
		  INTO :is_json[0], :is_json[1], :is_json[2], :is_json[3], :is_json[4],
		  :is_json[5], :is_json[6], :is_json[7]
		  FROM val;
	  for (int i = 0; i < sizeof(is_json); i++)
		  printf("Found is_json[%d]: %s\n", i, is_json[i] ? "true" : "false");

  EXEC SQL DISCONNECT;

  return 0;
}