use of com.alibaba.druid.DbType in project druid by alibaba.
the class Oracle_param_1 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "SELECT TO_CHAR(SYSDATE, '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_CHAR(SYSDATE, '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("[]", json);
String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
assertEquals("SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD')\n" + "FROM dual;", rsql);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class PGLimitTest method main.
/**
* @param args
*/
public static void main(String[] args) {
// "postgresql";
DbType dbType = JdbcConstants.POSTGRESQL;
// dbType = "mysql";
String sql = " select * from brandinfo where 1=1 and brandid > 100 order by brandid asc";
String sqlLimit = com.alibaba.druid.sql.PagerUtils.limit(sql, dbType, 2499, 100);
System.out.println("sqlLimit == " + sqlLimit);
String sqlCount = com.alibaba.druid.sql.PagerUtils.count(sql, dbType);
System.out.println("sqlCount == " + sqlCount);
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class SQL_Parser_Parameterize_Test method test_parameterized.
public void test_parameterized() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
List<Object> outParameters = new ArrayList<Object>();
String sql = "select * from t where id = 101 and age = 102 or name = 'wenshao'";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters);
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = ?\n" + "\tAND age = ?\n" + "\tOR name = ?", psql);
assertEquals(3, outParameters.size());
assertEquals(101, outParameters.get(0));
assertEquals(102, outParameters.get(1));
assertEquals("wenshao", outParameters.get(2));
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class SQL_Parser_Parameterize_Test method test_parameterized_2.
public void test_parameterized_2() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
List<Object> outParameters = new ArrayList<Object>();
String sql = "select * from t where id = 101 or id in (1,2,3,4)";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters, VisitorFeature.OutputParameterizedQuesUnMergeInList);
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = ?\n" + "\tOR id IN (?, ?, ?, ?)", psql);
assertEquals(5, outParameters.size());
assertEquals(101, outParameters.get(0));
assertEquals(1, outParameters.get(1));
assertEquals(2, outParameters.get(2));
assertEquals(3, outParameters.get(3));
assertEquals(4, outParameters.get(4));
}
use of com.alibaba.druid.DbType in project druid by alibaba.
the class MySqlParameterizedOutputVisitorTest_8 method test_parameterize.
public void test_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "insert into test values(2,1) on duplicate key update ts=ts % 10000 +1";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
String expected = "INSERT INTO test\n" + "VALUES (?, ?)\n" + "ON DUPLICATE KEY UPDATE ts = ts % ? + ?";
Assert.assertEquals(expected, psql);
paramaterizeAST(sql, expected);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
StringBuilder out = new StringBuilder();
ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
Assert.assertEquals(4, visitor.getParameters().size());
Assert.assertEquals(2, visitor.getParameters().get(0));
Assert.assertEquals(1, visitor.getParameters().get(1));
Assert.assertEquals(10000, visitor.getParameters().get(2));
Assert.assertEquals(1, visitor.getParameters().get(3));
}
Aggregations