Search in sources :

Example 41 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class ExportParameterInTest method test_exportParameter.

public void test_exportParameter() throws Exception {
    String sql = "select * from t_user where oid = '102' and uid in (1, 2, 3)";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    assertEquals(1, stmtList.size());
    SQLStatement stmt = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    List<Object> parameters = new ArrayList<Object>();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    stmt.accept(visitor);
    System.out.println(out);
    System.out.println(JSON.toJSONString(parameters));
    restore(out.toString(), parameters);
}
Also used : ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 42 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class ExportParameterShardingTest method test_exportParameter.

public void test_exportParameter() throws Exception {
    String sql = "select * from t_user_0000 where oid = 1001";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    assertEquals(1, stmtList.size());
    SQLStatement stmt = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    List<Object> parameters = new ArrayList<Object>();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    stmt.accept(visitor);
    System.out.println(out);
    System.out.println(JSON.toJSONString(parameters));
    String restoredSql = restore(out.toString(), parameters);
    assertEquals("SELECT *\n" + "FROM t_user_0000\n" + "WHERE oid = 1001", restoredSql);
}
Also used : ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 43 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MTSParserTest method test_mts_2.

public void test_mts_2() throws Exception {
    String sql = "  savepoInt `select`";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals("SAVEPOINT `select`", output);
}
Also used : MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 44 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MTSParserTest method test_lockTable.

public void test_lockTable() throws Exception {
    String sql = "LOCK TABLES t1 READ;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals("LOCK TABLES t1 READ", output);
}
Also used : MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 45 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MTSParserTest method test_mts_3.

public void test_mts_3() throws Exception {
    String sql = "Release sAVEPOINT xx   ";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals("RELEASE SAVEPOINT xx", output);
}
Also used : MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1652 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)915 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)431 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)346 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)273 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)254 Column (com.alibaba.druid.stat.TableStat.Column)122 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)109 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)107 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)100 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)60 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)46 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)44 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)34 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)33 Test (org.junit.Test)31 MysqlTest (com.alibaba.druid.sql.MysqlTest)29 ArrayList (java.util.ArrayList)29 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)28