mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql server parser, support merge. for issue #2118
This commit is contained in:
@@ -44,6 +44,10 @@ public class SQLServerLexer extends Lexer {
|
||||
map.put("BEGIN", Token.BEGIN);
|
||||
map.put("END", Token.END);
|
||||
|
||||
map.put("MERGE", Token.MERGE);
|
||||
map.put("USING", Token.USING);
|
||||
map.put("MATCHED", Token.MATCHED);
|
||||
|
||||
DEFAULT_SQL_SERVER_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
|
||||
@@ -334,19 +334,19 @@ public class SQLStatementParser extends SQLParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("RELEASE")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.RELEASE)) {
|
||||
SQLStatement stmt = parseReleaseSavePoint();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("SAVEPOINT")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SAVEPOINT)) {
|
||||
SQLStatement stmt = parseSavePoint();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("ROLLBACK")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.ROLLBACK)) {
|
||||
SQLRollbackStatement stmt = parseRollback();
|
||||
statementList.add(stmt);
|
||||
|
||||
@@ -358,6 +358,13 @@ public class SQLStatementParser extends SQLParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.MERGE)) {
|
||||
SQLStatement stmt = parseMerge();
|
||||
stmt.setParent(parent);
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.COMMIT)) {
|
||||
SQLStatement stmt = parseCommit();
|
||||
|
||||
|
||||
@@ -615,5 +615,10 @@ public final class FnvHash {
|
||||
long SORTED = fnv1a_64_lower("SORTED");
|
||||
long LIFECYCLE = fnv1a_64_lower("LIFECYCLE");
|
||||
long PARTITIONS = fnv1a_64_lower("PARTITIONS");
|
||||
|
||||
long ROLLBACK = fnv1a_64_lower("ROLLBACK");
|
||||
long SAVEPOINT = fnv1a_64_lower("SAVEPOINT");
|
||||
long RELEASE = fnv1a_64_lower("RELEASE");
|
||||
long MERGE = fnv1a_64_lower("MERGE");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.bvt.sql.sqlserver;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SQLServerMerge_0 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "MERGE INTO tb_device_category t1\n" +
|
||||
"USING ( select distinct device_category, factory_id from temp_device where device_category is not null\n" +
|
||||
") t2\n" +
|
||||
"on (t1.name = t2.device_category and t1.factory_id = t2.factory_id)\n" +
|
||||
"WHEN NOT MATCHED THEN\n" +
|
||||
"INSERT (name, factory_id) values (t2.device_category, t2.factory_id);";
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
parser.setParseCompleteValues(false);
|
||||
parser.setParseValuesSize(3);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
SQLMergeStatement mergeStmt = (SQLMergeStatement) stmt;
|
||||
|
||||
|
||||
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String formatSql = "MERGE INTO tb_device_category t1\n" +
|
||||
"USING (\n" +
|
||||
"\tSELECT DISTINCT device_category, factory_id\n" +
|
||||
"\tFROM temp_device\n" +
|
||||
"\tWHERE device_category IS NOT NULL\n" +
|
||||
") t2 ON (t1.name = t2.device_category\n" +
|
||||
"AND t1.factory_id = t2.factory_id) \n" +
|
||||
"WHEN NOT MATCHED THEN INSERT (name, factory_id) VALUES (t2.device_category, t2.factory_id);";
|
||||
assertEquals(formatSql, SQLUtils.toSQLServerString(mergeStmt));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user