public ArrayList<Statement> readQueries(File sql) { ArrayList<Statement> queries = new ArrayList<Statement>(); try { FileReader fr = new FileReader(sql); CCJSqlParser parser = new CCJSqlParser(fr); Statement stmt; try { while ((stmt = parser.Statement()) != null) { if ((stmt instanceof CreateTable) || (stmt instanceof Select)) { queries.add(stmt); } else { logger.warning("PANIC! I don't know how to handle: "); logger.warning("" + stmt); } } } catch (ParseException e) { logger.severe("PARSE EXCEPTION IN READING FILE: " + sql); e.printStackTrace(); } } catch (FileNotFoundException e) { logger.severe("SQL FILE NOT FOUND: " + sql); e.printStackTrace(); } return queries; }
public static void main(String[] args) throws IOException { // get the information of table address DatabaseCatalog databaseCatalog = DatabaseCatalog.getInstance(); databaseCatalog.autogetTableInfo(); Schema schema = Schema.getInstance(); schema.getSchemaInfor("samples/input/db/schema.txt"); Aliases aliases = Aliases.getInstance(); // aliases.addaliase("S", "Sailors"); // ScanOperator scanOperator = new ScanOperator("S"); // scanOperator.dump(); try { CCJSqlParser parser = new CCJSqlParser(new FileReader("/Users/hanwenwang/Desktop/queries.sql")); Statement statement; while ((statement = parser.Statement()) != null) { Select select = (Select) statement; System.out.println("Select body is " + select.getSelectBody()); PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); String aliasName = ((Table) plainSelect.getFromItem()).getAlias(); String tableName = ((Table) plainSelect.getFromItem()).getName(); aliases.addaliase(aliasName, tableName); ScanOperator scanOperator1 = new ScanOperator(aliasName); // scanOperator1.dump(); ProjectOperator projectOperator = new ProjectOperator(scanOperator1, plainSelect.getSelectItems()); projectOperator.dump(); } } catch (Exception e) { System.err.println("Exception occurred during parsing"); e.printStackTrace(); } }
public static void main(String[] args) throws IOException { DatabaseCatalog tableMap = DatabaseCatalog.getInstance(); tableMap.autogetTableInfo(); System.out.println(tableMap.getTableLocated("Sailors")); Schema schema = Schema.getInstance(); schema.getSchemaInfor("/Users/hanwenwang/Desktop/samples/input/db/schema.txt"); try { CCJSqlParser parser = new CCJSqlParser(new FileReader("/Users/hanwenwang/Desktop/queries 2.sql")); Statement statement; while ((statement = parser.Statement()) != null) { Select select = (Select) statement; System.out.println("Select body is " + select.getSelectBody()); PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); System.out.println(plainSelect.getSelectItems().toString()); ScanOperator scanOperator = new ScanOperator(plainSelect.getFromItem().toString()); SelectOperator selectOperator = new SelectOperator(scanOperator, plainSelect.getWhere()); ProjectOperator projectOperator = new ProjectOperator(selectOperator, plainSelect.getSelectItems()); projectOperator.dump(); } } catch (Exception e) { System.err.println("Exception occurred during parsing"); e.printStackTrace(); } }