Search in sources :

Example 46 with DbType

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);
}
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 47 with DbType

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);
}
Also used : DbType(com.alibaba.druid.DbType)

Example 48 with DbType

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));
}
Also used : ArrayList(java.util.ArrayList) DbType(com.alibaba.druid.DbType)

Example 49 with DbType

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));
}
Also used : ArrayList(java.util.ArrayList) DbType(com.alibaba.druid.DbType)

Example 50 with DbType

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));
}
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