SQLParser { public static Collection<DataTypeDefinition> parseDataTypes(String sqlFile) { Collection<DataTypeDefinition> dataTypeDefinitions = new LinkedList<>(); List<String> sqlStatements = loadStatements(sqlFile); for (String sqlStatement : sqlStatements) { SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(sqlStatement)); SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer)); parser.removeErrorListeners(); ParseTree tree = parser.sql_stmt(); ParseTreeWalker.DEFAULT.walk(new SQLiteBaseListener() { String tableName; @Override public void enterCreate_table_stmt(SQLiteParser.Create_table_stmtContext ctx) { super.enterCreate_table_stmt(ctx); if (ctx.table_name() != null) { this.tableName = cleanIdentifier(ctx.table_name().getText()); } } @Override public void enterAlter_table_stmt(SQLiteParser.Alter_table_stmtContext ctx) { super.enterAlter_table_stmt(ctx); if (ctx.table_name() != null) { this.tableName = cleanIdentifier(ctx.table_name().getText()); } } @Override public void enterColumn_def(SQLiteParser.Column_defContext ctx) { super.enterColumn_def(ctx); if (ctx.type_name() != null) { String columnName = cleanIdentifier(ctx.column_name().getText()); String dataType = ctx.type_name().getText(); dataTypeDefinitions.add(new DataTypeDefinition(this.tableName, columnName, dataType)); } } }, tree); } return dataTypeDefinitions; }  static Map<String, List<String>> parseSchema(String sqlFile); static Collection<PrimaryKeyDefinition> parsePrimaryKeys(String sqlFile); static Collection<ForeignKeyDefinition> parseForeignKeys(String sqlFile); static Collection<NotNullDefinition> parseNotNullConstraints(String sqlFile); static Collection<DataTypeDefinition> parseDataTypes(String sqlFile);  }
@Test public void testParseDataTypes() { String inputFile = Thread.currentThread().getContextClassLoader().getResource("test-schema.sql").getFile(); Collection<DataTypeDefinition> dataTypeDefinitions = SQLParser.parseDataTypes(inputFile); Assert.assertEquals( new HashSet<>(Arrays.asList( new DataTypeDefinition("TABLE1", "NAME", "VARCHAR(255)"), new DataTypeDefinition("TABLE1", "FIRST_NAME", "VARCHAR(255)"), new DataTypeDefinition("TABLE1", "PROFESSION", "VARCHAR(50)"), new DataTypeDefinition("TABLE2", "PROFESSION", "VARCHAR(255)"), new DataTypeDefinition("TABLE2", "RECOGNITION", "VARCHAR(255)"), new DataTypeDefinition("TABLE2", "COMMENTS", "TEXT") )), new HashSet<>(dataTypeDefinitions) ); }