PostgreSQL Source Code  git master
pgtypeslib-num_test.c
Go to the documentation of this file.
1 /* Processed by ecpg (regression mode) */
2 /* These include files are added by the preprocessor */
3 #include <ecpglib.h>
4 #include <ecpgerrno.h>
5 #include <sqlca.h>
6 /* End of automatic include section */
7 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
8 
9 #line 1 "num_test.pgc"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <pgtypes_numeric.h>
13 #include <decimal.h>
14 
15 
16 #line 1 "regression.h"
17 
18 
19 
20 
21 
22 
23 #line 6 "num_test.pgc"
24 
25 
26 
27 #line 1 "printf_hack.h"
28 /*
29  * print_double(x) has the same effect as printf("%g", x), but is intended
30  * to produce the same formatting across all platforms.
31  */
32 static void
33 print_double(double x)
34 {
35 #ifdef WIN32
36  /* Change Windows' 3-digit exponents to look like everyone else's */
37  char convert[128];
38  int vallen;
39 
40  sprintf(convert, "%g", x);
41  vallen = strlen(convert);
42 
43  if (vallen >= 6 &&
44  convert[vallen - 5] == 'e' &&
45  convert[vallen - 3] == '0')
46  {
47  convert[vallen - 3] = convert[vallen - 2];
48  convert[vallen - 2] = convert[vallen - 1];
49  convert[vallen - 1] = '\0';
50  }
51 
52  printf("%s", convert);
53 #else
54  printf("%g", x);
55 #endif
56 }
57 
58 #line 8 "num_test.pgc"
59 
60 
61 
62 int
63 main(void)
64 {
65  char *text="error\n";
66  numeric *value1, *value2, *res;
67  /* exec sql begin declare section */
68 
69  /* = {0, 0, 0, 0, 0, NULL, NULL} ; */
70 
71 #line 17 "num_test.pgc"
72  numeric * des ;
73 /* exec sql end declare section */
74 #line 19 "num_test.pgc"
75 
76  double d;
77  long l1, l2;
78  int i, min, max;
79 
80  ECPGdebug(1, stderr);
81  /* exec sql whenever sqlerror do sqlprint ( ) ; */
82 #line 25 "num_test.pgc"
83 
84 
85  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
86 #line 27 "num_test.pgc"
87 
88 if (sqlca.sqlcode < 0) sqlprint ( );}
89 #line 27 "num_test.pgc"
90 
91 
92  { ECPGsetcommit(__LINE__, "off", NULL);
93 #line 29 "num_test.pgc"
94 
95 if (sqlca.sqlcode < 0) sqlprint ( );}
96 #line 29 "num_test.pgc"
97 
98  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test ( text char ( 5 ) , num numeric ( 14 , 7 ) )", ECPGt_EOIT, ECPGt_EORT);
99 #line 30 "num_test.pgc"
100 
101 if (sqlca.sqlcode < 0) sqlprint ( );}
102 #line 30 "num_test.pgc"
103 
104 
105  value1 = PGTYPESnumeric_new();
106  PGTYPESnumeric_from_int(1407, value1);
107  text = PGTYPESnumeric_to_asc(value1, -1);
108  printf("from int = %s\n", text);
110  PGTYPESnumeric_free(value1);
111 
112  value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
113  value2 = PGTYPESnumeric_from_asc("10.0", NULL);
115  PGTYPESnumeric_add(value1, value2, res);
117  printf("add = %s\n", text);
119 
120  PGTYPESnumeric_sub(res, value2, res);
122  printf("sub = %s\n", text);
124  PGTYPESnumeric_free(value2);
125 
126  des = PGTYPESnumeric_new();
127  PGTYPESnumeric_copy(res, des);
128  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test ( text , num ) values ( 'test' , $1 )",
129  ECPGt_numeric,&(des),(long)1,(long)0,sizeof(numeric),
130  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
131 #line 55 "num_test.pgc"
132 
133 if (sqlca.sqlcode < 0) sqlprint ( );}
134 #line 55 "num_test.pgc"
135 
136 
137  value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
138  PGTYPESnumeric_mul(value1, value2, res);
139  PGTYPESnumeric_free(value2);
140 
141  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select num from test where text = 'test'", ECPGt_EOIT,
142  ECPGt_numeric,&(des),(long)1,(long)0,sizeof(numeric),
143  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
144 #line 61 "num_test.pgc"
145 
146 if (sqlca.sqlcode < 0) sqlprint ( );}
147 #line 61 "num_test.pgc"
148 
149 
150  PGTYPESnumeric_mul(res, des, res);
152  printf("mul = %s\n", text);
154  PGTYPESnumeric_free(des);
155 
156  value2 = PGTYPESnumeric_from_asc("10000", NULL);
157  PGTYPESnumeric_div(res, value2, res);
160  printf("div = %s ", text);
161  print_double(d);
162  printf("\n");
163 
164  PGTYPESnumeric_free(value1);
165  PGTYPESnumeric_free(value2);
166 
167  value1 = PGTYPESnumeric_from_asc("2E7", NULL);
168  value2 = PGTYPESnumeric_from_asc("14", NULL);
169  i = PGTYPESnumeric_to_long(value1, &l1) | PGTYPESnumeric_to_long(value2, &l2);
170  printf("to long(%d) = %ld %ld\n", i, l1, l2);
171 
173  PGTYPESnumeric_free(value1);
174  PGTYPESnumeric_free(value2);
176 
177  /* check conversion of numeric to int */
178  value1 = PGTYPESnumeric_from_asc("-2147483648", NULL);
179  PGTYPESnumeric_to_int(value1, &min);
180  printf("min int = %d\n", min);
181  PGTYPESnumeric_free(value1);
182 
183  value2 = PGTYPESnumeric_from_asc("2147483647", NULL);
184  PGTYPESnumeric_to_int(value2, &max);
185  printf("max int = %d\n", max);
186  PGTYPESnumeric_free(value2);
187 
188  { ECPGtrans(__LINE__, NULL, "rollback");
189 #line 101 "num_test.pgc"
190 
191 if (sqlca.sqlcode < 0) sqlprint ( );}
192 #line 101 "num_test.pgc"
193 
194  { ECPGdisconnect(__LINE__, "CURRENT");
195 #line 102 "num_test.pgc"
196 
197 if (sqlca.sqlcode < 0) sqlprint ( );}
198 #line 102 "num_test.pgc"
199 
200 
201  return 0;
202 }
bool ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
Definition: connect.c:153
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:676
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:255
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_numeric
Definition: ecpgtype.h:49
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
void sqlprint(void)
Definition: error.c:334
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2277
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
int x
Definition: isn.c:71
int i
Definition: isn.c:73
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
int PGTYPESnumeric_copy(numeric *src, numeric *dst)
Definition: numeric.c:1388
int PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:896
int PGTYPESnumeric_to_long(numeric *nv, long *lp)
Definition: numeric.c:1518
int PGTYPESnumeric_to_double(numeric *nv, double *dp)
Definition: numeric.c:1483
int PGTYPESnumeric_to_int(numeric *nv, int *ip)
Definition: numeric.c:1494
char * PGTYPESnumeric_to_asc(numeric *num, int dscale)
Definition: numeric.c:343
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:42
int PGTYPESnumeric_from_int(signed int int_val, numeric *var)
Definition: numeric.c:1309
int PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:765
numeric * PGTYPESnumeric_from_asc(char *str, char **endptr)
Definition: numeric.c:321
void PGTYPESnumeric_free(numeric *var)
Definition: numeric.c:385
int PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:1053
int PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:637
int main(void)
static void print_double(double x)
#define ECPGdebug(X, Y)
#define sprintf
Definition: port.h:240
#define printf(...)
Definition: port.h:244
#define sqlca
Definition: sqlca.h:59
Definition: c.h:690
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992