| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
|
| 10 |
public class ParserUtil { |
| 11 |
|
| 12 |
/** |
| 13 |
* A keyword. |
| 14 |
*/ |
| 15 |
public static final int KEYWORD = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* An identifier (table name, column name,...). |
| 19 |
*/ |
| 20 |
public static final int IDENTIFIER = 2; |
| 21 |
|
| 22 |
// Constants below must be sorted |
| 23 |
|
| 24 |
/** |
| 25 |
* The token "ALL". |
| 26 |
*/ |
| 27 |
public static final int ALL = IDENTIFIER + 1; |
| 28 |
|
| 29 |
/** |
| 30 |
* The token "AND". |
| 31 |
*/ |
| 32 |
public static final int AND = ALL + 1; |
| 33 |
|
| 34 |
/** |
| 35 |
* The token "ANY". |
| 36 |
*/ |
| 37 |
public static final int ANY = AND + 1; |
| 38 |
|
| 39 |
/** |
| 40 |
* The token "ARRAY". |
| 41 |
*/ |
| 42 |
public static final int ARRAY = ANY + 1; |
| 43 |
|
| 44 |
/** |
| 45 |
* The token "AS". |
| 46 |
*/ |
| 47 |
public static final int AS = ARRAY + 1; |
| 48 |
|
| 49 |
/** |
| 50 |
* The token "ASYMMETRIC". |
| 51 |
*/ |
| 52 |
public static final int ASYMMETRIC = AS + 1; |
| 53 |
|
| 54 |
/** |
| 55 |
* The token "AUTHORIZATION". |
| 56 |
*/ |
| 57 |
public static final int AUTHORIZATION = ASYMMETRIC + 1; |
| 58 |
|
| 59 |
/** |
| 60 |
* The token "BETWEEN". |
| 61 |
*/ |
| 62 |
public static final int BETWEEN = AUTHORIZATION + 1; |
| 63 |
|
| 64 |
/** |
| 65 |
* The token "CASE". |
| 66 |
*/ |
| 67 |
public static final int CASE = BETWEEN + 1; |
| 68 |
|
| 69 |
/** |
| 70 |
* The token "CAST". |
| 71 |
*/ |
| 72 |
public static final int CAST = CASE + 1; |
| 73 |
|
| 74 |
/** |
| 75 |
* The token "CHECK". |
| 76 |
*/ |
| 77 |
public static final int CHECK = CAST + 1; |
| 78 |
|
| 79 |
/** |
| 80 |
* The token "CONSTRAINT". |
| 81 |
*/ |
| 82 |
public static final int CONSTRAINT = CHECK + 1; |
| 83 |
|
| 84 |
/** |
| 85 |
* The token "CROSS". |
| 86 |
*/ |
| 87 |
public static final int CROSS = CONSTRAINT + 1; |
| 88 |
|
| 89 |
/** |
| 90 |
* The token "CURRENT_CATALOG". |
| 91 |
*/ |
| 92 |
public static final int CURRENT_CATALOG = CROSS + 1; |
| 93 |
|
| 94 |
/** |
| 95 |
* The token "CURRENT_DATE". |
| 96 |
*/ |
| 97 |
public static final int CURRENT_DATE = CURRENT_CATALOG + 1; |
| 98 |
|
| 99 |
/** |
| 100 |
* The token "CURRENT_PATH". |
| 101 |
*/ |
| 102 |
public static final int CURRENT_PATH = CURRENT_DATE + 1; |
| 103 |
|
| 104 |
/** |
| 105 |
* The token "CURRENT_ROLE". |
| 106 |
*/ |
| 107 |
public static final int CURRENT_ROLE = CURRENT_PATH + 1; |
| 108 |
|
| 109 |
/** |
| 110 |
* The token "CURRENT_SCHEMA". |
| 111 |
*/ |
| 112 |
public static final int CURRENT_SCHEMA = CURRENT_ROLE + 1; |
| 113 |
|
| 114 |
/** |
| 115 |
* The token "CURRENT_TIME". |
| 116 |
*/ |
| 117 |
public static final int CURRENT_TIME = CURRENT_SCHEMA + 1; |
| 118 |
|
| 119 |
/** |
| 120 |
* The token "CURRENT_TIMESTAMP". |
| 121 |
*/ |
| 122 |
public static final int CURRENT_TIMESTAMP = CURRENT_TIME + 1; |
| 123 |
|
| 124 |
/** |
| 125 |
* The token "CURRENT_USER". |
| 126 |
*/ |
| 127 |
public static final int CURRENT_USER = CURRENT_TIMESTAMP + 1; |
| 128 |
|
| 129 |
/** |
| 130 |
* The token "DAY". |
| 131 |
*/ |
| 132 |
public static final int DAY = CURRENT_USER + 1; |
| 133 |
|
| 134 |
/** |
| 135 |
* The token "DEFAULT". |
| 136 |
*/ |
| 137 |
public static final int DEFAULT = DAY + 1; |
| 138 |
|
| 139 |
/** |
| 140 |
* The token "DISTINCT". |
| 141 |
*/ |
| 142 |
public static final int DISTINCT = DEFAULT + 1; |
| 143 |
|
| 144 |
/** |
| 145 |
* The token "ELSE". |
| 146 |
*/ |
| 147 |
public static final int ELSE = DISTINCT + 1; |
| 148 |
|
| 149 |
/** |
| 150 |
* The token "END". |
| 151 |
*/ |
| 152 |
public static final int END = ELSE + 1; |
| 153 |
|
| 154 |
/** |
| 155 |
* The token "EXCEPT". |
| 156 |
*/ |
| 157 |
public static final int EXCEPT = END + 1; |
| 158 |
|
| 159 |
/** |
| 160 |
* The token "EXISTS". |
| 161 |
*/ |
| 162 |
public static final int EXISTS = EXCEPT + 1; |
| 163 |
|
| 164 |
/** |
| 165 |
* The token "FALSE". |
| 166 |
*/ |
| 167 |
public static final int FALSE = EXISTS + 1; |
| 168 |
|
| 169 |
/** |
| 170 |
* The token "FETCH". |
| 171 |
*/ |
| 172 |
public static final int FETCH = FALSE + 1; |
| 173 |
|
| 174 |
/** |
| 175 |
* The token "FOR". |
| 176 |
*/ |
| 177 |
public static final int FOR = FETCH + 1; |
| 178 |
|
| 179 |
/** |
| 180 |
* The token "FOREIGN". |
| 181 |
*/ |
| 182 |
public static final int FOREIGN = FOR + 1; |
| 183 |
|
| 184 |
/** |
| 185 |
* The token "FROM". |
| 186 |
*/ |
| 187 |
public static final int FROM = FOREIGN + 1; |
| 188 |
|
| 189 |
/** |
| 190 |
* The token "FULL". |
| 191 |
*/ |
| 192 |
public static final int FULL = FROM + 1; |
| 193 |
|
| 194 |
/** |
| 195 |
* The token "GROUP". |
| 196 |
*/ |
| 197 |
public static final int GROUP = FULL + 1; |
| 198 |
|
| 199 |
/** |
| 200 |
* The token "HAVING". |
| 201 |
*/ |
| 202 |
public static final int HAVING = GROUP + 1; |
| 203 |
|
| 204 |
/** |
| 205 |
* The token "HOUR". |
| 206 |
*/ |
| 207 |
public static final int HOUR = HAVING + 1; |
| 208 |
|
| 209 |
/** |
| 210 |
* The token "IF". |
| 211 |
*/ |
| 212 |
public static final int IF = HOUR + 1; |
| 213 |
|
| 214 |
/** |
| 215 |
* The token "IN". |
| 216 |
*/ |
| 217 |
public static final int IN = IF + 1; |
| 218 |
|
| 219 |
/** |
| 220 |
* The token "INNER". |
| 221 |
*/ |
| 222 |
public static final int INNER = IN + 1; |
| 223 |
|
| 224 |
/** |
| 225 |
* The token "INTERSECT". |
| 226 |
*/ |
| 227 |
public static final int INTERSECT = INNER + 1; |
| 228 |
|
| 229 |
/** |
| 230 |
* The token "INTERVAL". |
| 231 |
*/ |
| 232 |
public static final int INTERVAL = INTERSECT + 1; |
| 233 |
|
| 234 |
/** |
| 235 |
* The token "IS". |
| 236 |
*/ |
| 237 |
public static final int IS = INTERVAL + 1; |
| 238 |
|
| 239 |
/** |
| 240 |
* The token "JOIN". |
| 241 |
*/ |
| 242 |
public static final int JOIN = IS + 1; |
| 243 |
|
| 244 |
/** |
| 245 |
* The token "KEY". |
| 246 |
*/ |
| 247 |
public static final int KEY = JOIN + 1; |
| 248 |
|
| 249 |
/** |
| 250 |
* The token "LEFT". |
| 251 |
*/ |
| 252 |
public static final int LEFT = KEY + 1; |
| 253 |
|
| 254 |
/** |
| 255 |
* The token "LIKE". |
| 256 |
*/ |
| 257 |
public static final int LIKE = LEFT + 1; |
| 258 |
|
| 259 |
/** |
| 260 |
* The token "LIMIT". |
| 261 |
*/ |
| 262 |
public static final int LIMIT = LIKE + 1; |
| 263 |
|
| 264 |
/** |
| 265 |
* The token "LOCALTIME". |
| 266 |
*/ |
| 267 |
public static final int LOCALTIME = LIMIT + 1; |
| 268 |
|
| 269 |
/** |
| 270 |
* The token "LOCALTIMESTAMP". |
| 271 |
*/ |
| 272 |
public static final int LOCALTIMESTAMP = LOCALTIME + 1; |
| 273 |
|
| 274 |
/** |
| 275 |
* The token "MINUS". |
| 276 |
*/ |
| 277 |
public static final int MINUS = LOCALTIMESTAMP + 1; |
| 278 |
|
| 279 |
/** |
| 280 |
* The token "MINUTE". |
| 281 |
*/ |
| 282 |
public static final int MINUTE = MINUS + 1; |
| 283 |
|
| 284 |
/** |
| 285 |
* The token "MONTH". |
| 286 |
*/ |
| 287 |
public static final int MONTH = MINUTE + 1; |
| 288 |
|
| 289 |
/** |
| 290 |
* The token "NATURAL". |
| 291 |
*/ |
| 292 |
public static final int NATURAL = MONTH + 1; |
| 293 |
|
| 294 |
/** |
| 295 |
* The token "NOT". |
| 296 |
*/ |
| 297 |
public static final int NOT = NATURAL + 1; |
| 298 |
|
| 299 |
/** |
| 300 |
* The token "NULL". |
| 301 |
*/ |
| 302 |
public static final int NULL = NOT + 1; |
| 303 |
|
| 304 |
/** |
| 305 |
* The token "OFFSET". |
| 306 |
*/ |
| 307 |
public static final int OFFSET = NULL + 1; |
| 308 |
|
| 309 |
/** |
| 310 |
* The token "ON". |
| 311 |
*/ |
| 312 |
public static final int ON = OFFSET + 1; |
| 313 |
|
| 314 |
/** |
| 315 |
* The token "OR". |
| 316 |
*/ |
| 317 |
public static final int OR = ON + 1; |
| 318 |
|
| 319 |
/** |
| 320 |
* The token "ORDER". |
| 321 |
*/ |
| 322 |
public static final int ORDER = OR + 1; |
| 323 |
|
| 324 |
/** |
| 325 |
* The token "PRIMARY". |
| 326 |
*/ |
| 327 |
public static final int PRIMARY = ORDER + 1; |
| 328 |
|
| 329 |
/** |
| 330 |
* The token "QUALIFY". |
| 331 |
*/ |
| 332 |
public static final int QUALIFY = PRIMARY + 1; |
| 333 |
|
| 334 |
/** |
| 335 |
* The token "RIGHT". |
| 336 |
*/ |
| 337 |
public static final int RIGHT = QUALIFY + 1; |
| 338 |
|
| 339 |
/** |
| 340 |
* The token "ROW". |
| 341 |
*/ |
| 342 |
public static final int ROW = RIGHT + 1; |
| 343 |
|
| 344 |
/** |
| 345 |
* The token "ROWNUM". |
| 346 |
*/ |
| 347 |
public static final int ROWNUM = ROW + 1; |
| 348 |
|
| 349 |
/** |
| 350 |
* The token "SECOND". |
| 351 |
*/ |
| 352 |
public static final int SECOND = ROWNUM + 1; |
| 353 |
|
| 354 |
/** |
| 355 |
* The token "SELECT". |
| 356 |
*/ |
| 357 |
public static final int SELECT = SECOND + 1; |
| 358 |
|
| 359 |
/** |
| 360 |
* The token "SESSION_USER". |
| 361 |
*/ |
| 362 |
public static final int SESSION_USER = SELECT + 1; |
| 363 |
|
| 364 |
/** |
| 365 |
* The token "SET". |
| 366 |
*/ |
| 367 |
public static final int SET = SESSION_USER + 1; |
| 368 |
|
| 369 |
/** |
| 370 |
* The token "SOME". |
| 371 |
*/ |
| 372 |
public static final int SOME = SET + 1; |
| 373 |
|
| 374 |
/** |
| 375 |
* The token "SYMMETRIC". |
| 376 |
*/ |
| 377 |
public static final int SYMMETRIC = SOME + 1; |
| 378 |
|
| 379 |
/** |
| 380 |
* The token "SYSTEM_USER". |
| 381 |
*/ |
| 382 |
public static final int SYSTEM_USER = SYMMETRIC + 1; |
| 383 |
|
| 384 |
/** |
| 385 |
* The token "TABLE". |
| 386 |
*/ |
| 387 |
public static final int TABLE = SYSTEM_USER + 1; |
| 388 |
|
| 389 |
/** |
| 390 |
* The token "TO". |
| 391 |
*/ |
| 392 |
public static final int TO = TABLE + 1; |
| 393 |
|
| 394 |
/** |
| 395 |
* The token "TRUE". |
| 396 |
*/ |
| 397 |
public static final int TRUE = TO + 1; |
| 398 |
|
| 399 |
/** |
| 400 |
* The token "UESCAPE". |
| 401 |
*/ |
| 402 |
public static final int UESCAPE = TRUE + 1; |
| 403 |
|
| 404 |
/** |
| 405 |
* The token "UNION". |
| 406 |
*/ |
| 407 |
public static final int UNION = UESCAPE + 1; |
| 408 |
|
| 409 |
/** |
| 410 |
* The token "UNIQUE". |
| 411 |
*/ |
| 412 |
public static final int UNIQUE = UNION + 1; |
| 413 |
|
| 414 |
/** |
| 415 |
* The token "UNKNOWN". |
| 416 |
*/ |
| 417 |
public static final int UNKNOWN = UNIQUE + 1; |
| 418 |
|
| 419 |
/** |
| 420 |
* The token "USER". |
| 421 |
*/ |
| 422 |
public static final int USER = UNKNOWN + 1; |
| 423 |
|
| 424 |
/** |
| 425 |
* The token "USING". |
| 426 |
*/ |
| 427 |
public static final int USING = USER + 1; |
| 428 |
|
| 429 |
/** |
| 430 |
* The token "VALUE". |
| 431 |
*/ |
| 432 |
public static final int VALUE = USING + 1; |
| 433 |
|
| 434 |
/** |
| 435 |
* The token "VALUES". |
| 436 |
*/ |
| 437 |
public static final int VALUES = VALUE + 1; |
| 438 |
|
| 439 |
/** |
| 440 |
* The token "WHEN". |
| 441 |
*/ |
| 442 |
public static final int WHEN = VALUES + 1; |
| 443 |
|
| 444 |
/** |
| 445 |
* The token "WHERE". |
| 446 |
*/ |
| 447 |
public static final int WHERE = WHEN + 1; |
| 448 |
|
| 449 |
/** |
| 450 |
* The token "WINDOW". |
| 451 |
*/ |
| 452 |
public static final int WINDOW = WHERE + 1; |
| 453 |
|
| 454 |
/** |
| 455 |
* The token "WITH". |
| 456 |
*/ |
| 457 |
public static final int WITH = WINDOW + 1; |
| 458 |
|
| 459 |
/** |
| 460 |
* The token "YEAR". |
| 461 |
*/ |
| 462 |
public static final int YEAR = WITH + 1; |
| 463 |
|
| 464 |
/** |
| 465 |
* The token "_ROWID_". |
| 466 |
*/ |
| 467 |
public static final int _ROWID_ = YEAR + 1; |
| 468 |
|
| 469 |
// Constants above must be sorted |
| 470 |
|
| 471 |
/** |
| 472 |
* The ordinal number of the first keyword. |
| 473 |
*/ |
| 474 |
public static final int FIRST_KEYWORD = IDENTIFIER + 1; |
| 475 |
|
| 476 |
/** |
| 477 |
* The ordinal number of the last keyword. |
| 478 |
*/ |
| 479 |
public static final int LAST_KEYWORD = _ROWID_; |
| 480 |
|
| 481 |
private static final HashMap<String, Integer> KEYWORDS; |
| 482 |
|
| 483 |
static { |
| 484 |
HashMap<String, Integer> map = new HashMap<>(256); |
| 485 |
map.put("ALL", ALL); |
| 486 |
map.put("AND", AND); |
| 487 |
map.put("ANY", ANY); |
| 488 |
map.put("ARRAY", ARRAY); |
| 489 |
map.put("AS", AS); |
| 490 |
map.put("ASYMMETRIC", ASYMMETRIC); |
| 491 |
map.put("AUTHORIZATION", AUTHORIZATION); |
| 492 |
map.put("BETWEEN", BETWEEN); |
| 493 |
map.put("CASE", CASE); |
| 494 |
map.put("CAST", CAST); |
| 495 |
map.put("CHECK", CHECK); |
| 496 |
map.put("CONSTRAINT", CONSTRAINT); |
| 497 |
map.put("CROSS", CROSS); |
| 498 |
map.put("CURRENT_CATALOG", CURRENT_CATALOG); |
| 499 |
map.put("CURRENT_DATE", CURRENT_DATE); |
| 500 |
map.put("CURRENT_PATH", CURRENT_PATH); |
| 501 |
map.put("CURRENT_ROLE", CURRENT_ROLE); |
| 502 |
map.put("CURRENT_SCHEMA", CURRENT_SCHEMA); |
| 503 |
map.put("CURRENT_TIME", CURRENT_TIME); |
| 504 |
map.put("CURRENT_TIMESTAMP", CURRENT_TIMESTAMP); |
| 505 |
map.put("CURRENT_USER", CURRENT_USER); |
| 506 |
map.put("DAY", DAY); |
| 507 |
map.put("DEFAULT", DEFAULT); |
| 508 |
map.put("DISTINCT", DISTINCT); |
| 509 |
map.put("ELSE", ELSE); |
| 510 |
map.put("END", END); |
| 511 |
map.put("EXCEPT", EXCEPT); |
| 512 |
map.put("EXISTS", EXISTS); |
| 513 |
map.put("FALSE", FALSE); |
| 514 |
map.put("FETCH", FETCH); |
| 515 |
map.put("FOR", FOR); |
| 516 |
map.put("FOREIGN", FOREIGN); |
| 517 |
map.put("FROM", FROM); |
| 518 |
map.put("FULL", FULL); |
| 519 |
map.put("GROUP", GROUP); |
| 520 |
map.put("HAVING", HAVING); |
| 521 |
map.put("HOUR", HOUR); |
| 522 |
map.put("IF", IF); |
| 523 |
map.put("IN", IN); |
| 524 |
map.put("INNER", INNER); |
| 525 |
map.put("INTERSECT", INTERSECT); |
| 526 |
map.put("INTERVAL", INTERVAL); |
| 527 |
map.put("IS", IS); |
| 528 |
map.put("JOIN", JOIN); |
| 529 |
map.put("KEY", KEY); |
| 530 |
map.put("LEFT", LEFT); |
| 531 |
map.put("LIKE", LIKE); |
| 532 |
map.put("LIMIT", LIMIT); |
| 533 |
map.put("LOCALTIME", LOCALTIME); |
| 534 |
map.put("LOCALTIMESTAMP", LOCALTIMESTAMP); |
| 535 |
map.put("MINUS", MINUS); |
| 536 |
map.put("MINUTE", MINUTE); |
| 537 |
map.put("MONTH", MONTH); |
| 538 |
map.put("NATURAL", NATURAL); |
| 539 |
map.put("NOT", NOT); |
| 540 |
map.put("NULL", NULL); |
| 541 |
map.put("OFFSET", OFFSET); |
| 542 |
map.put("ON", ON); |
| 543 |
map.put("OR", OR); |
| 544 |
map.put("ORDER", ORDER); |
| 545 |
map.put("PRIMARY", PRIMARY); |
| 546 |
map.put("QUALIFY", QUALIFY); |
| 547 |
map.put("RIGHT", RIGHT); |
| 548 |
map.put("ROW", ROW); |
| 549 |
map.put("ROWNUM", ROWNUM); |
| 550 |
map.put("SECOND", SECOND); |
| 551 |
map.put("SELECT", SELECT); |
| 552 |
map.put("SESSION_USER", SESSION_USER); |
| 553 |
map.put("SET", SET); |
| 554 |
map.put("SOME", SOME); |
| 555 |
map.put("SYMMETRIC", SYMMETRIC); |
| 556 |
map.put("SYSTEM_USER", SYSTEM_USER); |
| 557 |
map.put("TABLE", TABLE); |
| 558 |
map.put("TO", TO); |
| 559 |
map.put("TRUE", TRUE); |
| 560 |
map.put("UESCAPE", UESCAPE); |
| 561 |
map.put("UNION", UNION); |
| 562 |
map.put("UNIQUE", UNIQUE); |
| 563 |
map.put("UNKNOWN", UNKNOWN); |
| 564 |
map.put("USER", USER); |
| 565 |
map.put("USING", USING); |
| 566 |
map.put("VALUE", VALUE); |
| 567 |
map.put("VALUES", VALUES); |
| 568 |
map.put("WHEN", WHEN); |
| 569 |
map.put("WHERE", WHERE); |
| 570 |
map.put("WINDOW", WINDOW); |
| 571 |
map.put("WITH", WITH); |
| 572 |
map.put("YEAR", YEAR); |
| 573 |
map.put("_ROWID_", _ROWID_); |
| 574 |
// Additional keywords |
| 575 |
map.put("BOTH", KEYWORD); |
| 576 |
map.put("GROUPS", KEYWORD); |
| 577 |
map.put("ILIKE", KEYWORD); |
| 578 |
map.put("LEADING", KEYWORD); |
| 579 |
map.put("OVER", KEYWORD); |
| 580 |
map.put("PARTITION", KEYWORD); |
| 581 |
map.put("RANGE", KEYWORD); |
| 582 |
map.put("REGEXP", KEYWORD); |
| 583 |
map.put("ROWS", KEYWORD); |
| 584 |
map.put("TOP", KEYWORD); |
| 585 |
map.put("TRAILING", KEYWORD); |
| 586 |
KEYWORDS = map; |
| 587 |
} |
| 588 |
|
| 589 |
private ParserUtil() { |
| 590 |
// utility class |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* Add double quotes around an identifier if required and appends it to the |
| 595 |
* specified string builder. |
| 596 |
* |
| 597 |
* @param builder string builder to append to |
| 598 |
* @param s the identifier |
| 599 |
* @param sqlFlags formatting flags |
| 600 |
* @return the specified builder |
| 601 |
*/ |
| 602 |
public static StringBuilder quoteIdentifier(StringBuilder builder, String s, int sqlFlags) { |
| 603 |
if (s == null) { |
| 604 |
return builder.append("\"\""); |
| 605 |
} |
| 606 |
if ((sqlFlags & HasSQL.QUOTE_ONLY_WHEN_REQUIRED) != 0 && isSimpleIdentifier(s, false, false)) { |
| 607 |
return builder.append(s); |
| 608 |
} |
| 609 |
return StringUtils.quoteIdentifier(builder, s); |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Checks if this string is a SQL keyword. |
| 614 |
* |
| 615 |
* @param s the token to check |
| 616 |
* @param ignoreCase true if case should be ignored, false if only upper case |
| 617 |
* tokens are detected as keywords |
| 618 |
* @return true if it is a keyword |
| 619 |
*/ |
| 620 |
public static boolean isKeyword(String s, boolean ignoreCase) { |
| 621 |
return getTokenType(s, ignoreCase, false) != IDENTIFIER; |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* Is this a simple identifier (in the JDBC specification sense). |
| 626 |
* |
| 627 |
* @param s identifier to check |
| 628 |
* @param databaseToUpper whether unquoted identifiers are converted to upper case |
| 629 |
* @param databaseToLower whether unquoted identifiers are converted to lower case |
| 630 |
* @return is specified identifier may be used without quotes |
| 631 |
* @throws NullPointerException if s is {@code null} |
| 632 |
*/ |
| 633 |
public static boolean isSimpleIdentifier(String s, boolean databaseToUpper, boolean databaseToLower) { |
| 634 |
if (databaseToUpper && databaseToLower) { |
| 635 |
throw new IllegalArgumentException("databaseToUpper && databaseToLower"); |
| 636 |
} |
| 637 |
int length = s.length(); |
| 638 |
if (length == 0 || !checkLetter(databaseToUpper, databaseToLower, s.charAt(0))) { |
| 639 |
return false; |
| 640 |
} |
| 641 |
for (int i = 1; i < length; i++) { |
| 642 |
char c = s.charAt(i); |
| 643 |
if (c != '_' && (c < '0' || c > '9') && !checkLetter(databaseToUpper, databaseToLower, c)) { |
| 644 |
return false; |
| 645 |
} |
| 646 |
} |
| 647 |
return getTokenType(s, !databaseToUpper, true) == IDENTIFIER; |
| 648 |
} |
| 649 |
|
| 650 |
private static boolean checkLetter(boolean databaseToUpper, boolean databaseToLower, char c) { |
| 651 |
if (databaseToUpper) { |
| 652 |
if (c < 'A' || c > 'Z') { |
| 653 |
return false; |
| 654 |
} |
| 655 |
} else if (databaseToLower) { |
| 656 |
if (c < 'a' || c > 'z') { |
| 657 |
return false; |
| 658 |
} |
| 659 |
} else { |
| 660 |
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) { |
| 661 |
return false; |
| 662 |
} |
| 663 |
} |
| 664 |
return true; |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* Get the token type. |
| 669 |
* |
| 670 |
* @param s the string with token |
| 671 |
* @param ignoreCase true if case should be ignored, false if only upper case |
| 672 |
* tokens are detected as keywords |
| 673 |
* @param additionalKeywords |
| 674 |
* whether context-sensitive keywords are returned as |
| 675 |
* {@link #KEYWORD} |
| 676 |
* @return the token type |
| 677 |
*/ |
| 678 |
public static int getTokenType(String s, boolean ignoreCase, boolean additionalKeywords) { |
| 679 |
int length = s.length(); |
| 680 |
if (length <= 1 || length > 17) { |
| 681 |
return IDENTIFIER; |
| 682 |
} |
| 683 |
if (ignoreCase) { |
| 684 |
s = StringUtils.toUpperEnglish(s); |
| 685 |
} |
| 686 |
Integer type = KEYWORDS.get(s); |
| 687 |
if (type == null) { |
| 688 |
return IDENTIFIER; |
| 689 |
} |
| 690 |
int t = type; |
| 691 |
return t == KEYWORD && !additionalKeywords ? IDENTIFIER : t; |
| 692 |
} |
| 693 |
|
| 694 |
} |