summaryrefslogtreecommitdiffstats
path: root/tools/sqldelim.c
blob: ed2e2b134bbde8b96273e849ba7e38022f34d249 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** A tokenizer for SQL
**
** This file contains C code that splits an SQL input string up into
** individual tokens, groups them back into statements, and passes the
** statements up to a user-defined callback.
*/

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <glib.h>

#include "sqldelim.h"

/*
** All the keywords of the SQL language are stored as in a hash
** table composed of instances of the following structure.
*/
typedef struct Keyword Keyword;
struct Keyword {
    const char *zName;      /* The keyword name */
};

#define MAX_TOKEN_LEN 11

/*
** These are the keywords that begin a new SQL statement.
** They MUST be in alphabetical order
*/
static const Keyword aKeywordTable[] = {
    { "ALTER" },
    { "CREATE" },
    { "DELETE" },
    { "DROP" },
    { "INSERT" },
    { "SELECT" },
    { "UPDATE" },
};

#define KEYWORD_COUNT (sizeof aKeywordTable / sizeof (Keyword))

/*
** Comparison function for binary search.
*/
G_GNUC_PURE
static int sql_compare_keyword(const void *m1, const void *m2){
    const uint8_t *p = m1;
    const Keyword *k = m2;
    const char *q = k->zName;

    for (; *p; p++, q++) {
        uint8_t c;
        if ((uint16_t) *p > 127)
            return 1;
        c = *p;
        if (c >= 'a' && c <= 'z')
            c ^= 'A' ^ 'a';
        if (c != *q)
            return (unsigned)c - (unsigned)*q;
    }

    return (unsigned)*p - (unsigned)*q;
}

/*
** This function looks up an identifier to determine if it is a
** keyword.  If it is a keyword, the token code of that keyword is 
** returned.  If the input is not a keyword, TK_ID is returned.
*/
static int sqlite_find_keyword(const char *z, int n)
{
    char str[MAX_TOKEN_LEN + 1];
    Keyword *r;

    if (n > MAX_TOKEN_LEN)
        return false;

    memcpy(str, z, n);
    str[n] = 0;
    r = bsearch(str, aKeywordTable, KEYWORD_COUNT, sizeof (Keyword), sql_compare_keyword);
    return r != NULL;
}


/*
** If X is a character that can be used in an identifier then
** isIdChar[X] will be 1.  Otherwise isIdChar[X] will be 0.
**
** In this implementation, an identifier can be a string of
** alphabetic characters, digits, and "_" plus any character
** with the high-order bit set.  The latter rule means that
** any sequence of UTF-8 characters or characters taken from
** an extended ISO8859 character set can form an identifier.
*/
static const uint8_t isIdChar[] = {
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
    0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  /* 2x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 8x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 9x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ax */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Bx */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Cx */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Dx */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ex */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Fx */
};


/*
** Return the length of the token that begins at z[0].  Return
** -1 if the token is (or might be) incomplete.  Store the token
** type in *tokenType before returning.
*/
static int sql_skip_token(const char **p, bool *cont)
{
    int i = 1;
    const uint8_t *z = (uint8_t *) *p;
    bool get_keyword = *cont;

    *cont = true;
    switch (*z) {
    case ' ': case '\t': case '\n': case '\f':
        while (isspace(z[i]) && z[i] != '\r') i++;
        *p += i;
        return false;
    case '-':
    case '(':
    case ')':
    case '*':
    case '=':
    case '<':
    case '>':
    case '!':
    case '?':
    case ',':
    case '.':
        *p += 1;
        return false;
    case '`': case '\'': {
        int delim = z[0];
        while (z[i])
            if (z[i++] == delim)
               break;
        *p += i;
        return false;
    }
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        while (isdigit(z[i])) i++;
        *p += i;
        return false;
    case '[':
        while (z[i] && z[i-1] != ']') i++;
        *p += i;
        return false;
    default:
        if (!isIdChar[*z]) {
            *p += 1;
            return true;
        }
        while (isIdChar[z[i]]) i++;
        if (get_keyword && sqlite_find_keyword(*p, i)) {
            return true;
        } else {
            /* Do not recognize a keyword at the beginning of the next chunk.  */
            if (!z[i]) {
                *cont = false;
            }
            *p += i;
            return false;
        }
    }
}

int sql_get_statement(const char *start,
                      int (*fn)(const char *stmt, void *opaque),
                      void *opaque)
{
    static GString str;
    static bool cont = false;

    const char *p = start;
    char *stmt;
    bool done;
    int ret = 0;

    /* Final part?  Build a statement with what's left.  */
    if (!*p) {
        goto stmt;
    }

    while (*p) {
        start = p;
        /* A semicolon is not part of the SQL syntax, skip it and conclude
         * this statement.
         */
        if (*p == ';') {
            done = true;
            p++;
        } else {
            done = sql_skip_token(&p, &cont);
            g_string_append_len(&str, start, p - start);
        }

        if (done) {
stmt:
            cont = false;
            stmt = g_strndup(str.str, str.len);
            g_string_erase(&str, 0, str.len);
            if (stmt[0]) {
                ret = fn(stmt, opaque);
            }
            free(stmt);
            if (ret) {
                return ret;
            }
        }
    }
    return 0;
}

#if 0
int main()
{
    uint8_t line[100], *stmt;
    const uint8_t *p;

    while (fgets(line, sizeof(line), stdin)) {
        sql_get_statement(line, puts);
    }
    sql_get_statement("", puts);
}
#endif