Search in sources :

Example 61 with Column

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

the class SQLite_SelectTest_0 method test_0.

public void test_0() throws Exception {
    String sql = "select first_name, last_name from mytable limit 3, 4";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.SQLITE);
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.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("mytable")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 62 with Column

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

the class SQLServerCreateTableTest_8 method test_0.

public void test_0() throws Exception {
    String sql = "create table ACT_RU_VARIABLE (" + " ID_ nvarchar(64) not null," + " DOUBLE_ double precision," + " LONG_ numeric(19,0)," + " TEXT_ nvarchar(4000)," + " primary key (ID_)" + ")";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals("CREATE TABLE ACT_RU_VARIABLE (" + "\n\tID_ nvarchar(64) NOT NULL," + "\n\tDOUBLE_ DOUBLE PRECISION," + "\n\tLONG_ numeric(19, 0)," + "\n\tTEXT_ nvarchar(4000)," + "\n\tPRIMARY KEY (ID_)" + "\n)", output);
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    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(4, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ACT_RU_VARIABLE")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "ID_")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "DOUBLE_")));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 63 with Column

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

the class ExportConditions method evaluate.

public String evaluate(String sql, String dbType, Boolean compactValues) {
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>();
        List<Condition> conditions = visitor.getConditions();
        for (int i = 0; i < conditions.size(); ++i) {
            TableStat.Condition condition = conditions.get(i);
            Column column = condition.getColumn();
            String operator = condition.getOperator();
            List<Object> values = condition.getValues();
            List<Object> row = new ArrayList<Object>();
            row.add(column.getTable());
            row.add(column.getName());
            row.add(operator);
            if (values.size() == 0) {
                row.add(null);
            } else if (values.size() == 1) {
                if (compactValues != null && compactValues.booleanValue()) {
                    row.add(values);
                } else {
                    row.add(values.get(0));
                }
            } else {
                row.add(values);
            }
            rows.add(row);
        }
        return JSONUtils.toJSONString(rows);
    } catch (Throwable ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Column(com.alibaba.druid.stat.TableStat.Column) Condition(com.alibaba.druid.stat.TableStat.Condition) List(java.util.List) ArrayList(java.util.ArrayList) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 64 with Column

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

the class DB2SelectTest_12 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT * FROM TASK_POLICE_QUERY_BATCH WHERE STATUS = '0' ORDER BY PRIORITY FETCH FIRST 100 ROWS ONLY";
    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(1, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("TASK_POLICE_QUERY_BATCH")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TASK_POLICE_QUERY_BATCH", "STATUS")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    Assert.assertEquals("SELECT *" + "\nFROM TASK_POLICE_QUERY_BATCH" + "\nWHERE STATUS = '0'" + "\nORDER BY PRIORITY" + //
    "\nFETCH FIRST 100 ROWS ONLY", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
    Assert.assertEquals("select *" + "\nfrom TASK_POLICE_QUERY_BATCH" + "\nwhere STATUS = '0'" + "\norder by PRIORITY" + //
    "\nfetch first 100 rows only", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
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 65 with Column

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

the class DB2SelectTest_13 method test_0.

public void test_0() throws Exception {
    String sql = //
    "SELECT WORKDEPT, EMPNO, SALARY, BONUS, COMM " + //
    " FROM DSN8B10.EMP" + //
    " WHERE WORKDEPT IN ('D11','D21')" + " FOR UPDATE;";
    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(5, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("DSN8B10.EMP")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("DSN8B10.EMP", "WORKDEPT")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    Assert.assertEquals("SELECT WORKDEPT, EMPNO, SALARY, BONUS, COMM" + "\nFROM DSN8B10.EMP" + "\nWHERE WORKDEPT IN ('D11', 'D21')" + //
    "\nFOR UPDATE", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
    Assert.assertEquals("select WORKDEPT, EMPNO, SALARY, BONUS, COMM" + "\nfrom DSN8B10.EMP" + "\nwhere WORKDEPT in ('D11', 'D21')" + //
    "\nfor update", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
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)

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