Merge pull request #257 from wenshao/master

improve sql parser
This commit is contained in:
温高铁
2013-01-25 08:32:17 -08:00
7 changed files with 357 additions and 108 deletions
@@ -26,6 +26,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
import javax.sql.ConnectionEventListener;
import javax.sql.StatementEventListener;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
@@ -34,7 +36,8 @@ import com.alibaba.druid.util.JdbcUtils;
* @author wenshao<szujobs@hotmail.com>
*/
public final class DruidConnectionHolder {
private final static Log LOG = LogFactory.getLog(DruidConnectionHolder.class);
private final DruidAbstractDataSource dataSource;
private final Connection conn;
private final List<ConnectionEventListener> connectionEventListeners = new CopyOnWriteArrayList<ConnectionEventListener>();
@@ -66,16 +69,31 @@ public final class DruidConnectionHolder {
this.lastActiveTimeMillis = connecttimeMillis;
this.underlyingAutoCommit = conn.getAutoCommit();
if (!JdbcConstants.SYBASE.equals(dataSource.getDbType())) {
this.underlyingHoldability = conn.getHoldability();
{
boolean initUnderlyHoldability = true;
if (JdbcConstants.SYBASE.equals(dataSource.getDbType()) //
|| JdbcConstants.DB2.equals(dataSource.getDbType()) //
) {
initUnderlyHoldability = false;
}
if (initUnderlyHoldability) {
try {
this.underlyingHoldability = conn.getHoldability();
} catch (SQLException e) {
LOG.warn("getHoldability error", e);
}
}
}
this.underlyingReadOnly = conn.isReadOnly();
try {
this.underlyingTransactionIsolation = conn.getTransactionIsolation();
} catch (SQLException e) {
if (!"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException".equals(e.getClass().getName())) {
if (!"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException".equals(e.getClass().getName())) { // compatible
// for
// alibaba
// corba
throw e;
}
}
@@ -31,7 +31,7 @@ import com.alibaba.druid.sql.parser.Token;
public class MySqlCreateTableParser extends SQLCreateTableParser {
public MySqlCreateTableParser(String sql) {
public MySqlCreateTableParser(String sql){
super(new MySqlExprParser(sql));
}
@@ -155,7 +155,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
}
accept(Token.RPAREN);
stmt.setPartitioning(clause);
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
clause.setPartitionCount(this.exprParser.expr());
@@ -202,6 +202,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
lexer.nextToken();
}
if (lexer.token() == Token.IDENTIFIER) {
name = this.exprParser.name();
}
accept(Token.LPAREN);
for (;;) {
key.getColumns().add(this.exprParser.expr());
@@ -212,6 +216,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
}
}
accept(Token.RPAREN);
if (name != null) {
key.setName(name);
}
return key;
}
@@ -223,5 +231,4 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
throw new ParserException("TODO");
}
}
@@ -455,7 +455,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public boolean visit(MySqlKey x) {
if (x.getName() != null) {
print("CONSTRAINT ");
x.accept(this);
x.getName().accept(this);
print(' ');
}
@@ -189,7 +189,7 @@ public class SQLEvalVisitorUtils {
}
String strValue = param0Value.toString();
int intValue = _int(param1Value);
int intValue = castToInteger(param1Value);
String result = strValue.substring(0, intValue);
@@ -211,7 +211,7 @@ public class SQLEvalVisitorUtils {
}
String strValue = param0Value.toString();
int intValue = _int(param1Value);
int intValue = castToInteger(param1Value);
String result = strValue.substring(strValue.length() - intValue, strValue.length());
@@ -326,8 +326,8 @@ public class SQLEvalVisitorUtils {
return false;
}
int intValue0 = _int(param0Value);
int intValue1 = _int(param1Value);
int intValue0 = castToInteger(param0Value);
int intValue1 = castToInteger(param1Value);
int result = intValue0 % intValue1;
@@ -351,7 +351,7 @@ public class SQLEvalVisitorUtils {
} else if (paramValue instanceof Long) {
result = Math.abs(((Long) paramValue).longValue());
} else {
result = _decimal(paramValue).abs();
result = castToDecimal(paramValue).abs();
}
x.putAttribute(EVAL_VALUE, result);
@@ -368,7 +368,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
double result = Math.acos(doubleValue);
if (Double.isNaN(result)) {
@@ -389,7 +389,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
double result = Math.asin(doubleValue);
if (Double.isNaN(result)) {
@@ -410,7 +410,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
double result = Math.atan(doubleValue);
if (Double.isNaN(result)) {
@@ -434,8 +434,8 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue0 = _double(param0Value);
double doubleValue1 = _double(param1Value);
double doubleValue0 = castToDouble(param0Value);
double doubleValue1 = castToDouble(param1Value);
double result = Math.atan2(doubleValue0, doubleValue1);
if (Double.isNaN(result)) {
@@ -456,7 +456,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.ceil(doubleValue);
if (Double.isNaN(result)) {
@@ -477,7 +477,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.cos(doubleValue);
if (Double.isNaN(result)) {
@@ -498,7 +498,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.sin(doubleValue);
if (Double.isNaN(result)) {
@@ -519,7 +519,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.log(doubleValue);
if (Double.isNaN(result)) {
@@ -540,7 +540,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.log10(doubleValue);
if (Double.isNaN(result)) {
@@ -561,7 +561,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.tan(doubleValue);
if (Double.isNaN(result)) {
@@ -582,7 +582,7 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue = _double(paramValue);
double doubleValue = castToDouble(paramValue);
int result = (int) Math.sqrt(doubleValue);
if (Double.isNaN(result)) {
@@ -606,8 +606,8 @@ public class SQLEvalVisitorUtils {
return false;
}
double doubleValue0 = _double(param0Value);
double doubleValue1 = _double(param1Value);
double doubleValue0 = castToDouble(param0Value);
double doubleValue1 = castToDouble(param1Value);
double result = Math.pow(doubleValue0, doubleValue1);
if (Double.isNaN(result)) {
@@ -678,7 +678,7 @@ public class SQLEvalVisitorUtils {
if (!x.getValueExpr().getAttributes().containsKey(EVAL_VALUE)) {
return false;
}
value = x.getValueExpr().getAttribute(EVAL_VALUE);
} else {
value = null;
@@ -837,18 +837,31 @@ public class SQLEvalVisitorUtils {
break;
case RegExp:
case RLike: {
String pattern = _string(right.getAttributes().get(EVAL_VALUE));
String input = _string(left.getAttributes().get(EVAL_VALUE));
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
String input = castToString(left.getAttributes().get(EVAL_VALUE));
boolean matchResult = Pattern.matches(pattern, input);
x.putAttribute(EVAL_VALUE, matchResult);
}
break;
case NotRegExp:
case NotRLike: {
String pattern = _string(right.getAttributes().get(EVAL_VALUE));
String input = _string(left.getAttributes().get(EVAL_VALUE));
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
String input = castToString(left.getAttributes().get(EVAL_VALUE));
boolean matchResult = !Pattern.matches(pattern, input);
x.putAttribute(EVAL_VALUE, matchResult);
}
case Like: {
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
String input = castToString(left.getAttributes().get(EVAL_VALUE));
boolean matchResult = like(input, pattern);
x.putAttribute(EVAL_VALUE, matchResult);
}
break;
case NotLike: {
String pattern = castToString(right.getAttributes().get(EVAL_VALUE));
String input = castToString(left.getAttributes().get(EVAL_VALUE));
boolean matchResult = !like(input, pattern);
x.putAttribute(EVAL_VALUE, matchResult);
}
break;
default:
@@ -883,7 +896,7 @@ public class SQLEvalVisitorUtils {
return false;
}
public static Boolean _bool(Object val) {
public static Boolean castToBoolean(Object val) {
if (val == null) {
return null;
}
@@ -899,7 +912,7 @@ public class SQLEvalVisitorUtils {
throw new IllegalArgumentException();
}
public static String _string(Object val) {
public static String castToString(Object val) {
Object value = val;
if (value == null) {
@@ -909,7 +922,7 @@ public class SQLEvalVisitorUtils {
return value.toString();
}
public static Byte _byte(Object val) {
public static Byte castToByte(Object val) {
if (val == null) {
return null;
}
@@ -921,7 +934,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).byteValue();
}
public static Short _short(Object val) {
public static Short castToShort(Object val) {
if (val == null) {
return null;
}
@@ -933,7 +946,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).shortValue();
}
public static Integer _int(Object val) {
public static Integer castToInteger(Object val) {
if (val == null) {
return null;
}
@@ -945,7 +958,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).intValue();
}
public static Long _long(Object val) {
public static Long castToLong(Object val) {
if (val == null) {
return null;
}
@@ -957,7 +970,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).longValue();
}
public static Float _float(Object val) {
public static Float castToFloat(Object val) {
if (val == null) {
return null;
}
@@ -969,7 +982,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).floatValue();
}
public static Double _double(Object val) {
public static Double castToDouble(Object val) {
if (val == null) {
return null;
}
@@ -981,7 +994,7 @@ public class SQLEvalVisitorUtils {
return ((Number) val).doubleValue();
}
public static BigInteger _bigInt(Object val) {
public static BigInteger castToBigInteger(Object val) {
if (val == null) {
return null;
}
@@ -997,7 +1010,7 @@ public class SQLEvalVisitorUtils {
return BigInteger.valueOf(((Number) val).longValue());
}
public static Date _date(Object val) {
public static Date castToDate(Object val) {
if (val == null) {
return null;
}
@@ -1011,13 +1024,13 @@ public class SQLEvalVisitorUtils {
}
if (val instanceof String) {
return _date((String) val);
return castToDate((String) val);
}
throw new DruidRuntimeException("can cast to date");
}
public static Date _date(String text) {
public static Date castToDate(String text) {
if (text == null || text.length() == 0) {
return null;
}
@@ -1037,7 +1050,7 @@ public class SQLEvalVisitorUtils {
}
}
public static BigDecimal _decimal(Object val) {
public static BigDecimal castToDecimal(Object val) {
if (val == null) {
return null;
}
@@ -1061,7 +1074,7 @@ public class SQLEvalVisitorUtils {
return BigDecimal.valueOf(((Number) val).longValue());
}
public static Object _sum(Object a, Object b) {
public static Object sum(Object a, Object b) {
if (a == null) {
return b;
}
@@ -1071,27 +1084,27 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).add(_decimal(b));
return castToDecimal(a).add(castToDecimal(b));
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).add(_bigInt(b));
return castToBigInteger(a).add(castToBigInteger(b));
}
if (a instanceof Long || b instanceof Long) {
return _long(a) + _long(b);
return castToLong(a) + castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) + _int(b);
return castToInteger(a) + castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) + _short(b);
return castToShort(a) + castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) + _byte(b);
return castToByte(a) + castToByte(b);
}
throw new IllegalArgumentException();
@@ -1103,27 +1116,27 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).divide(_decimal(b));
return castToDecimal(a).divide(castToDecimal(b));
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).divide(_bigInt(b));
return castToBigInteger(a).divide(castToBigInteger(b));
}
if (a instanceof Long || b instanceof Long) {
return _long(a) / _long(b);
return castToLong(a) / castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) / _int(b);
return castToInteger(a) / castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) / _short(b);
return castToShort(a) / castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) / _byte(b);
return castToByte(a) / castToByte(b);
}
throw new IllegalArgumentException();
@@ -1139,32 +1152,32 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).compareTo(_decimal(b)) > 0;
return castToDecimal(a).compareTo(castToDecimal(b)) > 0;
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).compareTo(_bigInt(b)) > 0;
return castToBigInteger(a).compareTo(castToBigInteger(b)) > 0;
}
if (a instanceof Long || b instanceof Long) {
return _long(a) > _long(b);
return castToLong(a) > castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) > _int(b);
return castToInteger(a) > castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) > _short(b);
return castToShort(a) > castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) > _byte(b);
return castToByte(a) > castToByte(b);
}
if (a instanceof Date || b instanceof Date) {
Date d1 = _date(a);
Date d2 = _date(b);
Date d1 = castToDate(a);
Date d2 = castToDate(b);
if (d1 == d2) {
return false;
@@ -1202,32 +1215,32 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).compareTo(_decimal(b)) < 0;
return castToDecimal(a).compareTo(castToDecimal(b)) < 0;
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).compareTo(_bigInt(b)) < 0;
return castToBigInteger(a).compareTo(castToBigInteger(b)) < 0;
}
if (a instanceof Long || b instanceof Long) {
return _long(a) < _long(b);
return castToLong(a) < castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) < _int(b);
return castToInteger(a) < castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) < _short(b);
return castToShort(a) < castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) < _byte(b);
return castToByte(a) < castToByte(b);
}
if (a instanceof Date || b instanceof Date) {
Date d1 = _date(a);
Date d2 = _date(b);
Date d1 = castToDate(a);
Date d2 = castToDate(b);
if (d1 == d2) {
return false;
@@ -1269,32 +1282,32 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).compareTo(_decimal(b)) == 0;
return castToDecimal(a).compareTo(castToDecimal(b)) == 0;
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).compareTo(_bigInt(b)) == 0;
return castToBigInteger(a).compareTo(castToBigInteger(b)) == 0;
}
if (a instanceof Long || b instanceof Long) {
return _long(a) == _long(b);
return castToLong(a) == castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) == _int(b);
return castToInteger(a) == castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) == _short(b);
return castToShort(a) == castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) == _byte(b);
return castToByte(a) == castToByte(b);
}
if (a instanceof Date || b instanceof Date) {
Date d1 = _date(a);
Date d2 = _date(b);
Date d1 = castToDate(a);
Date d2 = castToDate(b);
if (d1 == d2) {
return true;
@@ -1306,9 +1319,9 @@ public class SQLEvalVisitorUtils {
return d1.equals(d2);
}
if (a instanceof String || b instanceof String) {
return _string(a).equals(_string(b));
return castToString(a).equals(castToString(b));
}
throw new IllegalArgumentException();
@@ -1324,39 +1337,39 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).add(_decimal(b));
return castToDecimal(a).add(castToDecimal(b));
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).add(_bigInt(b));
return castToBigInteger(a).add(castToBigInteger(b));
}
if (a instanceof Double || b instanceof Double) {
return _double(a) + _double(b);
return castToDouble(a) + castToDouble(b);
}
if (a instanceof Float || b instanceof Float) {
return _float(a) + _float(b);
return castToFloat(a) + castToFloat(b);
}
if (a instanceof Long || b instanceof Long) {
return _long(a) + _long(b);
return castToLong(a) + castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) + _int(b);
return castToInteger(a) + castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) + _short(b);
return castToShort(a) + castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) + _byte(b);
return castToByte(a) + castToByte(b);
}
if (a instanceof String || b instanceof String) {
return _string(a) + _string(b);
return castToString(a) + castToString(b);
}
throw new IllegalArgumentException();
@@ -1372,35 +1385,35 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).subtract(_decimal(b));
return castToDecimal(a).subtract(castToDecimal(b));
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).subtract(_bigInt(b));
return castToBigInteger(a).subtract(castToBigInteger(b));
}
if (a instanceof Double || b instanceof Double) {
return _double(a) - _double(b);
return castToDouble(a) - castToDouble(b);
}
if (a instanceof Float || b instanceof Float) {
return _float(a) - _float(b);
return castToFloat(a) - castToFloat(b);
}
if (a instanceof Long || b instanceof Long) {
return _long(a) - _long(b);
return castToLong(a) - castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) - _int(b);
return castToInteger(a) - castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) - _short(b);
return castToShort(a) - castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) - _byte(b);
return castToByte(a) - castToByte(b);
}
throw new IllegalArgumentException();
@@ -1412,29 +1425,98 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof BigDecimal || b instanceof BigDecimal) {
return _decimal(a).multiply(_decimal(b));
return castToDecimal(a).multiply(castToDecimal(b));
}
if (a instanceof BigInteger || b instanceof BigInteger) {
return _bigInt(a).multiply(_bigInt(b));
return castToBigInteger(a).multiply(castToBigInteger(b));
}
if (a instanceof Long || b instanceof Long) {
return _long(a) * _long(b);
return castToLong(a) * castToLong(b);
}
if (a instanceof Integer || b instanceof Integer) {
return _int(a) * _int(b);
return castToInteger(a) * castToInteger(b);
}
if (a instanceof Short || b instanceof Short) {
return _short(a) * _short(b);
return castToShort(a) * castToShort(b);
}
if (a instanceof Byte || b instanceof Byte) {
return _byte(a) * _byte(b);
return castToByte(a) * castToByte(b);
}
throw new IllegalArgumentException();
}
public static boolean like(String input, String pattern) {
if (pattern == null) {
throw new IllegalArgumentException("pattern is null");
}
StringBuilder regexprBuilder = new StringBuilder(pattern.length() + 4);
final int STAT_NOTSET = 0;
final int STAT_RANGE = 1;
final int STAT_LITERAL = 2;
int stat = STAT_NOTSET;
int blockStart = -1;
for (int i = 0; i < pattern.length(); ++i) {
char ch = pattern.charAt(i);
if (stat == STAT_LITERAL //
&& (ch == '%' || ch == '_' || ch == '[')) {
String block = pattern.substring(blockStart, i);
regexprBuilder.append("\\Q");
regexprBuilder.append(block);
regexprBuilder.append("\\E");
blockStart = -1;
stat = STAT_NOTSET;
}
if (ch == '%') {
regexprBuilder.append(".*");
} else if (ch == '_') {
regexprBuilder.append('.');
} else if (ch == '[') {
if (stat == STAT_RANGE) {
throw new IllegalArgumentException("illegal pattern : " + pattern);
}
stat = STAT_RANGE;
blockStart = i;
} else if (ch == ']') {
if (stat != STAT_RANGE) {
throw new IllegalArgumentException("illegal pattern : " + pattern);
}
String block = pattern.substring(blockStart, i + 1);
regexprBuilder.append(block);
blockStart = -1;
} else {
if (stat == STAT_NOTSET) {
stat = STAT_LITERAL;
blockStart = i;
}
if (stat == STAT_LITERAL && i == pattern.length() - 1) {
String block = pattern.substring(blockStart, i + 1);
regexprBuilder.append("\\Q");
regexprBuilder.append(block);
regexprBuilder.append("\\E");
}
}
}
if ("%".equals(pattern) || "%%".equals(pattern)) {
return true;
}
// %xxxx
String regexpr = regexprBuilder.toString();
return Pattern.matches(regexpr, input);
}
}
@@ -0,0 +1,19 @@
package com.alibaba.druid.bvt.sql.eval;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
import com.alibaba.druid.util.JdbcConstants;
public class EvalLikeTest extends TestCase {
public void test_like() throws Exception {
Assert.assertEquals(true, SQLEvalVisitorUtils.evalExpr(JdbcConstants.MYSQL, "'a' LIKE '[a-d]'"));
}
public void test_not_like() throws Exception {
Assert.assertEquals(false, SQLEvalVisitorUtils.evalExpr(JdbcConstants.MYSQL, "'a' NOT LIKE '[a-d]'"));
}
}
@@ -0,0 +1,58 @@
package com.alibaba.druid.bvt.sql.eval;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
public class LikeTest extends TestCase {
public void test_any_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%"));
}
public void test_any_1() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%%"));
}
public void test_any_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%b%"));
}
public void test_any_3() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "a%"));
}
public void test_any_4() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%g"));
}
public void test_single_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("a", "_"));
}
public void test_single_1() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("ab", "__"));
}
public void test_single_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcd", "a__d"));
}
public void test_single_3() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdf", "a__d_"));
}
public void test_range_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdf", "abcd[a-f]"));
}
public void test_range_1() throws Exception {
Assert.assertFalse(SQLEvalVisitorUtils.like("abcdf", "abcd[a-e]"));
}
public void test_range_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("ab", "a[abf]"));
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 1999-2011 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;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlCreateTableTest4 extends MysqlTest {
public void test_0() throws Exception {
String sql = "create table t_temp_11593685fc3244cf81468209484078a0 (" + //
"id int not null auto_increment, " + //
"score float not null, " + //
"student bigint not null, " + //
"primary key(id)," + //
"key score(score), " + //
"key student(student))";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_temp_11593685fc3244cf81468209484078a0")));
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "id")));
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "score")));
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "student")));
}
}