Search in sources :

Example 16 with DbType

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

the class MySqlParameterizedOutputVisitorTest_37 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "insert into fc_sms_0011_201704 (c1, c2, c3) values (1, 'a', 'b')";
    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(false);
    statement.accept(visitor);
    assertEquals("INSERT INTO fc_sms (c1, c2, c3) VALUES (?, ?, ?)", 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 17 with DbType

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

the class MySqlParameterizedOutputVisitorTest_43 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "UPDATE `feel_07`.feed_item_receive SET `attributes` = ?, `gmt_modified` = ?, `lock_version` = ? WHERE `feed_id` = ?";
    String params = "[\"enableTime:1498682416713,src:top,importFrom:0\",\"2017-06-29 04:40:20\",1,313825887478L]";
    String table = "[\"`feel_07`.`feed_item_receive_0502`\"]";
    String restoredSql = restore(sql, table, params);
    assertEquals("UPDATE `feel_07`.`feed_item_receive_0502`\n" + "SET `attributes` = 'enableTime:1498682416713,src:top,importFrom:0', `gmt_modified` = '2017-06-29 04:40:20', `lock_version` = 1\n" + "WHERE `feed_id` = 313825887478", restoredSql);
}
Also used : DbType(com.alibaba.druid.DbType)

Example 18 with DbType

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

the class MySqlParameterizedOutputVisitorTest_43 method restore.

public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
    JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
    params = StringUtils.replace(params.replaceAll("''", "'"), "\\\"", "\"");
    JSONArray paramsArray = JSON.parseArray(params);
    DbType dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
    List<Object> paramsList = new ArrayList<Object>(paramsArray);
    visitor.setParameters(paramsList);
    SchemaStatVisitor schemaStatVisitor = new MySqlSchemaStatVisitor();
    stmt.accept(schemaStatVisitor);
    JSONArray srcArray = new JSONArray();
    for (Map.Entry<TableStat.Name, TableStat> entry : schemaStatVisitor.getTables().entrySet()) {
        System.out.println(entry.getKey().getName());
        srcArray.add(entry.getKey().getName());
    }
    for (int i = 0; i < srcArray.size(); i++) {
        visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
    }
    stmt.accept(visitor);
    return out.toString();
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) Map(java.util.Map) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 19 with DbType

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

the class MySqlParameterizedOutputVisitorTest_14 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "insert into\n" + "\t\tt_temp (processId,resultId,gmt_create,gmt_modified,result_content,result_number)\n" + "\t\tvalues\n" + "\t\t  \n" + "\t\t\t('4254cc14-1c83-4eaf-95ae-59438dd0cc17', '5fd20fa9-7659-4f8b-a4c2-2021a48317d8', now(),now(),null,null)";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    String s = "INSERT INTO t_temp (processId, resultId, gmt_create, gmt_modified, result_content\n" + "\t, result_number)\n" + "VALUES (?, ?, now(), now(), ?\n" + "\t, ?)";
    assertEquals(s, psql);
    paramaterizeAST(sql, "INSERT INTO t_temp (processId, resultId, gmt_create, gmt_modified, result_content\n" + "\t, result_number)\n" + "VALUES (?, ?, now(), now(), NULL\n" + "\t, NULL)");
}
Also used : DbType(com.alibaba.druid.DbType)

Example 20 with DbType

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

the class MySqlParameterizedOutputVisitorTest_16 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "/* 0bfacfa414829200086238910e/0.3// */" + "insert into `t1` (" + " `f0`, `f1`, `f2`, `f3`, `f4`, " + "`f5`, `f6`, `f7`, `f8`, `f9`, " + "`destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`, " + "`region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)" + " values ( 1, 2, 2, 3, 0, -4, 1, null, '2016-12-28 18:13:28.825', '2016-12-28 18:13:28.825', 1, 1, 0, 1, 0, null, null, null, null, 0)\n";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    assertEquals("INSERT INTO `t1` (`f0`, `f1`, `f2`, `f3`, `f4`\n" + "\t, `f5`, `f6`, `f7`, `f8`, `f9`\n" + "\t, `destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`\n" + "\t, `region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)\n" + "VALUES (?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?\n" + "\t, ?, ?, ?, ?, ?)", psql);
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    assertEquals(20, visitor.getParameters().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) 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