| 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 | | | package org.jcoderz.phoenix.sqlparser; |
| 34 | | | |
| 35 | | | import java.io.FileInputStream; |
| 36 | | | import java.util.ArrayList; |
| 37 | | | import java.util.Iterator; |
| 38 | | | import java.util.List; |
| 39 | | | import java.util.Stack; |
| 40 | | | import java.util.logging.Level; |
| 41 | | | import java.util.logging.Logger; |
| 42 | | | |
| 43 | | | |
| 44 | | | @author |
| 45 | | | |
| 46 | | | public class SqlParser |
| 47 | | | { |
| 48 | 75 | | private static final String CLASSNAME = SqlParser.class.getName(); |
| 49 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 50 | | | |
| 51 | 100 | | private static final SpecialColumnComment TIMESTAMP_SPECIAL_COMMENT |
| 52 | | | = new SpecialColumnComment(); |
| 53 | | | |
| 54 | 100 | | private static final SpecialColumnComment DATE_SPECIAL_COMMENT |
| 55 | | | = new SpecialColumnComment(); |
| 56 | | | |
| 57 | | | static |
| 58 | | | { |
| 59 | | | Token t; |
| 60 | | | try |
| 61 | | | { |
| 62 | 100 | | t = new Token(TokenType.COMMENT, |
| 63 | | | "-- @cmpgen.java-type=org.jcoderz.commons.types.Date"); |
| 64 | 100 | | TIMESTAMP_SPECIAL_COMMENT.parseComment(t); |
| 65 | 100 | | DATE_SPECIAL_COMMENT.parseComment(t); |
| 66 | | | |
| 67 | 100 | | t = new Token(TokenType.COMMENT, |
| 68 | | | "-- @cmpgen.store-method=toSqlTimestamp()"); |
| 69 | 100 | | TIMESTAMP_SPECIAL_COMMENT.parseComment(t); |
| 70 | 100 | | t = new Token(TokenType.COMMENT, |
| 71 | | | "-- @cmpgen.store-method=toSqlDate()"); |
| 72 | 100 | | DATE_SPECIAL_COMMENT.parseComment(t); |
| 73 | | | |
| 74 | 100 | | t = new Token(TokenType.COMMENT, |
| 75 | | | "-- @cmpgen.load-method=fromSqlTimestamp(java.sql.Timestamp)"); |
| 76 | 100 | | TIMESTAMP_SPECIAL_COMMENT.parseComment(t); |
| 77 | 100 | | t = new Token(TokenType.COMMENT, |
| 78 | | | "-- @cmpgen.load-method=fromSqlDate(java.sql.Date)"); |
| 79 | 100 | | DATE_SPECIAL_COMMENT.parseComment(t); |
| 80 | | | } |
| 81 | 0 | | catch (ParseException e) |
| 82 | | | { |
| 83 | 0 | | throw new RuntimeException("This must not happen!", e); |
| 84 | 100 | | } |
| 85 | 100 | | } |
| 86 | | | |
| 87 | | | private final ScannerInterface mScanner; |
| 88 | | | |
| 89 | 100 | | private final Stack mTokenStack = new Stack(); |
| 90 | | | |
| 91 | | | private SpecialColumnComment mSpecialColumnComment; |
| 92 | | | private SpecialAnnotationComment mColumnAnnotation; |
| 93 | | | |
| 94 | 100 | | private final List mSpecialStatementComments = new ArrayList(); |
| 95 | | | private SpecialAnnotationComment mStatementAnnotation; |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | @param |
| 101 | | | |
| 102 | | | public SqlParser (ScannerInterface scanner) |
| 103 | 100 | | { |
| 104 | 100 | | mScanner = scanner; |
| 105 | 100 | | mScanner.setReportWhitespace(false); |
| 106 | 100 | | } |
| 107 | | | |
| 108 | | | |
| 109 | | | |
| 110 | | | |
| 111 | | | @return |
| 112 | | | |
| 113 | | | |
| 114 | | | @throws |
| 115 | | | |
| 116 | | | private Token getNextToken () throws ParseException |
| 117 | | | { |
| 118 | | | Token result; |
| 119 | 100 | | if (mTokenStack.isEmpty()) |
| 120 | | | { |
| 121 | 100 | | result = mScanner.nextToken(); |
| 122 | | | } |
| 123 | | | else |
| 124 | | | { |
| 125 | 100 | | result = (Token) mTokenStack.pop(); |
| 126 | | | } |
| 127 | | | |
| 128 | 100 | | if (logger.isLoggable(Level.FINER)) |
| 129 | | | { |
| 130 | 0 | | final Exception x = new Exception(); |
| 131 | 0 | | int index = 1; |
| 132 | 0 | | final StackTraceElement caller = x.getStackTrace()[index]; |
| 133 | 0 | | StackTraceElement callerParent = null; |
| 134 | | | try |
| 135 | | | { |
| 136 | 0 | | callerParent = x.getStackTrace()[++index]; |
| 137 | | | } |
| 138 | 0 | | catch (ArrayIndexOutOfBoundsException x2) |
| 139 | | | { |
| 140 | | | |
| 141 | 0 | | } |
| 142 | | | |
| 143 | 0 | | logger.finest("TOKEN: " + result + " to " + caller |
| 144 | | | + " called by " + callerParent); |
| 145 | | | } |
| 146 | | | |
| 147 | 100 | | return result; |
| 148 | | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | |
| 153 | | | @param |
| 154 | | | |
| 155 | | | private void pushToken (Token token) |
| 156 | | | { |
| 157 | 100 | | mTokenStack.push(token); |
| 158 | 100 | | } |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | |
| 163 | | | |
| 164 | | | @param |
| 165 | | | @throws |
| 166 | | | |
| 167 | | | private void pushTokenAsIdentifier (Token token) throws ParseException |
| 168 | | | { |
| 169 | 100 | | Token pushToken = null; |
| 170 | 100 | | if (token.getType() == TokenType.IDENTIFIER) |
| 171 | | | { |
| 172 | 100 | | pushToken = token; |
| 173 | | | } |
| 174 | 0 | | else if (token.getType() == TokenType.SEQUENCE) |
| 175 | | | { |
| 176 | 0 | | pushToken = new Token(TokenType.IDENTIFIER, token.getValue()); |
| 177 | | | } |
| 178 | | | else |
| 179 | | | { |
| 180 | 0 | | unexpectedToken(token); |
| 181 | | | } |
| 182 | 100 | | mTokenStack.push(pushToken); |
| 183 | 100 | | } |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | @return |
| 189 | | | @throws |
| 190 | | | |
| 191 | | | public final List parse () |
| 192 | | | throws ParseException |
| 193 | | | { |
| 194 | 100 | | final List tableObjects = new ArrayList(); |
| 195 | 100 | | final List indexObjects = new ArrayList(); |
| 196 | 100 | | final List sequenceObjects = new ArrayList(); |
| 197 | | | |
| 198 | | | Token token; |
| 199 | 100 | (1) | while ((token = getNextToken()).getType() != TokenType.EOF) |
| 200 | | | { |
| 201 | 100 | | final SqlStatement stmt = handleStartOfStatement(token); |
| 202 | 100 | | if (stmt != null) |
| 203 | | | { |
| 204 | 100 | (2) | if (stmt instanceof CreateTableStatement) |
| 205 | | | { |
| 206 | 100 | | tableObjects.add(stmt); |
| 207 | | | } |
| 208 | 100 | | else if (stmt instanceof CreateIndexStatement) |
| 209 | | | { |
| 210 | 100 | | indexObjects.add(stmt); |
| 211 | | | } |
| 212 | 100 | | else if (stmt instanceof CreateSequenceStatement) |
| 213 | | | { |
| 214 | 100 | | sequenceObjects.add(stmt); |
| 215 | | | } |
| 216 | 100 | | logger.info("Parsed statement: " + stmt); |
| 217 | | | } |
| 218 | 100 | | } |
| 219 | | | |
| 220 | 100 | | for (final Iterator it = indexObjects.iterator(); it.hasNext(); ) |
| 221 | | | { |
| 222 | 100 | | final CreateIndexStatement stmt = (CreateIndexStatement) it.next(); |
| 223 | 100 | | final String tname = stmt.getTableName(); |
| 224 | 100 | | boolean found = false; |
| 225 | 100 | | for (final Iterator it2 = tableObjects.iterator(); it2.hasNext(); ) |
| 226 | | | { |
| 227 | 100 | | final CreateTableStatement stmt2 |
| 228 | | | = (CreateTableStatement) it2.next(); |
| 229 | 100 | | if (stmt2.getTableName().equalsIgnoreCase(tname)) |
| 230 | | | { |
| 231 | 100 | | stmt2.addIndex(stmt); |
| 232 | 100 | | found = true; |
| 233 | 100 | | break; |
| 234 | | | } |
| 235 | 100 | | } |
| 236 | | | |
| 237 | 100 | | if (! found) |
| 238 | | | { |
| 239 | 0 | | throw new ParseException( |
| 240 | | | "Index on table " + tname |
| 241 | | | + " declared, but create statement for table " + tname |
| 242 | | | + " not found", -1, -1); |
| 243 | | | } |
| 244 | 100 | | } |
| 245 | | | |
| 246 | 100 | | final List result = new ArrayList(); |
| 247 | 100 | | result.addAll(tableObjects); |
| 248 | 100 | | result.addAll(sequenceObjects); |
| 249 | 100 | | return result; |
| 250 | | | } |
| 251 | | | |
| 252 | | | private SqlStatement handleStartOfStatement (Token token) |
| 253 | | | throws ParseException |
| 254 | | | { |
| 255 | 100 | | SqlStatement result = null; |
| 256 | 100 | | if (token.getType() == TokenType.CREATE) |
| 257 | | | { |
| 258 | 100 | | logger.finer("Start parsing CREATE statement"); |
| 259 | 100 | | result = handleCreateStatement(); |
| 260 | | | } |
| 261 | 100 | | else if (token.getType() == TokenType.ALTER |
| 262 | | | || token.getType() == TokenType.DROP |
| 263 | | | || token.getType() == TokenType.INSERT |
| 264 | | | || token.getType() == TokenType.DELETE |
| 265 | | | || token.getType() == TokenType.SELECT) |
| 266 | | | { |
| 267 | 100 | | logger.info("Skipping " + token.getValue() + " statement"); |
| 268 | 100 | | eatUntilSemicolon(); |
| 269 | | | } |
| 270 | 100 | | else if (token.getType() == TokenType.COMMENT) |
| 271 | | | { |
| 272 | 100 | | pushToken(token); |
| 273 | 100 | | parseStatementComment(); |
| 274 | | | } |
| 275 | | | else |
| 276 | | | { |
| 277 | 0 | | logger.finer("Unable to handle token " + token); |
| 278 | 0 | (3) | throw new ParseException( |
| 279 | | | "Unexpected token", |
| 280 | | | mScanner.getLine(), |
| 281 | | | mScanner.getColumn()); |
| 282 | | | } |
| 283 | 100 | | return result; |
| 284 | | | } |
| 285 | | | |
| 286 | | (4)(5) | private SqlStatement handleCreateStatement () |
| 287 | | | throws ParseException |
| 288 | | | { |
| 289 | 100 | | final String methodName = "handleCreateStatement()"; |
| 290 | 100 | | logger.entering(CLASSNAME, methodName); |
| 291 | | | |
| 292 | | | |
| 293 | 100 | | Token token = getNextToken(); |
| 294 | 100 | | SqlStatement result = null; |
| 295 | | | |
| 296 | 100 | | final TokenType type = token.getType(); |
| 297 | | | |
| 298 | | | |
| 299 | 100 | | if (type == TokenType.TABLE) |
| 300 | | | { |
| 301 | 100 | | token = getNextToken(); |
| 302 | | | |
| 303 | 50 | | if (token.getType().equals(TokenType.IDENTIFIER) |
| 304 | | | || token.getType().equals(TokenType.SEQUENCE)) |
| 305 | | | { |
| 306 | 100 | | result = new CreateTableStatement(token.getValue()); |
| 307 | | | } |
| 308 | | | else |
| 309 | | | { |
| 310 | 0 | | unexpectedToken(token); |
| 311 | | | } |
| 312 | | | |
| 313 | | | |
| 314 | 100 | | assertNextToken(TokenType.OPEN_PAREN); |
| 315 | | | |
| 316 | | | do |
| 317 | | | { |
| 318 | 100 | | parseRelationalPropOrComment((CreateTableStatement) result); |
| 319 | | | |
| 320 | 100 | | token = getNextToken(); |
| 321 | 100 | | if (token.getType() == TokenType.CLOSE_PAREN) |
| 322 | | | { |
| 323 | | | |
| 324 | 100 | | break; |
| 325 | | | } |
| 326 | 100 | | else if (token.getType() == TokenType.COMMENT) |
| 327 | | | { |
| 328 | 100 | | pushToken(token); |
| 329 | 100 | | continue; |
| 330 | | | } |
| 331 | | | else |
| 332 | | | { |
| 333 | 100 | | pushToken(token); |
| 334 | 100 | | continue; |
| 335 | | | } |
| 336 | | | } |
| 337 | | | while (true); |
| 338 | | | |
| 339 | 100 | | eatUntilSemicolon(); |
| 340 | | | |
| 341 | 100 | (6) | if (mSpecialStatementComments.size() > 0) |
| 342 | | | { |
| 343 | 0 | | for (final Iterator it = mSpecialStatementComments.iterator(); |
| 344 | 0 | | it.hasNext(); ) |
| 345 | | | { |
| 346 | 0 | | final SpecialStatementComment comment |
| 347 | | | = (SpecialStatementComment) it.next(); |
| 348 | 0 | | if (comment.getType() == SpecialStatementComment.TYPE_BEAN_NAME) |
| 349 | | | { |
| 350 | 0 | | ((CreateTableStatement) result).setBeanName( |
| 351 | | | comment.getContent()); |
| 352 | | | } |
| 353 | 0 | | else if ( |
| 354 | | | comment.getType() == SpecialStatementComment.TYPE_JAVADOC) |
| 355 | | | { |
| 356 | 0 | | ((CreateTableStatement) result).setAdditionalJavadoc( |
| 357 | | | comment.getContent()); |
| 358 | | | } |
| 359 | 0 | | else if (comment.getType() |
| 360 | | | == SpecialStatementComment.TYPE_OPTIMISTIC_VERSION_COUNT) |
| 361 | | | { |
| 362 | 0 | | ((CreateTableStatement) result). |
| 363 | | | setOptimisticVersionCount(true); |
| 364 | | | } |
| 365 | 0 | | else if (comment.getType() |
| 366 | | | == SpecialStatementComment.TYPE_SKIP_APPSERVER_SUPPORT) |
| 367 | | | { |
| 368 | 0 | | ((CreateTableStatement) result). |
| 369 | | | setSkipAppserverSupport(true); |
| 370 | | | } |
| 371 | | | |
| 372 | 0 | | } |
| 373 | 0 | | mSpecialStatementComments.clear(); |
| 374 | | | } |
| 375 | | | } |
| 376 | 100 | | else if (type == TokenType.UNIQUE |
| 377 | | | || type == TokenType.BITMAP |
| 378 | | | || type == TokenType.INDEX) |
| 379 | | | { |
| 380 | 100 | | boolean isUnique = false; |
| 381 | 100 | | if (type == TokenType.UNIQUE) |
| 382 | | | { |
| 383 | 100 | | assertNextToken(TokenType.INDEX); |
| 384 | 100 | | isUnique = true; |
| 385 | | | } |
| 386 | 0 | | else if (type == TokenType.BITMAP) |
| 387 | | | { |
| 388 | 0 | | assertNextToken(TokenType.INDEX); |
| 389 | | | } |
| 390 | | | |
| 391 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 392 | 100 | | final CreateIndexStatement stmt |
| 393 | | | = new CreateIndexStatement(token.getValue()); |
| 394 | 100 | | stmt.setUnique(isUnique); |
| 395 | 100 | | parseCreateIndexStatement(stmt); |
| 396 | 100 | | result = stmt; |
| 397 | 100 | | eatUntilSemicolon(); |
| 398 | 100 | | } |
| 399 | 100 | | else if (type == TokenType.SEQUENCE) |
| 400 | | | { |
| 401 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 402 | 100 | | final CreateSequenceStatement stmt |
| 403 | | | = new CreateSequenceStatement(token.getValue()); |
| 404 | | | |
| 405 | 100 | | parseCreateSequenceStatement(stmt); |
| 406 | | | |
| 407 | 100 | | result = stmt; |
| 408 | 100 | | eatUntilSemicolon(); |
| 409 | | | |
| 410 | 100 | | logger.info("Parsed sequence: " + stmt); |
| 411 | 100 | | } |
| 412 | | | else |
| 413 | | | { |
| 414 | 0 | | logger.info("Can't parse create statement for " |
| 415 | | | + token.getValue() + ", skipping."); |
| 416 | 0 | | eatUntilSemicolon(); |
| 417 | | | } |
| 418 | | | |
| 419 | 100 | | if (mStatementAnnotation != null) |
| 420 | | | { |
| 421 | 0 | | result.setAnnotation(mStatementAnnotation.getAnnotation()); |
| 422 | 0 | | mStatementAnnotation = null; |
| 423 | | | } |
| 424 | | | |
| 425 | 100 | | logger.exiting(CLASSNAME, methodName, result); |
| 426 | 100 | | return result; |
| 427 | | | } |
| 428 | | | |
| 429 | | (7) | private void parseCreateSequenceStatement (CreateSequenceStatement stmt) |
| 430 | | | throws ParseException |
| 431 | | | { |
| 432 | | | Token token; |
| 433 | 100 | (8) | while ((token = getNextToken()).getType() != TokenType.SEMICOLON) |
| 434 | | | { |
| 435 | 100 | | final TokenType type = token.getType(); |
| 436 | 100 | | if (type == TokenType.INCREMENT) |
| 437 | | | { |
| 438 | 100 | | assertNextToken(TokenType.BY); |
| 439 | 100 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 440 | 100 | | stmt.setIncrementBy(Long.parseLong(token.getValue())); |
| 441 | | | } |
| 442 | 100 | | else if (type == TokenType.START) |
| 443 | | | { |
| 444 | 100 | | assertNextToken(TokenType.WITH); |
| 445 | 100 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 446 | 100 | (9) | stmt.setStartWith(new Long(Long.parseLong(token.getValue()))); |
| 447 | | | } |
| 448 | 100 | | else if (type == TokenType.MAXVALUE) |
| 449 | | | { |
| 450 | 0 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 451 | 0 | (10) | stmt.setMaxValue(new Long(Long.parseLong(token.getValue()))); |
| 452 | | | } |
| 453 | 100 | | else if (type == TokenType.NOMAXVALUE) |
| 454 | | | { |
| 455 | 0 | | stmt.setNoMaxValue(true); |
| 456 | | | } |
| 457 | 100 | | else if (type == TokenType.MINVALUE) |
| 458 | | | { |
| 459 | 0 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 460 | 0 | (11) | stmt.setMinValue(new Long(Long.parseLong(token.getValue()))); |
| 461 | | | } |
| 462 | 100 | | else if (type == TokenType.NOMINVALUE) |
| 463 | | | { |
| 464 | 0 | | stmt.setNoMinValue(true); |
| 465 | | | } |
| 466 | 100 | | else if (type == TokenType.CYCLE) |
| 467 | | | { |
| 468 | 0 | | stmt.setCycle(true); |
| 469 | | | } |
| 470 | 100 | | else if (type == TokenType.NOCYCLE) |
| 471 | | | { |
| 472 | 100 | | stmt.setCycle(false); |
| 473 | | | } |
| 474 | 100 | | else if (type == TokenType.CACHE) |
| 475 | | | { |
| 476 | 0 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 477 | 0 | | stmt.setCache(Long.parseLong(token.getValue())); |
| 478 | | | } |
| 479 | 100 | | else if (type == TokenType.NOCACHE) |
| 480 | | | { |
| 481 | 100 | | stmt.setCache(0); |
| 482 | | | } |
| 483 | 100 | | else if (type == TokenType.ORDER) |
| 484 | | | { |
| 485 | 100 | | stmt.setOrder(true); |
| 486 | | | } |
| 487 | 0 | | else if (type == TokenType.NOORDER) |
| 488 | | | { |
| 489 | 0 | | stmt.setOrder(false); |
| 490 | | | } |
| 491 | | | else |
| 492 | | | { |
| 493 | 0 | | unexpectedToken(token); |
| 494 | | | } |
| 495 | 100 | | } |
| 496 | | | |
| 497 | | | |
| 498 | 100 | | pushToken(token); |
| 499 | 100 | | } |
| 500 | | | |
| 501 | | | private void parseCreateIndexStatement (CreateIndexStatement result) |
| 502 | | | throws ParseException |
| 503 | | | { |
| 504 | | | |
| 505 | 100 | | assertNextToken(TokenType.ON); |
| 506 | 100 | | Token token = assertNextToken(TokenType.IDENTIFIER); |
| 507 | 100 | | result.setTableName(token.getValue()); |
| 508 | | | |
| 509 | 100 | | assertNextToken(TokenType.OPEN_PAREN); |
| 510 | | | |
| 511 | | | while (true) |
| 512 | | | { |
| 513 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 514 | 100 | | result.addColumn(token.getValue()); |
| 515 | | | |
| 516 | 100 | | token = getNextToken(); |
| 517 | 100 | | if (token.getType() == TokenType.CLOSE_PAREN) |
| 518 | | | { |
| 519 | 100 | | break; |
| 520 | | | } |
| 521 | 0 | | else if (! (token.getType() == TokenType.COMMA)) |
| 522 | | | { |
| 523 | 0 | | unexpectedToken(token); |
| 524 | | | } |
| 525 | | | } |
| 526 | 100 | | } |
| 527 | | | |
| 528 | | | |
| 529 | | | |
| 530 | | | |
| 531 | | | private void parseRelationalPropOrComment (CreateTableStatement createStmt) |
| 532 | | | throws ParseException |
| 533 | | | { |
| 534 | 100 | | final Token token = getNextToken(); |
| 535 | 100 | | if (isStartOfOutOfLineConstraint(token)) |
| 536 | | | { |
| 537 | | | |
| 538 | 100 | | pushToken(token); |
| 539 | 100 | | parseOutOfLineConstraint(createStmt); |
| 540 | | | } |
| 541 | | | |
| 542 | 100 | | else if (token.getType() == TokenType.IDENTIFIER |
| 543 | | | || token.getType() == TokenType.SEQUENCE) |
| 544 | | | { |
| 545 | | | |
| 546 | 100 | | pushTokenAsIdentifier(token); |
| 547 | 100 | | parseColumnSpec(createStmt); |
| 548 | | | } |
| 549 | 100 | | else if (token.getType() == TokenType.COMMENT) |
| 550 | | | { |
| 551 | | | |
| 552 | 100 | | pushToken(token); |
| 553 | 100 | | parseColumnComment(); |
| 554 | | | } |
| 555 | | | else |
| 556 | | | { |
| 557 | 0 | | unexpectedToken(token); |
| 558 | | | } |
| 559 | 100 | | } |
| 560 | | | |
| 561 | | (12)(13) | private void parseColumnSpec (CreateTableStatement createStmt) |
| 562 | | | throws ParseException |
| 563 | | | { |
| 564 | 100 | | final ColumnSpec colSpec = new ColumnSpec(); |
| 565 | | | |
| 566 | | | |
| 567 | 100 | | Token token = assertNextToken(TokenType.IDENTIFIER); |
| 568 | 100 | | colSpec.setColumnName(token.getValue()); |
| 569 | | | |
| 570 | | | |
| 571 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 572 | 100 | | colSpec.setColumnType(token.getValue()); |
| 573 | | | |
| 574 | 100 | (14) | if (token.getValue().equalsIgnoreCase("TIMESTAMP") |
| 575 | | | && mSpecialColumnComment == null) |
| 576 | | | { |
| 577 | 100 | | mSpecialColumnComment = TIMESTAMP_SPECIAL_COMMENT; |
| 578 | | | } |
| 579 | 100 | (15) | else if (token.getValue().equalsIgnoreCase("DATE") |
| 580 | | | && mSpecialColumnComment == null) |
| 581 | | | { |
| 582 | 100 | | mSpecialColumnComment = DATE_SPECIAL_COMMENT; |
| 583 | | | } |
| 584 | | | |
| 585 | 100 | | token = getNextToken(); |
| 586 | 100 | | if (token.getType() == TokenType.OPEN_PAREN) |
| 587 | | | { |
| 588 | 100 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 589 | 100 | | colSpec.addDatatypeAttribute(new NumericAttribute(token.getValue())); |
| 590 | 100 | | token = getNextToken(); |
| 591 | 100 | | if (token.getType() == TokenType.IDENTIFIER) |
| 592 | | | { |
| 593 | 0 | | colSpec.addDatatypeAttribute( |
| 594 | | | new StringAttribute(token.getValue())); |
| 595 | 0 | | assertNextToken(TokenType.CLOSE_PAREN); |
| 596 | | | } |
| 597 | 100 | | else if (token.getType() == TokenType.COMMA) |
| 598 | | | { |
| 599 | 100 | | token = assertNextToken(TokenType.NUMERIC_LITERAL); |
| 600 | 100 | | colSpec.addDatatypeAttribute( |
| 601 | | | new NumericAttribute(token.getValue())); |
| 602 | 100 | | assertNextToken(TokenType.CLOSE_PAREN); |
| 603 | | | } |
| 604 | 100 | | else if (token.getType() != TokenType.CLOSE_PAREN) |
| 605 | | | { |
| 606 | 0 | | throw new ParseException("Unexpected token: " + token, |
| 607 | | | mScanner.getLine(), mScanner.getColumn()); |
| 608 | | | } |
| 609 | | | } |
| 610 | | | else |
| 611 | | | { |
| 612 | 100 | | pushToken(token); |
| 613 | | | } |
| 614 | | | |
| 615 | 100 | | parseColumnAttributes(colSpec, createStmt); |
| 616 | | | |
| 617 | 100 | | token = getNextToken(); |
| 618 | 100 | | if (token.getType() != TokenType.COMMA) |
| 619 | | | { |
| 620 | 100 | | pushToken(token); |
| 621 | | | } |
| 622 | | | |
| 623 | | | |
| 624 | 100 | | if (mSpecialColumnComment != null) |
| 625 | | | { |
| 626 | 100 | | mSpecialColumnComment.validate(); |
| 627 | 100 | | colSpec.setJavaType(mSpecialColumnComment.getJavaType()); |
| 628 | 100 | | colSpec.setLoadMethod(mSpecialColumnComment.getLoadMethod()); |
| 629 | 100 | | colSpec.setStoreMethod(mSpecialColumnComment.getStoreMethod()); |
| 630 | 100 | | colSpec.setCurrencyColumn(mSpecialColumnComment.getCurrencyColumn()); |
| 631 | | | |
| 632 | 60 | | colSpec.setIsPeriodDefined( |
| 633 | | | mSpecialColumnComment.getPeriodFieldName() != null |
| 634 | | | && mSpecialColumnComment.getPeriodEndDateColumn() != null); |
| 635 | 100 | | colSpec.setPeriodFieldName( |
| 636 | | | mSpecialColumnComment.getPeriodFieldName()); |
| 637 | 100 | | colSpec.setPeriodEndDateColumn( |
| 638 | | | mSpecialColumnComment.getPeriodEndDateColumn()); |
| 639 | 100 | | colSpec.setWeblogicColumnType( |
| 640 | | | mSpecialColumnComment.getWeblogicColumnType()); |
| 641 | | | |
| 642 | 100 | | colSpec.setSkipInInterface(mSpecialColumnComment.isSkipInInterface()); |
| 643 | 100 | | mSpecialColumnComment = null; |
| 644 | | | } |
| 645 | 100 | | if (mColumnAnnotation != null) |
| 646 | | | { |
| 647 | 0 | | colSpec.setAnnotation(mColumnAnnotation.getAnnotation()); |
| 648 | 0 | | mColumnAnnotation = null; |
| 649 | | | } |
| 650 | | | |
| 651 | 100 | | createStmt.addColumn(colSpec); |
| 652 | 100 | | } |
| 653 | | | |
| 654 | | | private void parseStatementComment () |
| 655 | | | throws ParseException |
| 656 | | | { |
| 657 | 100 | | final Token token = assertNextToken(TokenType.COMMENT); |
| 658 | 100 | | final String val = token.getValue(); |
| 659 | | | |
| 660 | 100 | | if (val.indexOf("@cmpgen") != -1) |
| 661 | | | { |
| 662 | 0 | | final SpecialStatementComment comment |
| 663 | | | = SpecialStatementComment.parseComment(token); |
| 664 | 0 | | if (comment != null) |
| 665 | | | { |
| 666 | 0 | | mSpecialStatementComments.add(comment); |
| 667 | | | } |
| 668 | 0 | | } |
| 669 | 100 | | else if (val.indexOf("@@annotation") != -1) |
| 670 | | | { |
| 671 | 0 | | if (mStatementAnnotation == null) |
| 672 | | | { |
| 673 | 0 | | mStatementAnnotation = new SpecialAnnotationComment(); |
| 674 | | | } |
| 675 | 0 | | mStatementAnnotation.parseComment(token); |
| 676 | | | } |
| 677 | 100 | | } |
| 678 | | | |
| 679 | | | |
| 680 | | | private void parseColumnComment () throws ParseException |
| 681 | | | { |
| 682 | 100 | | final Token token = assertNextToken(TokenType.COMMENT); |
| 683 | 100 | | final String str = token.getValue(); |
| 684 | 100 | | if (str.indexOf("@cmpgen") != -1) |
| 685 | | | { |
| 686 | 100 | | if (mSpecialColumnComment == null) |
| 687 | | | { |
| 688 | 100 | | mSpecialColumnComment = new SpecialColumnComment(); |
| 689 | | | } |
| 690 | 100 | | mSpecialColumnComment.parseComment(token); |
| 691 | | | } |
| 692 | 100 | | else if (str.indexOf("@@annotation") != -1) |
| 693 | | | { |
| 694 | 0 | | if (mColumnAnnotation != null) |
| 695 | | | { |
| 696 | 0 | | throw new ParseException("Only one annotation per column allowed", |
| 697 | | | mScanner.getLine(), mScanner.getColumn()); |
| 698 | | | } |
| 699 | 0 | | mColumnAnnotation = new SpecialAnnotationComment(); |
| 700 | 0 | | mColumnAnnotation.parseComment(token); |
| 701 | | | } |
| 702 | | | else |
| 703 | | | { |
| 704 | 100 | | logger.finer("Ignoring non-special comment " + token); |
| 705 | | | } |
| 706 | 100 | | } |
| 707 | | | |
| 708 | | | |
| 709 | | | |
| 710 | | | |
| 711 | | | |
| 712 | | | |
| 713 | | | <ul> |
| 714 | | | <li></li> |
| 715 | | | <li></li> |
| 716 | | | </ul> |
| 717 | | | |
| 718 | | (16) | private void parseColumnAttributes ( |
| 719 | | | ColumnSpec colSpec, CreateTableStatement stmt) |
| 720 | | | throws ParseException |
| 721 | | | { |
| 722 | | | Token token; |
| 723 | | | |
| 724 | 100 | | token = getNextToken(); |
| 725 | 100 | | TokenType type = token.getType(); |
| 726 | 100 | | if (type == TokenType.COMMA |
| 727 | | | || type == TokenType.CLOSE_PAREN) |
| 728 | | | { |
| 729 | 100 | | pushToken(token); |
| 730 | | | } |
| 731 | 100 | | else if (type == TokenType.DEFAULT) |
| 732 | | | { |
| 733 | | | |
| 734 | | | |
| 735 | | | |
| 736 | 100 | | final StringBuffer defltExpr = new StringBuffer(); |
| 737 | 100 | | mScanner.setReportWhitespace(true); |
| 738 | | | while (true) |
| 739 | | | { |
| 740 | 100 | | token = getNextToken(); |
| 741 | 100 | | type = token.getType(); |
| 742 | 100 | | if (type == TokenType.OPEN_PAREN) |
| 743 | | | { |
| 744 | 100 | | defltExpr.append(token.getValue()); |
| 745 | 100 | | defltExpr.append(eatUntilClosingParen()); |
| 746 | 100 | | defltExpr.append( |
| 747 | | | assertNextToken(TokenType.CLOSE_PAREN).getValue()); |
| 748 | | | } |
| 749 | 100 | | else if (type == TokenType.COMMA |
| 750 | | | || type == TokenType.CLOSE_PAREN |
| 751 | | | || isStartOfInlineConstraint(token)) |
| 752 | | | { |
| 753 | 100 | | pushToken(token); |
| 754 | 100 | | break; |
| 755 | | | } |
| 756 | | | else |
| 757 | | | { |
| 758 | 100 | | defltExpr.append(token.getValue()); |
| 759 | | | } |
| 760 | | | } |
| 761 | 100 | | mScanner.setReportWhitespace(false); |
| 762 | 100 | | final DefaultClause dfltClause |
| 763 | | | = new DefaultClause(defltExpr.toString()); |
| 764 | 100 | | colSpec.addAttribute(dfltClause); |
| 765 | 100 | | } |
| 766 | 100 | | else if (isStartOfInlineConstraint(token)) |
| 767 | | | { |
| 768 | | | |
| 769 | | | |
| 770 | 100 | | pushToken(token); |
| 771 | 100 | | parseInlineConstraint(colSpec, stmt); |
| 772 | | | } |
| 773 | | | else |
| 774 | | | { |
| 775 | 0 | | unexpectedToken(token); |
| 776 | | | } |
| 777 | | | |
| 778 | | | while (true) |
| 779 | | | { |
| 780 | 100 | | token = getNextToken(); |
| 781 | 100 | | logger.finer("Checking for more inline constraints: got " + token); |
| 782 | 100 | | pushToken(token); |
| 783 | 100 | | if (isStartOfInlineConstraint(token)) |
| 784 | | | { |
| 785 | 100 | | parseInlineConstraint(colSpec, stmt); |
| 786 | | | } |
| 787 | 100 | | else if (token.getType() == TokenType.COMMA |
| 788 | | | || token.getType() == TokenType.CLOSE_PAREN) |
| 789 | | | { |
| 790 | 100 | | break; |
| 791 | | | } |
| 792 | | | else |
| 793 | | | { |
| 794 | 0 | | unexpectedToken(token); |
| 795 | | | } |
| 796 | | | } |
| 797 | 100 | | } |
| 798 | | | |
| 799 | | (17) | private void parseInlineConstraint ( |
| 800 | | | ColumnSpec column, CreateTableStatement stmt) |
| 801 | | | throws ParseException |
| 802 | | | { |
| 803 | | | Token token; |
| 804 | | | TokenType type; |
| 805 | | | |
| 806 | 100 | | String constraintName = "unnamed"; |
| 807 | 100 | | token = getNextToken(); |
| 808 | 100 | | type = token.getType(); |
| 809 | | | |
| 810 | 100 | | if (type == TokenType.CONSTRAINT) |
| 811 | | | { |
| 812 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 813 | 100 | | constraintName = token.getValue(); |
| 814 | | | } |
| 815 | | | else |
| 816 | | | { |
| 817 | 100 | | pushToken(token); |
| 818 | | | } |
| 819 | | | |
| 820 | 100 | | token = getNextToken(); |
| 821 | 100 | | type = token.getType(); |
| 822 | 100 | | if (type == TokenType.NOT) |
| 823 | | | { |
| 824 | 100 | | assertNextToken(TokenType.NULL); |
| 825 | 100 | | column.setNotNull(true); |
| 826 | 100 | (18) | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 827 | | | + column.getColumnName() + " is not nullable"); |
| 828 | | | } |
| 829 | 100 | | else if (type == TokenType.NULL) |
| 830 | | | { |
| 831 | 0 | | column.setNotNull(false); |
| 832 | 0 | | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 833 | | | + column.getColumnName() + " is nullable"); |
| 834 | | | } |
| 835 | 100 | | else if (type == TokenType.UNIQUE) |
| 836 | | | { |
| 837 | 100 | | column.setUnique(true); |
| 838 | 100 | | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 839 | | | + column.getColumnName() + " is unique"); |
| 840 | | | } |
| 841 | 100 | | else if (type == TokenType.PRIMARY) |
| 842 | | | { |
| 843 | 0 | | assertNextToken(TokenType.KEY); |
| 844 | 0 | | column.setPrimaryKey(true); |
| 845 | 0 | | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 846 | | | + column.getColumnName() + " is primary key"); |
| 847 | | | } |
| 848 | 100 | | else if (type == TokenType.CHECK) |
| 849 | | | { |
| 850 | 100 | | final StringBuffer checkCondition = new StringBuffer(); |
| 851 | 100 | | assertNextToken(TokenType.OPEN_PAREN); |
| 852 | 100 | | mScanner.setReportWhitespace(true); |
| 853 | 100 | | checkCondition.append(eatUntilClosingParen()); |
| 854 | 100 | | mScanner.setReportWhitespace(false); |
| 855 | 100 | | assertNextToken(TokenType.CLOSE_PAREN); |
| 856 | 100 | | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 857 | | | + column.getColumnName() + " check condition: " |
| 858 | | | + checkCondition); |
| 859 | 100 | | } |
| 860 | 0 | | else if (type == TokenType.REFERENCES) |
| 861 | | | { |
| 862 | 0 | | final String refTable |
| 863 | | | = assertNextToken(TokenType.IDENTIFIER).getValue(); |
| 864 | 0 | | String refColumn = null; |
| 865 | | | |
| 866 | 0 | | token = getNextToken(); |
| 867 | 0 | | if (token.getType() == TokenType.OPEN_PAREN) |
| 868 | | | { |
| 869 | 0 | | refColumn = assertNextToken(TokenType.IDENTIFIER).getValue(); |
| 870 | 0 | | assertNextToken(TokenType.CLOSE_PAREN); |
| 871 | | | } |
| 872 | | | else |
| 873 | | | { |
| 874 | 0 | | refColumn = column.getColumnName(); |
| 875 | 0 | | pushToken(token); |
| 876 | | | } |
| 877 | | | |
| 878 | 0 | | String deleteSemantic = "default"; |
| 879 | | | |
| 880 | 0 | | token = getNextToken(); |
| 881 | 0 | | type = token.getType(); |
| 882 | 0 | | if (type.equals(TokenType.ON)) |
| 883 | | | { |
| 884 | 0 | | assertNextToken(TokenType.DELETE); |
| 885 | 0 | | token = getNextToken(); |
| 886 | 0 | | type = token.getType(); |
| 887 | 0 | | if (type == TokenType.SET) |
| 888 | | | { |
| 889 | 0 | | assertNextToken(TokenType.NULL); |
| 890 | 0 | | deleteSemantic = "set null"; |
| 891 | | | } |
| 892 | | | else |
| 893 | | | { |
| 894 | 0 | | assertToken(token, TokenType.CASCADE); |
| 895 | 0 | | deleteSemantic = "cascade"; |
| 896 | | | } |
| 897 | | | } |
| 898 | | | else |
| 899 | | | { |
| 900 | | | |
| 901 | 0 | | pushToken(token); |
| 902 | | | } |
| 903 | | | |
| 904 | 0 | | final FkConstraint constraint = new FkConstraint( |
| 905 | | | constraintName, column.getColumnName(), refTable, refColumn); |
| 906 | 0 | | stmt.addFkConstraint(constraint); |
| 907 | 0 | | logger.finer("Parsed inline constraint " + constraintName + ": " |
| 908 | | | + column.getColumnName() + " references " |
| 909 | | | + refTable |
| 910 | | | + (refColumn != null ? "." + refColumn : "") |
| 911 | | | + " with " + deleteSemantic + " delete semantic"); |
| 912 | | | } |
| 913 | 100 | | } |
| 914 | | | |
| 915 | | | private boolean isStartOfInlineConstraint (Token token) |
| 916 | | | { |
| 917 | 100 | | final TokenType type = token.getType(); |
| 918 | 100 | | return type == TokenType.NOT |
| 919 | | | || type == TokenType.NULL |
| 920 | | | || type == TokenType.UNIQUE |
| 921 | | | || type == TokenType.PRIMARY |
| 922 | | | || type == TokenType.CHECK |
| 923 | | | || type == TokenType.REFERENCES |
| 924 | | | || type == TokenType.CONSTRAINT; |
| 925 | | | } |
| 926 | | | |
| 927 | | | private boolean isStartOfOutOfLineConstraint (Token token) |
| 928 | | | { |
| 929 | 100 | | final TokenType type = token.getType(); |
| 930 | 100 | | return type == TokenType.CONSTRAINT |
| 931 | | | || type == TokenType.UNIQUE |
| 932 | | | || type == TokenType.PRIMARY |
| 933 | | | || type == TokenType.FOREIGN |
| 934 | | | || type == TokenType.CHECK; |
| 935 | | | } |
| 936 | | | |
| 937 | | | private StringBuffer eatUntilClosingParen () |
| 938 | | | throws ParseException |
| 939 | | | { |
| 940 | 100 | | final StringBuffer sbuf = new StringBuffer(); |
| 941 | | | |
| 942 | | | Token token; |
| 943 | 100 | (19) | while ((token = getNextToken()).getType() != TokenType.CLOSE_PAREN) |
| 944 | | | { |
| 945 | 100 | | sbuf.append(token.getValue()); |
| 946 | 100 | | if (token.getType() == TokenType.OPEN_PAREN) |
| 947 | | | { |
| 948 | 100 | | final StringBuffer nested = eatUntilClosingParen(); |
| 949 | 100 | | sbuf.append(nested); |
| 950 | 100 | | token = assertNextToken(TokenType.CLOSE_PAREN); |
| 951 | 100 | | sbuf.append(token.getValue()); |
| 952 | | | } |
| 953 | 100 | | logger.finest("Skipping token " + token); |
| 954 | | | } |
| 955 | 100 | | pushToken(token); |
| 956 | | | |
| 957 | 100 | | return sbuf; |
| 958 | | | } |
| 959 | | | |
| 960 | | | private Token eatUntilSemicolon () |
| 961 | | | throws ParseException |
| 962 | | | { |
| 963 | | | Token token; |
| 964 | | | while (true) |
| 965 | | | { |
| 966 | 100 | | token = getNextToken(); |
| 967 | 100 | | if (token.getType() == TokenType.SEMICOLON) |
| 968 | | | { |
| 969 | 100 | | break; |
| 970 | | | } |
| 971 | 100 | | logger.fine("Skipping token " + token); |
| 972 | | | } |
| 973 | 100 | | return token; |
| 974 | | | } |
| 975 | | | |
| 976 | | | private void parseOutOfLineConstraint (CreateTableStatement statement) |
| 977 | | | throws ParseException |
| 978 | | | { |
| 979 | 100 | | String constraintName = "unnamed"; |
| 980 | | | |
| 981 | | | Token token; |
| 982 | | | TokenType type; |
| 983 | | | |
| 984 | | | |
| 985 | 100 | | token = getNextToken(); |
| 986 | 100 | | type = token.getType(); |
| 987 | 100 | | if (type == TokenType.CONSTRAINT) |
| 988 | | | { |
| 989 | 100 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 990 | 100 | | constraintName = token.getValue(); |
| 991 | | | } |
| 992 | | | else |
| 993 | | | { |
| 994 | 0 | | pushToken(token); |
| 995 | | | } |
| 996 | | | |
| 997 | | | |
| 998 | 100 | | token = getNextToken(); |
| 999 | 100 | | type = token.getType(); |
| 1000 | | | |
| 1001 | 100 | | if (type == TokenType.UNIQUE) |
| 1002 | | | { |
| 1003 | | | |
| 1004 | 0 | | assertNextToken(TokenType.OPEN_PAREN); |
| 1005 | 0 | | final List uniqueCols = new ArrayList(); |
| 1006 | 0 | (20) | while ((token = getNextToken()).getType() != TokenType.CLOSE_PAREN) |
| 1007 | | | { |
| 1008 | 0 | | if (token.getType() != TokenType.COMMA) |
| 1009 | | | { |
| 1010 | 0 | | uniqueCols.add(token.getValue()); |
| 1011 | | | } |
| 1012 | | | } |
| 1013 | 0 | (21) | logger.finer("Parsed OOL constraint " |
| 1014 | | | + constraintName + ": UNIQUE " + uniqueCols); |
| 1015 | 0 | | addUniqueColumns(statement, uniqueCols); |
| 1016 | 0 | | } |
| 1017 | 100 | | else if (type == TokenType.PRIMARY) |
| 1018 | | | { |
| 1019 | | | |
| 1020 | 100 | | assertNextToken(TokenType.KEY); |
| 1021 | 100 | | assertNextToken(TokenType.OPEN_PAREN); |
| 1022 | 100 | | final List pkCols = new ArrayList(); |
| 1023 | 100 | (22) | while ((token = getNextToken()).getType() != TokenType.CLOSE_PAREN) |
| 1024 | | | { |
| 1025 | 100 | | if (token.getType() != TokenType.COMMA) |
| 1026 | | | { |
| 1027 | 100 | | pkCols.add(token.getValue()); |
| 1028 | | | } |
| 1029 | | | } |
| 1030 | 100 | | logger.finer("Parsed OOL constraint " |
| 1031 | | | + constraintName + ": PRIMARY KEY " + pkCols); |
| 1032 | 100 | | addPkColumns(statement, pkCols); |
| 1033 | 100 | | } |
| 1034 | 100 | | else if (type == TokenType.FOREIGN) |
| 1035 | | | { |
| 1036 | 0 | | parseOolForeignKey(constraintName, statement); |
| 1037 | | | } |
| 1038 | 100 | | else if (type == TokenType.CHECK) |
| 1039 | | | { |
| 1040 | 100 | | token = assertNextToken(TokenType.OPEN_PAREN); |
| 1041 | 100 | | final StringBuffer checkCondition = new StringBuffer(); |
| 1042 | 100 | | checkCondition.append(token.getValue()); |
| 1043 | 100 | | mScanner.setReportWhitespace(true); |
| 1044 | 100 | | checkCondition.append(eatUntilClosingParen()); |
| 1045 | 100 | | mScanner.setReportWhitespace(false); |
| 1046 | 100 | | token = assertNextToken(TokenType.CLOSE_PAREN); |
| 1047 | 100 | | checkCondition.append(token.getValue()); |
| 1048 | 100 | | logger.finer("Parsed OOL constraint " |
| 1049 | | | + constraintName + ": CHECK " + checkCondition); |
| 1050 | | | } |
| 1051 | | | |
| 1052 | 100 | | parseConstraintState(); |
| 1053 | | | |
| 1054 | 100 | | token = getNextToken(); |
| 1055 | 100 | | if (token.getType() != TokenType.COMMA) |
| 1056 | | | { |
| 1057 | 100 | | pushToken(token); |
| 1058 | | | } |
| 1059 | 100 | | } |
| 1060 | | | |
| 1061 | | | |
| 1062 | | | |
| 1063 | | | |
| 1064 | | | |
| 1065 | | | |
| 1066 | | | |
| 1067 | | | @throws |
| 1068 | | | |
| 1069 | | | private void parseConstraintState () |
| 1070 | | | throws ParseException |
| 1071 | | | { |
| 1072 | 100 | | final Token token = getNextToken(); |
| 1073 | 100 | | final TokenType type = token.getType(); |
| 1074 | 100 | | if (type == TokenType.ENABLE |
| 1075 | | | || type == TokenType.DISABLE) |
| 1076 | | | { |
| 1077 | 100 | | logger.finer("Parsed constraint state: " + token); |
| 1078 | | | } |
| 1079 | 66 | | else if (type == TokenType.COMMA |
| 1080 | | | || type == TokenType.CLOSE_PAREN |
| 1081 | | | || type == TokenType.COMMENT) |
| 1082 | | | { |
| 1083 | | | |
| 1084 | 100 | | pushToken(token); |
| 1085 | | | } |
| 1086 | | | else |
| 1087 | | | { |
| 1088 | 0 | | unexpectedToken(token); |
| 1089 | | | } |
| 1090 | 100 | | } |
| 1091 | | | |
| 1092 | | (23)(24)(25) | private void parseOolForeignKey (String constraintName, |
| 1093 | | | CreateTableStatement stmt) |
| 1094 | | | throws ParseException |
| 1095 | | | { |
| 1096 | | | Token token; |
| 1097 | | | TokenType type; |
| 1098 | | | |
| 1099 | | | |
| 1100 | | | |
| 1101 | | | |
| 1102 | | | |
| 1103 | 0 | | assertNextToken(TokenType.KEY); |
| 1104 | 0 | | assertNextToken(TokenType.OPEN_PAREN); |
| 1105 | 0 | | final List fkCols = new ArrayList(); |
| 1106 | 0 | (26) | while ((token = getNextToken()).getType() != TokenType.CLOSE_PAREN) |
| 1107 | | | { |
| 1108 | 0 | | if (token.getType() != TokenType.COMMA) |
| 1109 | | | { |
| 1110 | 0 | | fkCols.add(token.getValue()); |
| 1111 | | | } |
| 1112 | | | } |
| 1113 | 0 | | assertNextToken(TokenType.REFERENCES); |
| 1114 | 0 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 1115 | 0 | (27) | final String refTable = token.getValue(); |
| 1116 | 0 | | final List refColumns = new ArrayList(); |
| 1117 | 0 | | token = getNextToken(); |
| 1118 | 0 | | if (token.getType() == TokenType.OPEN_PAREN) |
| 1119 | | | { |
| 1120 | | | while (true) |
| 1121 | | | { |
| 1122 | 0 | | token = assertNextToken(TokenType.IDENTIFIER); |
| 1123 | 0 | | refColumns.add(token.getValue()); |
| 1124 | | | |
| 1125 | 0 | | token = getNextToken(); |
| 1126 | | | |
| 1127 | 0 | | if (token.getType() == TokenType.CLOSE_PAREN) |
| 1128 | | | { |
| 1129 | 0 | | break; |
| 1130 | | | } |
| 1131 | 0 | | else if (token.getType() == TokenType.COMMA) |
| 1132 | | | { |
| 1133 | | | |
| 1134 | 0 | | continue; |
| 1135 | | | } |
| 1136 | | | else |
| 1137 | | | { |
| 1138 | 0 | | unexpectedToken(token); |
| 1139 | | | } |
| 1140 | | | } |
| 1141 | | | } |
| 1142 | | | else |
| 1143 | | | { |
| 1144 | 0 | | refColumns.addAll(fkCols); |
| 1145 | 0 | | pushToken(token); |
| 1146 | | | } |
| 1147 | | | |
| 1148 | 0 | | token = getNextToken(); |
| 1149 | 0 | | type = token.getType(); |
| 1150 | 0 | | String deleteSemantic = "default"; |
| 1151 | 0 | | if (type == TokenType.ON) |
| 1152 | | | { |
| 1153 | 0 | | assertNextToken(TokenType.DELETE); |
| 1154 | 0 | | token = getNextToken(); |
| 1155 | 0 | | if (token.getType() == TokenType.SET) |
| 1156 | | | { |
| 1157 | 0 | | assertNextToken(TokenType.NULL); |
| 1158 | 0 | | deleteSemantic = "set null"; |
| 1159 | | | } |
| 1160 | 0 | | else if (token.getType() == TokenType.CASCADE) |
| 1161 | | | { |
| 1162 | 0 | | deleteSemantic = "cascade"; |
| 1163 | | | } |
| 1164 | | | else |
| 1165 | | | { |
| 1166 | 0 | | unexpectedToken(token); |
| 1167 | | | } |
| 1168 | | | } |
| 1169 | | | else |
| 1170 | | | { |
| 1171 | 0 | | pushToken(token); |
| 1172 | | | } |
| 1173 | | | |
| 1174 | | | |
| 1175 | 0 | | String constraintState = null; |
| 1176 | 0 | | token = getNextToken(); |
| 1177 | 0 | | type = token.getType(); |
| 1178 | 0 | | if (type == TokenType.ENABLE |
| 1179 | | | || type == TokenType.DISABLE) |
| 1180 | | | { |
| 1181 | 0 | | constraintState = token.getValue(); |
| 1182 | | | } |
| 1183 | | | else |
| 1184 | | | { |
| 1185 | 0 | | pushToken(token); |
| 1186 | | | } |
| 1187 | | | |
| 1188 | 0 | | token = getNextToken(); |
| 1189 | 0 | | type = token.getType(); |
| 1190 | 0 | | if (type == TokenType.COMMA |
| 1191 | | | || type == TokenType.CLOSE_PAREN |
| 1192 | | | || type == TokenType.COMMENT) |
| 1193 | | | { |
| 1194 | | | |
| 1195 | 0 | | pushToken(token); |
| 1196 | | | |
| 1197 | 0 | | final FkConstraint constraint = new FkConstraint( |
| 1198 | | | constraintName, fkCols, refTable, refColumns); |
| 1199 | 0 | | stmt.addFkConstraint(constraint); |
| 1200 | 0 | | logger.finer("Parsed OOL constraint " |
| 1201 | | | + constraintName + ": FOREIGN KEY: " + fkCols |
| 1202 | | | + " reference " + refTable |
| 1203 | | | + "(" + refColumns + ")" |
| 1204 | | | + " with " + deleteSemantic + " delete semantic" |
| 1205 | | | + (constraintState != null |
| 1206 | | | ? (" and constraint state " + constraintState) |
| 1207 | | | : "")); |
| 1208 | 0 | | } |
| 1209 | | | else |
| 1210 | | | { |
| 1211 | 0 | | unexpectedToken(token); |
| 1212 | | | } |
| 1213 | 0 | | } |
| 1214 | | | |
| 1215 | | | private void addUniqueColumns (CreateTableStatement statement, List ukCols) |
| 1216 | | | throws ParseException |
| 1217 | | | { |
| 1218 | 0 | | for (final Iterator it = ukCols.iterator(); it.hasNext(); ) |
| 1219 | | | { |
| 1220 | 0 | | final String colName = (String) it.next(); |
| 1221 | 0 | | final ColumnSpec col = statement.getColumnByName(colName); |
| 1222 | 0 | | if (col == null) |
| 1223 | | | { |
| 1224 | 0 | | throw new ParseException("Column " + colName + " not found", |
| 1225 | | | mScanner.getLine(), mScanner.getColumn()); |
| 1226 | | | } |
| 1227 | 0 | | col.setUnique(true); |
| 1228 | 0 | | } |
| 1229 | 0 | | } |
| 1230 | | | |
| 1231 | | | |
| 1232 | | | |
| 1233 | | | |
| 1234 | | | |
| 1235 | | | @param |
| 1236 | | | |
| 1237 | | | private void addPkColumns (CreateTableStatement statement, List pkCols) |
| 1238 | | | throws ParseException |
| 1239 | | | { |
| 1240 | | | |
| 1241 | 100 | | checkForPrimaryKey(statement); |
| 1242 | | | |
| 1243 | 100 | | for (final Iterator it = pkCols.iterator(); it.hasNext(); ) |
| 1244 | | | { |
| 1245 | 100 | | final String colName = (String) it.next(); |
| 1246 | 100 | | final ColumnSpec col = statement.getColumnByName(colName); |
| 1247 | 100 | | if (col == null) |
| 1248 | | | { |
| 1249 | 0 | | throw new ParseException("Column " + colName + " not found", |
| 1250 | | | mScanner.getLine(), mScanner.getColumn()); |
| 1251 | | | } |
| 1252 | 100 | | col.setPrimaryKey(true); |
| 1253 | 100 | | } |
| 1254 | 100 | | } |
| 1255 | | | |
| 1256 | | | |
| 1257 | | | |
| 1258 | | | |
| 1259 | | | @param |
| 1260 | | | @throws |
| 1261 | | | |
| 1262 | | | private void checkForPrimaryKey (CreateTableStatement statement) |
| 1263 | | | throws ParseException |
| 1264 | | | { |
| 1265 | 100 | | for (final Iterator it = statement.getColumns().iterator(); |
| 1266 | 100 | | it.hasNext(); ) |
| 1267 | | | { |
| 1268 | 100 | | final ColumnSpec col = (ColumnSpec) it.next(); |
| 1269 | 100 | | if (col.isPrimaryKey()) |
| 1270 | | | { |
| 1271 | 0 | | throw new ParseException(col.getColumnName() |
| 1272 | | | + " is already primary key", |
| 1273 | | | mScanner.getLine(), mScanner.getColumn()); |
| 1274 | | | } |
| 1275 | 100 | | } |
| 1276 | 100 | | } |
| 1277 | | | |
| 1278 | | | |
| 1279 | | | |
| 1280 | | | @param |
| 1281 | | | @throws |
| 1282 | | | |
| 1283 | | | private Token assertNextToken (TokenType expectedType) |
| 1284 | | | throws ParseException |
| 1285 | | | { |
| 1286 | | | |
| 1287 | | | |
| 1288 | 100 | | final Token nextToken = getNextToken(); |
| 1289 | 100 | | if (nextToken.getType() != expectedType) |
| 1290 | | | { |
| 1291 | 0 | | throw new ParseException( |
| 1292 | | | "Unexpected token: expected " |
| 1293 | | | + expectedType |
| 1294 | | | + " but got " |
| 1295 | | | + nextToken.getType(), |
| 1296 | | | mScanner.getLine(), |
| 1297 | | | mScanner.getColumn()); |
| 1298 | | | } |
| 1299 | | | |
| 1300 | | | |
| 1301 | 100 | | return nextToken; |
| 1302 | | | } |
| 1303 | | | |
| 1304 | | | private void unexpectedToken (Token token) throws ParseException |
| 1305 | | | { |
| 1306 | 0 | | throw new ParseException( |
| 1307 | | | "Unexpected token: " + token, |
| 1308 | | | mScanner.getLine(), |
| 1309 | | | mScanner.getColumn()); |
| 1310 | | | } |
| 1311 | | | |
| 1312 | | | private void assertToken (Token token, TokenType expectedType) |
| 1313 | | | throws ParseException |
| 1314 | | | { |
| 1315 | 0 | | if (token.getType() != expectedType) |
| 1316 | | | { |
| 1317 | 0 | | throw new ParseException( |
| 1318 | | | "Unexpected token: expected " |
| 1319 | | | + expectedType |
| 1320 | | | + " but got " |
| 1321 | | | + token.getType(), |
| 1322 | | | mScanner.getLine(), |
| 1323 | | | mScanner.getColumn()); |
| 1324 | | | } |
| 1325 | 0 | | } |
| 1326 | | | |
| 1327 | | | |
| 1328 | | | |
| 1329 | | | |
| 1330 | | | @param |
| 1331 | | | @throws |
| 1332 | | | |
| 1333 | | | public static void main (String[] args) |
| 1334 | | (28) | throws Exception |
| 1335 | | | { |
| 1336 | 0 | | final FileInputStream fin = new FileInputStream(args[0]); |
| 1337 | 0 | | final SqlScanner scanner = new SqlScanner(fin); |
| 1338 | 0 | | final SqlParser parser = new SqlParser(scanner); |
| 1339 | 0 | | parser.parse(); |
| 1340 | 0 | | } |
| 1341 | | | } |