Search in sources :

Example 91 with DbType

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

the class MySqlParameterizedOutputVisitorTest_42 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "UPDATE offline_file_user" + " SET sended_file_num = sended_file_num-1, sended_flie_total_size = sended_flie_total_size-19039064" + " WHERE login_id = ?";
    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();
    System.out.println(psql);
    assertEquals("UPDATE offline_file_user\n" + "SET sended_file_num = sended_file_num - ?, sended_flie_total_size = sended_flie_total_size - ?\n" + "WHERE login_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("[1,19039064]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("UPDATE offline_file_user\n" + "SET sended_file_num = sended_file_num - 1, sended_flie_total_size = sended_flie_total_size - 19039064\n" + "WHERE login_id = ?", 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 92 with DbType

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

the class MySqlParameterizedOutputVisitorTest_24 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `xxx_reverse_order`.`id` from xxx_reverse_order_0446 xxx_reverse_order limit 1;";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_order`.`id`\n" + "FROM xxx_reverse_order xxx_reverse_order\n" + "LIMIT ?;", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, 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(0, parameters.size());
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("xxx_reverse_order", "xxx_reverse_order_0446");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `xxx_reverse_order`.`id`\n" + "FROM xxx_reverse_order_0446 xxx_reverse_order\n" + "LIMIT ?;", 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 93 with DbType

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

the class MySqlParameterizedOutputVisitorTest_26 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `xxx_reverse_od`.`id` from xxx_reverse_od_0446 `xxx_reverse_od` where `xxx_reverse_od`.name=1 limit 1;";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_od`.`id`\n" + "FROM xxx_reverse_od `xxx_reverse_od`\n" + "WHERE `xxx_reverse_od`.name = ?\n" + "LIMIT ?;", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, 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(0, parameters.size());
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("xxx_reverse_od", "xxx_reverse_od_0446");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `xxx_reverse_od`.`id`\n" + "FROM xxx_reverse_od_0446 `xxx_reverse_od`\n" + "WHERE `xxx_reverse_od`.name = ?\n" + "LIMIT ?;", 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 94 with DbType

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

the class MySqlParameterizedOutputVisitorTest_20 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select `a_entry_2664`.`id` from `a_entry_2664`";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT a_entry.`id`\n" + "FROM a_entry", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, 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(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 a_entry_2664.`id`\n" + "FROM a_entry_2664", 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 95 with DbType

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

the class MySqlParameterizedOutputVisitorTest_23 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select `wmc_xxx_s`.fid from `wmc_xxx_s_0479` as `wmc_xxx_s` where `wmc_xxx_s`.fid = 3";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `wmc_xxx_s`.fid\n" + "FROM wmc_xxx_s `wmc_xxx_s`\n" + "WHERE `wmc_xxx_s`.fid = ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, 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(0, parameters.size());
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("wmc_xxx_s", "wmc_xxx_s_0479");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `wmc_xxx_s`.fid\n" + "FROM wmc_xxx_s_0479 `wmc_xxx_s`\n" + "WHERE `wmc_xxx_s`.fid = ?", 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)

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