alloc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*****************************************************************************
  2. *
  3. * MODULE: SQL statement parser library
  4. *
  5. * AUTHOR(S): lex.l and yac.y were originaly 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. {
  36. n += 15;
  37. st->Col = (SQLPVALUE *) realloc ( st->Col, n * sizeof(SQLPVALUE));
  38. st->ColType = (int *) realloc ( st->ColType, n * sizeof(int));
  39. st->ColWidth = (int *) realloc ( st->ColWidth, n * sizeof(int));
  40. st->ColDecim = (int *) realloc ( st->ColDecim, n * sizeof(int));
  41. for (i = st->nCol; i < n; i++)
  42. {
  43. st->Col[i].s = NULL ;
  44. }
  45. st->aCol = n;
  46. }
  47. return (1);
  48. }
  49. /* allocate space for values */
  50. int sqpAllocVal(SQLPSTMT *st, int n)
  51. {
  52. int i;
  53. if ( n > st->aVal )
  54. {
  55. n += 15;
  56. st->Val = (SQLPVALUE *) realloc ( st->Val, n * sizeof(SQLPVALUE));
  57. for (i = st->nVal; i < n; i++)
  58. {
  59. st->Val[i].s = NULL ;
  60. }
  61. st->aVal = n;
  62. }
  63. return (1);
  64. }
  65. /* free space allocated by parser */
  66. int sqpFreeStmt(SQLPSTMT *st)
  67. {
  68. int i;
  69. /* columns */
  70. for (i=0; i < st->aCol; i++)
  71. free ( st->Col[i].s );
  72. free ( st->Col );
  73. free ( st->ColType );
  74. free ( st->ColWidth );
  75. free ( st->ColDecim );
  76. st->aCol = 0;
  77. st->nCol = 0;
  78. /* values */
  79. for (i=0; i < st->aVal; i++)
  80. free ( st->Val[i].s );
  81. free ( st->Val );
  82. st->aVal = 0;
  83. st->nVal = 0;
  84. free (st->orderCol);
  85. /* Nodes (where) */
  86. if ( st->upperNodeptr )
  87. sqpFreeNode ( st->upperNodeptr );
  88. free ( st );
  89. return (1);
  90. }