/src/interfaces/ecpg/preproc/descriptor.c

https://github.com/Epictetus/postgres · C · 346 lines · 315 code · 17 blank · 14 comment · 14 complexity · 52e47acf24e6aff7c351763377dc0f28 MD5 · raw file

  1. /*
  2. * functions needed for descriptor handling
  3. *
  4. * src/interfaces/ecpg/preproc/descriptor.c
  5. *
  6. * since descriptor might be either a string constant or a string var
  7. * we need to check for a constant if we expect a constant
  8. */
  9. #include "postgres_fe.h"
  10. #include "extern.h"
  11. /*
  12. * assignment handling function (descriptor)
  13. */
  14. static struct assignment *assignments;
  15. void
  16. push_assignment(char *var, enum ECPGdtype value)
  17. {
  18. struct assignment *new = (struct assignment *) mm_alloc(sizeof(struct assignment));
  19. new->next = assignments;
  20. new->variable = mm_alloc(strlen(var) + 1);
  21. strcpy(new->variable, var);
  22. new->value = value;
  23. assignments = new;
  24. }
  25. static void
  26. drop_assignments(void)
  27. {
  28. while (assignments)
  29. {
  30. struct assignment *old_head = assignments;
  31. assignments = old_head->next;
  32. free(old_head->variable);
  33. free(old_head);
  34. }
  35. }
  36. static void
  37. ECPGnumeric_lvalue(char *name)
  38. {
  39. const struct variable *v = find_variable(name);
  40. switch (v->type->type)
  41. {
  42. case ECPGt_short:
  43. case ECPGt_int:
  44. case ECPGt_long:
  45. case ECPGt_long_long:
  46. case ECPGt_unsigned_short:
  47. case ECPGt_unsigned_int:
  48. case ECPGt_unsigned_long:
  49. case ECPGt_unsigned_long_long:
  50. case ECPGt_const:
  51. fputs(name, yyout);
  52. break;
  53. default:
  54. mmerror(PARSE_ERROR, ET_ERROR, "variable \"%s\" must have a numeric type", name);
  55. break;
  56. }
  57. }
  58. /*
  59. * descriptor name lookup
  60. */
  61. static struct descriptor *descriptors;
  62. void
  63. add_descriptor(char *name, char *connection)
  64. {
  65. struct descriptor *new;
  66. if (name[0] != '"')
  67. return;
  68. new = (struct descriptor *) mm_alloc(sizeof(struct descriptor));
  69. new->next = descriptors;
  70. new->name = mm_alloc(strlen(name) + 1);
  71. strcpy(new->name, name);
  72. if (connection)
  73. {
  74. new->connection = mm_alloc(strlen(connection) + 1);
  75. strcpy(new->connection, connection);
  76. }
  77. else
  78. new->connection = connection;
  79. descriptors = new;
  80. }
  81. void
  82. drop_descriptor(char *name, char *connection)
  83. {
  84. struct descriptor *i;
  85. struct descriptor **lastptr = &descriptors;
  86. if (name[0] != '"')
  87. return;
  88. for (i = descriptors; i; lastptr = &i->next, i = i->next)
  89. {
  90. if (!strcmp(name, i->name))
  91. {
  92. if ((!connection && !i->connection)
  93. || (connection && i->connection
  94. && !strcmp(connection, i->connection)))
  95. {
  96. *lastptr = i->next;
  97. if (i->connection)
  98. free(i->connection);
  99. free(i->name);
  100. free(i);
  101. return;
  102. }
  103. }
  104. }
  105. mmerror(PARSE_ERROR, ET_WARNING, "descriptor \"%s\" does not exist", name);
  106. }
  107. struct descriptor
  108. *
  109. lookup_descriptor(char *name, char *connection)
  110. {
  111. struct descriptor *i;
  112. if (name[0] != '"')
  113. return NULL;
  114. for (i = descriptors; i; i = i->next)
  115. {
  116. if (!strcmp(name, i->name))
  117. {
  118. if ((!connection && !i->connection)
  119. || (connection && i->connection
  120. && !strcmp(connection, i->connection)))
  121. return i;
  122. }
  123. }
  124. mmerror(PARSE_ERROR, ET_WARNING, "descriptor \"%s\" does not exist", name);
  125. return NULL;
  126. }
  127. void
  128. output_get_descr_header(char *desc_name)
  129. {
  130. struct assignment *results;
  131. fprintf(yyout, "{ ECPGget_desc_header(__LINE__, %s, &(", desc_name);
  132. for (results = assignments; results != NULL; results = results->next)
  133. {
  134. if (results->value == ECPGd_count)
  135. ECPGnumeric_lvalue(results->variable);
  136. else
  137. mmerror(PARSE_ERROR, ET_WARNING, "descriptor header item \"%d\" does not exist", results->value);
  138. }
  139. drop_assignments();
  140. fprintf(yyout, "));\n");
  141. whenever_action(3);
  142. }
  143. void
  144. output_get_descr(char *desc_name, char *index)
  145. {
  146. struct assignment *results;
  147. fprintf(yyout, "{ ECPGget_desc(__LINE__, %s, %s,", desc_name, index);
  148. for (results = assignments; results != NULL; results = results->next)
  149. {
  150. const struct variable *v = find_variable(results->variable);
  151. switch (results->value)
  152. {
  153. case ECPGd_nullable:
  154. mmerror(PARSE_ERROR, ET_WARNING, "nullable is always 1");
  155. break;
  156. case ECPGd_key_member:
  157. mmerror(PARSE_ERROR, ET_WARNING, "key_member is always 0");
  158. break;
  159. default:
  160. break;
  161. }
  162. fprintf(yyout, "%s,", get_dtype(results->value));
  163. ECPGdump_a_type(yyout, v->name, v->type, v->brace_level, NULL, NULL, -1, NULL, NULL, mm_strdup("0"), NULL, NULL);
  164. }
  165. drop_assignments();
  166. fputs("ECPGd_EODT);\n", yyout);
  167. whenever_action(2 | 1);
  168. }
  169. void
  170. output_set_descr_header(char *desc_name)
  171. {
  172. struct assignment *results;
  173. fprintf(yyout, "{ ECPGset_desc_header(__LINE__, %s, (int)(", desc_name);
  174. for (results = assignments; results != NULL; results = results->next)
  175. {
  176. if (results->value == ECPGd_count)
  177. ECPGnumeric_lvalue(results->variable);
  178. else
  179. mmerror(PARSE_ERROR, ET_WARNING, "descriptor header item \"%d\" does not exist", results->value);
  180. }
  181. drop_assignments();
  182. fprintf(yyout, "));\n");
  183. whenever_action(3);
  184. }
  185. static const char *
  186. descriptor_item_name(enum ECPGdtype itemcode)
  187. {
  188. switch (itemcode)
  189. {
  190. case ECPGd_cardinality:
  191. return "CARDINALITY";
  192. case ECPGd_count:
  193. return "COUNT";
  194. case ECPGd_data:
  195. return "DATA";
  196. case ECPGd_di_code:
  197. return "DATETIME_INTERVAL_CODE";
  198. case ECPGd_di_precision:
  199. return "DATETIME_INTERVAL_PRECISION";
  200. case ECPGd_indicator:
  201. return "INDICATOR";
  202. case ECPGd_key_member:
  203. return "KEY_MEMBER";
  204. case ECPGd_length:
  205. return "LENGTH";
  206. case ECPGd_name:
  207. return "NAME";
  208. case ECPGd_nullable:
  209. return "NULLABLE";
  210. case ECPGd_octet:
  211. return "OCTET_LENGTH";
  212. case ECPGd_precision:
  213. return "PRECISION";
  214. case ECPGd_ret_length:
  215. return "RETURNED_LENGTH";
  216. case ECPGd_ret_octet:
  217. return "RETURNED_OCTET_LENGTH";
  218. case ECPGd_scale:
  219. return "SCALE";
  220. case ECPGd_type:
  221. return "TYPE";
  222. default:
  223. return NULL;
  224. }
  225. }
  226. void
  227. output_set_descr(char *desc_name, char *index)
  228. {
  229. struct assignment *results;
  230. fprintf(yyout, "{ ECPGset_desc(__LINE__, %s, %s,", desc_name, index);
  231. for (results = assignments; results != NULL; results = results->next)
  232. {
  233. const struct variable *v = find_variable(results->variable);
  234. switch (results->value)
  235. {
  236. case ECPGd_cardinality:
  237. case ECPGd_di_code:
  238. case ECPGd_di_precision:
  239. case ECPGd_precision:
  240. case ECPGd_scale:
  241. mmerror(PARSE_ERROR, ET_FATAL, "descriptor item \"%s\" is not implemented",
  242. descriptor_item_name(results->value));
  243. break;
  244. case ECPGd_key_member:
  245. case ECPGd_name:
  246. case ECPGd_nullable:
  247. case ECPGd_octet:
  248. case ECPGd_ret_length:
  249. case ECPGd_ret_octet:
  250. mmerror(PARSE_ERROR, ET_FATAL, "descriptor item \"%s\" cannot be set",
  251. descriptor_item_name(results->value));
  252. break;
  253. case ECPGd_data:
  254. case ECPGd_indicator:
  255. case ECPGd_length:
  256. case ECPGd_type:
  257. fprintf(yyout, "%s,", get_dtype(results->value));
  258. ECPGdump_a_type(yyout, v->name, v->type, v->brace_level, NULL, NULL, -1, NULL, NULL, mm_strdup("0"), NULL, NULL);
  259. break;
  260. default:
  261. ;
  262. }
  263. }
  264. drop_assignments();
  265. fputs("ECPGd_EODT);\n", yyout);
  266. whenever_action(2 | 1);
  267. }
  268. /* I consider dynamic allocation overkill since at most two descriptor
  269. variables are possible per statement. (input and output descriptor)
  270. And descriptors are no normal variables, so they don't belong into
  271. the variable list.
  272. */
  273. #define MAX_DESCRIPTOR_NAMELEN 128
  274. struct variable *
  275. descriptor_variable(const char *name, int input)
  276. {
  277. static char descriptor_names[2][MAX_DESCRIPTOR_NAMELEN];
  278. static const struct ECPGtype descriptor_type = {ECPGt_descriptor, NULL, NULL, NULL, {NULL}, 0};
  279. static const struct variable varspace[2] = {
  280. {descriptor_names[0], (struct ECPGtype *) & descriptor_type, 0, NULL},
  281. {descriptor_names[1], (struct ECPGtype *) & descriptor_type, 0, NULL}
  282. };
  283. strlcpy(descriptor_names[input], name, sizeof(descriptor_names[input]));
  284. return (struct variable *) & varspace[input];
  285. }
  286. struct variable *
  287. sqlda_variable(const char *name)
  288. {
  289. struct variable *p = (struct variable *) mm_alloc(sizeof(struct variable));
  290. p->name = mm_strdup(name);
  291. p->type = (struct ECPGtype *) mm_alloc(sizeof(struct ECPGtype));
  292. p->type->type = ECPGt_sqlda;
  293. p->type->size = NULL;
  294. p->type->struct_sizeof = NULL;
  295. p->type->u.element = NULL;
  296. p->type->counter = 0;
  297. p->brace_level = 0;
  298. p->next = NULL;
  299. return p;
  300. }