use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.
the class MySqlCreateTableParser method partitionClauseRest.
protected void partitionClauseRest(SQLPartitionBy clause) {
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
SQLIntegerExpr countExpr = this.exprParser.integerExpr();
clause.setPartitionsCount(countExpr);
}
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
if (identifierEquals("NUM")) {
lexer.nextToken();
}
clause.setPartitionsCount(this.exprParser.expr());
clause.putAttribute("ads.partition", Boolean.TRUE);
}
if (identifierEquals("SUBPARTITION")) {
lexer.nextToken();
accept(Token.BY);
SQLSubPartitionBy subPartitionByClause = null;
boolean linear = false;
if (identifierEquals("LINEAR")) {
lexer.nextToken();
linear = true;
}
if (lexer.token() == Token.KEY) {
MySqlSubPartitionByKey subPartitionKey = new MySqlSubPartitionByKey();
lexer.nextToken();
if (linear) {
clause.setLinear(true);
}
accept(Token.LPAREN);
for (; ; ) {
subPartitionKey.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
subPartitionByClause = subPartitionKey;
} else if (identifierEquals("HASH")) {
lexer.nextToken();
SQLSubPartitionByHash subPartitionHash = new SQLSubPartitionByHash();
if (linear) {
clause.setLinear(true);
}
if (lexer.token() == Token.KEY) {
lexer.nextToken();
subPartitionHash.setKey(true);
}
accept(Token.LPAREN);
subPartitionHash.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
subPartitionByClause = subPartitionHash;
} else if (identifierEquals("LIST")) {
lexer.nextToken();
MySqlSubPartitionByList subPartitionList = new MySqlSubPartitionByList();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLExpr expr = this.exprParser.expr();
if (expr instanceof SQLIdentifierExpr && (identifierEquals("bigint") || identifierEquals("long"))) {
String dataType = lexer.stringVal();
lexer.nextToken();
SQLColumnDefinition column = this.exprParser.createColumnDefinition();
column.setName((SQLIdentifierExpr) expr);
column.setDataType(new SQLDataTypeImpl(dataType));
subPartitionList.addColumn(column);
subPartitionList.putAttribute("ads.subPartitionList", Boolean.TRUE);
} else {
subPartitionList.setExpr(expr);
}
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
accept(Token.LPAREN);
for (; ; ) {
subPartitionList.addColumn(this.exprParser.parseColumn());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
subPartitionByClause = subPartitionList;
}
if (identifierEquals("SUBPARTITION")) {
lexer.nextToken();
acceptIdentifier("OPTIONS");
accept(Token.LPAREN);
SQLAssignItem option = this.exprParser.parseAssignItem();
accept(Token.RPAREN);
option.setParent(subPartitionByClause);
subPartitionByClause.getOptions().add(option);
}
if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
Number intValue = lexer.integerValue();
SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
subPartitionByClause.setSubPartitionsCount(numExpr);
lexer.nextToken();
}
if (subPartitionByClause != null) {
subPartitionByClause.setLinear(linear);
clause.setSubPartitionBy(subPartitionByClause);
}
}
}
use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.
the class MySqlExprParser method parseAssignItem.
public SQLAssignItem parseAssignItem() {
SQLAssignItem item = new SQLAssignItem();
SQLExpr var = primary();
String ident = null;
if (var instanceof SQLIdentifierExpr) {
ident = ((SQLIdentifierExpr) var).getName();
if ("GLOBAL".equalsIgnoreCase(ident)) {
ident = lexer.stringVal();
lexer.nextToken();
var = new SQLVariantRefExpr(ident, true);
} else if ("SESSION".equalsIgnoreCase(ident)) {
ident = lexer.stringVal();
lexer.nextToken();
var = new SQLVariantRefExpr(ident, false);
} else {
var = new SQLVariantRefExpr(ident);
}
}
if ("NAMES".equalsIgnoreCase(ident)) {
// skip
} else if ("CHARACTER".equalsIgnoreCase(ident)) {
var = new SQLIdentifierExpr("CHARACTER SET");
accept(Token.SET);
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
} else {
if (lexer.token() == Token.COLONEQ) {
lexer.nextToken();
} else {
accept(Token.EQ);
}
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
item.setValue(new SQLIdentifierExpr("ON"));
} else {
item.setValue(this.expr());
}
item.setTarget(var);
return item;
}
use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.
the class SQLExprParser method parseAssignItem.
public SQLAssignItem parseAssignItem() {
SQLAssignItem item = new SQLAssignItem();
SQLExpr var = primary();
if (var instanceof SQLIdentifierExpr) {
var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName());
}
item.setTarget(var);
if (lexer.token() == Token.COLONEQ) {
lexer.nextToken();
} else {
accept(Token.EQ);
}
if (lexer.token() == Token.ON) {
item.setValue(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
} else {
if (lexer.token() == Token.ALL) {
item.setValue(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
} else {
SQLExpr expr = expr();
if (lexer.token() == Token.COMMA && JdbcConstants.POSTGRESQL.equals(dbType)) {
SQLListExpr listExpr = new SQLListExpr();
listExpr.addItem(expr);
expr.setParent(listExpr);
do {
lexer.nextToken();
SQLExpr listItem = this.expr();
listItem.setParent(listExpr);
listExpr.addItem(listItem);
} while (lexer.token() == Token.COMMA);
item.setValue(listExpr);
} else {
item.setValue(expr);
}
}
}
return item;
}
use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.
the class OdpsStatementParser method parseOdpsInsert.
public OdpsInsert parseOdpsInsert() {
OdpsInsert insert = new OdpsInsert();
if (lexer.isKeepComments() && lexer.hasComment()) {
insert.addBeforeComment(lexer.readAndResetComments());
}
SQLSelectParser selectParser = createSQLSelectParser();
accept(Token.INSERT);
if (lexer.token() == Token.INTO) {
lexer.nextToken();
} else {
accept(Token.OVERWRITE);
insert.setOverwrite(true);
}
accept(Token.TABLE);
insert.setTableSource(this.exprParser.name());
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.LPAREN);
for (; ; ) {
SQLAssignItem ptExpr = new SQLAssignItem();
ptExpr.setTarget(this.exprParser.name());
if (lexer.token() == Token.EQ) {
lexer.nextToken();
SQLExpr ptValue = this.exprParser.expr();
ptExpr.setValue(ptValue);
}
insert.addPartition(ptExpr);
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
lexer.nextToken();
}
}
accept(Token.RPAREN);
}
SQLSelect query = selectParser.select();
insert.setQuery(query);
return insert;
}
use of com.alibaba.druid.sql.ast.statement.SQLAssignItem in project druid by alibaba.
the class MySqlWallVisitor method visit.
public boolean visit(SQLPropertyExpr x) {
if (x.getOwner() instanceof SQLVariantRefExpr) {
SQLVariantRefExpr varExpr = (SQLVariantRefExpr) x.getOwner();
SQLObject parent = x.getParent();
String varName = varExpr.getName();
if (varName.equalsIgnoreCase("@@session") || varName.equalsIgnoreCase("@@global")) {
if (!(parent instanceof SQLSelectItem) && !(parent instanceof SQLAssignItem)) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable in condition not allow", toSQL(x)));
return false;
}
if (!checkVar(x.getParent(), x.getName())) {
boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x);
if (!isTop) {
boolean allow = true;
if (isDeny(varName) && (WallVisitorUtils.isWhereOrHaving(x) || WallVisitorUtils.checkSqlExpr(varExpr))) {
allow = false;
}
if (!allow) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " + x.getName(), toSQL(x)));
}
}
}
return false;
}
}
WallVisitorUtils.check(this, x);
return true;
}
Aggregations