StandardDdlParser implements DdlParser, DdlConstants, DdlConstants.StatementStartPhrases { protected AstNode parseCreateTableStatement( DdlTokenStream tokens, AstNode parentNode ) throws ParsingException { assert tokens != null; assert parentNode != null; markStartOfStatement(tokens); tokens.consume(CREATE); String temporaryValue = null; if (tokens.canConsume("LOCAL")) { tokens.consume("TEMPORARY"); temporaryValue = "LOCAL"; } else if (tokens.canConsume("GLOBAL")) { tokens.consume("TEMPORARY"); temporaryValue = "GLOBAL"; } tokens.consume(TABLE); String tableName = parseName(tokens); AstNode tableNode = nodeFactory().node(tableName, parentNode, TYPE_CREATE_TABLE_STATEMENT); if (temporaryValue != null) { tableNode.setProperty(TEMPORARY, temporaryValue); } parseColumnsAndConstraints(tokens, tableNode); parseCreateTableOptions(tokens, tableNode); markEndOfStatement(tokens, tableNode); return tableNode; } StandardDdlParser(); DataTypeParser getDatatypeParser(); void setDatatypeParser( DataTypeParser datatypeParser ); AstNodeFactory nodeFactory(); AstNode getRootNode(); void setRootNode( AstNode rootNode ); @Override Object score( String ddl,
                         String fileName,
                         DdlParserScorer scorer ); String[] getIdentifyingKeywords(); @Override void parse( String ddl,
                       AstNode rootNode,
                       Object scoreReturnObject ); final void addProblem( DdlParserProblem problem,
                                  AstNode node ); final void addProblem( DdlParserProblem problem ); final List<DdlParserProblem> getProblems(); final void attachNewProblem( DdlParserProblem problem,
                                        AstNode parentNode ); void mergeNodes( DdlTokenStream tokens,
                            AstNode firstNode,
                            AstNode secondNode ); AstNode handleUnknownToken( DdlTokenStream tokens,
                                       String tokenValue ); final AstNode unknownTerminatedNode( AstNode parentNode ); final AstNode missingTerminatorNode( AstNode parentNode ); final boolean isMissingTerminatorNode( AstNode node ); final boolean isValidSchemaChild( AstNode node ); final boolean setAsSchemaChildNode( AstNode statementNode,
                                               boolean stmtIsMissingTerminator ); boolean doUseTerminator(); void setDoUseTerminator( boolean useTerminator ); String getStatementTypeName( String[] stmtPhrase ); final Position getCurrentMarkedPosition(); final void markStartOfStatement( DdlTokenStream tokens ); final void markEndOfStatement( DdlTokenStream tokens,
                                          AstNode statementNode ); @Override void postProcess( AstNode rootNode ); boolean isTestMode(); void setTestMode( boolean testMode ); @Override String getId(); @Override int hashCode(); @Override boolean equals( Object obj ); static final String ID; }
