use of com.alibaba.druid.sql.ast.expr.SQLNCharExpr in project druid by alibaba.
the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.
public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
MockResultSetMetaData metaData = rs.getMockMetaData();
Object[] row = new Object[query.getSelectList().size()];
for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
ColumnMetaData column = new ColumnMetaData();
SQLSelectItem item = query.getSelectList().get(i);
SQLExpr expr = item.getExpr();
if (expr instanceof SQLIntegerExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.INTEGER);
} else if (expr instanceof SQLNumberExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.DECIMAL);
} else if (expr instanceof SQLCharExpr) {
row[i] = ((SQLCharExpr) expr).getText();
column.setColumnType(Types.VARCHAR);
} else if (expr instanceof SQLNCharExpr) {
row[i] = ((SQLNCharExpr) expr).getText();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLBooleanExpr) {
row[i] = ((SQLBooleanExpr) expr).getValue();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLNullExpr) {
row[i] = null;
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
row[i] = new Timestamp(System.currentTimeMillis());
} else {
throw new SQLException("TODO");
}
} else if (expr instanceof SQLVariantRefExpr) {
SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
int varIndex = varExpr.getIndex();
if (statement instanceof MockPreparedStatement) {
MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
row[i] = mockPstmt.getParameters().get(varIndex);
} else {
row[i] = null;
}
} else {
row[i] = null;
}
metaData.getColumns().add(column);
}
rs.getRows().add(row);
return rs;
}
use of com.alibaba.druid.sql.ast.expr.SQLNCharExpr in project druid by alibaba.
the class MySqlStatementParser method parseValueClause.
private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize) {
for (int i = 0; ; ++i) {
if (lexer.token() != Token.LPAREN) {
throw new ParserException("syntax error, expect ')'");
}
lexer.nextTokenValue();
if (lexer.token() != Token.RPAREN) {
List<SQLExpr> valueExprList;
if (columnSize > 0) {
valueExprList = new ArrayList<SQLExpr>(columnSize);
} else {
valueExprList = new ArrayList<SQLExpr>();
}
for (; ; ) {
SQLExpr expr;
if (lexer.token() == Token.LITERAL_INT) {
expr = new SQLIntegerExpr(lexer.integerValue());
lexer.nextTokenComma();
} else if (lexer.token() == Token.LITERAL_CHARS) {
expr = new SQLCharExpr(lexer.stringVal());
lexer.nextTokenComma();
} else if (lexer.token() == Token.LITERAL_NCHARS) {
expr = new SQLNCharExpr(lexer.stringVal());
lexer.nextTokenComma();
} else {
expr = exprParser.expr();
}
if (lexer.token() == Token.COMMA) {
valueExprList.add(expr);
lexer.nextTokenValue();
continue;
} else if (lexer.token() == Token.RPAREN) {
valueExprList.add(expr);
break;
} else {
expr = this.exprParser.primaryRest(expr);
if (lexer.token() != Token.COMMA && lexer.token() != Token.RPAREN) {
expr = this.exprParser.exprRest(expr);
}
valueExprList.add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(valueExprList);
valueClauseList.add(values);
} else {
SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(new ArrayList<SQLExpr>(0));
valueClauseList.add(values);
}
if (lexer.token() != Token.RPAREN) {
throw new ParserException("syntax error");
}
if (!parseCompleteValues && valueClauseList.size() >= parseValuesSize) {
lexer.skipToEOF();
break;
}
lexer.nextTokenComma();
if (lexer.token() == Token.COMMA) {
lexer.nextTokenLParen();
continue;
} else {
break;
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLNCharExpr in project druid by alibaba.
the class WallVisitorUtils method getValue.
public static Object getValue(WallVisitor visitor, SQLExpr x) {
if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
return getValueFromAttributes(visitor, x);
}
if (x instanceof SQLBinaryOpExpr) {
return getValue(visitor, (SQLBinaryOpExpr) x);
}
if (x instanceof SQLBooleanExpr) {
return ((SQLBooleanExpr) x).getValue();
}
if (x instanceof SQLNumericLiteralExpr) {
return ((SQLNumericLiteralExpr) x).getNumber();
}
if (x instanceof SQLCharExpr) {
return ((SQLCharExpr) x).getText();
}
if (x instanceof SQLNCharExpr) {
return ((SQLNCharExpr) x).getText();
}
if (x instanceof SQLNotExpr) {
Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
if (result instanceof Boolean) {
return !((Boolean) result).booleanValue();
}
}
if (x instanceof SQLQueryExpr) {
if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
return Integer.valueOf(1);
}
if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
Object result = getValue(caseExpr);
if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
boolean leftIsName = false;
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
if (left instanceof SQLName) {
leftIsName = true;
}
}
if (!leftIsName && result != null) {
addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
}
}
return result;
}
}
String dbType = null;
if (visitor != null) {
dbType = visitor.getDbType();
}
if (//
x instanceof SQLMethodInvokeExpr || //
x instanceof SQLBetweenExpr || //
x instanceof SQLInListExpr || //
x instanceof SQLUnaryExpr) {
return eval(visitor, dbType, x, Collections.emptyList());
}
if (x instanceof SQLCaseExpr) {
if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
SQLCaseExpr caseExpr = (SQLCaseExpr) x;
boolean leftIsName = false;
if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
if (left instanceof SQLName) {
leftIsName = true;
}
}
if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
SQLCaseExpr.Item item = caseExpr.getItems().get(0);
Object conditionVal = getValue(visitor, item.getConditionExpr());
Object itemVal = getValue(visitor, item.getValueExpr());
if (conditionVal instanceof Boolean && itemVal != null) {
addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
}
}
}
return eval(visitor, dbType, x, Collections.emptyList());
}
return null;
}
use of com.alibaba.druid.sql.ast.expr.SQLNCharExpr in project druid by alibaba.
the class WallVisitorUtils method isSimpleConstExpr.
private static boolean isSimpleConstExpr(SQLExpr sqlExpr) {
List<SQLExpr> parts = getParts(sqlExpr);
if (parts.isEmpty()) {
return false;
}
for (SQLExpr part : parts) {
if (isFirst(part)) {
Object evalValue = part.getAttribute(EVAL_VALUE);
if (evalValue == null) {
if (part instanceof SQLBooleanExpr) {
evalValue = ((SQLBooleanExpr) part).getValue();
} else if (part instanceof SQLNumericLiteralExpr) {
evalValue = ((SQLNumericLiteralExpr) part).getNumber();
} else if (part instanceof SQLCharExpr) {
evalValue = ((SQLCharExpr) part).getText();
} else if (part instanceof SQLNCharExpr) {
evalValue = ((SQLNCharExpr) part).getText();
}
}
Boolean result = SQLEvalVisitorUtils.castToBoolean(evalValue);
if (result != null && result) {
return true;
}
}
boolean isSimpleConstExpr = false;
if (part == sqlExpr || part instanceof SQLLiteralExpr) {
isSimpleConstExpr = true;
} else if (part instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) part;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual || binaryOpExpr.getOperator() == SQLBinaryOperator.GreaterThan) {
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
isSimpleConstExpr = true;
}
}
}
if (!isSimpleConstExpr) {
return false;
}
}
return true;
}
Aggregations