mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
-112
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlReplaceStatement extends MySqlStatementImpl {
|
||||
|
||||
private boolean lowPriority = false;
|
||||
private boolean delayed = false;
|
||||
|
||||
private SQLExprTableSource tableSource;
|
||||
private final List<SQLExpr> columns = new ArrayList<SQLExpr>();
|
||||
private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
|
||||
private SQLQueryExpr query;
|
||||
|
||||
public SQLName getTableName() {
|
||||
if (tableSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (SQLName) tableSource.getExpr();
|
||||
}
|
||||
|
||||
public void setTableName(SQLName tableName) {
|
||||
this.setTableSource(new SQLExprTableSource(tableName));
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLExprTableSource tableSource) {
|
||||
if (tableSource != null) {
|
||||
tableSource.setParent(this);
|
||||
}
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void addColumn(SQLExpr column) {
|
||||
if (column != null) {
|
||||
column.setParent(this);
|
||||
}
|
||||
this.columns.add(column);
|
||||
}
|
||||
|
||||
public boolean isLowPriority() {
|
||||
return lowPriority;
|
||||
}
|
||||
|
||||
public void setLowPriority(boolean lowPriority) {
|
||||
this.lowPriority = lowPriority;
|
||||
}
|
||||
|
||||
public boolean isDelayed() {
|
||||
return delayed;
|
||||
}
|
||||
|
||||
public void setDelayed(boolean delayed) {
|
||||
this.delayed = delayed;
|
||||
}
|
||||
|
||||
public SQLQueryExpr getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(SQLQueryExpr query) {
|
||||
if (query != null) {
|
||||
query.setParent(this);
|
||||
}
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public List<ValuesClause> getValuesList() {
|
||||
return valuesList;
|
||||
}
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, valuesList);
|
||||
acceptChild(visitor, query);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -504,7 +504,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.REPLACE) {
|
||||
MySqlReplaceStatement stmt = parseReplicate();
|
||||
SQLReplaceStatement stmt = parseReplace();
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
@@ -1773,8 +1773,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public MySqlReplaceStatement parseReplicate() {
|
||||
MySqlReplaceStatement stmt = new MySqlReplaceStatement();
|
||||
public SQLReplaceStatement parseReplace() {
|
||||
SQLReplaceStatement stmt = new SQLReplaceStatement();
|
||||
|
||||
accept(Token.REPLACE);
|
||||
|
||||
@@ -2262,95 +2262,6 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return insertStatement;
|
||||
}
|
||||
|
||||
private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize, SQLObject parent) {
|
||||
for (int i = 0; ; ++i) {
|
||||
int startPos = lexer.pos() - 1;
|
||||
|
||||
if (lexer.token() != Token.LPAREN) {
|
||||
throw new ParserException("syntax error, expect ')', " + lexer.info());
|
||||
}
|
||||
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);
|
||||
values.setParent(parent);
|
||||
|
||||
if (lexer.isEnabled(SQLParserFeature.KeepInsertValueClauseOriginalString)) {
|
||||
int endPos = lexer.pos();
|
||||
String orginalString = lexer.subString(startPos, endPos - startPos);
|
||||
values.setOriginalString(orginalString);
|
||||
}
|
||||
|
||||
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. " + lexer.info());
|
||||
}
|
||||
|
||||
if (!parseCompleteValues && valueClauseList.size() >= parseValuesSize) {
|
||||
lexer.skipToEOF();
|
||||
break;
|
||||
}
|
||||
|
||||
lexer.nextTokenComma();
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextTokenLParen();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new MySqlSelectParser(this.exprParser);
|
||||
}
|
||||
|
||||
@@ -103,10 +103,6 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(MySqlLoadXmlStatement x);
|
||||
|
||||
void endVisit(MySqlReplaceStatement x);
|
||||
|
||||
boolean visit(MySqlReplaceStatement x);
|
||||
|
||||
void endVisit(MySqlShowColumnsStatement x);
|
||||
|
||||
boolean visit(MySqlShowColumnsStatement x);
|
||||
|
||||
@@ -186,17 +186,6 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlReplaceStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlReplaceStatement x) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlShowColumnsStatement x) {
|
||||
|
||||
|
||||
@@ -803,20 +803,22 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? "IGNORE " : "ignore ");
|
||||
}
|
||||
|
||||
if (x.getFrom() == null) {
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from == null) {
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
x.getTableSource().accept(this);
|
||||
} else {
|
||||
x.getTableSource().accept(this);
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
x.getFrom().accept(this);
|
||||
from.accept(this);
|
||||
}
|
||||
|
||||
if (x.getUsing() != null) {
|
||||
SQLTableSource using = x.getUsing();
|
||||
if (using != null) {
|
||||
println();
|
||||
print0(ucase ? "USING " : "using ");
|
||||
x.getUsing().accept(this);
|
||||
using.accept(this);
|
||||
}
|
||||
|
||||
SQLExpr where = x.getWhere();
|
||||
@@ -894,7 +896,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
SQLExpr column = columns.get(i);
|
||||
if (column instanceof SQLIdentifierExpr) {
|
||||
visit((SQLIdentifierExpr) column);
|
||||
print0(((SQLIdentifierExpr) column).getName());
|
||||
} else {
|
||||
printExpr(column);
|
||||
}
|
||||
@@ -1053,13 +1055,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlReplaceStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlReplaceStatement x) {
|
||||
public boolean visit(SQLReplaceStatement x) {
|
||||
print0(ucase ? "REPLACE " : "replace ");
|
||||
|
||||
if (x.isLowPriority()) {
|
||||
@@ -1574,9 +1570,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUpdateStatement x) {
|
||||
if (x.getReturning() != null && x.getReturning().size() > 0) {
|
||||
List<SQLExpr> returning = x.getReturning();
|
||||
if (returning != null && returning.size() > 0) {
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
printAndAccept(x.getReturning(), ", ");
|
||||
printAndAccept(returning, ", ");
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
}
|
||||
@@ -1614,11 +1611,12 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
println();
|
||||
print0(ucase ? "SET " : "set ");
|
||||
for (int i = 0, size = x.getItems().size(); i < size; ++i) {
|
||||
List<SQLUpdateSetItem> items = x.getItems();
|
||||
for (int i = 0, size = items.size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
print0(", ");
|
||||
}
|
||||
SQLUpdateSetItem item = x.getItems().get(i);
|
||||
SQLUpdateSetItem item = items.get(i);
|
||||
visit(item);
|
||||
}
|
||||
|
||||
@@ -3007,26 +3005,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBlockStatement x) {
|
||||
if (x.getLabelName() != null && !x.getLabelName().equals("")) {
|
||||
print0(x.getLabelName());
|
||||
SQLObject parent = x.getParent();
|
||||
String labelName = x.getLabelName();
|
||||
|
||||
if (labelName != null && !labelName.equals("")) {
|
||||
print0(labelName);
|
||||
print0(": ");
|
||||
}
|
||||
if (x.getParameters().size() != 0) {
|
||||
|
||||
List<SQLParameter> parameters = x.getParameters();
|
||||
if (parameters.size() != 0) {
|
||||
this.indentCount++;
|
||||
if (x.getParent() instanceof SQLCreateProcedureStatement) {
|
||||
if (parent instanceof SQLCreateProcedureStatement) {
|
||||
printIndent();
|
||||
}
|
||||
if (!(x.getParent() instanceof SQLCreateProcedureStatement)) {
|
||||
if (!(parent instanceof SQLCreateProcedureStatement)) {
|
||||
print0(ucase ? "DECLARE" : "declare");
|
||||
println();
|
||||
}
|
||||
|
||||
for (int i = 0, size = x.getParameters().size(); i < size; ++i) {
|
||||
for (int i = 0, size = parameters.size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
SQLParameter param = parameters.get(i);
|
||||
visit(param);
|
||||
print(';');
|
||||
}
|
||||
|
||||
@@ -3036,19 +3039,20 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? "BEGIN" : "begin");
|
||||
this.indentCount++;
|
||||
println();
|
||||
for (int i = 0, size = x.getStatementList().size(); i < size; ++i) {
|
||||
List<SQLStatement> statementList = x.getStatementList();
|
||||
for (int i = 0, size = statementList.size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
println();
|
||||
}
|
||||
SQLStatement stmt = x.getStatementList().get(i);
|
||||
SQLStatement stmt = statementList.get(i);
|
||||
stmt.accept(this);
|
||||
}
|
||||
this.indentCount--;
|
||||
println();
|
||||
print0(ucase ? "END" : "end");
|
||||
if (x.getLabelName() != null && !x.getLabelName().equals("")) {
|
||||
if (labelName != null && !labelName.equals("")) {
|
||||
print(' ');
|
||||
print0(x.getLabelName());
|
||||
print0(labelName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -3060,8 +3064,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? " THEN" : " then");
|
||||
this.indentCount++;
|
||||
println();
|
||||
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
|
||||
SQLStatement item = x.getStatements().get(i);
|
||||
List<SQLStatement> statements = x.getStatements();
|
||||
for (int i = 0, size = statements.size(); i < size; ++i) {
|
||||
SQLStatement item = statements.get(i);
|
||||
item.accept(this);
|
||||
if (i != size - 1) {
|
||||
println();
|
||||
@@ -3073,7 +3078,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
iterable_element.accept(this);
|
||||
}
|
||||
|
||||
if (x.getElseItem() != null) x.getElseItem().accept(this);
|
||||
if (x.getElseItem() != null) {
|
||||
x.getElseItem().accept(this);
|
||||
}
|
||||
|
||||
print0(ucase ? "END IF" : "end if");
|
||||
return false;
|
||||
|
||||
@@ -259,35 +259,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlReplaceStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlReplaceStatement x) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
setMode(x, Mode.Replace);
|
||||
|
||||
SQLName tableName = x.getTableName();
|
||||
|
||||
TableStat stat = getTableStat(tableName);
|
||||
|
||||
if (stat != null) {
|
||||
stat.incrementInsertCount();
|
||||
}
|
||||
|
||||
accept(x.getColumns());
|
||||
accept(x.getValuesList());
|
||||
accept(x.getQuery());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLStartTransactionStatement x) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user