@Test public void shouldParseCreateTableWithInlineUniqueConstraintWithColumns() { printTest("shouldParseCreateTableWithInlineUniqueConstraint()"); String tableName = "table_name_24_B"; String content = "CREATE TABLE table_name_24_B ( REALMUID NUMERIC(10) NOT NULL CONSTRAINT PK_AUTHREALMS UNIQUE (columnA, columnB));"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChildren().get(0); assertThat(result, is(tableNode)); assertEquals(tableName, tableNode.getName()); assertEquals(2, tableNode.getChildCount()); AstNode column = tableNode.getChildren().get(0); assertEquals("NUMERIC", column.getProperty(DATATYPE_NAME)); assertEquals(10, column.getProperty(DATATYPE_PRECISION)); assertTrue(column.getProperty(DATATYPE_LENGTH) == null); AstNode prim_key = tableNode.getChildren().get(1); assertEquals(2, prim_key.getChildCount()); AstNode columnRef = prim_key.getChildren().get(0); assertEquals("columnA", columnRef.getName()); }
@Test public void shouldParseCreateLocalTemporaryTable() { printTest("shouldParseCreateLocalTemporaryTable()"); String tableName = "MY_TABLE_A"; String content = "CREATE LOCAL TEMPORARY TABLE MY_TABLE_A (PARTID VARCHAR (255) NOT NULL DEFAULT (100), -- COLUMN 1 COMMENT with comma \nPARTCOLOR INTEGER NOT NULL);"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChildren().get(0); assertThat(result, is(tableNode)); assertEquals(tableName, tableNode.getName()); assertEquals(2, tableNode.getChildCount()); AstNode column = tableNode.getChildren().get(0); assertEquals("VARCHAR", column.getProperty(DATATYPE_NAME)); assertEquals(255L, column.getProperty(DATATYPE_LENGTH)); assertTrue(column.getProperty(DATATYPE_PRECISION) == null); assertEquals("NOT NULL", column.getProperty(NULLABLE)); assertEquals(DEFAULT_ID_LITERAL, column.getProperty(DEFAULT_OPTION)); assertEquals("100", column.getProperty(DEFAULT_VALUE)); }
@Test public void shouldParseCreateTableWithConstraint() { printTest("shouldParseCreateTableWithConstraint()"); String tableName = "MY_TABLE_B"; String content = "CREATE TABLE MY_TABLE_B (PARTID VARCHAR (255), PRIMARY KEY (C1, C2), \nPARTCOLOR INTEGER NOT NULL, CONSTRAINT PK_A PRIMARY KEY (FILE_UID) );"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChildren().get(0); assertThat(result, is(tableNode)); assertEquals(tableName, tableNode.getName()); assertEquals(4, tableNode.getChildCount()); AstNode column1 = tableNode.getChildren().get(0); assertEquals("VARCHAR", column1.getProperty(DATATYPE_NAME)); assertEquals(255L, column1.getProperty(DATATYPE_LENGTH)); assertTrue(column1.getProperty(DATATYPE_PRECISION) == null); }
@Test public void shouldParseCreateTableWithInlineConstraint() { printTest("shouldParseCreateTableWithInlineConstraint()"); String tableName = "table_name_22_B"; String content = "CREATE TABLE table_name_22_B ( column_name_1 VARCHAR(255) CONSTRAINT pk_name UNIQUE INITIALLY IMMEDIATE);"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChildren().get(0); assertThat(result, is(tableNode)); assertEquals(tableName, tableNode.getName()); assertEquals(2, tableNode.getChildCount()); AstNode column = tableNode.getChildren().get(0); assertEquals("VARCHAR", column.getProperty(DATATYPE_NAME)); assertEquals(255L, column.getProperty(DATATYPE_LENGTH)); assertTrue(column.getProperty(DATATYPE_PRECISION) == null); }
@Test public void shouldParseCreateTableWithInlineUniqueConstraint() { printTest("shouldParseCreateTableWithInlineUniqueConstraint()"); String tableName = "table_name_22_B"; String content = "CREATE TABLE table_name_22_B ( REALMUID NUMERIC(10) NOT NULL CONSTRAINT PK_AUTHREALMS UNIQUE);"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChildren().get(0); assertThat(result, is(tableNode)); assertEquals(tableName, tableNode.getName()); assertEquals(2, tableNode.getChildCount()); AstNode column = tableNode.getChildren().get(0); assertEquals("NUMERIC", column.getProperty(DATATYPE_NAME)); assertEquals(10, column.getProperty(DATATYPE_PRECISION)); assertTrue(column.getProperty(DATATYPE_LENGTH) == null); AstNode prim_key = tableNode.getChildren().get(1); AstNode columnRef = prim_key.getChildren().get(0); assertEquals("REALMUID", columnRef.getName()); }
@Test public void shouldParseCreateTableWithPrimaryKeyColumnConstraint() { printTest("shouldWork()"); String PK_COL = "customerfamilyhistoryid"; String content = "CREATE TABLE CustomerFamilyHistory ("; content += PK_COL + " numeric(10) CONSTRAINT customerfamilyhistoryid_pk PRIMARY KEY, firstname varchar(50) NOT NULL, lastname varchar(50) NOT NULL, age numeric(3), sibling varchar(20), customerid numeric(7) NOT NULL);"; DdlTokenStream tokens = getTokens(content); AstNode result = parser.parseCreateTableStatement(tokens, rootNode); assertThat(result, is(notNullValue())); assertEquals(1, rootNode.getChildCount()); AstNode tableNode = rootNode.getChild(0); assertEquals(7, tableNode.getChildCount()); boolean foundConstraint = false; for (AstNode kid : tableNode.getChildren()) { Object value = kid.getProperty(CONSTRAINT_TYPE); if (value != null) { assertFalse(foundConstraint); foundConstraint = true; assertEquals(value, PRIMARY_KEY); assertEquals(1, kid.getChildCount()); AstNode child = kid.getChild(0); assertTrue(hasMixinType(child, TYPE_COLUMN_REFERENCE)); assertEquals(PK_COL, child.getName()); } } }