DdlParsers { public AstNode parse( final String ddl, final String fileName ) throws ParsingException { CheckArg.isNotEmpty(ddl, "ddl"); RuntimeException firstException = null; final Map<DdlParser, Integer> scoreMap = new HashMap<DdlParser, Integer>(this.parsers.size()); final DdlParserScorer scorer = new DdlParserScorer(); for (final DdlParser parser : this.parsers) { try { parser.score(ddl, fileName, scorer); scoreMap.put(parser, scorer.getScore()); } catch (RuntimeException e) { if (firstException == null) { firstException = e; } } finally { scorer.reset(); } } if (scoreMap.isEmpty()) { if (firstException == null) { throw new ParsingException(Position.EMPTY_CONTENT_POSITION, DdlSequencerI18n.errorParsingDdlContent.text(this.parsers.size())); } throw firstException; } final List<Entry<DdlParser, Integer>> scoredParsers = new ArrayList<Entry<DdlParser, Integer>>(scoreMap.entrySet()); Collections.sort(scoredParsers, SORTER); firstException = null; AstNode astRoot = null; for (final Entry<DdlParser, Integer> scoredParser : scoredParsers) { try { final DdlParser parser = scoredParser.getKey(); astRoot = createDdlStatementsContainer(parser.getId()); parser.parse(ddl, astRoot, null); return astRoot; } catch (final RuntimeException e) { if (astRoot != null) { astRoot.removeFromParent(); } if (firstException == null) { firstException = e; } } } if (firstException == null) { throw new ParsingException(Position.EMPTY_CONTENT_POSITION, DdlSequencerI18n.errorParsingDdlContent.text()); } throw firstException; } DdlParsers();  DdlParsers( List<DdlParser> parsers ); DdlParser getParser( final String id ); Set<DdlParser> getParsers(); AstNode parseUsing( final String ddl,
                               final String parserId ); List<ParsingResult> parseUsing( final String ddl,
                                           final String firstParserId,
                                           final String secondParserId,
                                           final String... additionalParserIds ); List<ParsingResult> parseUsingAll( final String ddl ); AstNode parse( final String ddl,
                          final String fileName ); static final List<DdlParser> BUILTIN_PARSERS; }
@Test public void shouldParseUntypedDdlFileWithTypeInFilename() { printTest("shouldParseUntypedDdlFileWithTypeInFilename()"); String content = "-- SAMPLE DDL FILE\n" + "CREATE TABLE myTableName (PART_COLOR VARCHAR(255) NOT NULL, PART_ID INTEGER DEFAULT (100))\n" + "DROP TABLE list_customers CASCADE"; rootNode = parsers.parse(content, "someSQL92.ddl"); assertEquals("SQL92", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseTypedDdlFileWith() { printTest("shouldParseTypedDdlFileWith()"); String content = "-- SAMPLE SQL92 DDL FILE\n" + "CREATE TABLE myTableName (PART_COLOR VARCHAR(255) NOT NULL, PART_ID INTEGER DEFAULT (100))\n" + "DROP TABLE list_customers CASCADE"; rootNode = parsers.parse(content, null); assertEquals("SQL92", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseUntypedDdlFile() { printTest("shouldParseUntypedDdlFile()"); String content = "\n" + "-- SAMPLE DDL FILE\n" + "CREATE TABLE myTableName (PART_COLOR VARCHAR(255) NOT NULL, PART_ID INTEGER DEFAULT (100))\n" + "DROP TABLE list_customers CASCADE"; rootNode = parsers.parse(content, null); assertEquals("SQL92", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseUntypedDerbyFile() { printTest("shouldParseUntypedDerbyFile()"); String content = getFileContent(DDL_TEST_FILE_PATH + "dialect/derby/derby_test_statements.ddl"); rootNode = parsers.parse(content, null); assertEquals("DERBY", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseTypedDerbyFile() { printTest("shouldParseTypedDerbyFile()"); String content = getFileContent(DDL_TEST_FILE_PATH + "dialect/derby/derby_test_statements_typed.ddl"); rootNode = parsers.parse(content, null); assertEquals("DERBY", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseUntypedOracleFile() { printTest("shouldParseUntypedOracleFile()"); String content = getFileContent(DDL_TEST_FILE_PATH + "dialect/oracle/oracle_test_statements_3.ddl"); rootNode = parsers.parse(content, null); assertEquals("ORACLE", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); }
@Test public void shouldParseUntypedPostgresFile() { printTest("shouldParseUntypedPostgresFile()"); String content = getFileContent(DDL_TEST_FILE_PATH + "dialect/postgres/postgres_test_statements_1.ddl"); rootNode = parsers.parse(content, null); assertThat("POSTGRES", is((String)rootNode.getProperty(StandardDdlLexicon.PARSER_ID))); }
@Test public void shouldParseUnterminatedOracleFile() { printTest("shouldParseUnterminatedOracleFile()"); String content = getFileContent(DDL_TEST_FILE_PATH + "GFM_Physical.ddl"); rootNode = parsers.parse(content, null); assertEquals("ORACLE", rootNode.getProperty(StandardDdlLexicon.PARSER_ID)); setPrintToConsole(true); final AstNodeFactory nodeFactory = new AstNodeFactory(); List<AstNode> problems = nodeFactory.getChildrenForType(rootNode, TYPE_PROBLEM); for (AstNode problem : problems) { printTest(problem.toString()); } assertThat(rootNode.getChildCount(), is(123)); List<AstNode> schemaNodes = nodeFactory.getChildrenForType(rootNode, TYPE_CREATE_SCHEMA_STATEMENT); assertThat(schemaNodes.size(), is(1)); assertThat(schemaNodes.get(0).getChildCount(), is(53)); assertThat(schemaNodes.get(0).getName(), is("GLOBALFORCEMGMT")); List<AstNode> alterNodes = nodeFactory.getChildrenForType(rootNode, TYPE_ALTER_TABLE_STATEMENT); assertThat(alterNodes.size(), is(120)); List<AstNode> dropSchemaNodes = nodeFactory.getChildrenForType(rootNode, TYPE_DROP_SCHEMA_STATEMENT); assertThat(dropSchemaNodes.size(), is(1)); List<AstNode> unknownNodes = nodeFactory.getChildrenForType(rootNode, TYPE_UNKNOWN_STATEMENT); assertThat(unknownNodes.size(), is(1)); }
@Test public void shouldParseHugeOracleFile() { printTest("shouldParseHugeOracleFile()"); final String content = getFileContent(DDL_TEST_FILE_PATH + "dialect/oracle/huge.ddl"); this.rootNode = this.parsers.parse(content, null); assertThat("ORACLE", is((String)this.rootNode.getProperty(StandardDdlLexicon.PARSER_ID))); }