Search in sources :

Example 61 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SchemaStatTest4 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select name, age from t_user where id = 1";
    DbType dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    System.out.println(statVisitor.getColumns());
    System.out.println(statVisitor.getTables());
    System.out.println(statVisitor.getConditions());
    Assert.assertEquals(3, statVisitor.getColumns().size());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 62 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SchemaStatTest12 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select work_no , nick_name , name , ifnull(sum(investment_percentage ) / 100 , 0 ) as invest_percent " + "from ( " + "   select t1.id , t1.work_no , t2.nick_name , t2.name , t2.super_work_no , t2.super_nick_name , t2.super_name " + "       , t3.project_name , t1.investment_percentage , t4.dept_name " + "   from t_pm_aur t2 " + "       left outer join ( " + "           select * from t_pm_rs_acl " + "           where is_deleted = 0 and prj_biz_id is not null and ivt_pt > 0 and iwk = '2016W17' " + "       ) t1 on t2.work_no = t1.work_no " + "       left outer join ( " + "           select * from t_pm_prj_i " + "           where( status not in( 'DEPRECATED' , 'FINISHED' ) or status is null ) and( task_type not in( 'WEEKLY_REPORT' ) or task_type is null ) and is_deleted = 0 " + "       ) t3 on t1.prj_biz_id = t3.biz_id " + "       left outer join t_pm_dpt t4 on t2.dept_no = t4.biz_id " + "   where t4.dept_name like 'MY-XDSYB-JSB%' and t2.emp_type in( 'R' , 'V' ) and t2.is_deleted = 'N' and t2.work_status = 'A' " + ") t5 group by work_no , nick_name , name having invest_percent < 1.0 ";
    DbType dbType = JdbcConstants.ORACLE;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    System.out.println(stmt);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
    for (TableStat.Relationship relationship : relationships) {
        // table1.id = table2.id
        System.out.println(relationship);
    }
    System.out.println("columns : " + statVisitor.getColumns());
    System.out.println("group by : " + statVisitor.getGroupByColumns());
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println("conditions : " + statVisitor.getConditions());
    System.out.println("functionns : " + statVisitor.getFunctions());
    assertEquals(3, relationships.size());
    Assert.assertEquals(23, statVisitor.getColumns().size());
    Assert.assertEquals(19, statVisitor.getConditions().size());
    assertEquals(1, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 63 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class Oracle_param_0 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where rownum = 1";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    visitor.setExportTables(true);
    /*visitor.setPrettyFormat(false);*/
    SQLStatement stmt = stmtList.get(0);
    stmt.accept(visitor);
    // System.out.println(parameters);
    assertEquals(0, parameters.size());
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("a_entry", "a_entry_2664");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE rownum = 1", buf.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 64 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class Oracle_param_2 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD') FROM dual;";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    /*visitor.setPrettyFormat(false);*/
    statement.accept(visitor);
    /* JSONArray array = new JSONArray();
        for(String table : visitor.getTables()){
            array.add(table.replaceAll("`",""));
        }*/
    String psql = out.toString();
    assertEquals("SELECT TO_DATE(?, 'YYYY-MM-DD')\n" + "FROM dual;", psql);
    String params_json = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
    System.out.println(params_json);
    JSONArray jsonArray = JSON.parseArray(params_json);
    String json = JSONArray.toJSONString(jsonArray, SerializerFeature.WriteClassName);
    assertEquals("[\"2013-02-11\"]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD')\n" + "FROM dual;", rsql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 65 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class LogFilter method logExecutableSql.

private void logExecutableSql(StatementProxy statement, String sql) {
    if ((!isStatementExecutableSqlLogEnable()) || !isStatementLogEnabled()) {
        return;
    }
    int parametersSize = statement.getParametersSize();
    if (parametersSize == 0) {
        statementLog("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} executed. " + sql);
        return;
    }
    List<Object> parameters = new ArrayList<Object>(parametersSize);
    for (int i = 0; i < parametersSize; ++i) {
        JdbcParameter jdbcParam = statement.getParameter(i);
        parameters.add(jdbcParam != null ? jdbcParam.getValue() : null);
    }
    DbType dbType = DbType.of(statement.getConnectionProxy().getDirectDataSource().getDbType());
    String formattedSql = SQLUtils.format(sql, dbType, parameters, this.statementSqlFormatOption);
    statementLog("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} executed. " + formattedSql);
}
Also used : JdbcParameter(com.alibaba.druid.proxy.jdbc.JdbcParameter) ArrayList(java.util.ArrayList) DbType(com.alibaba.druid.DbType)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2