File: tdbcTokenize.c

package info (click to toggle)
tdbc 1.1.1-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 928 kB
  • sloc: sh: 2,256; tcl: 1,146; ansic: 886; makefile: 61
file content (218 lines) | stat: -rw-r--r-- 6,135 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * tdbcTokenize.c --
 *
 *	Code for a Tcl command that will extract subsitutable parameters
 *	from a SQL statement.
 *
 * Copyright (c) 2007 by D. Richard Hipp.
 * Copyright (c) 2010, 2011 by Kevin B. Kenny.
 *
 * Please refer to the file, 'license.terms' for the conditions on
 * redistribution of this file and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id$
 *
 *-----------------------------------------------------------------------------
 */

#include "tdbcInt.h"
#include <ctype.h>

/*
 *-----------------------------------------------------------------------------
 *
 * Tdbc_TokenizeSql --
 *
 *	Tokenizes a SQL statement.
 *
 * Results:
 *	Returns a zero-reference Tcl object that gives the statement in
 *	tokenized form, or NULL if an error occurs.
 *
 * Side effects:
 *	If an error occurs, and 'interp' is not NULL, stores an error
 *	message in the interpreter result.
 *
 * This is demonstration code for a TCL command that will extract
 * host parameters from an SQL statement.
 *
 * A "host parameter" is a variable within the SQL statement.
 * Different systems do host parameters in different ways.  This
 * tokenizer recognizes three forms:
 *
 *            $ident
 *            :ident
 *            @ident
 *
 * In other words, a host parameter is an identifier proceeded
 * by one of the '$', ':', or '@' characters.
 *
 * This function returns a Tcl_Obj representing a list.  The
 * concatenation of the returned list will be equivalent to the
 * input string.  Each element of the list will be either a
 * host parameter, a semicolon, or other text from the SQL
 * statement.
 *
 * Example:
 *
 *      tokenize_sql {SELECT * FROM table1 WHERE :name='bob';}
 *
 * Resulting in:
 *
 *      {SELECT * FROM table1 WHERE } {:name} {=} {'bob'} {;}
 *
 * The tokenizer knows about SQL comments and strings and will
 * not mistake a host parameter or semicolon embedded in a string
 * or comment as a real host parameter or semicolon.
 *
 *-----------------------------------------------------------------------------
 */

TDBCAPI Tcl_Obj*
Tdbc_TokenizeSql(
    Tcl_Interp *interp,
    const char* zSql
){
    Tcl_Obj *resultPtr;
    int i;

    resultPtr = Tcl_NewObj();
    for(i = 0; zSql[i]; i++){
        switch( zSql[i] ){

            /* Skip over quoted strings.  Strings can be quoted in several
            ** ways:    '...'   "..."   [....]
            */
            case '\'':
            case '"':
            case '[': {
                int endChar = zSql[i];
                if (endChar == '[') endChar = ']';
                for(i++; zSql[i] && zSql[i]!=endChar; i++){}
                if (zSql[i] == 0) i--;
                break;
            }

            /* Skip over SQL-style comments: -- to end of line
            */
            case '-': {
                if (zSql[i+1] == '-') {
                     for(i+=2; zSql[i] && zSql[i]!='\n'; i++){}
                     if (zSql[i] == 0) i--;
                }
                break;
            }

            /* Skip over C-style comments
            */
            case '/': {
                if (zSql[i+1] == '*') {
                     i += 3;
                     while (zSql[i] && (zSql[i]!='/' || zSql[i-1]!='*')) {
                         i++;
                     }
                     if (zSql[i] == 0) i--;
                }
                break;
            }

            /* Break up multiple SQL statements at each semicolon */
            case ';': {
                if (i>0 ){
                    Tcl_ListObjAppendElement(interp, resultPtr,
                              Tcl_NewStringObj(zSql, i));
                }
                Tcl_ListObjAppendElement(interp, resultPtr,
                          Tcl_NewStringObj(";",1));
                zSql += i + 1;
                i = -1;
                break;
            }

            /* Any of the characters ':', '$', or '@' which is followed
            ** by an alphanumeric or '_' and is not preceded by the same
            ** is a host parameter. A name following a doubled colon '::'
	    ** is also not a host parameter.
            */
	    case ':': {
		if (i > 0 && zSql[i-1] == ':') break;
	    }
		/* fallthru */

            case '$':
            case '@': {
                if (i>0 && (isalnum((unsigned char)(zSql[i-1]))
			    || zSql[i-1]=='_')) break;
                if (!isalnum((unsigned char)(zSql[i+1]))
		    && zSql[i+1]!='_') break;
                if (i>0 ){
                    Tcl_ListObjAppendElement(interp, resultPtr,
                              Tcl_NewStringObj(zSql, i));
                    zSql += i;
                }
                i = 1;
                while (zSql[i] && (isalnum((unsigned char)(zSql[i]))
				   || zSql[i]=='_')) {
                    i++;
                }
                Tcl_ListObjAppendElement(interp, resultPtr,
                          Tcl_NewStringObj(zSql, i));
                zSql += i;
                i = -1;
                break;
            }
        }
    }
    if (i>0) {
        Tcl_ListObjAppendElement(interp, resultPtr, Tcl_NewStringObj(zSql, i));
    }
    return resultPtr;
}

/*
 *-----------------------------------------------------------------------------
 *
 * TdbcTokenizeObjCmd --
 *
 *	Tcl command to tokenize a SQL statement.
 *
 * Usage:
 *	::tdbc::tokenize statement
 *
 * Results:
 *	Returns a list as from passing the given statement to
 *	Tdbc_TokenizeSql above.
 *
 *-----------------------------------------------------------------------------
 */

MODULE_SCOPE int
TdbcTokenizeObjCmd(
    ClientData clientData,	/* Unused */
    Tcl_Interp* interp,		/* Tcl interpreter */
    int objc,			/* Parameter count */
    Tcl_Obj *const objv[]	/* Parameter vector */
) {

    Tcl_Obj* retval;

    /* Check param count */

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "statement");
	return TCL_ERROR;
    }

    /* Parse the statement */

    retval = Tdbc_TokenizeSql(interp, Tcl_GetString(objv[1]));
    if (retval == NULL) {
	return TCL_ERROR;
    }

    /* Return the tokenized statement */

    Tcl_SetObjResult(interp, retval);
    return TCL_OK;

}