GRASS 8 Programmer's Manual
8.5.0dev(2025)-c070206eb1
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
62
int
sqpPrintStmt
(
SQLPSTMT
*
st
)
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
}
sqpOperatorName
char * sqpOperatorName(int)
Definition:
sql.c:325
st
struct state * st
Definition:
parser.c:104
sqpPrintStmt
int sqpPrintStmt(SQLPSTMT *st)
Definition:
print.c:62
sqlp.h
SQLP_VARCHAR
#define SQLP_VARCHAR
Definition:
sqlp.h:51
SQLP_DOUBLE
#define SQLP_DOUBLE
Definition:
sqlp.h:53
SQLP_S
#define SQLP_S
Definition:
sqlp.h:44
SQLP_INSERT
#define SQLP_INSERT
Definition:
sqlp.h:11
SQLP_D
#define SQLP_D
Definition:
sqlp.h:46
SQLP_TIME
#define SQLP_TIME
Definition:
sqlp.h:55
SQLP_NODE_VALUE
#define SQLP_NODE_VALUE
Definition:
sqlp.h:62
SQLP_ADD_COLUMN
#define SQLP_ADD_COLUMN
Definition:
sqlp.h:15
SQLP_DROP
#define SQLP_DROP
Definition:
sqlp.h:10
SQLP_NODE_EXPRESSION
#define SQLP_NODE_EXPRESSION
Definition:
sqlp.h:63
SQLP_SELECT
#define SQLP_SELECT
Definition:
sqlp.h:12
SQLP_CREATE
#define SQLP_CREATE
Definition:
sqlp.h:9
SQLP_EXPR
#define SQLP_EXPR
Definition:
sqlp.h:48
SQLP_DATE
#define SQLP_DATE
Definition:
sqlp.h:54
SQLP_DELETE
#define SQLP_DELETE
Definition:
sqlp.h:14
SQLP_UPDATE
#define SQLP_UPDATE
Definition:
sqlp.h:13
SQLP_I
#define SQLP_I
Definition:
sqlp.h:45
SQLP_NULL
#define SQLP_NULL
Definition:
sqlp.h:43
SQLP_INTEGER
#define SQLP_INTEGER
Definition:
sqlp.h:52
SQLP_DROP_COLUMN
#define SQLP_DROP_COLUMN
Definition:
sqlp.h:16
sqlpStmt
SQLPSTMT * sqlpStmt
Definition:
sql.c:37
stdio.h
SQLPSTMT
Definition:
sqlp.h:87
SQLPSTMT::stmt
char * stmt
Definition:
sqlp.h:88
SQLPSTMT::Col
SQLPVALUE * Col
Definition:
sqlp.h:93
SQLPSTMT::upperNodeptr
SQLPNODE * upperNodeptr
Definition:
sqlp.h:102
SQLPSTMT::orderCol
char * orderCol
Definition:
sqlp.h:103
SQLPSTMT::ColWidth
int * ColWidth
Definition:
sqlp.h:95
SQLPSTMT::orderDir
int orderDir
Definition:
sqlp.h:105
SQLPSTMT::command
int command
Definition:
sqlp.h:91
SQLPSTMT::table
char table[200+1]
Definition:
sqlp.h:92
SQLPSTMT::ColType
int * ColType
Definition:
sqlp.h:94
SQLPSTMT::Val
SQLPVALUE * Val
Definition:
sqlp.h:99
SQLPVALUE::d
double d
Definition:
sqlp.h:73
SQLPVALUE::s
char * s
Definition:
sqlp.h:71
SQLPVALUE::expr
struct sqlpnode * expr
Definition:
sqlp.h:74
SQLPVALUE::i
int i
Definition:
sqlp.h:72
SQLPVALUE::type
int type
Definition:
sqlp.h:70
sqlpnode
Definition:
sqlp.h:77
sqlpnode::left
struct sqlpnode * left
Definition:
sqlp.h:81
sqlpnode::column_name
char * column_name
Definition:
sqlp.h:83
sqlpnode::right
struct sqlpnode * right
Definition:
sqlp.h:82
sqlpnode::oper
int oper
Definition:
sqlp.h:80
sqlpnode::value
SQLPVALUE value
Definition:
sqlp.h:84
sqlpnode::node_type
int node_type
Definition:
sqlp.h:78
lib
db
sqlp
print.c
Generated on Sat Aug 30 2025 07:00:37 for GRASS 8 Programmer's Manual by
1.9.1