forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ma_parse.c
215 lines (179 loc) · 4.9 KB
/
ma_parse.c
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
/************************************************************************************
Copyright (C) 2013,2016 MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <ma_odbc.h>
const char *MADB_GetToken(const char **Stmt, const char *End)
{
const char *Pos= *Stmt;
while (iswspace(*Pos) && Pos < End)
Pos++;
/* if we reached end of string, return */
if (Pos == End)
return (*Stmt= End);
*Stmt= Pos;
while (*Stmt < End && !iswspace(**Stmt))
++*Stmt;
while (*Stmt < End && iswspace(**Stmt))
++*Stmt;
return *Stmt;
}
MADB_QUERY *MADB_Tokenize(const char *Stmt)
{
const char *End= (char *)Stmt + strlen(Stmt);
const char *Pos;
const char *Start= Stmt;
MADB_QUERY *Query= (MADB_QUERY *)MADB_CALLOC(sizeof(MADB_QUERY));
MADB_InitDynamicArray(&Query->tokens, sizeof(unsigned int), 20, 20);
while ((Pos = MADB_GetToken(&Stmt, End)) != End)
{
unsigned int Offset= (unsigned int)(Stmt - Start);
MADB_InsertDynamic(&Query->tokens, (char *)&Offset);
Stmt= Pos;
}
return Query;
}
void MADB_FreeTokens(MADB_QUERY *Query)
{
if (Query == NULL)
{
return;
}
MADB_DeleteDynamic(&Query->tokens);
MADB_FREE(Query);
}
char *MADB_Token(MADB_Stmt *Stmt, unsigned int Idx)
{
char *p;
unsigned int Offset= 0;
MADB_DynArray *Tokens;
p= Stmt->StmtString;
if (!Stmt->Tokens || !p)
return NULL;
Tokens= &Stmt->Tokens->tokens;
if (Idx >= Tokens->elements)
return NULL;
MADB_GetDynamic(Tokens, (char *)&Offset, Idx);
return Stmt->StmtString + Offset;
}
my_bool MADB_CompareToken(MADB_Stmt *Stmt, unsigned int Idx, char *Compare, size_t Length, unsigned int *Offset)
{
char *TokenString;
if (!(TokenString= MADB_Token(Stmt, Idx)))
return FALSE;
if (_strnicmp(TokenString, Compare, Length) == 0)
{
if (Offset)
*Offset= (unsigned int)(TokenString - Stmt->StmtString);
return TRUE;
}
return FALSE;
}
unsigned int MADB_FindToken(MADB_Stmt *Stmt, char *Compare)
{
unsigned int i, TokenCount= Stmt->Tokens->tokens.elements;
unsigned int Offset= 0;
for (i=0; i < TokenCount; i++)
{
if (MADB_CompareToken(Stmt, i, Compare, strlen(Compare), &Offset))
return Offset;
}
return 0;
}
char *MADB_ParseCursorName(MADB_Stmt *Stmt, unsigned int *Offset)
{
unsigned int i,
TokenCount= Stmt->Tokens->tokens.elements;
if (TokenCount < 4)
return NULL;
for (i=0; i < TokenCount - 3; i++)
{
if (MADB_CompareToken(Stmt, i, "WHERE", 5, Offset) &&
MADB_CompareToken(Stmt, i+1, "CURRENT", 7, 0) &&
MADB_CompareToken(Stmt, i+2, "OF", 2, 0))
{
return MADB_Token(Stmt, i + 3);
}
}
return NULL;
}
const char * SimpleParamDetector(const char *query)
{
return strchr(query, '?');
}
const char * MADB_FindParamPlaceholder(MADB_Stmt *Stmt)
{
/* Atm we do not need 100% accuracy with that. And unlikely will */
return SimpleParamDetector(Stmt->StmtString);
}
enum enum_madb_query_type MADB_GetQueryType(MADB_Stmt *Stmt)
{
char *p= Stmt->StmtString;
while (*p && !isalpha(*p))
++p;
if (_strnicmp(p, "SELECT", 6) == 0)
{
return MADB_QUERY_SELECT;
}
if (_strnicmp(p, "INSERT", 6) == 0)
{
return MADB_QUERY_INSERT;
}
if (_strnicmp(p, "UPDATE", 6) == 0)
{
return MADB_QUERY_UPDATE;
}
if (_strnicmp(p, "DELETE", 6) == 0)
{
return MADB_QUERY_DELETE;
}
if (_strnicmp(p, "CALL", 4) == 0)
{
return MADB_QUERY_CALL;
}
if (_strnicmp(p, "SHOW", 4) == 0)
{
return MADB_QUERY_SHOW;
}
if (_strnicmp(p, "ANALYZE", 7) == 0)
{
return MADB_QUERY_ANALYZE;
}
if (_strnicmp(p, "EXPLAIN", 7) == 0)
{
return MADB_QUERY_EXPLAIN;
}
if (_strnicmp(p, "CHECK", 5) == 0)
{
return MADB_QUERY_CHECK;
}
if (_strnicmp(p, "EXECUTE", 7) == 0)
{
return MADB_QUERY_EXECUTE;
}
return MADB_QUERY_NO_RESULT;
}
char* FixIsoFormat(char * StmtString)
{
size_t len;
StmtString= trim(StmtString);
len= strlen(StmtString);
if (StmtString[0] == '{' && StmtString[len -1] == '}')
{
StmtString[0]= ' ';
StmtString[len - 1]= '\0';
return trim(StmtString);
}
return StmtString;
}