GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71933
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
print.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: SQL statement parser library
5 *
6 * AUTHOR(S): lex.l and yac.y were originally taken from unixODBC and
7 * probably written by Peter Harvey <pharvey@codebydesigns.com>,
8 * modifications and other code by Radim Blazek
9 *
10 * PURPOSE: Parse input string containing SQL statement to
11 * SQLPSTMT structure.
12 * SQL parser may be used by simple database drivers.
13 *
14 * COPYRIGHT: (C) 2000 by the GRASS Development Team
15 *
16 * This program is free software under the GNU General Public
17 * License (>=v2). Read the file COPYING that comes with GRASS
18 * for details.
19 *
20 *****************************************************************************/
21 
22 #include <grass/sqlp.h>
23 #include <stdio.h>
24 
25 static void print_node(SQLPNODE * nptr, int level)
26 {
27  int i;
28 
29  for (i = 0; i < level; i++) {
30  fprintf(stderr, " ");
31  }
32 
33  if (nptr->node_type == SQLP_NODE_EXPRESSION) {
34  fprintf(stderr, "op: %s\n", sqpOperatorName(nptr->oper));
35  if (nptr->left) {
36  print_node(nptr->left, level + 1);
37  }
38  if (nptr->right) {
39  print_node(nptr->right, level + 1);
40  }
41  }
42  else if (nptr->node_type == SQLP_NODE_VALUE) {
43  switch (nptr->value.type) {
44  case SQLP_NULL:
45  fprintf(stderr, "val: NULL\n");
46  break;
47  case SQLP_D:
48  fprintf(stderr, "val: %e\n", nptr->value.d);
49  break;
50  case SQLP_I:
51  fprintf(stderr, "val: %d\n", nptr->value.i);
52  break;
53  case SQLP_S:
54  fprintf(stderr, "val: '%s'\n", nptr->value.s);
55  break;
56  }
57  }
58  else { /* SQLP_NODE_COLUMN */
59  fprintf(stderr, "col: %s\n", nptr->column_name);
60  }
61 }
62 
64 {
65  int i;
66 
67  fprintf(stderr, "********** SQL PARSER RESULT **********\n");
68  fprintf(stderr, "INPUT: %s\n", sqlpStmt->stmt);
69  fprintf(stderr, "COMMAND: ");
70  switch (sqlpStmt->command) {
71  case (SQLP_ADD_COLUMN):
72  fprintf(stderr, "ADD COLUMN\n");
73  break;
74  case (SQLP_CREATE):
75  fprintf(stderr, "CREATE\n");
76  break;
77  case (SQLP_DROP):
78  fprintf(stderr, "DROP\n");
79  break;
80  case (SQLP_DROP_COLUMN):
81  fprintf(stderr, "DROP COLUMN\n");
82  break;
83  case (SQLP_INSERT):
84  fprintf(stderr, "INSERT\n");
85  break;
86  case (SQLP_UPDATE):
87  fprintf(stderr, "UPDATE\n");
88  break;
89  case (SQLP_SELECT):
90  fprintf(stderr, "SELECT\n");
91  break;
92  case (SQLP_DELETE):
93  fprintf(stderr, "DELETE\n");
94  break;
95  default:
96  fprintf(stderr, "UNKNOWN\n");
97  }
98 
99  fprintf(stderr, "TABLE: %s\n", sqlpStmt->table);
100 
101  /* columns */
102  for (i = 0; i < st->nCol; i++) {
103  if (sqlpStmt->command == SQLP_CREATE) {
104  fprintf(stderr, "COLUMN %2d: ", i + 1);
105  switch (sqlpStmt->ColType[i]) {
106  case (SQLP_VARCHAR):
107  fprintf(stderr, "type:varchar width:%d",
108  sqlpStmt->ColWidth[i]);
109  break;
110  case (SQLP_INTEGER):
111  fprintf(stderr, "type:integer");
112  break;
113  case (SQLP_DOUBLE):
114  fprintf(stderr, "type:double");
115  break;
116  case (SQLP_DATE):
117  fprintf(stderr, "type:date");
118  break;
119  case (SQLP_TIME):
120  fprintf(stderr, "type:time");
121  break;
122  default:
123  fprintf(stderr, "type:unknown");
124  break;
125  }
126  fprintf(stderr, " name:%s\n", sqlpStmt->Col[i].s);
127  }
128  else {
129  fprintf(stderr, "COLUMN %2d: %s\n", i + 1, sqlpStmt->Col[i].s);
130  }
131  }
132 
133  /* values */
134  for (i = 0; i < st->nVal; i++) {
135  fprintf(stderr, "VALUE %2d ", i + 1);
136  switch (sqlpStmt->Val[i].type) {
137  case (SQLP_S):
138  fprintf(stderr, "(string) : %s\n", sqlpStmt->Val[i].s);
139  break;
140  case (SQLP_I):
141  fprintf(stderr, "(integer): %d\n", sqlpStmt->Val[i].i);
142  break;
143  case (SQLP_D):
144  fprintf(stderr, "(float) : %f\n", sqlpStmt->Val[i].d);
145  break;
146  case (SQLP_NULL):
147  fprintf(stderr, "(unknown) : null\n");
148  break;
149  case (SQLP_EXPR):
150  fprintf(stderr, "(expression) :\n");
151  print_node(sqlpStmt->Val[i].expr, 0);
152  break;
153  default:
154  fprintf(stderr, "unknown\n");
155  break;
156  }
157  }
158 
159  if (sqlpStmt->upperNodeptr) {
160  fprintf(stderr, "WHERE:\n");
161  print_node(sqlpStmt->upperNodeptr, 0);
162  }
163 
164 
165  if (sqlpStmt->command == SQLP_SELECT) {
166  if (sqlpStmt->orderDir) {
167  fprintf(stderr, "ORDER BY: %s %s\n", sqlpStmt->orderCol,
168  sqlpStmt->orderDir == 1 ? "ASC" : "DESC");
169  }
170  else {
171  fprintf(stderr, "ORDER BY: %s\n", sqlpStmt->orderCol);
172  }
173  }
174 
175 
176  fprintf(stderr, "***************************************\n");
177 
178  return (1);
179 }
int nVal
Definition: sqlp.h:104
#define SQLP_TIME
Definition: sqlp.h:55
SQLPVALUE * Val
Definition: sqlp.h:102
#define SQLP_INTEGER
Definition: sqlp.h:52
struct sqlpnode * expr
Definition: sqlp.h:76
SQLPVALUE * Col
Definition: sqlp.h:96
int type
Definition: sqlp.h:72
#define SQLP_DOUBLE
Definition: sqlp.h:53
int orderDir
Definition: sqlp.h:107
char * stmt
Definition: sqlp.h:91
#define SQLP_NODE_EXPRESSION
Definition: sqlp.h:63
#define SQLP_EXPR
Definition: sqlp.h:48
#define SQLP_DELETE
Definition: sqlp.h:14
double d
Definition: sqlp.h:75
int oper
Definition: sqlp.h:82
#define SQLP_DROP_COLUMN
Definition: sqlp.h:16
Definition: sqlp.h:89
int * ColType
Definition: sqlp.h:97
#define SQLP_DROP
Definition: sqlp.h:10
char table[SQLP_MAX_TABLE+1]
Definition: sqlp.h:95
char * sqpOperatorName(int oper)
Definition: sql.c:326
#define SQLP_VARCHAR
Definition: sqlp.h:51
#define SQLP_DATE
Definition: sqlp.h:54
struct state * st
Definition: parser.c:103
SQLPVALUE value
Definition: sqlp.h:86
#define SQLP_I
Definition: sqlp.h:45
#define SQLP_S
Definition: sqlp.h:44
SQLPNODE * upperNodeptr
Definition: sqlp.h:105
SQLPSTMT * sqlpStmt
Definition: sql.c:38
#define SQLP_CREATE
Definition: sqlp.h:9
int * ColWidth
Definition: sqlp.h:98
int nCol
Definition: sqlp.h:101
Definition: sqlp.h:79
int command
Definition: sqlp.h:94
char * column_name
Definition: sqlp.h:85
int i
Definition: sqlp.h:74
#define SQLP_ADD_COLUMN
Definition: sqlp.h:15
#define SQLP_UPDATE
Definition: sqlp.h:13
#define SQLP_INSERT
Definition: sqlp.h:11
#define SQLP_NULL
Definition: sqlp.h:43
char * s
Definition: sqlp.h:73
#define SQLP_SELECT
Definition: sqlp.h:12
int node_type
Definition: sqlp.h:81
struct sqlpnode * left
Definition: sqlp.h:83
char * orderCol
Definition: sqlp.h:106
#define SQLP_NODE_VALUE
Definition: sqlp.h:62
#define SQLP_D
Definition: sqlp.h:46
struct sqlpnode * right
Definition: sqlp.h:84