Files
Druid/src/test/java/com/alibaba/druid/sql/OnlineSQLTest.java
T
wenshao 413be781fb 代码重构
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@280 b9813039-fb51-4c41-a8b9-e21c2acb5095
2011-06-23 01:48:21 +00:00

166 lines
5.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.alibaba.druid.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisitor;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleParameterizedOutputVisitor;
public class OnlineSQLTest extends TestCase {
private String url = "jdbc:mysql://10.20.129.146/dragoon_v25monitordb_online";
private String user = "dragoon";
private String password = "dragoon";
public void test_list_sql() throws Exception {
// reset();
// 这些中文括号
//update(7216, "", 4);
//update(7223, "", 4);
//update(8387, "", 4);
// 语法错误
// update(17018, "", 4); //alarm_type&?
// update(17841, "", 4); //alarm_type&?
// update(17845, "", 4); //alarm_type&?
// update(18247, "", 4); //alarm_type&?
// update(19469, "", 4); //alarm_type&?
update(19730, "", 4); //alarm_type&?
update(20164, "", 4); //alarm_type&?
update(20386, "", 4); //alarm_type&?
update(20440, "", 4); //alarm_type&?
update(21208, "", 4); //alarm_type&?
// IBATIS NAME
update(18035, "", 4); //alarm_type&?
Connection conn = DriverManager.getConnection(url, user, password);
int count = 0;
String sql = "SELECT id, value FROM m_sql_const WHERE flag IS NULL LIMIT 100";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String value = rs.getString(2);
if (value.indexOf('(') != -1) {
update(id, "", 4);
continue;
}
System.out.println(value);
System.out.println();
try {
validateOracle(id, value);
} catch (Exception e) {
e.printStackTrace();
System.out.println("id : " + id);
continue;
}
count++;
}
rs.close();
stmt.close();
System.out.println("COUNT : " + count);
conn.close();
}
void reset() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "UPDATE m_sql_const SET flag = NULL, value2 = NULL";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
stmt.close();
conn.close();
}
void update(int id, String value2, int flag) throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "UPDATE m_sql_const SET flag = ?, value2 = ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, flag);
stmt.setString(2, value2);
stmt.setInt(3, id);
stmt.executeUpdate();
stmt.close();
conn.close();
}
void validate(int id, String sql) throws Exception {
sql = sql.trim();
boolean sqlFlag = false;
String lowerSql = sql.toLowerCase();
if (lowerSql.startsWith("insert") || lowerSql.startsWith("select") || lowerSql.startsWith("upate") || lowerSql.startsWith("delete")
|| lowerSql.startsWith("create") || lowerSql.startsWith("drop")) {
sqlFlag = true;
}
if (!sqlFlag) {
update(id, sql, 2);
return;
}
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
Assert.assertEquals(1, statementList.size());
StringBuilder out = new StringBuilder();
MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
statemen.accept(visitor);
update(id, out.toString(), 1);
System.out.println(sql);
System.out.println(out.toString());
}
void validateOracle(int id, String sql) throws Exception {
sql = sql.trim();
boolean sqlFlag = false;
String lowerSql = sql.toLowerCase();
if (lowerSql.startsWith("insert") || lowerSql.startsWith("select") || lowerSql.startsWith("upate") || lowerSql.startsWith("delete")
|| lowerSql.startsWith("create") || lowerSql.startsWith("drop")) {
sqlFlag = true;
}
if (!sqlFlag) {
update(id, sql, 2);
return;
}
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
Assert.assertEquals(1, statementList.size());
StringBuilder out = new StringBuilder();
OracleParameterizedOutputVisitor visitor = new OracleParameterizedOutputVisitor(out);
statemen.accept(visitor);
update(id, out.toString(), 1);
System.out.println(sql);
System.out.println(out.toString());
}
}