GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
db/sqlp/alloc.c
Go to the documentation of this file.
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 * SPDX-FileCopyrightText: 2000 GRASS Development Team
14 * SPDX-License-Identifier: GPL-2.0-or-later
15 *
16 *****************************************************************************/
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <grass/sqlp.h>
21
22/* alloc structure */
24{
25 SQLPSTMT *st;
26
27 st = (SQLPSTMT *)calloc(1, sizeof(SQLPSTMT));
28
29 return (st);
30}
31
32/* allocate space for columns */
33int sqpAllocCol(SQLPSTMT *st, int n)
34{
35 int i;
36
37 if (n > st->aCol) {
38 n += 15;
39 st->Col = (SQLPVALUE *)realloc(st->Col, n * sizeof(SQLPVALUE));
40 st->ColType = (int *)realloc(st->ColType, n * sizeof(int));
41 st->ColWidth = (int *)realloc(st->ColWidth, n * sizeof(int));
42 st->ColDecim = (int *)realloc(st->ColDecim, n * sizeof(int));
43
44 for (i = st->nCol; i < n; i++) {
45 st->Col[i].s = NULL;
46 }
47
48 st->aCol = n;
49 }
50 return (1);
51}
52
53/* allocate space for values */
54int sqpAllocVal(SQLPSTMT *st, int n)
55{
56 int i;
57
58 if (n > st->aVal) {
59 n += 15;
60 st->Val = (SQLPVALUE *)realloc(st->Val, n * sizeof(SQLPVALUE));
61
62 for (i = st->nVal; i < n; i++) {
63 st->Val[i].s = NULL;
64 }
65
66 st->aVal = n;
67 }
68 return (1);
69}
70
71/* free space allocated by parser */
73{
74 int i;
75
76 /* columns */
77 for (i = 0; i < st->aCol; i++)
78 free(st->Col[i].s);
79
80 free(st->Col);
81 free(st->ColType);
82 free(st->ColWidth);
83 free(st->ColDecim);
84 st->aCol = 0;
85 st->nCol = 0;
86
87 /* values */
88 for (i = 0; i < st->aVal; i++)
89 free(st->Val[i].s);
90
91 free(st->Val);
92 st->aVal = 0;
93 st->nVal = 0;
94
95 free(st->orderCol);
96
97 /* Nodes (where) */
98 if (st->upperNodeptr)
99 sqpFreeNode(st->upperNodeptr);
100
101 free(st);
102 return (1);
103}
#define NULL
Definition ccmath.h:32
int sqpFreeStmt(SQLPSTMT *st)
int sqpAllocCol(SQLPSTMT *st, int n)
SQLPSTMT * sqpInitStmt(void)
int sqpAllocVal(SQLPSTMT *st, int n)
void sqpFreeNode(SQLPNODE *)
Definition sql.c:256
struct state * st
Definition parser.c:102
void free(void *)