PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pgtypeslib-num_test.c File Reference
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
#include <stdio.h>
#include <stdlib.h>
#include <pgtypes_numeric.h>
#include <decimal.h>
Include dependency graph for pgtypeslib-num_test.c:

Go to the source code of this file.

Macros

#define ECPGdebug(X, Y)   ECPGdebug((X)+100,(Y))
 

Functions

static void print_double (double x)
 
int main (void)
 

Macro Definition Documentation

◆ ECPGdebug

#define ECPGdebug (   X,
 
)    ECPGdebug((X)+100,(Y))

Definition at line 7 of file pgtypeslib-num_test.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 63 of file pgtypeslib-num_test.c.

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:2275
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
int i
Definition: isn.c:72
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
static void print_double(double x)
#define ECPGdebug(X, Y)
#define printf(...)
Definition: port.h:244
#define sqlca
Definition: sqlca.h:59
Definition: c.h:666

References ECPGconnect(), ECPGdebug, ECPGdisconnect(), ECPGdo(), ECPGsetcommit(), ECPGst_normal, ECPGt_EOIT, ECPGt_EORT, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGtrans(), i, PGTYPESchar_free(), PGTYPESnumeric_add(), PGTYPESnumeric_copy(), PGTYPESnumeric_div(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PGTYPESnumeric_from_int(), PGTYPESnumeric_mul(), PGTYPESnumeric_new(), PGTYPESnumeric_sub(), PGTYPESnumeric_to_asc(), PGTYPESnumeric_to_double(), PGTYPESnumeric_to_int(), PGTYPESnumeric_to_long(), print_double(), printf, res, sqlca, and sqlprint().

◆ print_double()

static void print_double ( double  x)
static

Definition at line 33 of file pgtypeslib-num_test.c.

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 }
int x
Definition: isn.c:70
#define sprintf
Definition: port.h:240
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992

References convert(), printf, sprintf, and x.

Referenced by main().