GRASS Programmer's Manual
6.4.1(2011)
|
00001 00002 /***************************************************************************** 00003 * 00004 * MODULE: SQL statement parser library 00005 * 00006 * AUTHOR(S): lex.l and yac.y were originaly taken from unixODBC and 00007 * probably written by Peter Harvey <pharvey@codebydesigns.com>, 00008 * modifications and other code by Radim Blazek 00009 * 00010 * PURPOSE: Parse input string containing SQL statement to 00011 * SQLPSTMT structure. 00012 * SQL parser may be used by simple database drivers. 00013 * 00014 * COPYRIGHT: (C) 2000 by the GRASS Development Team 00015 * 00016 * This program is free software under the GNU General Public 00017 * License (>=v2). Read the file COPYING that comes with GRASS 00018 * for details. 00019 * 00020 *****************************************************************************/ 00021 00022 #include <stdlib.h> 00023 #include <stdio.h> 00024 #include <grass/sqlp.h> 00025 00026 /* alloc structure */ 00027 SQLPSTMT *sqpInitStmt(void) 00028 { 00029 SQLPSTMT *st; 00030 00031 st = (SQLPSTMT *) calloc(1, sizeof(SQLPSTMT)); 00032 00033 return (st); 00034 } 00035 00036 /* allocate space for columns */ 00037 int sqpAllocCol(SQLPSTMT * st, int n) 00038 { 00039 int i; 00040 00041 if (n > st->aCol) { 00042 n += 15; 00043 st->Col = (SQLPVALUE *) realloc(st->Col, n * sizeof(SQLPVALUE)); 00044 st->ColType = (int *)realloc(st->ColType, n * sizeof(int)); 00045 st->ColWidth = (int *)realloc(st->ColWidth, n * sizeof(int)); 00046 st->ColDecim = (int *)realloc(st->ColDecim, n * sizeof(int)); 00047 00048 for (i = st->nCol; i < n; i++) { 00049 st->Col[i].s = NULL; 00050 } 00051 00052 st->aCol = n; 00053 } 00054 return (1); 00055 } 00056 00057 /* allocate space for values */ 00058 int sqpAllocVal(SQLPSTMT * st, int n) 00059 { 00060 int i; 00061 00062 if (n > st->aVal) { 00063 n += 15; 00064 st->Val = (SQLPVALUE *) realloc(st->Val, n * sizeof(SQLPVALUE)); 00065 00066 for (i = st->nVal; i < n; i++) { 00067 st->Val[i].s = NULL; 00068 } 00069 00070 st->aVal = n; 00071 } 00072 return (1); 00073 } 00074 00075 /* free space allocated by parser */ 00076 int sqpFreeStmt(SQLPSTMT * st) 00077 { 00078 int i; 00079 00080 /* columns */ 00081 for (i = 0; i < st->aCol; i++) 00082 free(st->Col[i].s); 00083 00084 free(st->Col); 00085 free(st->ColType); 00086 free(st->ColWidth); 00087 free(st->ColDecim); 00088 st->aCol = 0; 00089 st->nCol = 0; 00090 00091 /* values */ 00092 for (i = 0; i < st->aVal; i++) 00093 free(st->Val[i].s); 00094 00095 free(st->Val); 00096 st->aVal = 0; 00097 st->nVal = 0; 00098 00099 free(st->orderCol); 00100 00101 /* Nodes (where) */ 00102 if (st->upperNodeptr) 00103 sqpFreeNode(st->upperNodeptr); 00104 00105 free(st); 00106 return (1); 00107 }