use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project sharding-jdbc by dangdangdotcom.
the class MySQLInsertVisitor method supplyAutoIncrementColumn.
private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
boolean isPreparedStatement = !getParameters().isEmpty();
GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
if (isPreparedStatement) {
generatedKeyContext.getColumns().addAll(autoIncrementColumns);
}
TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
for (String each : autoIncrementColumns) {
SQLExpr sqlExpr;
Object id = tableRule.generateId(each);
generatedKeyContext.putValue(each, id);
if (isPreparedStatement) {
sqlExpr = new SQLVariantRefExpr("?");
getParameters().add(id);
((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
} else {
sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
}
getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
columns.add(new SQLIdentifierExpr(each));
values.add(sqlExpr);
}
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class PGExprParser method parseInterval.
@Override
protected SQLExpr parseInterval() {
accept(Token.INTERVAL);
PGIntervalExpr intervalExpr = new PGIntervalExpr();
if (lexer.token() != Token.LITERAL_CHARS) {
return new SQLIdentifierExpr("INTERVAL");
}
intervalExpr.setValue(new SQLCharExpr(lexer.stringVal()));
lexer.nextToken();
return intervalExpr;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class SQLStatementParser method parseCreateView.
public SQLCreateViewStatement parseCreateView() {
SQLCreateViewStatement createView = new SQLCreateViewStatement(getDbType());
if (lexer.token() == Token.CREATE) {
lexer.nextToken();
}
if (lexer.token() == Token.OR) {
lexer.nextToken();
accept(Token.REPLACE);
createView.setOrReplace(true);
}
if (identifierEquals("ALGORITHM")) {
lexer.nextToken();
accept(Token.EQ);
String algorithm = lexer.stringVal();
createView.setAlgorithm(algorithm);
lexer.nextToken();
}
if (identifierEquals("DEFINER")) {
lexer.nextToken();
accept(Token.EQ);
SQLName definer = this.exprParser.name();
createView.setDefiner(definer);
lexer.nextToken();
}
if (identifierEquals("SQL")) {
lexer.nextToken();
acceptIdentifier("SECURITY");
String sqlSecurity = lexer.stringVal();
createView.setSqlSecurity(sqlSecurity);
lexer.nextToken();
}
this.accept(Token.VIEW);
if (lexer.token() == Token.IF || identifierEquals("IF")) {
lexer.nextToken();
accept(Token.NOT);
accept(Token.EXISTS);
createView.setIfNotExists(true);
}
createView.setName(exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
SQLCreateViewStatement.Column column = new SQLCreateViewStatement.Column();
SQLExpr expr = this.exprParser.expr();
column.setExpr(expr);
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
column.setComment((SQLCharExpr) exprParser.primary());
}
column.setParent(createView);
createView.addColumn(column);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
accept(Token.RPAREN);
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
SQLCharExpr comment = (SQLCharExpr) exprParser.primary();
createView.setComment(comment);
}
this.accept(Token.AS);
createView.setSubQuery(new SQLSelectParser(this.exprParser).select());
return createView;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class OracleExprParser method methodRest.
@Override
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
if (acceptLPAREN) {
accept(Token.LPAREN);
}
if (lexer.token() == Token.PLUS) {
lexer.nextToken();
accept(Token.RPAREN);
return new OracleOuterExpr(expr);
}
if (expr instanceof SQLIdentifierExpr) {
String methodName = ((SQLIdentifierExpr) expr).getName();
SQLMethodInvokeExpr methodExpr = new SQLMethodInvokeExpr(methodName);
if ("trim".equalsIgnoreCase(methodName)) {
if (//
identifierEquals("LEADING") || //
identifierEquals("TRAILING") || identifierEquals("BOTH")) {
methodExpr.putAttribute("trim_option", lexer.stringVal());
lexer.nextToken();
}
if (lexer.token() != Token.FROM) {
SQLExpr trim_character = this.primary();
trim_character.setParent(methodExpr);
if (trim_character instanceof SQLCharExpr) {
methodExpr.putAttribute("trim_character", trim_character);
} else {
methodExpr.getParameters().add(trim_character);
}
}
if (lexer.token() == Token.FROM) {
lexer.nextToken();
SQLExpr trim_source = this.expr();
methodExpr.addParameter(trim_source);
}
accept(Token.RPAREN);
return primaryRest(methodExpr);
}
}
return super.methodRest(expr, false);
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class WallVisitorUtils method check.
public static boolean check(WallVisitor visitor, SQLBinaryOpExpr x) {
if (x.getOperator() == SQLBinaryOperator.BooleanOr || x.getOperator() == SQLBinaryOperator.BooleanAnd) {
List<SQLExpr> groupList = SQLUtils.split(x);
for (SQLExpr item : groupList) {
item.accept(visitor);
}
return false;
}
if (x.getOperator() == SQLBinaryOperator.Add || x.getOperator() == SQLBinaryOperator.Concat) {
List<SQLExpr> groupList = SQLUtils.split(x);
if (groupList.size() >= 4) {
int chrCount = 0;
for (int i = 0; i < groupList.size(); ++i) {
SQLExpr item = groupList.get(i);
if (item instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) item;
String methodName = methodExpr.getMethodName().toLowerCase();
if ("chr".equals(methodName) || "char".equals(methodName)) {
if (methodExpr.getParameters().get(0) instanceof SQLLiteralExpr) {
chrCount++;
}
}
} else if (item instanceof SQLCharExpr) {
if (((SQLCharExpr) item).getText().length() > 5) {
chrCount = 0;
continue;
}
}
if (chrCount >= 4) {
addViolation(visitor, ErrorCode.EVIL_CONCAT, "evil concat", x);
break;
}
}
}
}
return true;
}
Aggregations