forked from griddb/griddb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_string.h
487 lines (456 loc) · 12.8 KB
/
function_string.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
Copyright (c) 2017 TOSHIBA Digital Solutions Corporation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
@file
@brief Definition of String functions for TQL
*/
#ifndef FUNCTIONS_STRING_H_
#define FUNCTIONS_STRING_H_
#include <algorithm>
#include <cctype>
#include <string>
#include "expression.h"
#include "qp_def.h"
#include "utf8.h"
/*!
* @brief STRING(x)
*
* @return x as string
*/
class FunctorString : public TqlFunc {
public:
using TqlFunc::operator();
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.size() != 1) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
else if (args[0]->isNullValue()) {
return Expr::newNullValue(txn);
}
return Expr::newStringValue(args[0]->getValueAsString(txn), txn);
}
virtual ~FunctorString() {}
};
/*!
* @brief CHAR_LENGTH(str)
*
* @return length
*/
class FunctorCharLength : public TqlFunc {
public:
using TqlFunc::operator();
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.size() != 1) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
else if (args[0]->isNullValue()) {
return Expr::newNullValue(txn);
}
else if (!args[0]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 1 is not a string");
}
else {
/* DO NOTHING */
}
const char *s = args[0]->getValueAsString(txn);
return Expr::newNumericValue(static_cast<int32_t>(_utf8Strlen(s)), txn);
}
virtual ~FunctorCharLength() {}
};
/*!
* @brief CONCAT(str1, str2, ...)
*
* @return concatinated string
*/
class FunctorConcat : public TqlFunc {
public:
using TqlFunc::operator();
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.empty() || args.size() < 2) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
size_t i;
util::String s("", QP_ALLOCATOR);
for (i = 0; i < args.size(); i++) {
if (args[i]->isNullValue()) {
continue;
}
if (!args[i]->isString()) {
util::NormalOStringStream os;
os << "Argument " << i + 1 << " is not a string";
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
os.str().c_str());
}
s += args[i]->getValueAsString(txn);
}
return Expr::newStringValue(s.c_str(), txn);
}
virtual ~FunctorConcat() {}
};
#define USE_SQL92_LIKE 0 /* Case sensitive : 1=Ignore case */
/*!
* @brief LIKE(str1, str2, [escape])
*
* @return whether str1 contains str2
*/
class FunctorLike : public TqlFunc {
#ifndef USE_SQL92_LIKE
inline bool match(
const char *text, int textlen, const char *pattern, int patternlen) {
char first = pattern[0];
char last = pattern[patternlen - 1];
if (first == '%') {
pattern = &pattern[1];
if (last == '%') {
return std::find(text, pattern, 0) != std::string::npos;
}
return text.find(pattern, 0, patternlen) != std::string::npos;
}
else if (last == '%') {
std::string tmp("", pattern.c_str(), patternlen - 1);
pattern = tmp.c_str();
return text.find(pattern, text.length() - patternlen) !=
std::string::npos;
}
else {
/* DO NOTHING */
}
return (text.compare(pattern) == 0);
}
#else
/*!
* @brief A structure defining how to do GLOB-style comparisons.
* Implementation is taken from SQLite
*/
struct compareInfo {
uint8_t matchAll;
uint8_t matchOne;
uint8_t matchSet;
uint8_t noCase;
};
/*!
* @brief SQL LIKE Pattern matcher
*
* @param zPattern Pattern string (described in SQL-LIKE grammer)
* @param zString Target string
* @param pInfo compareInfo
* @param esc Escape character
*
* @return 1 when matched, 0 otherwise
* @note cf.(Public Domain)http://www.sqlite.org
*/
int patternCompare(const char *zPattern, const char *zString,
const struct compareInfo *pInfo, const int esc) {
int c, c2;
int invert;
int seen;
uint8_t matchOne = pInfo->matchOne;
uint8_t matchAll = pInfo->matchAll;
uint8_t matchSet = pInfo->matchSet;
uint8_t noCase = pInfo->noCase;
int prevEscape = 0; /* True if the previous character was 'escape' */
while ((c = _READ_UTF8(zPattern, &zPattern)) != 0) {
if (!prevEscape && c == matchAll) {
while ((c = _READ_UTF8(zPattern, &zPattern)) == matchAll ||
c == matchOne) {
if (c == matchOne && _READ_UTF8(zString, &zString) == 0) {
return 0;
}
}
if (c == 0) {
return 1;
}
else if (c == esc) {
c = _READ_UTF8(zPattern, &zPattern);
if (c == 0) {
return 0;
}
}
else if (c == matchSet) {
if (esc != 0) { /* This is GLOB, not LIKE */
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"GLOB cannot take ESCAPE");
}
else if (matchSet >=
0x80) { /* '[' is a single-byte character */
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"matchSet must be single byte.");
}
else {
/* DO NOTHING */
}
while (*zString &&
patternCompare(&zPattern[-1], zString, pInfo, esc) ==
0) {
_SKIP_UTF8(zString);
}
return *zString != 0;
}
else {
/* DO NOTHING */
}
while ((c2 = _READ_UTF8(zString, &zString)) != 0) {
if (noCase) {
c2 = std::toupper(c2);
c = std::toupper(c);
while (c2 != 0 && c2 != c) {
c2 = _READ_UTF8(zString, &zString);
c2 = std::toupper(c2);
}
}
else {
while (c2 != 0 && c2 != c) {
c2 = _READ_UTF8(zString, &zString);
}
}
if (c2 == 0) return 0;
if (patternCompare(zPattern, zString, pInfo, esc)) return 1;
}
return 0;
}
else if (!prevEscape && c == matchOne) {
if (_READ_UTF8(zString, &zString) == 0) {
return 0;
}
}
else if (c == matchSet) {
int prior_c = 0;
if (esc != 0) { /* This only occurs for GLOB, not LIKE */
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"GLOB cannot take ESCAPE");
}
seen = 0;
invert = 0;
c = _READ_UTF8(zString, &zString);
if (c == 0) return 0;
c2 = _READ_UTF8(zPattern, &zPattern);
if (c2 == '^') {
invert = 1;
c2 = _READ_UTF8(zPattern, &zPattern);
}
if (c2 == ']') {
if (c == ']') seen = 1;
c2 = _READ_UTF8(zPattern, &zPattern);
}
while (c2 && c2 != ']') {
if (c2 == '-' && zPattern[0] != ']' && zPattern[0] != 0 &&
prior_c > 0) {
c2 = _READ_UTF8(zPattern, &zPattern);
if (c >= prior_c && c <= c2) seen = 1;
prior_c = 0;
}
else {
if (c == c2) {
seen = 1;
}
prior_c = c2;
}
c2 = _READ_UTF8(zPattern, &zPattern);
}
if (c2 == 0 || (seen ^ invert) == 0) {
return 0;
}
}
else if (esc == c && !prevEscape) {
prevEscape = 1;
}
else {
c2 = _READ_UTF8(zString, &zString);
if (noCase) {
c = std::toupper(c);
c2 = std::toupper(c2);
}
if (c != c2) {
return 0;
}
prevEscape = 0;
}
}
return *zString == 0;
}
/*!
* @brief Matcher entry function
*
* @param text Target text to search
* @param pattern Search pattern
* @param esc Escape character
*
* @return matched or not
*/
inline bool match(const char *text, const char *pattern, int esc) {
/* The correct SQL-92 behavior is for the LIKE operator to ignore
** case. Thus 'a' LIKE 'A' would be true. */
const struct compareInfo likeInfoNorm = {'%', '_', 0, USE_SQL92_LIKE};
return (patternCompare(pattern, text, &likeInfoNorm, esc) != 0);
}
#endif /* USE_SQL92_LIKE */
public:
using TqlFunc::operator();
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.size() < 3 || args.size() > 4) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
else if (args[0]->isNullValue() || args[1]->isNullValue() ||
args[2]->isNullValue() ||
(args.size() == 4 && args[3]->isNullValue())) {
return Expr::newNullValue(txn);
}
else if (!args[0]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 1 is not a string");
}
else if (!args[1]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 2 is not a string");
}
else if (!args[2]->isBoolean()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_INTERNAL_INVALID_ARGUMENT,
"Internal logic error: internal argument is not a boolean");
}
else if ((args.size() == 4 && !args[3]->isString())) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 3 is not a string");
}
else {
/* DO NOTHING */
}
const char *s1 = args[0]->getValueAsString(txn);
const char *s2 = args[1]->getValueAsString(txn);
bool invert = args[2]->getValueAsBool();
int esc = 0;
if (args.size() == 4) {
const char *escStr = args[3]->getValueAsString(txn);
if (escStr[0] == '\0' || escStr[1] != '\0') {
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_RANGE,
"ESCAPE CHAR must be only 1 character");
}
esc = escStr[0];
}
#ifndef USE_SQL92_LIKE
return Expr::newBooleanValue(
(invert) ? !match(s1, s2) : match(s1, s2), txn);
#else
return Expr::newBooleanValue(
(invert) ? !match(s1, s2, esc) : match(s1, s2, esc), txn);
#endif
}
virtual ~FunctorLike() {}
};
/*!
* @brief SUBSTRING(str, offset, [length=inf])
*
* @return substring start from 'offset' whose length is 'length'
*/
class FunctorSubstring : public TqlFunc {
public:
using TqlFunc::operator();
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.size() > 3 || args.size() < 2) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
else if (args[0]->isNullValue() || args[1]->isNullValue() ||
(args.size() == 3 && args[2]->isNullValue())) {
return Expr::newNullValue(txn);
}
else if (!args[0]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 1 is not a string");
}
else if (!args[1]->isNumericInteger()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 2 is not an numeric value");
}
else if (args.size() == 3 && !args[2]->isNumericInteger()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 3 is not an numeric value");
}
else {
/* DO NOTHING */
}
int offset = args[1]->getValueAsInt();
if (offset <= 0) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_RANGE,
"Argument 2 is invalid range");
}
size_t off = static_cast<size_t>(offset - 1);
const char *str = args[0]->getValueAsString(txn);
util::String newstr(
"", QP_ALLOCATOR);
if (args.size() == 2) {
_utf8Substring(str, static_cast<int>(off), -1, newstr);
}
else {
int len = args[2]->getValueAsInt();
if (len < 0) {
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_RANGE,
"Argument 3 is invalid as length");
}
_utf8Substring(str, static_cast<int>(off), len, newstr);
}
return Expr::newStringValue(newstr.c_str(), txn);
}
virtual ~FunctorSubstring() {}
};
/*!
* @brief UPPER(str) and LOWER(str) base
*
* @return string transformed
*/
template <int (*T)(int)>
class FunctorTrans : public TqlFunc {
public:
Expr *operator()(ExprList &args, TransactionContext &txn, ObjectManager &) {
if (args.empty() || args.size() > 1) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
else if (args[0]->isNullValue()) {
return Expr::newNullValue(txn);
}
else if (!args[0]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 1 is not a string");
}
else {
/* DO NOTHING */
}
util::String transformed(args[0]->getValueAsString(txn),
QP_ALLOCATOR);
std::transform(
transformed.begin(), transformed.end(), transformed.begin(), T);
return Expr::newStringValue(transformed.c_str(), txn);
}
virtual ~FunctorTrans() {}
};
/*!
* @brief UPPER(str)
*/
typedef FunctorTrans<std::toupper> Functor_upper;
/*!
* @brief LOWER(str)
*/
typedef FunctorTrans<std::tolower> Functor_lower;
#endif /*FUNCTIONS_STRING_HPP*/