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(); } }
@SuppressWarnings("unchecked") @Override public void visit(PlainSelect select) { if (!CollectionUtils.isEmpty(select.getGroupByColumnReferences()) || !CollectionUtils.isEmpty(select.getJoins())) { isSimple = false; } else { select.getFromItem().accept(this); for (SelectItem item : (List<SelectItem>) select.getSelectItems()) { item.accept(this); } } }
/** * 静态调用接口,获取所有表的ID * * @param sql 传入的sql语句 * @return 返回-1表示当前语句不是select语句,返回0表示当前SQL有问题,返回>0的数字表示查找出的表数目 */ public static int getOriginalTableName(String sql) { int num_Table = 0; Statement statement = null; try { statement = CCJSqlParserUtil.parse(sql); if (statement instanceof Select) { Select selectStatement = (Select) statement; System.err.println(sql); PlainSelect plainSelect = null; plainSelect = (PlainSelect) selectStatement.getSelectBody(); if (plainSelect != null) { System.out.println("\n-------------------------------------------"); if (plainSelect.getFromItem() != null) { plainSelect.getFromItem().toString(); num_Table++; } System.out.print(plainSelect.getFromItem().toString() + "\t"); if (plainSelect.getJoins() != null) { for (Join join2 : plainSelect.getJoins()) { System.out.print(join2.toString() + "\t"); num_Table++; } } System.out.println("\n-------------------------------------------"); } else { num_Table = -1; return num_Table; } } } catch (JSQLParserException ex) { ex.printStackTrace(); num_Table = 0; LogWriter.println(num_Table); return num_Table; } LogWriter.println("共统计出表个数=>" + num_Table); return num_Table; }
public void testFrom() throws JSQLParserException { String statement = "SELECT * FROM mytable as mytable0, mytable1 alias_tab1, mytable2 as alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9"; String statementToString = "SELECT * FROM mytable as mytable0, mytable1 as alias_tab1, mytable2 as alias_tab2, (SELECT * FROM mytable3) AS mytable4 WHERE mytable.col = 9"; PlainSelect plainSelect = (PlainSelect) ((Select) parserManager.parse(new StringReader(statement))).getSelectBody(); assertEquals(3, plainSelect.getJoins().size()); assertEquals("mytable0", ((Table) plainSelect.getFromItem()).getAlias()); assertEquals("alias_tab1", ((Join) plainSelect.getJoins().get(0)).getRightItem().getAlias()); assertEquals("alias_tab2", ((Join) plainSelect.getJoins().get(1)).getRightItem().getAlias()); assertEquals("mytable4", ((Join) plainSelect.getJoins().get(2)).getRightItem().getAlias()); assertEquals(statementToString.toUpperCase(), plainSelect.toString().toUpperCase()); }
/** @brief The following functions override functions of the interfaces. */ public void visit(PlainSelect plainSelect) throws Exception { if (plainSelect.getFromItem() != null) { if (plainSelect.getFromItem().getAlias() != null) { this.aliasTableNameList.add(plainSelect.getFromItem().getAlias().getName()); } plainSelect.getFromItem().accept(this); } if (plainSelect.getJoins() != null) { for (Iterator joinsIt = plainSelect.getJoins().iterator(); joinsIt.hasNext(); ) { Join join = (Join) joinsIt.next(); if (join.getRightItem().getAlias() != null) { this.aliasTableNameList.add(join.getRightItem().getAlias().getName()); } if (join.getOnExpression() != null) { join.getOnExpression().accept(this); } join.getRightItem().accept(this); } } // Select selectItem From fromItem, joinItem Where whereClause. if (plainSelect.getSelectItems() != null) { for (SelectItem selectItem : plainSelect.getSelectItems()) { selectItem.accept(this); } } if (plainSelect.getWhere() != null) { plainSelect.getWhere().accept(this); } if (plainSelect.getGroupByColumnReferences() != null) { for (Iterator groupByIt = plainSelect.getGroupByColumnReferences().iterator(); groupByIt.hasNext(); ) { Expression groupBy = (Expression) groupByIt.next(); groupBy.accept(this); } } if (plainSelect.getClusterByElements() != null) { for (Iterator clusterByit = plainSelect.getClusterByElements().iterator(); clusterByit.hasNext(); ) { ClusterByElement clusterByElement = (ClusterByElement) clusterByit.next(); visit(clusterByElement); } } if (plainSelect.getDistributeByElements() != null) { for (Iterator distributeByIt = plainSelect.getDistributeByElements().iterator(); distributeByIt.hasNext(); ) { DistributeByElement distributeByElement = (DistributeByElement) distributeByIt.next(); visit(distributeByElement); } } if (plainSelect.getOrderByElements() != null) { for (Iterator orderByIt = plainSelect.getOrderByElements().iterator(); orderByIt.hasNext(); ) { OrderByElement orderByElement = (OrderByElement) orderByIt.next(); orderByElement.accept(this); } } if (plainSelect.getSortByElements() != null) { for (Iterator sortByIt = plainSelect.getSortByElements().iterator(); sortByIt.hasNext(); ) { SortByElement sortByElement = (SortByElement) sortByIt.next(); visit(sortByElement); } } if (plainSelect.getHaving() != null) { plainSelect.getHaving().accept(this); } }