PostgreSQL Source Code  git master
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.

Referenced by main().

Function Documentation

◆ main()

int main ( void  )

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

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_long(), print_double(), printf, sqlca, and sqlprint().

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;
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);
109  PGTYPESchar_free(text);
110  PGTYPESnumeric_free(value1);
111 
112  value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
113  value2 = PGTYPESnumeric_from_asc("10.0", NULL);
114  res = PGTYPESnumeric_new();
115  PGTYPESnumeric_add(value1, value2, res);
116  text = PGTYPESnumeric_to_asc(res, -1);
117  printf("add = %s\n", text);
118  PGTYPESchar_free(text);
119 
120  PGTYPESnumeric_sub(res, value2, res);
121  text = PGTYPESnumeric_to_asc(res, -1);
122  printf("sub = %s\n", text);
123  PGTYPESchar_free(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);
151  text = PGTYPESnumeric_to_asc(res, -1);
152  printf("mul = %s\n", text);
153  PGTYPESchar_free(text);
154  PGTYPESnumeric_free(des);
155 
156  value2 = PGTYPESnumeric_from_asc("10000", NULL);
157  PGTYPESnumeric_div(res, value2, res);
158  text = PGTYPESnumeric_to_asc(res, -1);
159  PGTYPESnumeric_to_double(res, &d);
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 
172  PGTYPESchar_free(text);
173  PGTYPESnumeric_free(value1);
174  PGTYPESnumeric_free(value2);
175  PGTYPESnumeric_free(res);
176 
177  { ECPGtrans(__LINE__, NULL, "rollback");
178 #line 90 "num_test.pgc"
179 
180 if (sqlca.sqlcode < 0) sqlprint ( );}
181 #line 90 "num_test.pgc"
182 
183  { ECPGdisconnect(__LINE__, "CURRENT");
184 #line 91 "num_test.pgc"
185 
186 if (sqlca.sqlcode < 0) sqlprint ( );}
187 #line 91 "num_test.pgc"
188 
189 
190  return 0;
191 }
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:684
int PGTYPESnumeric_copy(numeric *, numeric *)
Definition: numeric.c:1475
#define sqlca
Definition: sqlca.h:59
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:2279
#define printf(...)
Definition: port.h:198
#define ECPGdebug(X, Y)
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:470
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:127
int PGTYPESnumeric_to_long(numeric *, long *)
Definition: numeric.c:1600
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:271
int PGTYPESnumeric_div(numeric *, numeric *, numeric *)
Definition: numeric.c:1138
int PGTYPESnumeric_from_int(signed int, numeric *)
Definition: numeric.c:1396
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
char * PGTYPESnumeric_to_asc(numeric *, int)
Definition: numeric.c:428
void sqlprint(void)
Definition: error.c:336
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:406
int PGTYPESnumeric_to_double(numeric *, double *)
Definition: numeric.c:1570
int PGTYPESnumeric_add(numeric *, numeric *, numeric *)
Definition: numeric.c:722
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:199
bool ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
Definition: connect.c:165
int i
static void print_double(double x)
Definition: c.h:549
int PGTYPESnumeric_mul(numeric *, numeric *, numeric *)
Definition: numeric.c:981
int PGTYPESnumeric_sub(numeric *, numeric *, numeric *)
Definition: numeric.c:850

◆ print_double()

static void print_double ( double  x)
static

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

References convert(), printf, and sprintf.

Referenced by main().

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 }
#define printf(...)
Definition: port.h:198
#define sprintf
Definition: port.h:194
static void convert(const int32 val, char *const buf)
Definition: zic.c:1959