Search in sources :

Example 66 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class DB2SelectTest_4 method test_0.

public void test_0() throws Exception {
    String sql = "select * from t OPTIMIZE FOR 10 ROWS";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t", "*")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM t" + //
    "\nOPTIMIZE FOR 10", output);
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 67 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class DB2SelectTest_5 method test_0.

public void test_0() throws Exception {
    String sql = //
    "SELECT * FROM (SELECT TEMP_TAB.*,ROWNUMBER() OVER() AS IDX FROM (SELECT" + //
    "            DISTINCT  ( OH.ORDER_ID )" + //
    "        FROM" + //
    "            ORDER_HEADER OH," + //
    "            ORDER_ITEM OI," + //
    "            ORDER_PAYMENT_PERFERENCE OPP," + //
    "            ORDER_SHIPMENT_PERFERENCE OSP" + //
    "        WHERE" + //
    "            OH.ORDER_ID = OI.ORDER_ID" + //
    "        AND OH.ORDER_ID = OPP.ORDER_ID" + //
    "        AND OH.ORDER_ID = OSP.ORDER_ID" + //
    "             " + //
    "                AND OH.ORDER_ID = ? ) AS TEMP_TAB ) AS TEMP_TAB_WITH_IDX " + //
    " WHERE TEMP_TAB_WITH_IDX.IDX > 0 AND TEMP_TAB_WITH_IDX.IDX <= 20" + "";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(4, visitor.getTables().size());
    Assert.assertEquals(4, visitor.getColumns().size());
    Assert.assertEquals(4, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ORDER_HEADER")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("ORDER_HEADER", "ORDER_ID")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM (SELECT TEMP_TAB.*, ROWNUMBER() OVER () AS IDX" + //
    "\n\tFROM (SELECT DISTINCT OH.ORDER_ID" + //
    "\n\t\tFROM ORDER_HEADER OH, ORDER_ITEM OI, ORDER_PAYMENT_PERFERENCE OPP, ORDER_SHIPMENT_PERFERENCE OSP" + //
    "\n\t\tWHERE OH.ORDER_ID = OI.ORDER_ID" + //
    "\n\t\t\tAND OH.ORDER_ID = OPP.ORDER_ID" + //
    "\n\t\t\tAND OH.ORDER_ID = OSP.ORDER_ID" + //
    "\n\t\t\tAND OH.ORDER_ID = ?" + //
    "\n\t\t) TEMP_TAB" + //
    "\n\t) TEMP_TAB_WITH_IDX" + //
    "\nWHERE TEMP_TAB_WITH_IDX.IDX > 0" + //
    "\n\tAND TEMP_TAB_WITH_IDX.IDX <= 20", output);
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 68 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class DB2InsertTest_0 method test_0.

public void test_0() throws Exception {
    String sql = "insert into t (id,name) values (?,?), (?,?), (?,?)";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t", "name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
    Assert.assertEquals("INSERT INTO t (id, name)\n" + //
    "VALUES (?, ?), (?, ?), (?, ?)", output);
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 69 with Column

use of com.alibaba.druid.stat.TableStat.Column in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method getColumn.

@Override
protected Column getColumn(SQLExpr expr) {
    Map<String, String> aliasMap = getAliasMap();
    if (aliasMap == null) {
        return null;
    }
    if (expr instanceof SQLPropertyExpr) {
        SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
        String column = ((SQLPropertyExpr) expr).getName();
        if (owner instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) owner).getName();
            String table = tableName;
            if (aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null) {
                return new Column(table, column);
            }
            return handleSubQueryColumn(tableName, column);
        }
        return null;
    }
    if (expr instanceof SQLIdentifierExpr) {
        Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
        if (attrColumn != null) {
            return attrColumn;
        }
        String column = ((SQLIdentifierExpr) expr).getName();
        String table = getCurrentTable();
        if (table != null && aliasMap.containsKey(table)) {
            table = aliasMap.get(table);
            if (table == null) {
                return null;
            }
        }
        if (table != null) {
            return new Column(table, column);
        }
        if (variants.containsKey(column)) {
            return null;
        }
        return new Column("UNKNOWN", column);
    }
    if (expr instanceof SQLBetweenExpr) {
        SQLBetweenExpr betweenExpr = (SQLBetweenExpr) expr;
        if (betweenExpr.getTestExpr() != null) {
            String tableName = null;
            String column = null;
            if (betweenExpr.getTestExpr() instanceof SQLPropertyExpr) {
                //字段带别名的
                tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) betweenExpr.getTestExpr()).getOwner()).getName();
                column = ((SQLPropertyExpr) betweenExpr.getTestExpr()).getName();
                SQLObject query = this.subQueryMap.get(tableName);
                if (query == null) {
                    if (aliasMap.containsKey(tableName)) {
                        tableName = aliasMap.get(tableName);
                    }
                    return new Column(tableName, column);
                }
                return handleSubQueryColumn(tableName, column);
            } else if (betweenExpr.getTestExpr() instanceof SQLIdentifierExpr) {
                column = ((SQLIdentifierExpr) betweenExpr.getTestExpr()).getName();
                //字段不带别名的,此处如果是多表,容易出现ambiguous,
                //不知道这个字段是属于哪个表的,fdbparser用了defaultTable,即join语句的leftTable
                tableName = getOwnerTableName(betweenExpr, column);
            }
            String table = tableName;
            if (aliasMap.containsKey(table)) {
                table = aliasMap.get(table);
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null && !"".equals(table)) {
                return new Column(table, column);
            }
        }
    }
    return null;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 70 with Column

use of com.alibaba.druid.stat.TableStat.Column in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

@Override
public boolean visit(SQLBetweenExpr x) {
    String begin = null;
    if (x.beginExpr instanceof SQLCharExpr) {
        begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
    } else {
        begin = x.beginExpr.toString();
    }
    String end = null;
    if (x.endExpr instanceof SQLCharExpr) {
        end = (String) ((SQLCharExpr) x.endExpr).getValue();
    } else {
        end = x.endExpr.toString();
    }
    Column column = getColumn(x);
    if (column == null) {
        return true;
    }
    Condition condition = null;
    for (Condition item : this.getConditions()) {
        if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
            condition = item;
            break;
        }
    }
    if (condition == null) {
        condition = new Condition();
        condition.setColumn(column);
        condition.setOperator("between");
        this.conditions.add(condition);
    }
    condition.getValues().add(begin);
    condition.getValues().add(end);
    return true;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Aggregations

Column (com.alibaba.druid.stat.TableStat.Column)134 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)122 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)74 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)73 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)19 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)19 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)12 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)12 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)10 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)8 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)8 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)8 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)6 MysqlTest (com.alibaba.druid.sql.MysqlTest)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 Test (org.junit.Test)5 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4