Search in sources :

Example 86 with DbType

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

the class MySqlParameterizedOutputVisitorTest_32 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "/* cds internal mark */select count(*) as count  from (" + "( select env_type from `tmall_miaoscm`.`miao_sale_ledger_0060` where `id` > 1 limit 136 )" + " union all ( select env_type from `tmall_miaoscm`.`miao_sale_ledger_0060` where `id` > 2331 limit 136 )" + ") as miao_sale_ledger_0060 where `miao_sale_ledger_0060`.`env_type` = 3";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT count(*) AS count\n" + "FROM (\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > ?\n" + "\tLIMIT ?)\n" + "\tUNION ALL\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > ?\n" + "\tLIMIT ?)\n" + ") miao_sale_ledger_0060\n" + "WHERE `miao_sale_ledger_0060`.`env_type` = ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    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(5, parameters.size());
    // SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
    // List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement pstmt = SQLUtils.parseStatements(psql, dbType).get(0);
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("udata", "udata_0888");
    visitor1.setInputParameters(visitor.getParameters());
    pstmt.accept(visitor1);
    assertEquals("SELECT count(*) AS count\n" + "FROM (\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > 1\n" + "\tLIMIT 136)\n" + "\tUNION ALL\n" + "\t(SELECT env_type\n" + "\tFROM `tmall_miaoscm`.miao_sale_ledger\n" + "\tWHERE `id` > 2331\n" + "\tLIMIT 136)\n" + ") miao_sale_ledger_0060\n" + "WHERE `miao_sale_ledger_0060`.`env_type` = 3", 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 87 with DbType

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

the class MySqlParameterizedOutputVisitorTest_34 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where id = 1 or id = 2 or id = 3";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setOutputParameters(parameters);
    visitor.setExportTables(true);
    /*visitor.setPrettyFormat(false);*/
    SQLStatement stmt = stmtList.get(0);
    stmt.accept(visitor);
    // System.out.println(parameters);
    assertEquals(1, parameters.size());
    // SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
    // List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement pstmt = SQLUtils.parseStatements(psql, dbType).get(0);
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("udata", "udata_0888");
    visitor1.setInputParameters(visitor.getParameters());
    pstmt.accept(visitor1);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id IN (1, 2, 3)", 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 88 with DbType

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

the class MySqlParameterizedOutputVisitorTest_38 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT lower(hex(file_md5)) as file_md5,\n" + "        lower(hex(thumb)) as thumb,st\n" + "        FROM t_f_p_thumb\n" + "        WHERE file_md5 = x'84C1F969587F5FD1942148EE9D36A0FB'";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    // List<Object> parameters = new ArrayList<Object>();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    // visitor.setParameters(parameters);
    visitor.setExportTables(true);
    visitor.setPrettyFormat(true);
    statement.accept(visitor);
    assertEquals("SELECT lower(hex(file_md5)) AS file_md5\n" + "\t, lower(hex(thumb)) AS thumb, st\n" + "FROM t_f_p_thumb\n" + "WHERE file_md5 = ?", out.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 89 with DbType

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

the class MySqlParameterizedOutputVisitorTest_39 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where 1 = 1 or id = 3";
    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.MYSQL);
    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 *\n" + "FROM t\n" + "WHERE 1 = 1\n" + "\tOR id = ?", 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("[3]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE 1 = 1\n" + "\tOR id = 3", 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 90 with DbType

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

the class MySqlParameterizedOutputVisitorTest_40 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where 1 <> 1 or id = 3";
    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.MYSQL);
    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 *\n" + "FROM t\n" + "WHERE 1 <> 1\n" + "\tOR id = ?", 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("[3]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE 1 <> 1\n" + "\tOR id = 3", 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)

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