use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class Left method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if (x.getParameters().size() != 2) {
return SQLEvalVisitor.EVAL_ERROR;
}
SQLExpr param0 = x.getParameters().get(0);
SQLExpr param1 = x.getParameters().get(1);
param0.accept(visitor);
param1.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
Object param1Value = param1.getAttributes().get(EVAL_VALUE);
if (param0Value == null || param1Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
String strValue = param0Value.toString();
int intValue = SQLEvalVisitorUtils.castToInteger(param1Value);
if (intValue > strValue.length()) {
return SQLEvalVisitor.EVAL_ERROR;
}
String result = strValue.substring(0, intValue);
return result;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class Lpad method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
List<SQLExpr> params = x.getParameters();
int paramSize = params.size();
if (paramSize != 3) {
return SQLEvalVisitor.EVAL_ERROR;
}
SQLExpr param0 = params.get(0);
SQLExpr param1 = params.get(1);
SQLExpr param2 = params.get(2);
param0.accept(visitor);
param1.accept(visitor);
param2.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
Object param1Value = param1.getAttributes().get(EVAL_VALUE);
Object param2Value = param2.getAttributes().get(EVAL_VALUE);
if (param0Value == null || param1Value == null || param2Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
String strValue0 = param0Value.toString();
int len = ((Number) param1Value).intValue();
String strValue1 = param2Value.toString();
String result = strValue0;
if (result.length() > len) {
return result.substring(0, len);
}
while (result.length() < len) {
result = strValue1 + result;
}
return result;
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class Ltrim method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if (x.getParameters().size() != 1) {
return SQLEvalVisitor.EVAL_ERROR;
}
SQLExpr param0 = x.getParameters().get(0);
param0.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
String strValue = param0Value.toString();
int index = -1;
for (int i = 0; i < strValue.length(); ++i) {
if (!Character.isWhitespace(strValue.charAt(i))) {
index = i;
break;
}
}
if (index <= 0) {
return strValue;
} else {
return strValue.substring(index);
}
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class OneParamFunctions method eval.
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if (x.getParameters().size() == 0) {
return SQLEvalVisitor.EVAL_ERROR;
}
SQLExpr param = x.getParameters().get(0);
param.accept(visitor);
Object paramValue = param.getAttributes().get(EVAL_VALUE);
if (paramValue == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
if (paramValue == EVAL_VALUE_NULL) {
return EVAL_VALUE_NULL;
}
String method = x.getMethodName();
if ("md5".equalsIgnoreCase(method)) {
String text = paramValue.toString();
return Utils.md5(text);
}
if ("bit_count".equalsIgnoreCase(method)) {
if (paramValue instanceof BigInteger) {
return ((BigInteger) paramValue).bitCount();
}
if (paramValue instanceof BigDecimal) {
BigDecimal decimal = (BigDecimal) paramValue;
BigInteger bigInt = decimal.setScale(0, BigDecimal.ROUND_HALF_UP).toBigInteger();
return bigInt.bitCount();
}
Long val = SQLEvalVisitorUtils.castToLong(paramValue);
return Long.bitCount(val);
}
if ("soundex".equalsIgnoreCase(method)) {
String text = paramValue.toString();
return soundex(text);
}
if ("space".equalsIgnoreCase(method)) {
int intVal = SQLEvalVisitorUtils.castToInteger(paramValue);
char[] chars = new char[intVal];
for (int i = 0; i < chars.length; ++i) {
chars[i] = ' ';
}
return new String(chars);
}
throw new UnsupportedOperationException(method);
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class WallVisitorUtils method checkSelelct.
public static void checkSelelct(WallVisitor visitor, SQLSelectQueryBlock x) {
for (SQLSelectItem item : x.getSelectList()) {
item.setParent(x);
}
if (x.getInto() != null) {
checkReadOnly(visitor, x.getInto());
}
if (!visitor.getConfig().isSelectIntoAllow() && x.getInto() != null) {
addViolation(visitor, ErrorCode.SELECT_INTO_NOT_ALLOW, "select into not allow", x);
return;
}
if (x.getFrom() != null) {
x.getFrom().setParent(x);
}
SQLExpr where = x.getWhere();
if (where != null) {
where.setParent(x);
checkCondition(visitor, x.getWhere());
Object whereValue = getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck());
if (Boolean.TRUE == whereValue) {
if (visitor.getConfig().isSelectWhereAlwayTrueCheck() && visitor.isSqlEndOfComment() && !isSimpleConstExpr(where)) {
// 简单表达式
addViolation(visitor, ErrorCode.ALWAYS_TRUE, "select alway true condition not allow", x);
}
}
}
checkSelectForMultiTenant(visitor, x);
// checkConditionForMultiTenant(visitor, x.getWhere(), x);
}
Aggregations