mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for oracle sql parser
This commit is contained in:
@@ -5,9 +5,13 @@ import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import oracle.jdbc.OracleStatement;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
@@ -22,6 +26,8 @@ public class TestTransform extends OracleTest {
|
||||
private String password;
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
public void setUp() throws Exception {
|
||||
jdbcUrl = "jdbc:oracle:thin:@10.20.149.18:1521:emdb";
|
||||
user = "wardon";
|
||||
@@ -32,26 +38,104 @@ public class TestTransform extends OracleTest {
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
dataSource.setMaxActive(50);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setMaxOpenPreparedStatements(100);
|
||||
}
|
||||
|
||||
public void test_transform() throws Exception {
|
||||
public void f_test_createIndex() throws Exception {
|
||||
String sql = "CREATE INDEX i_db_day_sql_fulltext on db_day_sql_fulltext (sql_id)";
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
stmt.execute(sql);
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
private int updateCount = 0;
|
||||
|
||||
public void updateRecord(String sqlId, String result) throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement("UPDATE db_day_sql_fulltext SET SQL_PARSE_RESULT = ? WHERE sql_id = ?");
|
||||
|
||||
stmt.setString(1, result);
|
||||
stmt.setString(2, sqlId);
|
||||
|
||||
int updateCount = stmt.executeUpdate();
|
||||
if (updateCount != 1) {
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
|
||||
System.out.println((this.updateCount++) + " : " + sqlId);
|
||||
}
|
||||
|
||||
public void f_test_transform() throws Exception {
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
int count = 0;
|
||||
{
|
||||
String sql = "SELECT COUNT(*) FROM db_day_sql_fulltext WHERE SQL_PARSE_RESULT IS NULL";
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
rs.next();
|
||||
count = rs.getInt(1);
|
||||
rs.close();
|
||||
stmt.close();
|
||||
}
|
||||
System.out.println("COUNT : " + count);
|
||||
|
||||
String sql = "SELECT SNAP_DATE, DBNAME, SQL_ID, PIECE, SQL_TEXT" + //
|
||||
" , COMMAND_TYPE, LAST_SNAP_DATE, DB_PK, SQL_PARSE_RESULT " + //
|
||||
" FROM db_day_sql_fulltext " + //
|
||||
" WHERE SQL_PARSE_RESULT IS NULL" + //
|
||||
" ORDER BY sql_id, piece";
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
OracleStatement oracleStmt = stmt.unwrap(OracleStatement.class);
|
||||
oracleStmt.setRowPrefetch(100);
|
||||
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
Record r = null;
|
||||
|
||||
|
||||
int i = 0;
|
||||
while (rs.next()) {
|
||||
r = new Record();
|
||||
|
||||
|
||||
i++;
|
||||
|
||||
Date d1 = rs.getDate(1);
|
||||
String s2 = rs.getString(2);
|
||||
String sqlId = rs.getString(3);
|
||||
|
||||
|
||||
if (sqlId.equals("018vcjmqxuguv")) {
|
||||
continue;
|
||||
}
|
||||
if (sqlId.equals("0d69js78vuh8j")) {
|
||||
continue;
|
||||
}
|
||||
if (sqlId.equals("0khp9aykj08vq")) {
|
||||
continue;
|
||||
}
|
||||
if (sqlId.equals("0ns0zv7qaxzus")) {
|
||||
continue;
|
||||
}
|
||||
if (sqlId.equals("0tg8ryp81gq1w")) {
|
||||
continue;
|
||||
}
|
||||
if (sqlId.equals("0tvydgpa3jmkv")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
r.setSnapshotDate(d1);
|
||||
r.setDbName(s2);
|
||||
r.setSqlId(sqlId);
|
||||
@@ -61,82 +145,170 @@ public class TestTransform extends OracleTest {
|
||||
r.setCommandType(rs.getInt(6));
|
||||
r.setLastSnapshotDate(rs.getDate(7));
|
||||
r.setDbPk(rs.getLong(8));
|
||||
|
||||
System.out.println(r.getSqlText());
|
||||
|
||||
// System.out.println(i + "(" + r.getDbName() + "/" + r.getSqlId() + ") : " + r.getSqlText());
|
||||
schemaStat(r);
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void schemaStat(Record r) {
|
||||
|
||||
public void schemaStat(final Record r) throws Exception {
|
||||
executor.submit(new Runnable() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
schemaStatInternal(r);
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void schemaStatInternal(Record r) throws Exception {
|
||||
String sql = r.getSqlText();
|
||||
|
||||
|
||||
if (sql.startsWith("/* MV_REFRESH (INS) */")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("/* MV_REFRESH (DEL) */")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (sql.startsWith("grant") || sql.startsWith("GRANT")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("SELECT /*+ Q1630000 NO_EXPAND ROWID(A1) */ A1.\"PRODUCT_ID\",A1.\"SUMMARY\"")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("declare") || sql.startsWith("DECLARE")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("alter table")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("SELECT /* OPT_DYN_SAMP */")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("COMMENT")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("create index")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("alter index")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("create table")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("CREATE")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("explain plan")) {
|
||||
return;
|
||||
}
|
||||
if (sql.startsWith("ALTER TABLE")) {
|
||||
return;
|
||||
}
|
||||
if (r.getSqlId().equals("09x7ytfdk63sp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
System.out.println(output(statementList));
|
||||
// System.out.println(output(statementList));
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("Tables : " + visitor.getTables().toString());
|
||||
buf.append("\nColumns : " + visitor.getColumns().toString());
|
||||
buf.append("\nCoditions : " + visitor.getConditions().toString());
|
||||
buf.append("\nrelationships " + visitor.getRelationships().toString());
|
||||
|
||||
// System.out.println(buf.toString());
|
||||
|
||||
// System.out.println();
|
||||
// System.out.println();
|
||||
// System.out.println();
|
||||
|
||||
updateRecord(r.getSqlId(), buf.toString());
|
||||
}
|
||||
|
||||
|
||||
public void clearResult() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
|
||||
stmt.execute("DELETE FROM db_day_sql_fulltext");
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void f_test_migrate() throws Exception {
|
||||
public void test_migrate() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
int count = 0;
|
||||
{
|
||||
String sql = "SELECT COUNT(*) FROM db_day_sqltext";
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
rs.next();
|
||||
count = rs.getInt(1);
|
||||
rs.close();
|
||||
stmt.close();
|
||||
}
|
||||
System.out.println("COUNT : " + count);
|
||||
|
||||
clearResult();
|
||||
|
||||
|
||||
String sql = "SELECT SNAP_DATE, DBNAME, SQL_ID, PIECE, SQL_TEXT" + //
|
||||
" , COMMAND_TYPE, LAST_SNAP_DATE, DB_PK, SQL_PARSE_RESULT " + //
|
||||
" FROM db_day_sqltext " + //
|
||||
" WHERE db_pk = 40 and snap_date = trunc(sysdate) " + //
|
||||
" ORDER BY sql_id, piece";
|
||||
Connection conn = dataSource.getConnection();
|
||||
// " WHERE db_pk = 40 and snap_date = trunc(sysdate) " + //
|
||||
" ORDER BY db_pk, sql_id, piece";
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
OracleStatement oracleStmt = stmt.unwrap(OracleStatement.class);
|
||||
oracleStmt.setRowPrefetch(1000);
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
List<Record> list = new ArrayList<Record>();
|
||||
Record r = null;
|
||||
|
||||
String lastSqlId = null;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
while (rs.next()) {
|
||||
j++;
|
||||
Date d1 = rs.getDate(1);
|
||||
String s2 = rs.getString(2);
|
||||
String sqlId = rs.getString(3);
|
||||
|
||||
if (lastSqlId == null || !lastSqlId.equals(sqlId)) {
|
||||
if (r != null) {
|
||||
System.out.println(r.getSqlText());
|
||||
System.out.println((i++) + "/" + j + " : " + r.getSqlId());
|
||||
System.out.println();
|
||||
|
||||
insert(r);
|
||||
try {
|
||||
setInfo(r);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
list.add(r);
|
||||
if (list.size() > 100) {
|
||||
insert(list);
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
|
||||
r = new Record();
|
||||
@@ -152,19 +324,46 @@ public class TestTransform extends OracleTest {
|
||||
} else {
|
||||
String part = rs.getString(5);
|
||||
if (part != null) {
|
||||
r.setSqlText(r.getSqlText() + part);
|
||||
r.appendSqlText(part);
|
||||
}
|
||||
|
||||
}
|
||||
lastSqlId = sqlId;
|
||||
}
|
||||
|
||||
insert(list);
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void insert(Record r) throws Exception {
|
||||
public void setInfo(Record r) {
|
||||
OracleStatementParser parser = new OracleStatementParser(r.getSqlText());
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
// System.out.println(output(statementList));
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("Tables : " + visitor.getTables().toString());
|
||||
buf.append("\nColumns : " + visitor.getColumns().toString());
|
||||
buf.append("\nCoditions : " + visitor.getConditions().toString());
|
||||
buf.append("\nrelationships " + visitor.getRelationships().toString());
|
||||
|
||||
r.setResult(buf.toString());
|
||||
|
||||
}
|
||||
|
||||
public void insert(List<Record> list) throws Exception {
|
||||
if (list.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO db_day_sql_fulltext " + //
|
||||
"(SNAP_DATE, DBNAME, SQL_ID, PIECE, SQL_TEXT" + //
|
||||
", COMMAND_TYPE, LAST_SNAP_DATE, DB_PK, SQL_PARSE_RESULT)" + //
|
||||
@@ -173,18 +372,21 @@ public class TestTransform extends OracleTest {
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
|
||||
stmt.setDate(1, r.getSnapshotDate());
|
||||
stmt.setString(2, r.getDbName());
|
||||
stmt.setString(3, r.getSqlId());
|
||||
stmt.setInt(4, r.getPiece());
|
||||
stmt.setString(5, r.getSqlText());
|
||||
for (Record r : list) {
|
||||
stmt.setDate(1, r.getSnapshotDate());
|
||||
stmt.setString(2, r.getDbName());
|
||||
stmt.setString(3, r.getSqlId());
|
||||
stmt.setInt(4, r.getPiece());
|
||||
stmt.setString(5, r.getSqlText());
|
||||
|
||||
stmt.setInt(6, r.getCommandType());
|
||||
stmt.setDate(7, r.getLastSnapshotDate());
|
||||
stmt.setLong(8, r.getDbPk());
|
||||
stmt.setString(9, r.getSqlText());
|
||||
|
||||
stmt.execute();
|
||||
stmt.setInt(6, r.getCommandType());
|
||||
stmt.setDate(7, r.getLastSnapshotDate());
|
||||
stmt.setLong(8, r.getDbPk());
|
||||
stmt.setString(9, r.getResult());
|
||||
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
|
||||
stmt.close();
|
||||
|
||||
@@ -193,14 +395,23 @@ public class TestTransform extends OracleTest {
|
||||
|
||||
public static class Record {
|
||||
|
||||
private Date snapshotDate;
|
||||
private String dbName;
|
||||
private String sqlId;
|
||||
private String sqlText;
|
||||
private Integer piece;
|
||||
private Integer commandType;
|
||||
private Date lastSnapshotDate;
|
||||
private Long dbPk;
|
||||
private Date snapshotDate;
|
||||
private String dbName;
|
||||
private String sqlId;
|
||||
private StringBuffer sqlText = new StringBuffer();
|
||||
private Integer piece;
|
||||
private Integer commandType;
|
||||
private Date lastSnapshotDate;
|
||||
private Long dbPk;
|
||||
private String result;
|
||||
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public Date getSnapshotDate() {
|
||||
return snapshotDate;
|
||||
@@ -227,11 +438,15 @@ public class TestTransform extends OracleTest {
|
||||
}
|
||||
|
||||
public String getSqlText() {
|
||||
return sqlText;
|
||||
return sqlText.toString();
|
||||
}
|
||||
|
||||
public void setSqlText(String sqlText) {
|
||||
this.sqlText = sqlText;
|
||||
this.sqlText = new StringBuffer(sqlText);
|
||||
}
|
||||
|
||||
public void appendSqlText(String sqlText) {
|
||||
this.sqlText.append(sqlText);
|
||||
}
|
||||
|
||||
public Integer getPiece() {
|
||||
|
||||
Reference in New Issue
Block a user