aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/test/num_test.pgc
blob: f3991ccc8a22f6c21b962b087a6e8c199b90f8c5 (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
#include <stdio.h>
#include <pgtypes_numeric.h>
#include <decimal.h>

int
main(void)
{
	char *text="error\n";
	numeric *value1, *value2, *res;
	exec sql begin declare section;
		numeric(14,7) des = {0, 0, 0, 0, 0, NULL, NULL} ;
	exec sql end declare section;
	double d;
	FILE *dbgs;
	
	if ((dbgs = fopen("log", "w")) != NULL)
	         ECPGdebug(1, dbgs);
	exec sql whenever sqlerror do sqlprint();

	exec sql connect to mm;
	exec sql create table test (text char(5), num numeric(14,7));
	
	value1 = PGTYPESnumeric_new();
	PGTYPESnumeric_from_int(1407, value1);
	text = PGTYPESnumeric_to_asc(value1, -1);
	printf("long = %s\n", text);
		
	value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
	value2 = PGTYPESnumeric_from_asc("10.0", NULL);
	res = PGTYPESnumeric_new();
	PGTYPESnumeric_add(value1, value2, res);
	text = PGTYPESnumeric_to_asc(res, -1);
	printf("add = %s\n", text);
	
	PGTYPESnumeric_sub(res, value2, res);
	text = PGTYPESnumeric_to_asc(res, -1);
	printf("sub = %s\n", text);
		
	PGTYPESnumeric_copy(res, &des);
	exec sql insert into test (text, num) values ('test', :des);
	
	value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
	PGTYPESnumeric_mul(value1, value2, res);

	exec sql select num into :des from test where text = 'test';
	
	PGTYPESnumeric_mul(res, &des, res);
	text = PGTYPESnumeric_to_asc(res, -1);
	printf("mul = %s\n", text);

	value2 = PGTYPESnumeric_from_asc("10000", NULL);
	PGTYPESnumeric_div(res, value2, res);
	text = PGTYPESnumeric_to_asc(res, -1);
	PGTYPESnumeric_to_double(res, &d);
	printf("div = %s %e\n", text, d);

	exec sql rollback;
	exec sql disconnect;

	if (dbgs != NULL)
                fclose(dbgs);
		
	return (0);
}