PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

int main (void)
 

Macro Definition Documentation

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

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

Referenced by main().

Function Documentation

int main ( void  )

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

References ECPGconnect(), ECPGdebug, ECPGdisconnect(), ECPGdo(), ECPGsetcommit(), ECPGst_normal, ECPGt_EOIT, ECPGt_EORT, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGtrans(), free, i, NULL, 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(), sqlca, and sqlprint().

36 {
37  char *text="error\n";
38  numeric *value1, *value2, *res;
39  /* exec sql begin declare section */
40 
41  /* = {0, 0, 0, 0, 0, NULL, NULL} ; */
42 
43 #line 22 "num_test.pgc"
44  numeric * des ;
45 /* exec sql end declare section */
46 #line 24 "num_test.pgc"
47 
48  double d;
49  long l1, l2;
50  int i;
51 
52  ECPGdebug(1, stderr);
53  /* exec sql whenever sqlerror do sqlprint ( ) ; */
54 #line 30 "num_test.pgc"
55 
56 
57  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
58 #line 32 "num_test.pgc"
59 
60 if (sqlca.sqlcode < 0) sqlprint ( );}
61 #line 32 "num_test.pgc"
62 
63 
64  { ECPGsetcommit(__LINE__, "off", NULL);
65 #line 34 "num_test.pgc"
66 
67 if (sqlca.sqlcode < 0) sqlprint ( );}
68 #line 34 "num_test.pgc"
69 
70  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test ( text char ( 5 ) , num numeric ( 14 , 7 ) )", ECPGt_EOIT, ECPGt_EORT);
71 #line 35 "num_test.pgc"
72 
73 if (sqlca.sqlcode < 0) sqlprint ( );}
74 #line 35 "num_test.pgc"
75 
76 
77  value1 = PGTYPESnumeric_new();
78  PGTYPESnumeric_from_int(1407, value1);
79  text = PGTYPESnumeric_to_asc(value1, -1);
80  printf("from int = %s\n", text);
81  free(text);
82  PGTYPESnumeric_free(value1);
83 
84  value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
85  value2 = PGTYPESnumeric_from_asc("10.0", NULL);
86  res = PGTYPESnumeric_new();
87  PGTYPESnumeric_add(value1, value2, res);
88  text = PGTYPESnumeric_to_asc(res, -1);
89  printf("add = %s\n", text);
90  free(text);
91 
92  PGTYPESnumeric_sub(res, value2, res);
93  text = PGTYPESnumeric_to_asc(res, -1);
94  printf("sub = %s\n", text);
95  free(text);
96  PGTYPESnumeric_free(value2);
97 
98  des = PGTYPESnumeric_new();
99  PGTYPESnumeric_copy(res, des);
100  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test ( text , num ) values ( 'test' , $1 )",
101  ECPGt_numeric,&(des),(long)1,(long)0,sizeof(numeric),
102  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
103 #line 60 "num_test.pgc"
104 
105 if (sqlca.sqlcode < 0) sqlprint ( );}
106 #line 60 "num_test.pgc"
107 
108 
109  value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
110  PGTYPESnumeric_mul(value1, value2, res);
111  PGTYPESnumeric_free(value2);
112 
113  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select num from test where text = 'test'", ECPGt_EOIT,
114  ECPGt_numeric,&(des),(long)1,(long)0,sizeof(numeric),
115  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
116 #line 66 "num_test.pgc"
117 
118 if (sqlca.sqlcode < 0) sqlprint ( );}
119 #line 66 "num_test.pgc"
120 
121 
122  PGTYPESnumeric_mul(res, des, res);
123  text = PGTYPESnumeric_to_asc(res, -1);
124  printf("mul = %s\n", text);
125  free(text);
126  PGTYPESnumeric_free(des);
127 
128  value2 = PGTYPESnumeric_from_asc("10000", NULL);
129  PGTYPESnumeric_div(res, value2, res);
130  text = PGTYPESnumeric_to_asc(res, -1);
131  PGTYPESnumeric_to_double(res, &d);
132  printf("div = %s %e\n", text, d);
133 
134  PGTYPESnumeric_free(value1);
135  PGTYPESnumeric_free(value2);
136 
137  value1 = PGTYPESnumeric_from_asc("2E7", NULL);
138  value2 = PGTYPESnumeric_from_asc("14", NULL);
139  i = PGTYPESnumeric_to_long(value1, &l1) | PGTYPESnumeric_to_long(value2, &l2);
140  printf("to long(%d) = %ld %ld\n", i, l1, l2);
141 
142  free(text);
143  PGTYPESnumeric_free(value1);
144  PGTYPESnumeric_free(value2);
145  PGTYPESnumeric_free(res);
146 
147  { ECPGtrans(__LINE__, NULL, "rollback");
148 #line 93 "num_test.pgc"
149 
150 if (sqlca.sqlcode < 0) sqlprint ( );}
151 #line 93 "num_test.pgc"
152 
153  { ECPGdisconnect(__LINE__, "CURRENT");
154 #line 94 "num_test.pgc"
155 
156 if (sqlca.sqlcode < 0) sqlprint ( );}
157 #line 94 "num_test.pgc"
158 
159 
160  return (0);
161 }
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:669
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:2030
#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
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
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
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
Definition: c.h:439
int PGTYPESnumeric_mul(numeric *, numeric *, numeric *)
Definition: numeric.c:981
int PGTYPESnumeric_sub(numeric *, numeric *, numeric *)
Definition: numeric.c:850