summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/sql/bytea.pgc
blob: e87412311945ab43bc61bf162c84afb579218adf (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

exec sql include ../regression;
exec sql whenever sqlerror sqlprint;

static void
dump_binary(char *buf, int len, int ind)
{
	int i;

	printf("len=%d, ind=%d, data=0x", len, ind);
	for (i = 0; i < len; ++i)
		printf("%02x", 0xff & buf[i]);
	printf("\n");
}

#define DATA_SIZE 0x200
#define LACK_SIZE 13
#
int
main(void)
{
exec sql begin declare section;
	bytea send_buf[2][512];
	bytea recv_buf[2][DATA_SIZE];
	bytea recv_vlen_buf[][DATA_SIZE];
	bytea recv_short_buf[DATA_SIZE - LACK_SIZE];
	int ind[2];
exec sql end declare section;
	int i, j, c;

#define init() { \
	for (i = 0; i < 2; ++i) \
	{ \
		memset(recv_buf[i].arr, 0x0, sizeof(recv_buf[i].arr)); \
		recv_buf[i].len = 0; \
		ind[i] = 0; \
	} \
	recv_vlen_buf = NULL, \
	memset(recv_short_buf.arr, 0x0, sizeof(recv_short_buf.arr)); \
} \
while (0)

	ECPGdebug(1, stderr);

	for (i = 0; i < 2; ++i)
	{
		for (j = 0, c = 0xff; (c == -1 ? c = 0xff : 1), j < DATA_SIZE; ++j, --c)
			send_buf[i].arr[j] = c;

		send_buf[i].len = DATA_SIZE;
	}

    exec sql connect to REGRESSDB1;

	exec sql create table if not exists test (data1 bytea, data2 bytea);

	exec sql prepare ins_stmt from "insert into test values(?,?)";
	exec sql prepare sel_stmt from "select data1,data2 from test";
	exec sql allocate descriptor idesc;
	exec sql allocate descriptor odesc;

	/* Test for static sql statement with normal host variable, indicator */
	init();
	exec sql truncate test;
	exec sql insert into test values(:send_buf[0], :send_buf[1]);
	exec sql select data1,data2 into :recv_buf[0]:ind[0], :recv_short_buf:ind[1] from test;
	dump_binary(recv_buf[0].arr, recv_buf[0].len, ind[0]);
	dump_binary(recv_short_buf.arr, recv_short_buf.len, ind[1]);

	/* Test for cursor */
	init();
	exec sql truncate test;
	exec sql insert into test values(:send_buf[0], :send_buf[1]);
	exec sql declare cursor1 cursor for select data1 from test where data1 = :send_buf[0];
	exec sql open cursor1;
	exec sql fetch from cursor1 INTO :recv_buf[0];
	exec sql close cursor1;
	exec sql free cursor1 ;
	dump_binary(recv_buf[0].arr, recv_buf[0].len, 0);

	/* Test for variable length array */
	init();
	exec sql truncate test;
	exec sql insert into test values(:send_buf[0], :send_buf[1]);
	exec sql insert into test values(:send_buf[0], :send_buf[1]);
	exec sql select data1 into :recv_vlen_buf from test;
	dump_binary(recv_vlen_buf[0].arr, recv_vlen_buf[0].len, 0);
	dump_binary(recv_vlen_buf[1].arr, recv_vlen_buf[1].len, 0);
	free(recv_vlen_buf);

	/* Test for dynamic sql statement with normal host variable, indicator */
	init();
	exec sql truncate test;
	exec sql execute ins_stmt using :send_buf[0], :send_buf[1];
	exec sql execute sel_stmt into  :recv_buf[0]:ind[0], :recv_short_buf:ind[1];
	dump_binary(recv_buf[0].arr, recv_buf[0].len, ind[0]);
	dump_binary(recv_short_buf.arr, recv_short_buf.len, ind[1]);

	/* Test for dynamic sql statement with sql descriptor */
	init();
	exec sql truncate test;
	exec sql set descriptor idesc value 1 data = :send_buf[0];
	exec sql set descriptor idesc value 2 data = :send_buf[1];
	exec sql execute ins_stmt using sql descriptor idesc;
	exec sql execute sel_stmt into  sql descriptor odesc;
	exec sql get descriptor odesc value 1 :recv_buf[0] = data, :ind[0] = indicator;
	exec sql get descriptor odesc value 2 :recv_short_buf = data, :ind[1] = indicator;
	dump_binary(recv_buf[0].arr, recv_buf[0].len, ind[0]);
	dump_binary(recv_short_buf.arr, recv_short_buf.len, ind[1]);

	exec sql drop table test;
	exec sql commit;
	exec sql disconnect;

	return 0;
}