Skip to content

Commit

Permalink
fix #600
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Feb 15, 2018
1 parent 4e8ce68 commit 5a9d899
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,12 @@ public enum SQLType {
*
* <p>Such as {@code CREATE}, {@code ALTER}, {@code DROP}, {@code TRUNCATE}.</p>
*/
DDL
DDL,

/**
* Transaction Control Language.
*
* <p>Such as {@code SET}, {@code COMMIT}, {@code ROLLBACK}, {@code SAVEPOIINT}, {@code BEGIN}.</p>
*/
TCL
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ public ResultSet getResultSet() throws SQLException {
for (PreparedStatement each : routedStatements) {
resultSets.add(each.getResultSet());
}
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
if (routeResult.getSqlStatement() instanceof SelectStatement) {
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
} else {
currentResultSet = resultSets.get(0);
}
return currentResultSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ public ResultSet getResultSet() throws SQLException {
for (Statement each : routedStatements) {
resultSets.add(each.getResultSet());
}
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
if (routeResult.getSqlStatement() instanceof SelectStatement) {
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
} else {
currentResultSet = resultSets.get(0);
}
return currentResultSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public enum MySQLKeyword implements Keyword {
LIMIT,
OFFSET,
VALUE,
BEGIN,
FORCE,
PARTITION,
DISTINCTROW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public enum OracleKeyword implements Keyword {

LOCKED,
COMMIT,
CREATION,
UPDATED,
UPSERT,
Expand Down Expand Up @@ -75,8 +74,6 @@ public enum OracleKeyword implements Keyword {
MAXTRANS,
MINEXTENTS,
MAXEXTENTS,
BEGIN,
SAVEPOINT,
MATCHED,
LOB,
DIMENSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public enum SQLServerKeyword implements Keyword {
ONLY,
OUTPUT,
AUTO,
BEGIN,
IDENTITY,
BROWSE,
TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public enum DefaultKeyword implements Keyword {
HAVING,
UNION,

/*
TCL
*/
COMMIT,
ROLLBACK,
SAVEPOINT,
BEGIN,

/*
Other Command
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected TokenType[] getCustomizedAvailableKeywordsForSelectItemAlias() {
DefaultKeyword.TRUNCATE, DefaultKeyword.END, DefaultKeyword.FULL, DefaultKeyword.CLOSE, DefaultKeyword.CAST, DefaultKeyword.ESCAPE, DefaultKeyword.SOME, DefaultKeyword.UNTIL,
DefaultKeyword.OPEN, DefaultKeyword.OVER, DefaultKeyword.PASSWORD, DefaultKeyword.LOCAL, DefaultKeyword.GLOBAL, DefaultKeyword.STORAGE, DefaultKeyword.DATA,
DefaultKeyword.COALESCE, DefaultKeyword.VARCHAR2, DefaultKeyword.DATE, DefaultKeyword.TIME, DefaultKeyword.BOOLEAN, DefaultKeyword.ANY, DefaultKeyword.GREATEST,
DefaultKeyword.LEAST, DefaultKeyword.POSITION, DefaultKeyword.SUBSTRING, DefaultKeyword.TRIM,
MySQLKeyword.OFFSET, MySQLKeyword.VALUE, MySQLKeyword.BEGIN, MySQLKeyword.QUICK, MySQLKeyword.CACHE, MySQLKeyword.SQL_CACHE, MySQLKeyword.SQL_NO_CACHE, MySQLKeyword.SQL_BUFFER_RESULT,
DefaultKeyword.LEAST, DefaultKeyword.POSITION, DefaultKeyword.SUBSTRING, DefaultKeyword.TRIM, DefaultKeyword.BEGIN,
MySQLKeyword.OFFSET, MySQLKeyword.VALUE, MySQLKeyword.QUICK, MySQLKeyword.CACHE, MySQLKeyword.SQL_CACHE, MySQLKeyword.SQL_NO_CACHE, MySQLKeyword.SQL_BUFFER_RESULT,
MySQLKeyword.FIRST, MySQLKeyword.ALGORITHM, MySQLKeyword.DISCARD, MySQLKeyword.IMPORT, MySQLKeyword.VALIDATION, MySQLKeyword.REORGANIZE, MySQLKeyword.EXCHANGE, MySQLKeyword.REBUILD,
MySQLKeyword.REPAIR, MySQLKeyword.REMOVE, MySQLKeyword.UPGRADE, MySQLKeyword.KEY_BLOCK_SIZE, MySQLKeyword.AUTO_INCREMENT, MySQLKeyword.AVG_ROW_LENGTH, MySQLKeyword.CHECKSUM,
MySQLKeyword.COMPRESSION, MySQLKeyword.CONNECTION, MySQLKeyword.DIRECTORY, MySQLKeyword.DELAY_KEY_WRITE, MySQLKeyword.ENCRYPTION, MySQLKeyword.ENGINE, MySQLKeyword.INSERT_METHOD,
Expand All @@ -41,8 +41,9 @@ protected TokenType[] getCustomizedAvailableKeywordsForTableAlias() {
return new TokenType[]{
DefaultKeyword.FUNCTION, DefaultKeyword.COMMENT, DefaultKeyword.ROW, DefaultKeyword.EXECUTE, DefaultKeyword.MODIFY,
DefaultKeyword.VALIDATE, DefaultKeyword.USER, DefaultKeyword.IDENTIFIED, DefaultKeyword.END, DefaultKeyword.CLOSE, DefaultKeyword.CAST,
DefaultKeyword.ESCAPE, DefaultKeyword.SOME, DefaultKeyword.OPEN, DefaultKeyword.OVER, DefaultKeyword.COALESCE, DefaultKeyword.VARCHAR2, DefaultKeyword.DATE, DefaultKeyword.ANY,
MySQLKeyword.OFFSET, MySQLKeyword.BEGIN, MySQLKeyword.QUICK, MySQLKeyword.CACHE, MySQLKeyword.SQL_CACHE, MySQLKeyword.SQL_NO_CACHE, MySQLKeyword.SQL_BUFFER_RESULT,
DefaultKeyword.ESCAPE, DefaultKeyword.SOME, DefaultKeyword.OPEN, DefaultKeyword.OVER, DefaultKeyword.COALESCE, DefaultKeyword.VARCHAR2,
DefaultKeyword.DATE, DefaultKeyword.ANY, DefaultKeyword.BEGIN,
MySQLKeyword.OFFSET, MySQLKeyword.QUICK, MySQLKeyword.CACHE, MySQLKeyword.SQL_CACHE, MySQLKeyword.SQL_NO_CACHE, MySQLKeyword.SQL_BUFFER_RESULT,
MySQLKeyword.FIRST, MySQLKeyword.ALGORITHM, MySQLKeyword.DISCARD, MySQLKeyword.IMPORT, MySQLKeyword.VALIDATION, MySQLKeyword.REORGANIZE, MySQLKeyword.EXCHANGE, MySQLKeyword.REBUILD,
MySQLKeyword.REPAIR, MySQLKeyword.KEY_BLOCK_SIZE, MySQLKeyword.AUTO_INCREMENT, MySQLKeyword.AVG_ROW_LENGTH, MySQLKeyword.CHECKSUM,
MySQLKeyword.COMPRESSION, MySQLKeyword.CONNECTION, MySQLKeyword.DIRECTORY, MySQLKeyword.DELAY_KEY_WRITE, MySQLKeyword.ENCRYPTION, MySQLKeyword.ENGINE, MySQLKeyword.INSERT_METHOD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ protected TokenType[] getCustomizedAvailableKeywordsForSelectItemAlias() {
DefaultKeyword.STORAGE, DefaultKeyword.DATA, DefaultKeyword.COALESCE, DefaultKeyword.CHARACTER, DefaultKeyword.VARYING, DefaultKeyword.INT, DefaultKeyword.DEC, DefaultKeyword.NUMERIC,
DefaultKeyword.REAL, DefaultKeyword.DOUBLE, DefaultKeyword.PRECISION, DefaultKeyword.TIME, DefaultKeyword.INTERVAL, DefaultKeyword.BOOLEAN, DefaultKeyword.BLOB, DefaultKeyword.XOR,
DefaultKeyword.GREATEST, DefaultKeyword.LEAST, DefaultKeyword.POSITION, DefaultKeyword.SUBSTRING, DefaultKeyword.TRIM, DefaultKeyword.BOTH, DefaultKeyword.LEADING,
DefaultKeyword.TRAILING, DefaultKeyword.CONVERT, DefaultKeyword.CONSTRAINT, DefaultKeyword.PRIMARY, DefaultKeyword.FOREIGN, DefaultKeyword.KEY, DefaultKeyword.REFERENCES,
OracleKeyword.LOCKED, OracleKeyword.COMMIT, OracleKeyword.CREATION, OracleKeyword.UPDATED, OracleKeyword.UPSERT, OracleKeyword.CONNECT_BY_ROOT,
DefaultKeyword.TRAILING, DefaultKeyword.CONVERT, DefaultKeyword.CONSTRAINT, DefaultKeyword.PRIMARY, DefaultKeyword.FOREIGN, DefaultKeyword.KEY,
DefaultKeyword.REFERENCES, DefaultKeyword.COMMIT, DefaultKeyword.BEGIN, DefaultKeyword.SAVEPOINT,
OracleKeyword.LOCKED, OracleKeyword.CREATION, OracleKeyword.UPDATED, OracleKeyword.UPSERT, OracleKeyword.CONNECT_BY_ROOT,
OracleKeyword.STORE, OracleKeyword.MERGE, OracleKeyword.PURGE, OracleKeyword.GOTO, OracleKeyword.ONLY, OracleKeyword.AUTOMATIC, OracleKeyword.MAIN,
OracleKeyword.PCTINCREASE, OracleKeyword.CHUNK, OracleKeyword.LIMIT, OracleKeyword.GROUPING, OracleKeyword.ROLLUP, OracleKeyword.CUBE,
OracleKeyword.UNLIMITED, OracleKeyword.SIBLINGS, OracleKeyword.INCLUDE, OracleKeyword.EXCLUDE, OracleKeyword.PIVOT, OracleKeyword.UNPIVOT,
OracleKeyword.EXCEPTION, OracleKeyword.EXCEPTIONS, OracleKeyword.ERRORS, OracleKeyword.DEFERRED, OracleKeyword.NAV, OracleKeyword.VERSIONS,
OracleKeyword.WAIT, OracleKeyword.SAMPLE, OracleKeyword.CONTINUE, OracleKeyword.TIMESTAMP, OracleKeyword.SEGMENT, OracleKeyword.PARTITION,
OracleKeyword.SUBPARTITION, OracleKeyword.RETURN, OracleKeyword.RETURNING, OracleKeyword.REJECT, OracleKeyword.MAXTRANS, OracleKeyword.MINEXTENTS,
OracleKeyword.BEGIN, OracleKeyword.SAVEPOINT, OracleKeyword.MATCHED, OracleKeyword.LOB, OracleKeyword.DIMENSION, OracleKeyword.FORCE, OracleKeyword.FIRST,
OracleKeyword.MATCHED, OracleKeyword.LOB, OracleKeyword.DIMENSION, OracleKeyword.FORCE, OracleKeyword.FIRST,
OracleKeyword.NEXT, OracleKeyword.LAST, OracleKeyword.EXTRACT, OracleKeyword.RULES, OracleKeyword.INITIALLY, OracleKeyword.KEEP, OracleKeyword.KEEP_DUPLICATES,
OracleKeyword.REFERENCE, OracleKeyword.SEED, OracleKeyword.IGNORE, OracleKeyword.MEASURES, OracleKeyword.LOGGING, OracleKeyword.MAXSIZE, OracleKeyword.FLASH_CACHE,
OracleKeyword.CELL_FLASH_CACHE, OracleKeyword.SKIP, OracleKeyword.NONE, OracleKeyword.NULLS, OracleKeyword.SINGLE, OracleKeyword.SCN, OracleKeyword.INITRANS,
Expand All @@ -58,13 +59,13 @@ protected TokenType[] getCustomizedAvailableKeywordsForTableAlias() {
DefaultKeyword.OVER, DefaultKeyword.LOOP, DefaultKeyword.EXPLAIN, DefaultKeyword.COALESCE, DefaultKeyword.CHARACTER, DefaultKeyword.VARYING, DefaultKeyword.INT,
DefaultKeyword.DEC, DefaultKeyword.NUMERIC, DefaultKeyword.REAL, DefaultKeyword.DOUBLE, DefaultKeyword.PRECISION, DefaultKeyword.INTERVAL, DefaultKeyword.BLOB,
DefaultKeyword.XOR, DefaultKeyword.BOTH, DefaultKeyword.LEADING, DefaultKeyword.TRAILING, DefaultKeyword.CONVERT, DefaultKeyword.CONSTRAINT, DefaultKeyword.PRIMARY,
DefaultKeyword.FOREIGN, DefaultKeyword.KEY, DefaultKeyword.REFERENCES,
OracleKeyword.LOCKED, OracleKeyword.COMMIT, OracleKeyword.CREATION, OracleKeyword.UPDATED, OracleKeyword.UPSERT, OracleKeyword.STORE, OracleKeyword.MERGE,
DefaultKeyword.FOREIGN, DefaultKeyword.KEY, DefaultKeyword.REFERENCES, DefaultKeyword.BEGIN, DefaultKeyword.SAVEPOINT, DefaultKeyword.COMMIT,
OracleKeyword.LOCKED, OracleKeyword.CREATION, OracleKeyword.UPDATED, OracleKeyword.UPSERT, OracleKeyword.STORE, OracleKeyword.MERGE,
OracleKeyword.PURGE, OracleKeyword.GOTO, OracleKeyword.ONLY, OracleKeyword.AUTOMATIC, OracleKeyword.MAIN, OracleKeyword.PCTINCREASE, OracleKeyword.CHUNK,
OracleKeyword.LIMIT, OracleKeyword.GROUPING, OracleKeyword.ROLLUP, OracleKeyword.CUBE, OracleKeyword.UNLIMITED, OracleKeyword.SIBLINGS, OracleKeyword.INCLUDE,
OracleKeyword.EXCLUDE, OracleKeyword.PIVOT, OracleKeyword.UNPIVOT, OracleKeyword.EXCEPTION, OracleKeyword.EXCEPTIONS, OracleKeyword.ERRORS, OracleKeyword.DEFERRED,
OracleKeyword.NAV, OracleKeyword.VERSIONS, OracleKeyword.WAIT, OracleKeyword.CONTINUE, OracleKeyword.TIMESTAMP, OracleKeyword.SEGMENT, OracleKeyword.RETURN,
OracleKeyword.RETURNING, OracleKeyword.REJECT, OracleKeyword.MAXTRANS, OracleKeyword.MINEXTENTS, OracleKeyword.BEGIN, OracleKeyword.SAVEPOINT, OracleKeyword.MATCHED,
OracleKeyword.RETURNING, OracleKeyword.REJECT, OracleKeyword.MAXTRANS, OracleKeyword.MINEXTENTS, OracleKeyword.MATCHED,
OracleKeyword.LOB, OracleKeyword.DIMENSION, OracleKeyword.FORCE, OracleKeyword.FIRST, OracleKeyword.NEXT, OracleKeyword.LAST, OracleKeyword.EXTRACT, OracleKeyword.RULES,
OracleKeyword.INITIALLY, OracleKeyword.KEEP, OracleKeyword.KEEP_DUPLICATES, OracleKeyword.REFERENCE, OracleKeyword.SEED, OracleKeyword.IGNORE, OracleKeyword.MEASURES,
OracleKeyword.LOGGING, OracleKeyword.MAXSIZE, OracleKeyword.FLASH_CACHE, OracleKeyword.CELL_FLASH_CACHE, OracleKeyword.SKIP, OracleKeyword.NONE, OracleKeyword.NULLS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.shardingjdbc.core.parsing.parser.sql;

import io.shardingjdbc.core.parsing.parser.sql.tcl.TCLParserFactory;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
Expand Down Expand Up @@ -72,6 +73,12 @@ public static SQLParser newInstance(final DatabaseType dbType, final TokenType t
return DropParserFactory.newInstance(dbType, shardingRule, lexerEngine);
case TRUNCATE:
return TruncateParserFactory.newInstance(dbType, shardingRule, lexerEngine);
case SET:
case COMMIT:
case ROLLBACK:
case SAVEPOINT:
case BEGIN:
return TCLParserFactory.newInstance(dbType, shardingRule, lexerEngine);
default:
throw new SQLParsingUnsupportedException(lexerEngine.getCurrentToken().getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@

import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.parsing.parser.sql.AbstractSQLStatement;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
* DDL statement.
*
* @author zhangliang
*/
@Getter
@Setter
@ToString(callSuper = true)
public final class DDLStatement extends AbstractSQLStatement {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@

import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.parsing.parser.sql.AbstractSQLStatement;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
* DML statement.
*
* @author zhangliang
*/
@Getter
@Setter
@ToString(callSuper = true)
public class DMLStatement extends AbstractSQLStatement {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@

import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.parsing.parser.sql.AbstractSQLStatement;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
* DQL statement.
*
* @author zhangliang
*/
@Getter
@Setter
@ToString(callSuper = true)
public class DQLStatement extends AbstractSQLStatement {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/

package io.shardingjdbc.core.parsing.parser.sql.tcl;

import io.shardingjdbc.core.parsing.parser.sql.SQLParser;

/**
* TCL parser.
*
* @author zhangliang
*/
public final class TCLParser implements SQLParser {

@Override
public TCLStatement parse() {
return new TCLStatement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/

package io.shardingjdbc.core.parsing.parser.sql.tcl;

import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* Transaction Control Language parser factory.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TCLParserFactory {

/**
* Create TCL parser instance.
*
* @param dbType database type
* @param shardingRule databases and tables sharding rule
* @param lexerEngine lexical analysis engine.
* @return create parser instance
*/
public static TCLParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) {
return new TCLParser();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/

package io.shardingjdbc.core.parsing.parser.sql.tcl;

import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.parsing.parser.sql.AbstractSQLStatement;
import lombok.ToString;

/**
* Transaction Control Language statement.
*
* @author zhangliang
*/
@ToString(callSuper = true)
public final class TCLStatement extends AbstractSQLStatement {

public TCLStatement() {
super(SQLType.TCL);
}
}
8 changes: 8 additions & 0 deletions sharding-jdbc-core/src/test/resources/parser/tcl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<asserts>
<assert id="setAutoCommit" />
<assert id="commit" value="COMMIT" />
<assert id="rollback" value="ROLLBACK" />
<assert id="savepoint" value="SAVEPOINT" />
<assert id="begin" value="BEGIN" />
</asserts>
8 changes: 8 additions & 0 deletions sharding-jdbc-core/src/test/resources/sql/tcl/tcl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<sqls>
<sql id="setAutoCommit" value="SET AUTOCOMMIT=1" />
<sql id="commit" value="COMMIT" />
<sql id="rollback" value="ROLLBACK" />
<sql id="savepoint" value="SAVEPOINT" />
<sql id="begin" value="BEGIN" />
</sqls>

0 comments on commit 5a9d899

Please sign in to comment.