summaryrefslogtreecommitdiffstats
path: root/src/interfaces/ecpg/test/sql/array.pgc
blob: 8ca9992e47a83e5aa97fb4b5d4cf21e83c59f5a7 (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
#include <locale.h>
#include <string.h>
#include <stdlib.h>

#include <pgtypes_date.h>
#include <pgtypes_interval.h>
#include <pgtypes_numeric.h>
#include <pgtypes_timestamp.h>

exec sql whenever sqlerror sqlprint;

exec sql include sqlca;
exec sql include ../regression;

int
main (void)
{
EXEC SQL BEGIN DECLARE SECTION;
	int i = 1, j;
	int *did = &i;
	short a[10] = {9,8,7,6,5,4,3,2,1,0};
	timestamp ts[10];
	date d[10];
	interval in[10];
	numeric n[10];
	char text[25] = "klmnopqrst";
	char *t = (char *)malloc(11);
	double f;
EXEC SQL END DECLARE SECTION;

	strcpy(t, "0123456789");
	setlocale(LC_ALL, "C");

	ECPGdebug(1, stderr);

	for (j = 0; j < 10; j++) {
		char str[28];
		numeric *value;
		interval *inter;

		sprintf(str, "2000-1-1 0%d:00:00", j);
		ts[j] = PGTYPEStimestamp_from_asc(str, NULL);
		sprintf(str, "2000-1-1%d\n", j);
		d[j] = PGTYPESdate_from_asc(str, NULL);
		sprintf(str, "%d hours", j+10);
		inter = PGTYPESinterval_from_asc(str, NULL);
		in[j] = *inter;
		value = PGTYPESnumeric_new();
		PGTYPESnumeric_from_int(j, value);
		n[j] = *value;
	}

        EXEC SQL CONNECT TO REGRESSDB1;

	EXEC SQL SET AUTOCOMMIT = ON;

	EXEC SQL BEGIN WORK;

	EXEC SQL CREATE TABLE test (f float, i int, a int[10], text char(10), ts timestamp[10], n numeric[10], d date[10], inter interval[10]);

	EXEC SQL INSERT INTO test(f,i,a,text,ts,n,d,inter) VALUES(404.90,3,'{0,1,2,3,4,5,6,7,8,9}','abcdefghij',:ts,:n,:d,:in);

	EXEC SQL INSERT INTO test(f,i,a,text,ts,n,d,inter) VALUES(140787.0,2,:a,:text,:ts,:n,:d,:in);

	EXEC SQL INSERT INTO test(f,i,a,text,ts,n,d,inter) VALUES(14.07,:did,:a,:t,:ts,:n,:d,:in);

	EXEC SQL COMMIT;

	for (j = 0; j < 10; j++) {
		ts[j] = PGTYPEStimestamp_from_asc("1900-01-01 00:00:00", NULL);
		d[j] = PGTYPESdate_from_asc("1900-01-01", NULL);
		in[j] = *PGTYPESinterval_new();
		n[j] = *PGTYPESnumeric_new();
	}
	EXEC SQL BEGIN WORK;

	EXEC SQL SELECT f,text
         INTO :f,:text
         FROM test
         WHERE i = 1;

	printf("Found f=%f text=%10.10s\n", f, text);

	f=140787;
	EXEC SQL SELECT a,text,ts,n,d,inter
	 INTO :a,:t,:ts,:n,:d,:in
	 FROM test
	 WHERE f = :f;

	for (i = 0; i < 10; i++)
		printf("Found a[%d] = %d ts[%d] = %s n[%d] = %s d[%d] = %s in[%d] = %s\n", i, a[i], i, PGTYPEStimestamp_to_asc(ts[i]), i, PGTYPESnumeric_to_asc(&(n[i]), -1), i, PGTYPESdate_to_asc(d[i]), i, PGTYPESinterval_to_asc(&(in[i])));

	printf("Found text=%10.10s\n", t);

	EXEC SQL SELECT a
	 INTO :text
	 FROM test
	 WHERE f = :f;

	printf("Found text=%s\n", text);

	EXEC SQL DROP TABLE test;

	EXEC SQL COMMIT;

	EXEC SQL DISCONNECT;

	free(t);

	return 0;
}