use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class SQLUtils method addSelectItem.
public static String addSelectItem(String selectSql, String expr, String alias, boolean first, String dbType) {
List<SQLStatement> stmtList = parseStatements(selectSql, dbType);
if (stmtList.size() == 0) {
throw new IllegalArgumentException("not support empty-statement :" + selectSql);
}
if (stmtList.size() > 1) {
throw new IllegalArgumentException("not support multi-statement :" + selectSql);
}
SQLStatement stmt = stmtList.get(0);
SQLExpr columnExpr = toSQLExpr(expr, dbType);
addSelectItem(stmt, columnExpr, alias, first);
return toSQLString(stmt, dbType);
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class MySqlExprParser method additiveRest.
public SQLExpr additiveRest(SQLExpr expr) {
if (lexer.token() == Token.PLUS) {
lexer.nextToken();
SQLExpr rightExp = multiplicative();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp, JdbcConstants.MYSQL);
expr = additiveRest(expr);
} else if (lexer.token() == Token.SUB) {
lexer.nextToken();
SQLExpr rightExp = multiplicative();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp, JdbcConstants.MYSQL);
expr = additiveRest(expr);
}
return expr;
}
use of com.alibaba.druid.sql.ast.SQLExpr 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.SQLExpr in project druid by alibaba.
the class MySqlExprParser method notRationalRest.
public SQLExpr notRationalRest(SQLExpr expr) {
if (identifierEquals("REGEXP")) {
lexer.nextToken();
SQLExpr rightExp = primary();
rightExp = relationalRest(rightExp);
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
}
return super.notRationalRest(expr);
}
use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.
the class MySqlExprParser method primary.
public SQLExpr primary() {
final Token tok = lexer.token();
if (identifierEquals("outfile")) {
lexer.nextToken();
SQLExpr file = primary();
SQLExpr expr = new MySqlOutFileExpr(file);
return primaryRest(expr);
}
switch(tok) {
case LITERAL_ALIAS:
String aliasValue = lexer.stringVal();
lexer.nextToken();
return primaryRest(new SQLCharExpr(aliasValue));
case VARIANT:
SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
lexer.nextToken();
if (varRefExpr.getName().equalsIgnoreCase("@@global")) {
accept(Token.DOT);
varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true);
lexer.nextToken();
} else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) {
varRefExpr.setName("@'" + lexer.stringVal() + "'");
lexer.nextToken();
} else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) {
varRefExpr.setName("@@'" + lexer.stringVal() + "'");
lexer.nextToken();
}
return primaryRest(varRefExpr);
case VALUES:
lexer.nextToken();
if (lexer.token() != Token.LPAREN) {
throw new ParserException("syntax error, illegal values clause");
}
return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
case BINARY:
lexer.nextToken();
if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI || lexer.token() == Token.EOF) {
return new SQLIdentifierExpr("BINARY");
} else {
SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
return primaryRest(binaryExpr);
}
case CACHE:
case GROUP:
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
default:
return super.primary();
}
}
Aggregations