mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修正输出格式
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@159 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -2,7 +2,9 @@ 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;
|
||||
|
||||
@@ -20,20 +22,23 @@ public class OnlineSQLTest extends TestCase {
|
||||
private String password = "dragoon";
|
||||
|
||||
public void test_list_sql() throws Exception {
|
||||
//reset();
|
||||
Connection conn = DriverManager.getConnection(url, user, password);
|
||||
|
||||
int count = 0;
|
||||
String sql = "SELECT id, value FROM m_sql_const";
|
||||
String sql = "SELECT id, value FROM m_sql_const WHERE flag IS NULL LIMIT 1";
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt(1);
|
||||
String value = rs.getString(2);
|
||||
System.out.println(value);
|
||||
System.out.println();
|
||||
try {
|
||||
validate(value);
|
||||
validate(id, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
@@ -44,8 +49,43 @@ public class OnlineSQLTest extends TestCase {
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void reset() throws SQLException {
|
||||
Connection conn = DriverManager.getConnection(url, user, password);
|
||||
|
||||
void validate(String sql) throws Exception {
|
||||
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) throws SQLException {
|
||||
Connection conn = DriverManager.getConnection(url, user, password);
|
||||
|
||||
String sql = "UPDATE m_sql_const SET flag = 1, value2 = ? WHERE id = ?";
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, value2);
|
||||
stmt.setInt(2, 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);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -58,6 +98,7 @@ public class OnlineSQLTest extends TestCase {
|
||||
MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
update(id, out.toString());
|
||||
System.out.println(sql);
|
||||
System.out.println(out.toString());
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user