mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.alibaba.druid.bvt.sql;
|
||||
|
||||
import com.alibaba.druid.sql.parser.SymbolTable;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SymbolTableTest extends TestCase {
|
||||
public void test_symbols() throws Exception {
|
||||
SymbolTable symbols = new SymbolTable(65535);
|
||||
|
||||
String[] strings = new String[10];
|
||||
for (int i = 0; i < strings.length; ++i) {
|
||||
String str = "abc" + i;
|
||||
strings[i] = str;
|
||||
symbols.addSymbol(str, FnvHash.fnv1a_64(str));
|
||||
}
|
||||
|
||||
for (int i = 0; i < strings.length; ++i) {
|
||||
String str = strings[i];
|
||||
long hash = FnvHash.fnv1a_64(str);
|
||||
String symbol = symbols.findSymbol(hash);
|
||||
assertSame(str, symbol);
|
||||
}
|
||||
{
|
||||
byte[] bytes = "kkk#abc0#aa".getBytes();
|
||||
long hash = FnvHash.fnv1a_64(bytes, 4, 8);
|
||||
String symbol = symbols.addSymbol(bytes, 4, 8, hash);
|
||||
assertSame(strings[0], symbol);
|
||||
}
|
||||
|
||||
byte[] bytes = "xab#time:3333".getBytes();
|
||||
System.out.println(indexOfTime(bytes, 1));
|
||||
System.out.println("xab#time:3333".indexOf("#time"));
|
||||
}
|
||||
|
||||
public static int indexOfTime(byte[] bytes, int fromIndex) {
|
||||
int end = bytes.length - 5;
|
||||
for (int i = fromIndex; i < end; ++i) {
|
||||
if (bytes[i] == '#'
|
||||
&& bytes[i + 1] == 't'
|
||||
&& bytes[i + 2] == 'i'
|
||||
&& bytes[i + 3] == 'm'
|
||||
&& bytes[i + 4] == 'e') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
package com.alibaba.druid.bvt.sql;
|
||||
|
||||
import com.alibaba.druid.sql.parser.SymbolTable;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by yunning on 16/7/10.
|
||||
*/
|
||||
public class TestNewSpout {
|
||||
|
||||
|
||||
public static int indexOf_nsharp(byte[] bytes, int fromIndex) {
|
||||
int end = bytes.length - 2;
|
||||
for (int i = fromIndex; i < end; ++i) {
|
||||
if (bytes[i] == '\n'
|
||||
&& bytes[i + 1] == '#') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int indexOf_nReturn(byte[] bytes, int fromIndex) {
|
||||
for (int i = fromIndex; i < bytes.length; ++i) {
|
||||
if (bytes[i] == '\n') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int indexOfTime(byte[] bytes, int fromIndex) {
|
||||
int end = bytes.length - 5;
|
||||
for (int i = fromIndex; i < end; ++i) {
|
||||
if (bytes[i] == '#'
|
||||
&& bytes[i + 1] == 't'
|
||||
&& bytes[i + 2] == 'i'
|
||||
&& bytes[i + 3] == 'm'
|
||||
&& bytes[i + 4] == 'e') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static final SymbolTable dbSymbols = new SymbolTable(1024 * 256);
|
||||
|
||||
public static String substring(byte[] bytes, int from, int to) {
|
||||
int newLength = to - from;
|
||||
byte[] copy = new byte[newLength];
|
||||
System.arraycopy(bytes, from, copy, 0, newLength);
|
||||
return new String(copy, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final String datass = "#time:1470191976686205\n" +
|
||||
"#user@host:monitor[monitor] @ [127.0.0.1]\n" +
|
||||
"#db:test\n" +
|
||||
"#table_name:aa\n" +
|
||||
"set session time_zone='+08:00';\n" +
|
||||
"#query_time:0.000028123456789987654321\n" +
|
||||
"#lock_time:0.000000\n" +
|
||||
"#rows_sent:0\n" +
|
||||
"#rows_examined:0\n" +
|
||||
"#rows_affected:0\n" +
|
||||
"#innodb_pages_read:1\n" +
|
||||
"#innodb_pages_io_read:2\n" +
|
||||
"#id:55438\n" +
|
||||
"#time:1470191976686207\n" +
|
||||
"#user@host:monitor[monitor] @ [127.0.0.1]\n" +
|
||||
"#db:test\n" +
|
||||
"#table_name:aa\n" +
|
||||
"#time:1470191976686206\n" +
|
||||
"#user@host:monitor[monitor] @ [127.0.0.1]\n" +
|
||||
"#db:test\n" +
|
||||
"#table_name:aa\n" +
|
||||
"set session time_zone='+0800';\n" +
|
||||
"#query_time:0.000028123456789987654322\n" +
|
||||
"#lock_time:0.000000\n" +
|
||||
"#rows_sent:0\n" +
|
||||
"#rows_examined:0\n" +
|
||||
"#rows_affected:0\n" +
|
||||
"#innodb_pages_read:0\n" +
|
||||
"#innodb_pages_io_read:0\n" +
|
||||
"#id:123";
|
||||
|
||||
byte[] datas = datass.getBytes();
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < 1; i++) {
|
||||
final int len = datas.length;
|
||||
int offset = indexOfTime(datas, 0);
|
||||
long now = System.nanoTime();
|
||||
while (offset != -1) {
|
||||
int rowOffset = offset;
|
||||
int k = 0;
|
||||
final int[] offsets = new int[12];
|
||||
boolean isNoFirst = false;
|
||||
while (rowOffset != -1 && k < 12) {
|
||||
if (isNoFirst && rowOffset + 5 <= len - 1) {
|
||||
if (datas[rowOffset + 2] == 't' &&
|
||||
datas[rowOffset + 3] == 'i' &&
|
||||
datas[rowOffset + 4] == 'm' &&
|
||||
datas[rowOffset + 5] == 'e') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
offsets[k] = rowOffset;
|
||||
rowOffset = indexOf_nsharp(datas, rowOffset + 1);
|
||||
k = k + 1;
|
||||
isNoFirst = true;
|
||||
}
|
||||
offset = rowOffset == -1 ? -1 : indexOfTime(datas, rowOffset + 1);
|
||||
if (k < 12) {
|
||||
continue;
|
||||
}
|
||||
SqlInfo sqlInfo = new SqlInfo();
|
||||
// sqlInfo.setHostName(hostName);
|
||||
|
||||
boolean isError = false;
|
||||
String sql = null;
|
||||
|
||||
//time
|
||||
int currItemOffset = offsets[0];
|
||||
int nextItemOffset = offsets[1];
|
||||
/* String item = datas.substring(currItemOffset + 6, nextItemOffset);
|
||||
long itemTime = Long.parseLong(item);*/
|
||||
long itemTime = MathUtil.str2Long(datas, currItemOffset + 6, nextItemOffset);
|
||||
System.out.println("TIME : " + itemTime);
|
||||
if (itemTime < 0 || itemTime > 2000000000000000L) {
|
||||
}
|
||||
sqlInfo.setTime(itemTime);
|
||||
|
||||
//user@host
|
||||
currItemOffset = offsets[1];
|
||||
nextItemOffset = offsets[2];
|
||||
String item = substring(datas, currItemOffset + 12, nextItemOffset);
|
||||
System.out.println("user@host : " + item);
|
||||
// sqlInfo.setUserHost(item);
|
||||
|
||||
//db
|
||||
currItemOffset = offsets[2];
|
||||
nextItemOffset = offsets[3];
|
||||
int dbOffset = currItemOffset + 5;
|
||||
int dbLen = nextItemOffset - dbOffset;
|
||||
long dbHash = FnvHash.fnv1a_64(datas, dbOffset, nextItemOffset);
|
||||
String db = dbSymbols.addSymbol(datas, dbOffset, dbLen, dbHash);
|
||||
|
||||
// String db = substring(datas, currItemOffset + 5, nextItemOffset);
|
||||
System.out.println("db : " + db);
|
||||
if (dbLen > 0 && SqlUtil.filterDbs.indexOf(db) != -1) {
|
||||
// sqlInfo.setNotValiDB(true);
|
||||
} else {
|
||||
}
|
||||
|
||||
//table_name
|
||||
currItemOffset = offsets[3];
|
||||
nextItemOffset = offsets[4];
|
||||
int tsqlPos = currItemOffset + 13;
|
||||
int idx2 = indexOf_nReturn(datas, tsqlPos);
|
||||
String table = substring(datas, tsqlPos, idx2);
|
||||
sql = substring(datas, idx2 + 1, nextItemOffset);
|
||||
System.out.println("table : " + table + " : " + sql);
|
||||
|
||||
int totalLen = sql.length();
|
||||
if (totalLen < 9 ||
|
||||
totalLen >= 8000) {
|
||||
// sqlInfo.setParseRes(0);
|
||||
} else {
|
||||
}
|
||||
|
||||
//query_time
|
||||
currItemOffset = offsets[4];
|
||||
nextItemOffset = offsets[5];
|
||||
int rtBound = Math.min(nextItemOffset - currItemOffset, 24);
|
||||
double itemRt = MathUtil.str2Double(datas, currItemOffset + 13, currItemOffset + rtBound);
|
||||
sqlInfo.setQueryTime(itemRt);
|
||||
|
||||
//lock_time
|
||||
currItemOffset = offsets[5];
|
||||
nextItemOffset = offsets[6];
|
||||
int lockTimeBound = Math.min(nextItemOffset - currItemOffset, 23);
|
||||
double itemLockTime = MathUtil.str2Double(datas, currItemOffset + 12, currItemOffset + lockTimeBound);
|
||||
sqlInfo.setLockTime(itemLockTime);
|
||||
|
||||
//rows_sent
|
||||
currItemOffset = offsets[6];
|
||||
nextItemOffset = offsets[7];
|
||||
long itemRowsSent = MathUtil.str2Long(datas, currItemOffset + 12, nextItemOffset);
|
||||
if (itemRowsSent < 0 || itemRowsSent > 1000000000) {
|
||||
}
|
||||
// sqlInfo.setRowsSent(itemRowsSent);
|
||||
|
||||
//rows_examined
|
||||
currItemOffset = offsets[7];
|
||||
nextItemOffset = offsets[8];
|
||||
long itemRowsExam = MathUtil.str2Long(datas, currItemOffset + 16, nextItemOffset);
|
||||
if (itemRowsExam < 0 || itemRowsExam > 1000000000) {
|
||||
}
|
||||
// sqlInfo.setRowsExamined(itemRowsExam);
|
||||
|
||||
//rows_affected
|
||||
currItemOffset = offsets[8];
|
||||
nextItemOffset = offsets[9];
|
||||
long itemRowsAffected = MathUtil.str2Long(datas, currItemOffset + 16, nextItemOffset);
|
||||
if (itemRowsAffected < 0 || itemRowsAffected > 1000000000) {
|
||||
}
|
||||
sqlInfo.setRowsAffected(itemRowsAffected);
|
||||
|
||||
//innodb_pages_read
|
||||
currItemOffset = offsets[9];
|
||||
nextItemOffset = offsets[10];
|
||||
long itemPageRead = MathUtil.str2Long(datas, currItemOffset + 20, nextItemOffset);
|
||||
if (itemPageRead < 0 || itemPageRead > 1000000000) {
|
||||
}
|
||||
sqlInfo.setInnodbPagesRead(itemPageRead);
|
||||
|
||||
//innodb_pages_io_read
|
||||
currItemOffset = offsets[10];
|
||||
nextItemOffset = offsets[11];
|
||||
long itemPageIORead = MathUtil.str2Long(datas, currItemOffset + 23, nextItemOffset);
|
||||
if (itemPageIORead < 0 || itemPageIORead > 1000000000) {
|
||||
}
|
||||
// sqlInfo.setInnodbPagesIoRead(itemPageIORead);
|
||||
// currItemOffset = nextItemOffset;
|
||||
|
||||
/*nextItemOffset = datas.indexOf("\n#", currItemOffset + 1);
|
||||
if (nextItemOffset >= end) {
|
||||
return null;
|
||||
}
|
||||
itemEnd = nextItemOffset == -1 ? end : nextItemOffset;
|
||||
item = datas.substring(currItemOffset + 1, itemEnd);
|
||||
sqlInfo.setId(Long.parseLong(item.trim().substring(4)));
|
||||
num++;*/
|
||||
// sqlInfo.setId(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println(System.currentTimeMillis() - start);
|
||||
}
|
||||
|
||||
static class SqlInfo {
|
||||
private long innodbPagesRead;
|
||||
private long time;
|
||||
private double queryTime;
|
||||
private double lockTime;
|
||||
public long rowsAffected;
|
||||
|
||||
public long getInnodbPagesRead() {
|
||||
return innodbPagesRead;
|
||||
}
|
||||
|
||||
public void setInnodbPagesRead(long innodbPagesRead) {
|
||||
this.innodbPagesRead = innodbPagesRead;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public void setQueryTime(double queryTime) {
|
||||
this.queryTime = queryTime;
|
||||
}
|
||||
|
||||
public void setLockTime(double lockTime) {
|
||||
this.lockTime = lockTime;
|
||||
}
|
||||
|
||||
public void setRowsAffected(long rowsAffected) {
|
||||
this.rowsAffected = rowsAffected;
|
||||
}
|
||||
}
|
||||
|
||||
static class MathUtil {
|
||||
public static long str2Long(byte[] data, int start, int end) {
|
||||
long value = 0;
|
||||
for (int i = start; i < end; ++i) {
|
||||
byte ch = data[i];
|
||||
if (ch == '.') break;
|
||||
int digit = data[i] - '0';
|
||||
value = value * 10 + digit;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static double str2Double(byte[] data,int start,int end) {
|
||||
long value = 0;
|
||||
long power = 0;
|
||||
for (int i = start; i < end; ++i) {
|
||||
byte ch = data[i];
|
||||
if (ch == '.') {
|
||||
power = 1;
|
||||
continue;
|
||||
}
|
||||
int digit = ch - '0';
|
||||
value = value * 10 + digit;
|
||||
power *= 10;
|
||||
}
|
||||
double doubleValue = (double) value;
|
||||
if (power == 0) {
|
||||
return doubleValue;
|
||||
} else {
|
||||
return doubleValue / power;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class SqlUtil {
|
||||
|
||||
public static Set<String> filterDb = new HashSet<String>();
|
||||
|
||||
private static int[] orders = new int[]{2, 3, 0, 1};
|
||||
|
||||
static {
|
||||
filterDb.add("DRC");
|
||||
filterDb.add("INFORMATION_SCHEMA");
|
||||
filterDb.add("INNODB_MEMCACHE");
|
||||
filterDb.add("MYSQL");
|
||||
filterDb.add("PERFORMANCE_SCHEMA");
|
||||
filterDb.add("RECYCLE_BIN");
|
||||
filterDb.add("ROCKSDB");
|
||||
filterDb.add("TEST");
|
||||
filterDb.add("DRC".toLowerCase());
|
||||
filterDb.add("INFORMATION_SCHEMA".toLowerCase());
|
||||
filterDb.add("INNODB_MEMCACHE".toLowerCase());
|
||||
filterDb.add("MYSQL".toLowerCase());
|
||||
filterDb.add("PERFORMANCE_SCHEMA".toLowerCase());
|
||||
filterDb.add("RECYCLE_BIN".toLowerCase());
|
||||
filterDb.add("ROCKSDB".toLowerCase());
|
||||
filterDb.add("TEST".toLowerCase());
|
||||
}
|
||||
|
||||
public static final String filterDbs = org.apache.commons.lang3.StringUtils.join(SqlUtil.filterDb, ",");
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_60_firstComment extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "/* xxx * xxxx */SELECT * from t where t ! = 1";
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != 1", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select *\n" +
|
||||
"from t\n" +
|
||||
"where t != 1", //
|
||||
output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != ?", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_61_firstComment_2 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "/* xxx * xxxx */ SELECT * from t where t ! = 1";
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != 1", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select *\n" +
|
||||
"from t\n" +
|
||||
"where t != 1", //
|
||||
output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != ?", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_62_optimized extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT * from t where t ! = 1";
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != 1", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
assertEquals("select *\n" +
|
||||
"from t\n" +
|
||||
"where t != 1", //
|
||||
output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE t != ?", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.postgresql.select;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGSelectTest57 extends TestCase {
|
||||
private final String dbType = JdbcConstants.POSTGRESQL;
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT ts#, file#, block#, cols, IFNULL(size$, -1), pctfree$, pctused$\n" +
|
||||
" , initrans, maxtrans, hashkeys, func, extind\n" +
|
||||
" , avgchn, IFNULL(degree, 1)\n" +
|
||||
" , IFNULL(instances, 1)\n" +
|
||||
" , IFNULL(flags, 0)\n" +
|
||||
" , IFNULL(spare1, 0)\n" +
|
||||
"FROM clu$\n" +
|
||||
"WHERE obj# ='633'";
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
assertEquals("SELECT ts#, file#, block#, cols\n" +
|
||||
"\t, IFNULL(size$, -1), pctfree$\n" +
|
||||
"\t, pctused$, initrans, maxtrans, hashkeys, func\n" +
|
||||
"\t, extind, avgchn, IFNULL(degree, 1)\n" +
|
||||
"\t, IFNULL(instances, 1)\n" +
|
||||
"\t, IFNULL(flags, 0)\n" +
|
||||
"\t, IFNULL(spare1, 0)\n" +
|
||||
"FROM clu$\n" +
|
||||
"WHERE obj# = '633'", SQLUtils.toPGString(stmt));
|
||||
|
||||
assertEquals("select ts#, file#, block#, cols\n" +
|
||||
"\t, IFNULL(size$, -1), pctfree$\n" +
|
||||
"\t, pctused$, initrans, maxtrans, hashkeys, func\n" +
|
||||
"\t, extind, avgchn, IFNULL(degree, 1)\n" +
|
||||
"\t, IFNULL(instances, 1)\n" +
|
||||
"\t, IFNULL(flags, 0)\n" +
|
||||
"\t, IFNULL(spare1, 0)\n" +
|
||||
"from clu$\n" +
|
||||
"where obj# = '633'", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
assertEquals(18, visitor.getColumns().size());
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.alibaba.druid.bvt.sql.transform.datatype.oracle2pg;
|
||||
|
||||
import com.alibaba.druid.sql.SQLTransformUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class Oracle2MySql_method_sys_guid extends TestCase {
|
||||
public void test_oracle2pg_int_1() throws Exception {
|
||||
String sql = "sys_guid()";
|
||||
SQLMethodInvokeExpr expr = (SQLMethodInvokeExpr) SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).expr();
|
||||
SQLExpr targetExpr = SQLTransformUtils.transformOracleToPostgresql(expr);
|
||||
assertEquals("uuid_generate_v4()", targetExpr.toString());
|
||||
}
|
||||
}
|
||||
+8
@@ -98,4 +98,12 @@ public class Oracle2PG_DataTypeTest_chars extends TestCase {
|
||||
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
|
||||
assertEquals("TEXT", pgDataType.toString());
|
||||
}
|
||||
|
||||
|
||||
public void test_oracle2pg_bfile() throws Exception {
|
||||
String sql = "bfile";
|
||||
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
|
||||
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
|
||||
assertEquals("VARCHAR(255)", pgDataType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -48,5 +48,13 @@ public class Oracle2PG_DataTypeTest_double extends TestCase {
|
||||
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
|
||||
assertEquals("DOUBLE PRECISION", pgDataType.toString());
|
||||
}
|
||||
|
||||
|
||||
public void test_oracle2pg_number_star() throws Exception {
|
||||
String sql = "number(*)";
|
||||
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
|
||||
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
|
||||
assertEquals("DOUBLE PRECISION", pgDataType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ public class Oracle2PG_DataTypeTest_number_int extends TestCase {
|
||||
String sql = "number(*)";
|
||||
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
|
||||
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
|
||||
assertEquals("BIGINT", pgDataType.toString());
|
||||
assertEquals("DOUBLE PRECISION", pgDataType.toString());
|
||||
}
|
||||
|
||||
public void test_oracle2pg_int_21() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user