GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 originaly 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 
63 int sqpPrintStmt(SQLPSTMT * st)
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 }
char * sqpOperatorName(int oper)
Definition: sql.c:326