alloc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*****************************************************************************
  2. *
  3. * MODULE: SQL statement parser library
  4. *
  5. * AUTHOR(S): lex.l and yac.y were originally taken from unixODBC and
  6. * probably written by Peter Harvey <pharvey@codebydesigns.com>,
  7. * modifications and other code by Radim Blazek
  8. *
  9. * PURPOSE: Parse input string containing SQL statement to
  10. * SQLPSTMT structure.
  11. * SQL parser may be used by simple database drivers.
  12. *
  13. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. *****************************************************************************/
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <grass/sqlp.h>
  23. /* alloc structure */
  24. SQLPSTMT *sqpInitStmt(void)
  25. {
  26. SQLPSTMT *st;
  27. st = (SQLPSTMT *) calloc(1, sizeof(SQLPSTMT));
  28. return (st);
  29. }
  30. /* allocate space for columns */
  31. int sqpAllocCol(SQLPSTMT * st, int n)
  32. {
  33. int i;
  34. if (n > st->aCol) {
  35. n += 15;
  36. st->Col = (SQLPVALUE *) realloc(st->Col, n * sizeof(SQLPVALUE));
  37. st->ColType = (int *)realloc(st->ColType, n * sizeof(int));
  38. st->ColWidth = (int *)realloc(st->ColWidth, n * sizeof(int));
  39. st->ColDecim = (int *)realloc(st->ColDecim, n * sizeof(int));
  40. for (i = st->nCol; i < n; i++) {
  41. st->Col[i].s = NULL;
  42. }
  43. st->aCol = n;
  44. }
  45. return (1);
  46. }
  47. /* allocate space for values */
  48. int sqpAllocVal(SQLPSTMT * st, int n)
  49. {
  50. int i;
  51. if (n > st->aVal) {
  52. n += 15;
  53. st->Val = (SQLPVALUE *) realloc(st->Val, n * sizeof(SQLPVALUE));
  54. for (i = st->nVal; i < n; i++) {
  55. st->Val[i].s = NULL;
  56. }
  57. st->aVal = n;
  58. }
  59. return (1);
  60. }
  61. /* free space allocated by parser */
  62. int sqpFreeStmt(SQLPSTMT * st)
  63. {
  64. int i;
  65. /* columns */
  66. for (i = 0; i < st->aCol; i++)
  67. free(st->Col[i].s);
  68. free(st->Col);
  69. free(st->ColType);
  70. free(st->ColWidth);
  71. free(st->ColDecim);
  72. st->aCol = 0;
  73. st->nCol = 0;
  74. /* values */
  75. for (i = 0; i < st->aVal; i++)
  76. free(st->Val[i].s);
  77. free(st->Val);
  78. st->aVal = 0;
  79. st->nVal = 0;
  80. free(st->orderCol);
  81. /* Nodes (where) */
  82. if (st->upperNodeptr)
  83. sqpFreeNode(st->upperNodeptr);
  84. free(st);
  85. return (1);
  86. }