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" getMaxOffset()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); 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 /* 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 } toString()203