/src/interfaces/ecpg/test/expected/preproc-outofscope.c

https://github.com/abee008/postgres · C · 372 lines · 243 code · 117 blank · 12 comment · 19 complexity · 4ae59b7787a79f2ab1fc89c5236f8842 MD5 · raw 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. #line 1 "outofscope.pgc"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #line 1 "regression.h"
  14. #line 6 "outofscope.pgc"
  15. #line 1 "pgtypes_numeric.h"
  16. #ifndef PGTYPES_NUMERIC
  17. #define PGTYPES_NUMERIC
  18. #include <pgtypes.h>
  19. #define NUMERIC_POS 0x0000
  20. #define NUMERIC_NEG 0x4000
  21. #define NUMERIC_NAN 0xC000
  22. #define NUMERIC_NULL 0xF000
  23. #define NUMERIC_MAX_PRECISION 1000
  24. #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
  25. #define NUMERIC_MIN_DISPLAY_SCALE 0
  26. #define NUMERIC_MIN_SIG_DIGITS 16
  27. #define DECSIZE 30
  28. typedef unsigned char NumericDigit;
  29. typedef struct
  30. {
  31. int ndigits; /* number of digits in digits[] - can be 0! */
  32. int weight; /* weight of first digit */
  33. int rscale; /* result scale */
  34. int dscale; /* display scale */
  35. int sign; /* NUMERIC_POS, NUMERIC_NEG, or NUMERIC_NAN */
  36. NumericDigit *buf; /* start of alloc'd space for digits[] */
  37. NumericDigit *digits; /* decimal digits */
  38. } numeric;
  39. typedef struct
  40. {
  41. int ndigits; /* number of digits in digits[] - can be 0! */
  42. int weight; /* weight of first digit */
  43. int rscale; /* result scale */
  44. int dscale; /* display scale */
  45. int sign; /* NUMERIC_POS, NUMERIC_NEG, or NUMERIC_NAN */
  46. NumericDigit digits[DECSIZE]; /* decimal digits */
  47. } decimal;
  48. #ifdef __cplusplus
  49. extern "C"
  50. {
  51. #endif
  52. numeric *PGTYPESnumeric_new(void);
  53. decimal *PGTYPESdecimal_new(void);
  54. void PGTYPESnumeric_free(numeric *);
  55. void PGTYPESdecimal_free(decimal *);
  56. numeric *PGTYPESnumeric_from_asc(char *, char **);
  57. char *PGTYPESnumeric_to_asc(numeric *, int);
  58. int PGTYPESnumeric_add(numeric *, numeric *, numeric *);
  59. int PGTYPESnumeric_sub(numeric *, numeric *, numeric *);
  60. int PGTYPESnumeric_mul(numeric *, numeric *, numeric *);
  61. int PGTYPESnumeric_div(numeric *, numeric *, numeric *);
  62. int PGTYPESnumeric_cmp(numeric *, numeric *);
  63. int PGTYPESnumeric_from_int(signed int, numeric *);
  64. int PGTYPESnumeric_from_long(signed long int, numeric *);
  65. int PGTYPESnumeric_copy(numeric *, numeric *);
  66. int PGTYPESnumeric_from_double(double, numeric *);
  67. int PGTYPESnumeric_to_double(numeric *, double *);
  68. int PGTYPESnumeric_to_int(numeric *, int *);
  69. int PGTYPESnumeric_to_long(numeric *, long *);
  70. int PGTYPESnumeric_to_decimal(numeric *, decimal *);
  71. int PGTYPESnumeric_from_decimal(decimal *, numeric *);
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif /* PGTYPES_NUMERIC */
  76. #line 8 "outofscope.pgc"
  77. /* exec sql begin declare section */
  78. #line 1 "struct.h"
  79. /* dec_t */
  80. typedef struct mytype MYTYPE ;
  81. #line 9 "struct.h"
  82. typedef struct mynulltype MYNULLTYPE ;
  83. #line 19 "struct.h"
  84. #line 11 "outofscope.pgc"
  85. struct mytype {
  86. #line 3 "struct.h"
  87. int id ;
  88. #line 4 "struct.h"
  89. char t [ 64 ] ;
  90. #line 5 "struct.h"
  91. double d1 ;
  92. #line 6 "struct.h"
  93. double d2 ;
  94. #line 7 "struct.h"
  95. char c [ 30 ] ;
  96. } ; struct mynulltype {
  97. #line 13 "struct.h"
  98. int id ;
  99. #line 14 "struct.h"
  100. int t ;
  101. #line 15 "struct.h"
  102. int d1 ;
  103. #line 16 "struct.h"
  104. int d2 ;
  105. #line 17 "struct.h"
  106. int c ;
  107. } ;/* exec sql end declare section */
  108. #line 12 "outofscope.pgc"
  109. /* exec sql whenever sqlerror stop ; */
  110. #line 14 "outofscope.pgc"
  111. /* Functions for test 1 */
  112. static void
  113. get_var1(MYTYPE **myvar0, MYNULLTYPE **mynullvar0)
  114. {
  115. /* exec sql begin declare section */
  116. #line 22 "outofscope.pgc"
  117. MYTYPE * myvar = malloc ( sizeof ( MYTYPE ) ) ;
  118. #line 23 "outofscope.pgc"
  119. MYNULLTYPE * mynullvar = malloc ( sizeof ( MYNULLTYPE ) ) ;
  120. /* exec sql end declare section */
  121. #line 24 "outofscope.pgc"
  122. /* Test DECLARE ... SELECT ... INTO with pointers */
  123. ECPGset_var( 0, ( myvar ), __LINE__);\
  124. ECPGset_var( 1, ( mynullvar ), __LINE__);\
  125. /* declare mycur cursor for select * from a1 */
  126. #line 28 "outofscope.pgc"
  127. if (sqlca.sqlcode < 0) exit (1);
  128. #line 28 "outofscope.pgc"
  129. #line 28 "outofscope.pgc"
  130. if (sqlca.sqlcode != 0)
  131. exit(1);
  132. *myvar0 = myvar;
  133. *mynullvar0 = mynullvar;
  134. }
  135. static void
  136. open_cur1(void)
  137. {
  138. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare mycur cursor for select * from a1", ECPGt_EOIT,
  139. ECPGt_int,&((*( MYTYPE *)(ECPGget_var( 0)) ).id),(long)1,(long)1,sizeof( struct mytype ),
  140. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).id),(long)1,(long)1,sizeof( struct mynulltype ),
  141. ECPGt_char,&((*( MYTYPE *)(ECPGget_var( 0)) ).t),(long)64,(long)1,sizeof( struct mytype ),
  142. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).t),(long)1,(long)1,sizeof( struct mynulltype ),
  143. ECPGt_double,&((*( MYTYPE *)(ECPGget_var( 0)) ).d1),(long)1,(long)1,sizeof( struct mytype ),
  144. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).d1),(long)1,(long)1,sizeof( struct mynulltype ),
  145. ECPGt_double,&((*( MYTYPE *)(ECPGget_var( 0)) ).d2),(long)1,(long)1,sizeof( struct mytype ),
  146. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).d2),(long)1,(long)1,sizeof( struct mynulltype ),
  147. ECPGt_char,&((*( MYTYPE *)(ECPGget_var( 0)) ).c),(long)30,(long)1,sizeof( struct mytype ),
  148. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).c),(long)1,(long)1,sizeof( struct mynulltype ), ECPGt_EORT);
  149. #line 40 "outofscope.pgc"
  150. if (sqlca.sqlcode < 0) exit (1);}
  151. #line 40 "outofscope.pgc"
  152. }
  153. static void
  154. get_record1(void)
  155. {
  156. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch mycur", ECPGt_EOIT,
  157. ECPGt_int,&((*( MYTYPE *)(ECPGget_var( 0)) ).id),(long)1,(long)1,sizeof( struct mytype ),
  158. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).id),(long)1,(long)1,sizeof( struct mynulltype ),
  159. ECPGt_char,&((*( MYTYPE *)(ECPGget_var( 0)) ).t),(long)64,(long)1,sizeof( struct mytype ),
  160. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).t),(long)1,(long)1,sizeof( struct mynulltype ),
  161. ECPGt_double,&((*( MYTYPE *)(ECPGget_var( 0)) ).d1),(long)1,(long)1,sizeof( struct mytype ),
  162. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).d1),(long)1,(long)1,sizeof( struct mynulltype ),
  163. ECPGt_double,&((*( MYTYPE *)(ECPGget_var( 0)) ).d2),(long)1,(long)1,sizeof( struct mytype ),
  164. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).d2),(long)1,(long)1,sizeof( struct mynulltype ),
  165. ECPGt_char,&((*( MYTYPE *)(ECPGget_var( 0)) ).c),(long)30,(long)1,sizeof( struct mytype ),
  166. ECPGt_int,&((*( MYNULLTYPE *)(ECPGget_var( 1)) ).c),(long)1,(long)1,sizeof( struct mynulltype ), ECPGt_EORT);
  167. #line 46 "outofscope.pgc"
  168. if (sqlca.sqlcode < 0) exit (1);}
  169. #line 46 "outofscope.pgc"
  170. }
  171. static void
  172. close_cur1(void)
  173. {
  174. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close mycur", ECPGt_EOIT, ECPGt_EORT);
  175. #line 52 "outofscope.pgc"
  176. if (sqlca.sqlcode < 0) exit (1);}
  177. #line 52 "outofscope.pgc"
  178. }
  179. int
  180. main (void)
  181. {
  182. MYTYPE *myvar;
  183. MYNULLTYPE *mynullvar;
  184. int loopcount;
  185. char msg[128];
  186. ECPGdebug(1, stderr);
  187. strcpy(msg, "connect");
  188. { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0);
  189. #line 66 "outofscope.pgc"
  190. if (sqlca.sqlcode < 0) exit (1);}
  191. #line 66 "outofscope.pgc"
  192. strcpy(msg, "set");
  193. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
  194. #line 69 "outofscope.pgc"
  195. if (sqlca.sqlcode < 0) exit (1);}
  196. #line 69 "outofscope.pgc"
  197. strcpy(msg, "create");
  198. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table a1 ( id serial primary key , t text , d1 numeric , d2 float8 , c character ( 10 ) )", ECPGt_EOIT, ECPGt_EORT);
  199. #line 72 "outofscope.pgc"
  200. if (sqlca.sqlcode < 0) exit (1);}
  201. #line 72 "outofscope.pgc"
  202. strcpy(msg, "insert");
  203. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into a1 ( id , t , d1 , d2 , c ) values ( default , 'a' , 1.0 , 2 , 'a' )", ECPGt_EOIT, ECPGt_EORT);
  204. #line 75 "outofscope.pgc"
  205. if (sqlca.sqlcode < 0) exit (1);}
  206. #line 75 "outofscope.pgc"
  207. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into a1 ( id , t , d1 , d2 , c ) values ( default , null , null , null , null )", ECPGt_EOIT, ECPGt_EORT);
  208. #line 76 "outofscope.pgc"
  209. if (sqlca.sqlcode < 0) exit (1);}
  210. #line 76 "outofscope.pgc"
  211. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into a1 ( id , t , d1 , d2 , c ) values ( default , 'b' , 2.0 , 3 , 'b' )", ECPGt_EOIT, ECPGt_EORT);
  212. #line 77 "outofscope.pgc"
  213. if (sqlca.sqlcode < 0) exit (1);}
  214. #line 77 "outofscope.pgc"
  215. strcpy(msg, "commit");
  216. { ECPGtrans(__LINE__, NULL, "commit");
  217. #line 80 "outofscope.pgc"
  218. if (sqlca.sqlcode < 0) exit (1);}
  219. #line 80 "outofscope.pgc"
  220. /* Test out-of-scope DECLARE/OPEN/FETCH/CLOSE */
  221. get_var1(&myvar, &mynullvar);
  222. open_cur1();
  223. for (loopcount = 0; loopcount < 100; loopcount++)
  224. {
  225. memset(myvar, 0, sizeof(MYTYPE));
  226. get_record1();
  227. if (sqlca.sqlcode == ECPG_NOT_FOUND)
  228. break;
  229. printf("id=%d%s t='%s'%s d1=%f%s d2=%f%s c = '%s'%s\n",
  230. myvar->id, mynullvar->id ? " (NULL)" : "",
  231. myvar->t, mynullvar->t ? " (NULL)" : "",
  232. myvar->d1, mynullvar->d1 ? " (NULL)" : "",
  233. myvar->d2, mynullvar->d2 ? " (NULL)" : "",
  234. myvar->c, mynullvar->c ? " (NULL)" : "");
  235. }
  236. close_cur1();
  237. free(myvar);
  238. free(mynullvar);
  239. strcpy(msg, "drop");
  240. { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table a1", ECPGt_EOIT, ECPGt_EORT);
  241. #line 107 "outofscope.pgc"
  242. if (sqlca.sqlcode < 0) exit (1);}
  243. #line 107 "outofscope.pgc"
  244. strcpy(msg, "commit");
  245. { ECPGtrans(__LINE__, NULL, "commit");
  246. #line 110 "outofscope.pgc"
  247. if (sqlca.sqlcode < 0) exit (1);}
  248. #line 110 "outofscope.pgc"
  249. strcpy(msg, "disconnect");
  250. { ECPGdisconnect(__LINE__, "CURRENT");
  251. #line 113 "outofscope.pgc"
  252. if (sqlca.sqlcode < 0) exit (1);}
  253. #line 113 "outofscope.pgc"
  254. return 0;
  255. }