| 1 | /* ============================================================= |
| 2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
| 3 | * ============================================================= |
| 4 | * |
| 5 | * (C) Copyright 2004-2006, by Volker Berlin. |
| 6 | * |
| 7 | * Project Info: http://www.smallsql.de/ |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU Lesser General Public License as published by |
| 11 | * the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 | * License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 22 | * USA. |
| 23 | * |
| 24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 25 | * in the United States and other countries.] |
| 26 | * |
| 27 | * --------------- |
| 28 | * SQLTokenizer.java |
| 29 | * --------------- |
| 30 | * Author: Volker Berlin |
| 31 | * |
| 32 | */ |
| 33 | package smallsql.database; |
| 34 | |
| 35 | import java.util.*; |
| 36 | import java.sql.Types; |
| 37 | |
| 38 | public class SQLTokenizer { |
| 39 | |
| 40 | public static List parseSQL( char[] sql ){ |
| 41 | SearchNode node = searchTree; |
| 42 | ArrayList tokens = new ArrayList(); |
| 43 | int value = 0; |
| 44 | int tokenStart = 0; |
| 45 | boolean wasWhiteSpace = true; |
| 46 | char quote = 0; |
| 47 | StringBuffer quoteBuffer = new StringBuffer(); |
| 48 | |
| 49 | for(int i=0; i<sql.length; i++){ |
| 50 | char c = sql[i]; |
| 51 | switch(c){ |
| 52 | case '\"': |
| 53 | case '\'': |
| 54 | if(quote == 0){ |
| 55 | quote = c; |
| 56 | }else if(quote == c){ |
| 57 | // prüfen auf escaped quote |
| 58 | if(i+1<sql.length && sql[i+1] == quote){ |
| 59 | quoteBuffer.append(quote); |
| 60 | i++; |
| 61 | }else{ |
| 62 | tokens.add( new SQLToken( quoteBuffer.toString(), (quote == '\'') ? STRING : IDENTIFER, tokenStart, i+1) ); |
| 63 | quoteBuffer.setLength(0); |
| 64 | quote = 0; |
| 65 | tokenStart = i+1; |
| 66 | wasWhiteSpace = true; |
| 67 | } |
| 68 | }else quoteBuffer.append(c); |
| 69 | break; |
| 70 | case '.': |
| 71 | if(quote == 0){ |
| 72 | // there are follow cases with a point |
| 73 | // "abc"."abc" --> identifer --> multiple tokens |
| 74 | // "5"."3" --> identifer --> multiple tokens |
| 75 | // 5.3 --> number --> one token |
| 76 | // 5.e3 --> number --> one token |
| 77 | // .3 --> number --> one token |
| 78 | // .e3 --> identifer --> multiple tokens |
| 79 | int k=tokenStart; |
| 80 | if(k == i){ // point is first charcter |
| 81 | if(sql.length> k+1){ |
| 82 | char cc = sql[k+1]; |
| 83 | if((cc >= '0') && cc <= '9') break; // is a number --> break |
| 84 | } |
| 85 | }else{ |
| 86 | for(; k<i; k++){ |
| 87 | char cc = sql[k]; |
| 88 | if((cc != '-' && cc != '$' && cc < '0') || cc > '9') break; // is identifer --> break |
| 89 | } |
| 90 | if(k>=i) break; // vorhergehende token sind nur Ziffern, also kein Bezeichner sonder FLießkommazahl |
| 91 | } |
| 92 | } |
| 93 | // Zeichen vorher ist keine Zahl, also Bezeichner |
| 94 | // kein break; |
| 95 | case '-': |
| 96 | if(quote == 0 && !wasWhiteSpace){ |
| 97 | char c1 = sql[tokenStart]; |
| 98 | char cx = sql[i-1]; |
| 99 | if(((c1 >= '0' && c1 <= '9') || c1 == '.') && (cx == 'e' || cx == 'E')) |
| 100 | //negative exponential number |
| 101 | break; |
| 102 | if(c1 == '$' && tokenStart+1 == i) |
| 103 | // money number |
| 104 | break; |
| 105 | } |
| 106 | case ' ': |
| 107 | case '\t': |
| 108 | case '\n': |
| 109 | case '\r': |
| 110 | case ',': |
| 111 | case '(': |
| 112 | case ')': |
| 113 | case '{': |
| 114 | case '}': |
| 115 | case '*': |
| 116 | case '+': |
| 117 | case '/': |
| 118 | case '%': |
| 119 | case '&': |
| 120 | case '|': |
| 121 | case '=': |
| 122 | case '<': |
| 123 | case '>': |
| 124 | case '?': |
| 125 | case '^': |
| 126 | case '~': |
| 127 | if(quote == 0){ |
| 128 | if(!wasWhiteSpace){ |
| 129 | tokens.add( new SQLToken( value, tokenStart, i) ); |
| 130 | value = 0; |
| 131 | } |
| 132 | switch(c){ |
| 133 | case ' ': |
| 134 | case '\t': |
| 135 | case '\n': |
| 136 | case '\r': |
| 137 | // skipp this characters, this are not tokens, this are only source formater |
| 138 | break; |
| 139 | case '<': |
| 140 | if((i+1 < sql.length) && (sql[i+1] == '>')){ |
| 141 | tokens.add( new SQLToken( UNEQUALS, i, i+2) ); |
| 142 | i++; |
| 143 | break; |
| 144 | } |
| 145 | case '>': |
| 146 | if((i+1 < sql.length) && (sql[i+1] == '=')){ |
| 147 | tokens.add( new SQLToken( 100 + c, i, i+2) ); |
| 148 | i++; |
| 149 | break; |
| 150 | } |
| 151 | default: |
| 152 | tokens.add( new SQLToken( c, i, i+1) ); |
| 153 | } |
| 154 | wasWhiteSpace = true; |
| 155 | tokenStart = i+1; |
| 156 | }else{ |
| 157 | quoteBuffer.append(c); |
| 158 | } |
| 159 | break; |
| 160 | default: |
| 161 | if(quote == 0){ |
| 162 | if(wasWhiteSpace){ |
| 163 | node = searchTree; |
| 164 | }else{ |
| 165 | if(node == null){ |
| 166 | value = 0; |
| 167 | wasWhiteSpace = false; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | c |= 0x20; // case insensitiv |
| 172 | while(node != null && node.letter != c) node = node.nextEntry; |
| 173 | if(node != null){ |
| 174 | value = node.value; |
| 175 | node = node.nextLetter; |
| 176 | }else{ |
| 177 | value = 0; |
| 178 | node = null; |
| 179 | } |
| 180 | }else{ |
| 181 | quoteBuffer.append(c); |
| 182 | } |
| 183 | wasWhiteSpace = false; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if(!wasWhiteSpace){ |
| 188 | tokens.add( new SQLToken( value, tokenStart, sql.length) ); |
| 189 | } |
| 190 | return tokens; |
| 191 | } |
| 192 | |
| 193 | static private void addKeyWord( String keyword, int value){ |
| 194 | keywords.put( Utils.getInteger( value), keyword ); |
| 195 | |
| 196 | char[] letters = keyword.toCharArray(); |
| 197 | if(searchTree == null){ |
| 198 | searchTree = new SearchNode(); |
| 199 | searchTree.letter = (char)(letters[0] | 0x20); |
| 200 | } |
| 201 | SearchNode prev = null; |
| 202 | SearchNode node = searchTree; |
| 203 | boolean wasNextEntry = true; // of node nextEntry oder nextLetter von prev ist |
| 204 | for(int i=0; i<letters.length; i++){ |
| 205 | char c = (char)(letters[i] | 0x20); |
| 206 | while(node != null && node.letter != c) { |
| 207 | prev = node; |
| 208 | node = node.nextEntry; |
| 209 | wasNextEntry = true; |
| 210 | } |
| 211 | if(node == null){ |
| 212 | node = new SearchNode(); |
| 213 | node.letter = c; |
| 214 | if(wasNextEntry) |
| 215 | prev.nextEntry = node; |
| 216 | else prev.nextLetter = node; |
| 217 | wasNextEntry = false; |
| 218 | prev = node; |
| 219 | node = null; |
| 220 | }else{ |
| 221 | prev = node; |
| 222 | node = node.nextLetter; |
| 223 | wasNextEntry = false; |
| 224 | } |
| 225 | } |
| 226 | prev.value = value; |
| 227 | } |
| 228 | |
| 229 | static final String getKeyWord(int key){ |
| 230 | return (String)keywords.get( Utils.getInteger(key) ); |
| 231 | } |
| 232 | |
| 233 | static final int getSQLDataType(int type){ |
| 234 | // on change of this map the order from getTypeInfo need to be change |
| 235 | switch(type){ |
| 236 | case SQLTokenizer.BIT: |
| 237 | return Types.BIT; |
| 238 | case SQLTokenizer.BOOLEAN: |
| 239 | return Types.BOOLEAN; |
| 240 | case SQLTokenizer.BINARY: |
| 241 | return Types.BINARY; |
| 242 | case SQLTokenizer.VARBINARY: |
| 243 | return Types.VARBINARY; |
| 244 | case SQLTokenizer.LONGVARBINARY: |
| 245 | return Types.LONGVARBINARY; |
| 246 | case SQLTokenizer.BLOB: |
| 247 | return Types.BLOB; |
| 248 | case SQLTokenizer.TINYINT: |
| 249 | return Types.TINYINT; |
| 250 | case SQLTokenizer.SMALLINT: |
| 251 | return Types.SMALLINT; |
| 252 | case SQLTokenizer.INT: |
| 253 | return Types.INTEGER; |
| 254 | case SQLTokenizer.BIGINT: |
| 255 | return Types.BIGINT; |
| 256 | case SQLTokenizer.SMALLMONEY: |
| 257 | case SQLTokenizer.MONEY: |
| 258 | case SQLTokenizer.DECIMAL: |
| 259 | return Types.DECIMAL; |
| 260 | case SQLTokenizer.NUMERIC: |
| 261 | return Types.NUMERIC; |
| 262 | case SQLTokenizer.REAL: |
| 263 | return Types.REAL; |
| 264 | case SQLTokenizer.FLOAT: |
| 265 | return Types.FLOAT; |
| 266 | case SQLTokenizer.DOUBLE: |
| 267 | return Types.DOUBLE; |
| 268 | case SQLTokenizer.DATE: |
| 269 | return Types.DATE; |
| 270 | case SQLTokenizer.TIME: |
| 271 | return Types.TIME; |
| 272 | case SQLTokenizer.TIMESTAMP: |
| 273 | case SQLTokenizer.SMALLDATETIME: |
| 274 | return Types.TIMESTAMP; |
| 275 | case SQLTokenizer.CHAR: |
| 276 | case SQLTokenizer.NCHAR: |
| 277 | return Types.CHAR; |
| 278 | case SQLTokenizer.VARCHAR: |
| 279 | case SQLTokenizer.NVARCHAR: |
| 280 | return Types.VARCHAR; |
| 281 | case SQLTokenizer.LONGNVARCHAR: |
| 282 | case SQLTokenizer.LONGVARCHAR: |
| 283 | return Types.LONGVARCHAR; |
| 284 | case SQLTokenizer.CLOB: |
| 285 | case SQLTokenizer.NCLOB: |
| 286 | return Types.CLOB; |
| 287 | case SQLTokenizer.JAVA_OBJECT: |
| 288 | return Types.JAVA_OBJECT; |
| 289 | case SQLTokenizer.UNIQUEIDENTIFIER: |
| 290 | return -11; |
| 291 | case SQLTokenizer.NULL: |
| 292 | return Types.NULL; |
| 293 | default: throw new Error("DataType:"+type); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static SearchNode searchTree; |
| 298 | |
| 299 | static Hashtable keywords = new Hashtable(337); |
| 300 | static final int VALUE = 0; |
| 301 | static final int STRING = 3; |
| 302 | static final int IDENTIFER = 4; |
| 303 | static final int NUMBERVALUE= 5; |
| 304 | static{ |
| 305 | // for Error messages |
| 306 | keywords.put( new Integer(VALUE), "<expression>" ); |
| 307 | keywords.put( new Integer(IDENTIFER), "<identifer>" ); |
| 308 | keywords.put( new Integer(NUMBERVALUE), "<number>" ); |
| 309 | } |
| 310 | |
| 311 | static final int PERCENT = '%'; // 37 |
| 312 | static final int BIT_AND = '&'; // 38 |
| 313 | static final int PARENTHESIS_L = '('; // 40 |
| 314 | static final int PARENTHESIS_R = ')'; // 41 |
| 315 | static final int ASTERISK = '*'; // 42 |
| 316 | static final int PLUS = '+'; // 43 |
| 317 | static final int COMMA = ','; // 44 |
| 318 | static final int MINUS = '-'; // 45 |
| 319 | static final int POINT = '.'; // 46 |
| 320 | static final int SLACH = '/'; // 47 |
| 321 | static final int LESSER = '<'; // 60 |
| 322 | static final int EQUALS = '='; // 61 |
| 323 | static final int GREATER = '>'; // 62 |
| 324 | static final int QUESTION = '?'; // 63 |
| 325 | static final int BIT_XOR = '^'; // 94 |
| 326 | static final int ESCAPE_L = '{'; // 123 |
| 327 | static final int BIT_OR = '|'; // 124 |
| 328 | static final int ESCAPE_R = '}'; // 125 |
| 329 | static final int TILDE = '~'; // 126 |
| 330 | |
| 331 | static final int LESSER_EQU = 100 + LESSER; // <= |
| 332 | static final int UNEQUALS = 100 + EQUALS; // <> |
| 333 | static final int GREATER_EQU = 100 + GREATER; // >= |
| 334 | |
| 335 | static{ |
| 336 | // für Fehlermeldungen |
| 337 | keywords.put( new Integer(LESSER_EQU), "<=" ); |
| 338 | keywords.put( new Integer(UNEQUALS), "<>" ); |
| 339 | keywords.put( new Integer(GREATER_EQU), ">=" ); |
| 340 | } |
| 341 | static final int SELECT = 200; |
| 342 | static final int DELETE = 201; |
| 343 | //static final int INSERT = 202; |
| 344 | static final int INTO = 203; |
| 345 | static final int UPDATE = 204; |
| 346 | static final int CREATE = 205; |
| 347 | static final int DROP = 206; |
| 348 | static final int ALTER = 207; |
| 349 | static final int SET = 208; |
| 350 | static final int EXECUTE = 209; |
| 351 | static final int FROM = 210; |
| 352 | static final int WHERE = 211; |
| 353 | static final int GROUP = 212; |
| 354 | static final int BY = 213; |
| 355 | static final int HAVING = 214; |
| 356 | static final int ORDER = 215; |
| 357 | static final int ASC = 216; |
| 358 | static final int DESC = 217; |
| 359 | static final int VALUES = 218; |
| 360 | static final int AS = 219; |
| 361 | static final int DEFAULT = 220; |
| 362 | static final int IDENTITY = 221; |
| 363 | static final int INNER = 222; |
| 364 | static final int JOIN = 223; |
| 365 | static final int ON = 224; |
| 366 | static final int OUTER = 225; |
| 367 | static final int FULL = 226; |
| 368 | static final int CROSS = 227; |
| 369 | static final int USE = 228; |
| 370 | static final int TOP = 229; |
| 371 | |
| 372 | static final int DATABASE = 230; |
| 373 | static final int TABLE = 231; |
| 374 | static final int VIEW = 232; |
| 375 | static final int INDEX = 233; |
| 376 | static final int PROCEDURE = 234; |
| 377 | |
| 378 | static final int TRANSACTION= 240; |
| 379 | static final int ISOLATION = 241; |
| 380 | static final int LEVEL = 242; |
| 381 | static final int READ = 243; |
| 382 | static final int COMMITTED = 244; |
| 383 | static final int UNCOMMITTED= 245; |
| 384 | static final int REPEATABLE = 246; |
| 385 | static final int SERIALIZABLE= 247; |
| 386 | |
| 387 | static final int CONSTRAINT = 250; |
| 388 | static final int PRIMARY = 251; |
| 389 | static final int FOREIGN = 252; |
| 390 | static final int KEY = 253; |
| 391 | static final int UNIQUE = 254; |
| 392 | static final int CLUSTERED = 255; |
| 393 | static final int NONCLUSTERED=256; |
| 394 | static final int REFERENCES = 257; |
| 395 | |
| 396 | static final int UNION = 260; |
| 397 | static final int ALL = 261; |
| 398 | static final int DISTINCT = 262; |
| 399 | static final int CASE = 263; |
| 400 | static final int WHEN = 264; |
| 401 | static final int THEN = 265; |
| 402 | static final int ELSE = 266; |
| 403 | static final int END = 267; |
| 404 | static final int SWITCH = 268; |
| 405 | |
| 406 | static final String DESC_STR = "DESC"; |
| 407 | static{ |
| 408 | addKeyWord( "SELECT", SELECT); |
| 409 | addKeyWord( "DELETE", DELETE); |
| 410 | // addKeyWord( "INSERT", INSERT); |
| 411 | addKeyWord( "INTO", INTO); |
| 412 | addKeyWord( "UPDATE", UPDATE); |
| 413 | addKeyWord( "CREATE", CREATE); |
| 414 | addKeyWord( "DROP", DROP); |
| 415 | addKeyWord( "ALTER", ALTER); |
| 416 | addKeyWord( "SET", SET); |
| 417 | addKeyWord( "EXEC", EXECUTE); // alias für EXECUTE; alias zuerst setzten, damit in Hashtable |
| 418 | addKeyWord( "EXECUTE", EXECUTE); |
| 419 | addKeyWord( "FROM", FROM); |
| 420 | addKeyWord( "WHERE", WHERE); |
| 421 | addKeyWord( "GROUP", GROUP); |
| 422 | addKeyWord( "BY", BY); |
| 423 | addKeyWord( "HAVING", HAVING); |
| 424 | addKeyWord( "ORDER", ORDER); |
| 425 | addKeyWord( "ASC", ASC); |
| 426 | addKeyWord( DESC_STR, DESC); |
| 427 | addKeyWord( "VALUES", VALUES); |
| 428 | addKeyWord( "AS", AS); |
| 429 | addKeyWord( "DEFAULT", DEFAULT); |
| 430 | addKeyWord( "IDENTITY", IDENTITY); |
| 431 | addKeyWord( "INNER", INNER); |
| 432 | addKeyWord( "JOIN", JOIN); |
| 433 | addKeyWord( "ON", ON); |
| 434 | addKeyWord( "OUTER", OUTER); |
| 435 | addKeyWord( "FULL", FULL); |
| 436 | addKeyWord( "CROSS", CROSS); |
| 437 | addKeyWord( "USE", USE); |
| 438 | addKeyWord( "TOP", TOP); |
| 439 | |
| 440 | addKeyWord( "DATABASE", DATABASE); |
| 441 | addKeyWord( "TABLE", TABLE); |
| 442 | addKeyWord( "VIEW", VIEW); |
| 443 | addKeyWord( "INDEX", INDEX); |
| 444 | addKeyWord( "PROCEDURE",PROCEDURE); |
| 445 | |
| 446 | addKeyWord( "TRANSACTION", TRANSACTION); |
| 447 | addKeyWord( "ISOLATION", ISOLATION); |
| 448 | addKeyWord( "LEVEL", LEVEL); |
| 449 | addKeyWord( "READ", READ); |
| 450 | addKeyWord( "COMMITTED", COMMITTED); |
| 451 | addKeyWord( "UNCOMMITTED", UNCOMMITTED); |
| 452 | addKeyWord( "REPEATABLE", REPEATABLE); |
| 453 | addKeyWord( "SERIALIZABLE", SERIALIZABLE); |
| 454 | |
| 455 | addKeyWord( "CONSTRAINT", CONSTRAINT); |
| 456 | addKeyWord( "PRIMARY", PRIMARY); |
| 457 | addKeyWord( "FOREIGN", FOREIGN); |
| 458 | addKeyWord( "KEY", KEY); |
| 459 | addKeyWord( "UNIQUE", UNIQUE); |
| 460 | addKeyWord( "CLUSTERED", CLUSTERED); |
| 461 | addKeyWord( "NONCLUSTERED", NONCLUSTERED); |
| 462 | addKeyWord( "REFERENCES", REFERENCES); |
| 463 | |
| 464 | addKeyWord( "UNION", UNION); |
| 465 | addKeyWord( "ALL", ALL); |
| 466 | addKeyWord( "DISTINCT", DISTINCT); |
| 467 | addKeyWord( "CASE", CASE); |
| 468 | addKeyWord( "WHEN", WHEN); |
| 469 | addKeyWord( "THEN", THEN); |
| 470 | addKeyWord( "ELSE", ELSE); |
| 471 | addKeyWord( "END", END); |
| 472 | addKeyWord( "SWITCH", SWITCH); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | //data types |
| 477 | static final int BIT = 300; |
| 478 | static final int BOOLEAN = 301; |
| 479 | static final int BINARY = 310; |
| 480 | static final int VARBINARY = 311; |
| 481 | static final int RAW = 312; |
| 482 | static final int LONGVARBINARY = 313; |
| 483 | static final int BLOB = 316; |
| 484 | static final int TINYINT = 321; |
| 485 | static final int SMALLINT = 322; |
| 486 | static final int INT = 323; |
| 487 | static final int COUNTER = 324; //alias für INT IDENTITY, wird von ACCESS verwendet |
| 488 | static final int BIGINT = 325; |
| 489 | static final int SMALLMONEY = 330; |
| 490 | static final int MONEY = 331; |
| 491 | static final int DECIMAL = 332; |
| 492 | static final int NUMERIC = 333; |
| 493 | static final int REAL = 336; |
| 494 | static final int FLOAT = 337; |
| 495 | static final int DOUBLE = 338; |
| 496 | static final int DATE = 340; |
| 497 | static final int TIME = 341; |
| 498 | static final int TIMESTAMP = 342; |
| 499 | static final int SMALLDATETIME = 343; |
| 500 | static final int CHAR = 350; |
| 501 | static final int NCHAR = 352; |
| 502 | static final int VARCHAR = 353; |
| 503 | static final int NVARCHAR = 355; |
| 504 | static final int SYSNAME = 357; |
| 505 | static final int LONGVARCHAR = 359; |
| 506 | static final int LONGNVARCHAR = 360; |
| 507 | static final int LONG = 361; |
| 508 | static final int CLOB = 362; |
| 509 | static final int NCLOB = 363; |
| 510 | static final int UNIQUEIDENTIFIER= 370; |
| 511 | static final int JAVA_OBJECT = 371; |
| 512 | |
| 513 | static{ |
| 514 | addKeyWord( "BIT", BIT); |
| 515 | addKeyWord( "BOOLEAN", BOOLEAN); |
| 516 | addKeyWord( "BINARY", BINARY); |
| 517 | addKeyWord( "VARBINARY", VARBINARY); |
| 518 | addKeyWord( "RAW", RAW); // alias for Oracle RAW and LONG RAW |
| 519 | addKeyWord( "IMAGE", LONGVARBINARY); // alias for MS SQL Server data type IMAGE |
| 520 | addKeyWord( "LONGVARBINARY",LONGVARBINARY); |
| 521 | addKeyWord( "BLOB", BLOB); |
| 522 | addKeyWord( "BYTE", TINYINT); |
| 523 | addKeyWord( "TINYINT", TINYINT); |
| 524 | addKeyWord( "SMALLINT", SMALLINT); |
| 525 | addKeyWord( "INTEGER", INT); |
| 526 | addKeyWord( "INT", INT); |
| 527 | addKeyWord( "COUNTER", COUNTER); |
| 528 | addKeyWord( "BIGINT", BIGINT); |
| 529 | addKeyWord( "SMALLMONEY", SMALLMONEY); |
| 530 | addKeyWord( "MONEY", MONEY); |
| 531 | addKeyWord( "NUMBER", DECIMAL); |
| 532 | addKeyWord( "VARNUM", DECIMAL); |
| 533 | addKeyWord( "DECIMAL", DECIMAL); |
| 534 | addKeyWord( "NUMERIC", NUMERIC); |
| 535 | addKeyWord( "REAL", REAL); |
| 536 | addKeyWord( "FLOAT", FLOAT); |
| 537 | addKeyWord( "DOUBLE", DOUBLE); |
| 538 | addKeyWord( "DATE", DATE); |
| 539 | addKeyWord( "TIME", TIME); |
| 540 | addKeyWord( "DATETIME", TIMESTAMP); // alias for MS SQL Server data type DATETIME |
| 541 | addKeyWord( "TIMESTAMP", TIMESTAMP); |
| 542 | addKeyWord( "SMALLDATETIME",SMALLDATETIME); |
| 543 | addKeyWord( "CHARACTER", CHAR); // alias for CHAR |
| 544 | addKeyWord( "CHAR", CHAR); |
| 545 | addKeyWord( "NCHAR", NCHAR); |
| 546 | addKeyWord( "VARCHAR2", VARCHAR); // alias for Oracle VARCHAR2 |
| 547 | addKeyWord( "VARCHAR", VARCHAR); |
| 548 | addKeyWord( "NVARCHAR2", NVARCHAR); // alias for Oracle VARCHAR2 |
| 549 | addKeyWord( "NVARCHAR", NVARCHAR); |
| 550 | addKeyWord( "SYSNAME", SYSNAME); |
| 551 | addKeyWord( "TEXT", LONGVARCHAR); |
| 552 | addKeyWord( "LONGVARCHAR", LONGVARCHAR); |
| 553 | addKeyWord( "NTEXT", LONGNVARCHAR); |
| 554 | addKeyWord( "LONGNVARCHAR", LONGNVARCHAR); |
| 555 | addKeyWord( "LONG", LONG); // alias for Oracle LONG and LONG RAW |
| 556 | addKeyWord( "CLOB", CLOB); |
| 557 | addKeyWord( "NCLOB", NCLOB); |
| 558 | addKeyWord( "UNIQUEIDENTIFIER",UNIQUEIDENTIFIER); |
| 559 | addKeyWord( "SQL_VARIANT", JAVA_OBJECT); // alias for MS SQL Server data type SQL_VARIANT |
| 560 | addKeyWord( "JAVA_OBJECT", JAVA_OBJECT); |
| 561 | } |
| 562 | |
| 563 | //escape commands |
| 564 | static final int D = 400; |
| 565 | static final int T = 401; |
| 566 | static final int TS = 402; |
| 567 | static final int FN = 403; |
| 568 | static final int CALL = 404; |
| 569 | static final int OJ = 405; |
| 570 | static{ |
| 571 | addKeyWord( "D", D); |
| 572 | addKeyWord( "T", T); |
| 573 | addKeyWord( "TS", TS); |
| 574 | addKeyWord( "FN", FN); |
| 575 | addKeyWord( "CALL", CALL); |
| 576 | addKeyWord( "OJ", OJ); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | static final int OR = 500; |
| 581 | static final int AND = 501; |
| 582 | static final int IS = 502; |
| 583 | static final int NOT = 503; |
| 584 | static final int NULL = 504; |
| 585 | static final int TRUE = 505; |
| 586 | static final int FALSE = 506; |
| 587 | static final int BETWEEN= 507; |
| 588 | static final int LIKE = 508; |
| 589 | static final int IN = 509; |
| 590 | static{ |
| 591 | addKeyWord( "OR", OR); |
| 592 | addKeyWord( "AND", AND); |
| 593 | addKeyWord( "IS", IS); |
| 594 | addKeyWord( "NOT", NOT); |
| 595 | addKeyWord( "NULL", NULL); |
| 596 | addKeyWord( "YES", TRUE); //alias for TRUE |
| 597 | addKeyWord( "TRUE", TRUE); |
| 598 | addKeyWord( "NO", FALSE); //alias for FALSE |
| 599 | addKeyWord( "FALSE", FALSE); |
| 600 | addKeyWord( "BETWEEN", BETWEEN); |
| 601 | addKeyWord( "LIKE", LIKE); |
| 602 | addKeyWord( "IN", IN); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | //NUMERIC FUNCTIONS |
| 607 | static final int ABS = 1000; //first numeric function --> see SSDatabaseMetaData.getNumericFunctions |
| 608 | static final int ACOS = 1001; |
| 609 | static final int ASIN = 1002; |
| 610 | static final int ATAN = 1003; |
| 611 | static final int ATAN2 = 1004; |
| 612 | static final int CEILING = 1005; |
| 613 | static final int COS = 1006; |
| 614 | static final int COT = 1007; |
| 615 | static final int DEGREES = 1008; |
| 616 | static final int EXP = 1009; |
| 617 | static final int FLOOR = 1010; |
| 618 | static final int LOG = 1011; |
| 619 | static final int LOG10 = 1012; |
| 620 | static final int MOD = 1013; |
| 621 | static final int PI = 1014; |
| 622 | static final int POWER = 1015; |
| 623 | static final int RADIANS = 1016; |
| 624 | static final int RAND = 1017; |
| 625 | static final int ROUND = 1018; |
| 626 | static final int SIGN = 1019; |
| 627 | static final int SIN = 1020; |
| 628 | static final int SQRT = 1021; |
| 629 | static final int TAN = 1022; |
| 630 | static final int TRUNCATE = 1023; //last numeric function --> see SSDatabaseMetaData.getNumericFunctions |
| 631 | static{ |
| 632 | addKeyWord( "ABS", ABS); |
| 633 | addKeyWord( "ACOS", ACOS); |
| 634 | addKeyWord( "ASIN", ASIN); |
| 635 | addKeyWord( "ATAN", ATAN); |
| 636 | addKeyWord( "ATN2", ATAN2); //alias for MS SQL Server |
| 637 | addKeyWord( "ATAN2", ATAN2); |
| 638 | addKeyWord( "CEILING", CEILING); |
| 639 | addKeyWord( "COS", COS); |
| 640 | addKeyWord( "COT", COT); |
| 641 | addKeyWord( "DEGREES", DEGREES); |
| 642 | addKeyWord( "EXP", EXP); |
| 643 | addKeyWord( "FLOOR", FLOOR); |
| 644 | addKeyWord( "LOG", LOG); |
| 645 | addKeyWord( "LOG10", LOG10); |
| 646 | addKeyWord( "MOD", MOD); |
| 647 | addKeyWord( "PI", PI); |
| 648 | addKeyWord( "POWER", POWER); |
| 649 | addKeyWord( "RADIANS", RADIANS); |
| 650 | addKeyWord( "RAND", RAND); |
| 651 | addKeyWord( "ROUND", ROUND); |
| 652 | addKeyWord( "SIGN", SIGN); |
| 653 | addKeyWord( "SIN", SIN); |
| 654 | addKeyWord( "SQRT", SQRT); |
| 655 | addKeyWord( "TAN", TAN); |
| 656 | addKeyWord( "TRUNCATE", TRUNCATE); |
| 657 | } |
| 658 | |
| 659 | //String Functionens |
| 660 | static final int ASCII = 1100; //first string function --> see SSDatabaseMetaData.getStringFunctions |
| 661 | static final int _CHAR = 1101; |
| 662 | static final int CONCAT = 1102; |
| 663 | static final int DIFFERENCE = 1103; |
| 664 | static final int INSERT = 1104; |
| 665 | static final int LCASE = 1105; |
| 666 | static final int LEFT = 1106; |
| 667 | static final int LENGTH = 1107; |
| 668 | static final int LOCATE = 1108; |
| 669 | static final int LTRIM = 1109; |
| 670 | static final int REPEAT = 1110; |
| 671 | static final int REPLACE = 1111; |
| 672 | static final int RIGHT = 1112; |
| 673 | static final int RTRIM = 1113; |
| 674 | static final int SOUNDEX = 1114; |
| 675 | static final int SPACE = 1115; |
| 676 | static final int SUBSTRING = 1116; |
| 677 | static final int TRIM = 1117; |
| 678 | static final int UCASE = 1118; //last string function --> see SSDatabaseMetaData.getStringFunctions |
| 679 | static{ |
| 680 | addKeyWord( "ASCII", ASCII); |
| 681 | keywords.put( new Integer(_CHAR), "CHAR" ); // needed for metadata functions |
| 682 | addKeyWord( "CONCAT", CONCAT); |
| 683 | addKeyWord( "DIFFERENCE",DIFFERENCE); |
| 684 | addKeyWord( "INSERT", INSERT); |
| 685 | addKeyWord( "LCASE", LCASE); |
| 686 | addKeyWord( "LEFT", LEFT); |
| 687 | addKeyWord( "DATALENGTH",LENGTH); //alias for MS SQL Server |
| 688 | addKeyWord( "LEN", LENGTH); //alias for MS SQL Server |
| 689 | addKeyWord( "LENGTH", LENGTH); |
| 690 | addKeyWord( "CHARINDEX",LOCATE); //alias for MS SQL Server |
| 691 | addKeyWord( "LOCATE", LOCATE); |
| 692 | addKeyWord( "LTRIM", LTRIM); |
| 693 | addKeyWord( "REPEAT", REPEAT); |
| 694 | addKeyWord( "REPLACE", REPLACE); |
| 695 | addKeyWord( "RIGHT", RIGHT); |
| 696 | addKeyWord( "RTRIM", RTRIM); |
| 697 | addKeyWord( "SOUNDEX", SOUNDEX); |
| 698 | addKeyWord( "SPACE", SPACE); |
| 699 | addKeyWord( "SUBSTRING",SUBSTRING); |
| 700 | addKeyWord( "TRIM", TRIM); |
| 701 | addKeyWord( "UCASE", UCASE); |
| 702 | } |
| 703 | |
| 704 | //TIME and DATE FUNCTIONS |
| 705 | static final int CURDATE = 1200; //first time date function --> see SSDatabaseMetaData.getTimeDateFunctions |
| 706 | static final int CURTIME = 1201; |
| 707 | static final int DAYNAME = 1202; |
| 708 | static final int DAYOFMONTH = 1203; |
| 709 | static final int DAYOFWEEK = 1204; |
| 710 | static final int DAYOFYEAR = 1205; |
| 711 | static final int DAY = 1206; |
| 712 | static final int HOUR = 1207; |
| 713 | static final int MILLISECOND= 1208; |
| 714 | static final int MINUTE = 1209; |
| 715 | static final int MONTH = 1210; |
| 716 | static final int MONTHNAME = 1211; |
| 717 | static final int NOW = 1212; |
| 718 | static final int QUARTER = 1213; |
| 719 | static final int SECOND = 1214; |
| 720 | static final int TIMESTAMPADD=1215; |
| 721 | static final int TIMESTAMPDIFF=1216; |
| 722 | static final int WEEK = 1217; |
| 723 | static final int YEAR = 1218; //last time date function --> see SSDatabaseMetaData.getTimeDateFunctions |
| 724 | static{ |
| 725 | addKeyWord( "CURDATE", CURDATE); |
| 726 | addKeyWord( "CURTIME", CURTIME); |
| 727 | addKeyWord( "DAYNAME", DAYNAME); |
| 728 | addKeyWord( "DAYOFMONTH", DAYOFMONTH); |
| 729 | addKeyWord( "DAYOFWEEK", DAYOFWEEK); |
| 730 | addKeyWord( "DAYOFYEAR", DAYOFYEAR); |
| 731 | addKeyWord( "DAY", DAY); |
| 732 | addKeyWord( "HOUR", HOUR); |
| 733 | addKeyWord( "MILLISECOND", MILLISECOND); |
| 734 | addKeyWord( "MINUTE", MINUTE); |
| 735 | addKeyWord( "MONTH", MONTH); |
| 736 | addKeyWord( "MONTHNAME", MONTHNAME); |
| 737 | addKeyWord( "GETDATE", NOW); //alias for MS SQL Server |
| 738 | addKeyWord( "NOW", NOW); |
| 739 | addKeyWord( "QUARTER", QUARTER); |
| 740 | addKeyWord( "SECOND", SECOND); |
| 741 | addKeyWord( "DATEADD", TIMESTAMPADD); //alias for MS SQL Server |
| 742 | addKeyWord( "TIMESTAMPADD", TIMESTAMPADD); |
| 743 | addKeyWord( "DATEDIFF", TIMESTAMPDIFF); //alias for MS SQL Server |
| 744 | addKeyWord( "TIMESTAMPDIFF",TIMESTAMPDIFF); |
| 745 | addKeyWord( "WEEK", WEEK); |
| 746 | addKeyWord( "YEAR", YEAR); |
| 747 | } |
| 748 | |
| 749 | // Time intervals |
| 750 | static final int SQL_TSI_FRAC_SECOND= 1250; |
| 751 | static final int SQL_TSI_SECOND = 1251; |
| 752 | static final int SQL_TSI_MINUTE = 1252; |
| 753 | static final int SQL_TSI_HOUR = 1253; |
| 754 | static final int SQL_TSI_DAY = 1254; |
| 755 | static final int SQL_TSI_WEEK = 1255; |
| 756 | static final int SQL_TSI_MONTH = 1256; |
| 757 | static final int SQL_TSI_QUARTER = 1257; |
| 758 | static final int SQL_TSI_YEAR = 1258; |
| 759 | static{ |
| 760 | addKeyWord( "MS", SQL_TSI_FRAC_SECOND); |
| 761 | addKeyWord( "SQL_TSI_FRAC_SECOND", SQL_TSI_FRAC_SECOND); |
| 762 | addKeyWord( "S", SQL_TSI_SECOND); |
| 763 | addKeyWord( "SS", SQL_TSI_SECOND); |
| 764 | addKeyWord( "SQL_TSI_SECOND", SQL_TSI_SECOND); |
| 765 | addKeyWord( "MI", SQL_TSI_MINUTE); |
| 766 | addKeyWord( "N", SQL_TSI_MINUTE); |
| 767 | addKeyWord( "SQL_TSI_MINUTE", SQL_TSI_MINUTE); |
| 768 | addKeyWord( "HH", SQL_TSI_HOUR); |
| 769 | addKeyWord( "SQL_TSI_HOUR", SQL_TSI_HOUR); |
| 770 | //addKeyWord( "D", SQL_TSI_DAY); |
| 771 | addKeyWord( "DD", SQL_TSI_DAY); |
| 772 | addKeyWord( "SQL_TSI_DAY", SQL_TSI_DAY); |
| 773 | addKeyWord( "WK", SQL_TSI_WEEK); |
| 774 | addKeyWord( "WW", SQL_TSI_WEEK); |
| 775 | addKeyWord( "SQL_TSI_WEEK", SQL_TSI_WEEK); |
| 776 | addKeyWord( "M", SQL_TSI_MONTH); |
| 777 | addKeyWord( "MM", SQL_TSI_MONTH); |
| 778 | addKeyWord( "SQL_TSI_MONTH", SQL_TSI_MONTH); |
| 779 | addKeyWord( "Q", SQL_TSI_QUARTER); |
| 780 | addKeyWord( "QQ", SQL_TSI_QUARTER); |
| 781 | addKeyWord( "SQL_TSI_QUARTER", SQL_TSI_QUARTER); |
| 782 | addKeyWord( "YY", SQL_TSI_YEAR); |
| 783 | addKeyWord( "YYYY", SQL_TSI_YEAR); |
| 784 | addKeyWord( "SQL_TSI_YEAR", SQL_TSI_YEAR); |
| 785 | } |
| 786 | |
| 787 | //SYSTEM FUNCTIONS |
| 788 | //static final int DATABASE = 1300; |
| 789 | static final int IFNULL = 1301; //first system function --> see SSDatabaseMetaData.getSystemFunctions |
| 790 | static final int USER = 1302; |
| 791 | static final int CONVERT = 1303; |
| 792 | static final int CAST = 1304; |
| 793 | static final int IIF = 1305; //last system function --> see SSDatabaseMetaData.getSystemFunctions |
| 794 | static{ |
| 795 | addKeyWord( "ISNULL", IFNULL); //alias for IFNULL, used from MS SQL Server with 2 parameter, from MS Access with 1 parameter |
| 796 | addKeyWord( "IFNULL", IFNULL); |
| 797 | addKeyWord( "USER", USER); |
| 798 | addKeyWord( "CONVERT", CONVERT); |
| 799 | addKeyWord( "CAST", CAST); |
| 800 | addKeyWord( "IIF", IIF); |
| 801 | } |
| 802 | |
| 803 | // data types for ecape function CONVERT |
| 804 | static final int SQL_BIGINT = 1350; |
| 805 | static final int SQL_BINARY = 1351; |
| 806 | static final int SQL_BIT = 1352; |
| 807 | static final int SQL_CHAR = 1353; |
| 808 | static final int SQL_DATE = 1354; |
| 809 | static final int SQL_DECIMAL = 1355; |
| 810 | static final int SQL_DOUBLE = 1356; |
| 811 | static final int SQL_FLOAT = 1357; |
| 812 | static final int SQL_INTEGER = 1358; |
| 813 | static final int SQL_LONGVARBINARY = 1359; |
| 814 | static final int SQL_LONGVARCHAR = 1360; |
| 815 | static final int SQL_REAL = 1361; |
| 816 | static final int SQL_SMALLINT = 1362; |
| 817 | static final int SQL_TIME = 1363; |
| 818 | static final int SQL_TIMESTAMP = 1364; |
| 819 | static final int SQL_TINYINT = 1365; |
| 820 | static final int SQL_VARBINARY = 1366; |
| 821 | static final int SQL_VARCHAR = 1367; |
| 822 | static{ |
| 823 | addKeyWord( "SQL_BIGINT", SQL_BIGINT); |
| 824 | addKeyWord( "SQL_BINARY", SQL_BINARY); |
| 825 | addKeyWord( "SQL_BIT", SQL_BIT); |
| 826 | addKeyWord( "SQL_CHAR", SQL_CHAR); |
| 827 | addKeyWord( "SQL_DATE", SQL_DATE); |
| 828 | addKeyWord( "SQL_DECIMAL", SQL_DECIMAL); |
| 829 | addKeyWord( "SQL_DOUBLE", SQL_DOUBLE); |
| 830 | addKeyWord( "SQL_FLOAT", SQL_FLOAT); |
| 831 | addKeyWord( "SQL_INTEGER", SQL_INTEGER); |
| 832 | addKeyWord( "SQL_LONGVARBINARY",SQL_LONGVARBINARY); |
| 833 | addKeyWord( "SQL_LONGVARCHAR", SQL_LONGVARCHAR); |
| 834 | addKeyWord( "SQL_REAL", SQL_REAL); |
| 835 | addKeyWord( "SQL_SMALLINT", SQL_SMALLINT); |
| 836 | addKeyWord( "SQL_TIME", SQL_TIME); |
| 837 | addKeyWord( "SQL_TIMESTAMP", SQL_TIMESTAMP); |
| 838 | addKeyWord( "SQL_TINYINT", SQL_TINYINT); |
| 839 | addKeyWord( "SQL_VARBINARY", SQL_VARBINARY); |
| 840 | addKeyWord( "SQL_VARCHAR", SQL_VARCHAR); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | //Aggregate Function |
| 845 | static final int COUNT = 1400; |
| 846 | static final int MIN = 1401; |
| 847 | static final int MAX = 1402; |
| 848 | static final int SUM = 1403; |
| 849 | static final int FIRST = 1404; |
| 850 | static final int LAST = 1405; |
| 851 | static final int AVG = 1406; |
| 852 | static{ |
| 853 | addKeyWord( "COUNT", COUNT); |
| 854 | addKeyWord( "MIN", MIN); |
| 855 | addKeyWord( "MAX", MAX); |
| 856 | addKeyWord( "SUM", SUM); |
| 857 | addKeyWord( "FIRST", FIRST); |
| 858 | addKeyWord( "LAST", LAST); |
| 859 | addKeyWord( "AVG", AVG); |
| 860 | } |
| 861 | |
| 862 | } |
| 863 | |
| 864 | class SearchNode{ |
| 865 | int value; |
| 866 | char letter; |
| 867 | SearchNode nextLetter; // nächste Buchstabe eines Keywortes |
| 868 | SearchNode nextEntry; // nächte Eintrag eines Buchstabens, der die gleich Startsequenz hat |
| 869 | } |
| 870 | |
| 871 | |