GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
print.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  * 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
17  * GRASS for details.
18  *
19  *****************************************************************************/
20 
21 #include <grass/sqlp.h>
22 #include <stdio.h>
23 
24 static void print_node(SQLPNODE *nptr, int level)
25 {
26  int i;
27 
28  for (i = 0; i < level; i++) {
29  fprintf(stderr, " ");
30  }
31 
32  if (nptr->node_type == SQLP_NODE_EXPRESSION) {
33  fprintf(stderr, "op: %s\n", sqpOperatorName(nptr->oper));
34  if (nptr->left) {
35  print_node(nptr->left, level + 1);
36  }
37  if (nptr->right) {
38  print_node(nptr->right, level + 1);
39  }
40  }
41  else if (nptr->node_type == SQLP_NODE_VALUE) {
42  switch (nptr->value.type) {
43  case SQLP_NULL:
44  fprintf(stderr, "val: NULL\n");
45  break;
46  case SQLP_D:
47  fprintf(stderr, "val: %e\n", nptr->value.d);
48  break;
49  case SQLP_I:
50  fprintf(stderr, "val: %d\n", nptr->value.i);
51  break;
52  case SQLP_S:
53  fprintf(stderr, "val: '%s'\n", nptr->value.s);
54  break;
55  }
56  }
57  else { /* SQLP_NODE_COLUMN */
58  fprintf(stderr, "col: %s\n", nptr->column_name);
59  }
60 }
61 
63 {
64  int i;
65 
66  fprintf(stderr, "********** SQL PARSER RESULT **********\n");
67  fprintf(stderr, "INPUT: %s\n", sqlpStmt->stmt);
68  fprintf(stderr, "COMMAND: ");
69  switch (sqlpStmt->command) {
70  case (SQLP_ADD_COLUMN):
71  fprintf(stderr, "ADD COLUMN\n");
72  break;
73  case (SQLP_CREATE):
74  fprintf(stderr, "CREATE\n");
75  break;
76  case (SQLP_DROP):
77  fprintf(stderr, "DROP\n");
78  break;
79  case (SQLP_DROP_COLUMN):
80  fprintf(stderr, "DROP COLUMN\n");
81  break;
82  case (SQLP_INSERT):
83  fprintf(stderr, "INSERT\n");
84  break;
85  case (SQLP_UPDATE):
86  fprintf(stderr, "UPDATE\n");
87  break;
88  case (SQLP_SELECT):
89  fprintf(stderr, "SELECT\n");
90  break;
91  case (SQLP_DELETE):
92  fprintf(stderr, "DELETE\n");
93  break;
94  default:
95  fprintf(stderr, "UNKNOWN\n");
96  }
97 
98  fprintf(stderr, "TABLE: %s\n", sqlpStmt->table);
99 
100  /* columns */
101  for (i = 0; i < st->nCol; i++) {
102  if (sqlpStmt->command == SQLP_CREATE) {
103  fprintf(stderr, "COLUMN %2d: ", i + 1);
104  switch (sqlpStmt->ColType[i]) {
105  case (SQLP_VARCHAR):
106  fprintf(stderr, "type:varchar width:%d", sqlpStmt->ColWidth[i]);
107  break;
108  case (SQLP_INTEGER):
109  fprintf(stderr, "type:integer");
110  break;
111  case (SQLP_DOUBLE):
112  fprintf(stderr, "type:double");
113  break;
114  case (SQLP_DATE):
115  fprintf(stderr, "type:date");
116  break;
117  case (SQLP_TIME):
118  fprintf(stderr, "type:time");
119  break;
120  default:
121  fprintf(stderr, "type:unknown");
122  break;
123  }
124  fprintf(stderr, " name:%s\n", sqlpStmt->Col[i].s);
125  }
126  else {
127  fprintf(stderr, "COLUMN %2d: %s\n", i + 1, sqlpStmt->Col[i].s);
128  }
129  }
130 
131  /* values */
132  for (i = 0; i < st->nVal; i++) {
133  fprintf(stderr, "VALUE %2d ", i + 1);
134  switch (sqlpStmt->Val[i].type) {
135  case (SQLP_S):
136  fprintf(stderr, "(string) : %s\n", sqlpStmt->Val[i].s);
137  break;
138  case (SQLP_I):
139  fprintf(stderr, "(integer): %d\n", sqlpStmt->Val[i].i);
140  break;
141  case (SQLP_D):
142  fprintf(stderr, "(float) : %f\n", sqlpStmt->Val[i].d);
143  break;
144  case (SQLP_NULL):
145  fprintf(stderr, "(unknown) : null\n");
146  break;
147  case (SQLP_EXPR):
148  fprintf(stderr, "(expression) :\n");
149  print_node(sqlpStmt->Val[i].expr, 0);
150  break;
151  default:
152  fprintf(stderr, "unknown\n");
153  break;
154  }
155  }
156 
157  if (sqlpStmt->upperNodeptr) {
158  fprintf(stderr, "WHERE:\n");
159  print_node(sqlpStmt->upperNodeptr, 0);
160  }
161 
162  if (sqlpStmt->command == SQLP_SELECT) {
163  if (sqlpStmt->orderDir) {
164  fprintf(stderr, "ORDER BY: %s %s\n", sqlpStmt->orderCol,
165  sqlpStmt->orderDir == 1 ? "ASC" : "DESC");
166  }
167  else {
168  fprintf(stderr, "ORDER BY: %s\n", sqlpStmt->orderCol);
169  }
170  }
171 
172  fprintf(stderr, "***************************************\n");
173 
174  return (1);
175 }
char * sqpOperatorName(int)
Definition: sql.c:325
struct state * st
Definition: parser.c:104
int sqpPrintStmt(SQLPSTMT *st)
Definition: print.c:62
#define SQLP_VARCHAR
Definition: sqlp.h:51
#define SQLP_DOUBLE
Definition: sqlp.h:53
#define SQLP_S
Definition: sqlp.h:44
#define SQLP_INSERT
Definition: sqlp.h:11
#define SQLP_D
Definition: sqlp.h:46
#define SQLP_TIME
Definition: sqlp.h:55
#define SQLP_NODE_VALUE
Definition: sqlp.h:62
#define SQLP_ADD_COLUMN
Definition: sqlp.h:15
#define SQLP_DROP
Definition: sqlp.h:10
#define SQLP_NODE_EXPRESSION
Definition: sqlp.h:63
#define SQLP_SELECT
Definition: sqlp.h:12
#define SQLP_CREATE
Definition: sqlp.h:9
#define SQLP_EXPR
Definition: sqlp.h:48
#define SQLP_DATE
Definition: sqlp.h:54
#define SQLP_DELETE
Definition: sqlp.h:14
#define SQLP_UPDATE
Definition: sqlp.h:13
#define SQLP_I
Definition: sqlp.h:45
#define SQLP_NULL
Definition: sqlp.h:43
#define SQLP_INTEGER
Definition: sqlp.h:52
#define SQLP_DROP_COLUMN
Definition: sqlp.h:16
SQLPSTMT * sqlpStmt
Definition: sql.c:37
Definition: sqlp.h:87
char table[SQLP_MAX_TABLE+1]
Definition: sqlp.h:92
char * stmt
Definition: sqlp.h:88
SQLPVALUE * Col
Definition: sqlp.h:93
SQLPNODE * upperNodeptr
Definition: sqlp.h:102
char * orderCol
Definition: sqlp.h:103
int * ColWidth
Definition: sqlp.h:95
int orderDir
Definition: sqlp.h:105
int command
Definition: sqlp.h:91
int * ColType
Definition: sqlp.h:94
SQLPVALUE * Val
Definition: sqlp.h:99
double d
Definition: sqlp.h:73
char * s
Definition: sqlp.h:71
struct sqlpnode * expr
Definition: sqlp.h:74
int i
Definition: sqlp.h:72
int type
Definition: sqlp.h:70
Definition: sqlp.h:77
struct sqlpnode * left
Definition: sqlp.h:81
char * column_name
Definition: sqlp.h:83
struct sqlpnode * right
Definition: sqlp.h:82
int oper
Definition: sqlp.h:80
SQLPVALUE value
Definition: sqlp.h:84
int node_type
Definition: sqlp.h:78