use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.
the class OdpsStatementParser method parseSet.
public SQLStatement parseSet() {
List<String> comments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
comments = lexer.readAndResetComments();
}
accept(Token.SET);
if (identifierEquals("LABEL")) {
OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
if (comments != null) {
stmt.addBeforeComment(comments);
}
lexer.nextToken();
stmt.setLabel(lexer.stringVal());
lexer.nextToken();
accept(Token.TO);
if (lexer.token() == Token.USER) {
lexer.nextToken();
SQLName name = this.exprParser.name();
stmt.setUser(name);
return stmt;
}
accept(Token.TABLE);
SQLExpr expr = this.exprParser.name();
stmt.setTable(new SQLExprTableSource(expr));
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
this.exprParser.names(stmt.getColumns(), stmt);
accept(Token.RPAREN);
}
return stmt;
} else {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
if (comments != null) {
stmt.addBeforeComment(comments);
}
parseAssignItems(stmt.getItems(), stmt);
return stmt;
}
}
use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.
the class DALParserTest method testSet_2.
public void testSet_2() throws Exception {
String sql = "SET `sysVar1` = ?, @@gloBal . `var2` :=1";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(set);
Assert.assertEquals("SET @@`sysVar1` = ?, @@global.`var2` = 1", output);
}
use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.
the class DALParserTest method testSet_3.
public void testSet_3() throws Exception {
String sql = "SET @usrVar1 := ?, @@`var2` =1, @@var3:=?, @'var\\'3'=?";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(set);
Assert.assertEquals("SET @usrVar1 = ?, @@`var2` = 1, @@var3 = ?, @'var\\'3' = ?", output);
}
use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.
the class SQLUtils method toSQLString.
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters, FormatOption option, Map<String, String> tableMapping) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
if (parameters != null) {
visitor.setParameters(parameters);
}
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
visitor.setPrettyFormat(option.isPrettyFormat());
visitor.setParameterized(option.isParameterized());
if (tableMapping != null) {
visitor.setTableMapping(tableMapping);
}
for (int i = 0; i < statementList.size(); i++) {
SQLStatement stmt = statementList.get(i);
if (i > 0) {
visitor.print(";");
SQLStatement preStmt = statementList.get(i - 1);
List<String> comments = preStmt.getAfterCommentsDirect();
if (comments != null) {
for (int j = 0; j < comments.size(); ++j) {
String comment = comments.get(j);
if (j != 0) {
visitor.println();
}
visitor.print(comment);
}
}
visitor.println();
if (!(stmt instanceof SQLSetStatement)) {
visitor.println();
}
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
if (comments != null) {
for (String comment : comments) {
visitor.println(comment);
}
}
}
stmt.accept(visitor);
if (i == statementList.size() - 1) {
Boolean semi = (Boolean) stmt.getAttribute("format.semi");
if (semi != null && semi.booleanValue()) {
// if (stmt.hasAfterComment()) {
// visitor.println();
// }
visitor.print(";");
}
List<String> comments = stmt.getAfterCommentsDirect();
if (comments != null) {
for (int j = 0; j < comments.size(); ++j) {
String comment = comments.get(j);
if (j != 0) {
visitor.println();
}
visitor.print(comment);
}
}
}
}
return out.toString();
}
use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.
the class DALParserTest method testSet_1.
public void testSet_1() throws Exception {
String sql = "seT sysVar1 = ? ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(set);
Assert.assertEquals("SET @@sysVar1 = ?", output);
}
Aggregations