提交 360f07b4 编写于 作者: H haocao

Add oracle group by support.

上级 4f5dc3f9
......@@ -45,6 +45,9 @@ public enum OracleKeyword implements Keyword {
PCTINCREASE,
CHUNK,
LIMIT,
GROUPING,
ROLLUP,
CUBE,
UNLIMITED,
SIBLINGS,
INCLUDE,
......
......@@ -82,6 +82,7 @@ public class SQLParser extends AbstractParser {
*
* @return 表达式
*/
// TODO 完善Expression解析的各种场景
public final SQLExpression parseExpression() {
String literals = getLexer().getCurrentToken().getLiterals();
final SQLExpression expression = getExpression(literals);
......
......@@ -170,40 +170,23 @@ public class OracleSelectParser extends AbstractSelectParser {
@Override
protected void parseGroupBy() {
// TODO
// if (getSqlParser().equalAny(DefaultKeyword.GROUP)) {
// getSqlParser().getLexer().nextToken();
// getSqlParser().accept(DefaultKeyword.BY);
// while (true) {
// if (getSqlParser().getLexer().identifierEquals("GROUPING")) {
// throw new UnsupportedOperationException("Cannot support GROUPING SETS");
// }
// addGroupByItem(getSqlParser().expr());
// if (!getSqlParser().equalAny(Symbol.COMMA)) {
// break;
// }
// getSqlParser().getLexer().nextToken();
// }
// if (getSqlParser().skipIfEqual(Token.HAVING)) {
// getSqlParser().expr();
// }
// } else if (getSqlParser().skipIfEqual(Token.HAVING)) {
// SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
// groupBy.setHaving(getSqlParser().expr());
// if (getSqlParser().skipIfEqual(DefaultKeyword.GROUP)) {
// getSqlParser().accept(DefaultKeyword.BY);
// while (true) {
// if (getSqlParser().getLexer().identifierEquals("GROUPING")) {
// throw new UnsupportedOperationException("Cannot support GROUPING SETS");
// }
// addGroupByItem(getSqlParser().expr());
// if (!getSqlParser().equalAny(Symbol.COMMA)) {
// break;
// }
// getSqlParser().getLexer().nextToken();
// }
// }
// }
if (getSqlParser().equalAny(DefaultKeyword.GROUP)) {
getSqlParser().getLexer().nextToken();
getSqlParser().accept(DefaultKeyword.BY);
while (true) {
if (getSqlParser().equalAny(OracleKeyword.ROLLUP, OracleKeyword.CUBE, OracleKeyword.GROUPING)) {
throw new UnsupportedOperationException("Cannot support ROLLUP, CUBE, GROUPING SETS");
}
addGroupByItem(getSqlParser().parseExpression());
if (!getSqlParser().equalAny(Symbol.COMMA)) {
break;
}
getSqlParser().getLexer().nextToken();
}
if (getSqlParser().skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
}
}
@Override
......
......@@ -221,10 +221,10 @@ public abstract class AbstractSelectParser implements SQLStatementParser {
sqlParser.getLexer().nextToken();
}
if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
sqlParser.parseExpression(selectStatement);
throw new UnsupportedOperationException("Cannot support Having");
}
} else if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
sqlParser.parseExpression(selectStatement);
throw new UnsupportedOperationException("Cannot support Having");
}
}
......
......@@ -19,6 +19,7 @@ package com.dangdang.ddframe.rdb.integrate;
import com.dangdang.ddframe.rdb.integrate.sql.DatabaseTestSQL;
import com.dangdang.ddframe.rdb.integrate.sql.mysql.MySQLTestSQL;
import com.dangdang.ddframe.rdb.integrate.sql.oracle.OracleSQLTestSQL;
import com.dangdang.ddframe.rdb.integrate.sql.postgresql.PostgreSQLTestSQL;
import com.dangdang.ddframe.rdb.sharding.constant.DatabaseType;
import org.apache.commons.dbcp.BasicDataSource;
......@@ -47,8 +48,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.dangdang.ddframe.rdb.sharding.constant.DatabaseType.H2;
import static com.dangdang.ddframe.rdb.sharding.constant.DatabaseType.MySQL;
import static com.dangdang.ddframe.rdb.sharding.constant.DatabaseType.*;
import static org.dbunit.Assertion.assertEquals;
public abstract class AbstractDBUnitTest {
......@@ -101,8 +101,9 @@ public abstract class AbstractDBUnitTest {
case MySQL:
return new MySQLTestSQL();
case PostgreSQL:
case Oracle:
return new PostgreSQLTestSQL();
case Oracle:
return new OracleSQLTestSQL();
default:
throw new UnsupportedOperationException(dbEnv.getDatabaseType().name());
}
......
......@@ -94,7 +94,7 @@ public abstract class AbstractDatabaseTestSQL implements DatabaseTestSQL {
+ "AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s GROUP BY o.user_id";
private static final String SELECT_GROUP_WITH_BINDING_TABLE_AND_CONFIG_SQL = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id "
+ "JOIN t_config c ON o.status = c.status WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN ? AND ? AND c.status = %s ORDER BY i.item_id";
+ "JOIN t_config c ON o.status = c.status WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s AND c.status = %s ORDER BY i.item_id";
private static final String SELECT_GROUP_WITHOUT_GROUPED_COLUMN_SQL = "SELECT count(*) as items_count FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id"
+ " WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s GROUP BY o.user_id";
......
/*
* 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 com.dangdang.ddframe.rdb.integrate.sql.oracle;
import com.dangdang.ddframe.rdb.integrate.sql.AbstractDatabaseTestSQL;
public final class OracleSQLTestSQL extends AbstractDatabaseTestSQL {
private static final String SELECT_LIMIT_WITH_BINDING_TABLE_SQL = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id"
+ " WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s ORDER BY i.item_id DESC OFFSET %s LIMIT %s";
@Override
public String getSelectLimitWithBindingTableSql() {
return SELECT_LIMIT_WITH_BINDING_TABLE_SQL;
}
}
......@@ -107,7 +107,8 @@ public final class ShardingTablesOnlyForPreparedStatementWithSelectTest extends
@Test
public void assertSelectGroupByWithoutGroupedColumn() throws SQLException, DatabaseUnitException {
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) ? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectGroupByWithoutGroupedColumn.xml"
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) || Oracle.name().equalsIgnoreCase(currentDbType())
? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectGroupByWithoutGroupedColumn.xml"
: TABLE_ONLY_PREFIX + "/expect/select/SelectGroupByWithoutGroupedColumn.xml";
assertDataSet(expectedDataSetFile, getShardingDataSource().getConnection(),
"t_order_item", replacePreparedStatement(sql.getSelectGroupWithoutGroupedColumnSql()), 10, 11, 1000, 1109);
......@@ -116,7 +117,8 @@ public final class ShardingTablesOnlyForPreparedStatementWithSelectTest extends
@Test
public void assertSelectNoShardingTable() throws SQLException, DatabaseUnitException {
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) ? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectNoShardingTable.xml"
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) || Oracle.name().equalsIgnoreCase(currentDbType())
? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectNoShardingTable.xml"
: TABLE_ONLY_PREFIX + "/expect/select/SelectNoShardingTable.xml";
assertDataSet(expectedDataSetFile, getShardingDataSource().getConnection(),
"t_order_item", sql.getSelectWithNoShardingTableSql());
......@@ -124,7 +126,8 @@ public final class ShardingTablesOnlyForPreparedStatementWithSelectTest extends
@Test
public void assertSelectWithBindingTableAndConfigTable() throws SQLException, DatabaseUnitException {
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) ? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectWithBindingTableAndConfigTable.xml"
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) || Oracle.name().equalsIgnoreCase(currentDbType())
? TABLE_ONLY_PREFIX + "/expect/select/postgresql/SelectWithBindingTableAndConfigTable.xml"
: TABLE_ONLY_PREFIX + "/expect/select/SelectWithBindingTableAndConfigTable.xml";
assertDataSet(expectedDataSetFile, shardingDataSource.getConnection(),
"t_order_item", replacePreparedStatement(sql.getSelectGroupWithBindingTableAndConfigSql()), 10, 11, 1009, 1108, "init");
......
......@@ -86,7 +86,8 @@ public final class ShardingTablesOnlyForStatementWithSelectTest extends Abstract
@Test
public void assertSelectGroupByWithoutGroupedColumn() throws SQLException, DatabaseUnitException {
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) ? "integrate/dataset/tbl/expect/select/postgresql/SelectGroupByWithoutGroupedColumn.xml"
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) || Oracle.name().equalsIgnoreCase(currentDbType())
? "integrate/dataset/tbl/expect/select/postgresql/SelectGroupByWithoutGroupedColumn.xml"
: "integrate/dataset/tbl/expect/select/SelectGroupByWithoutGroupedColumn.xml";
assertDataSet(expectedDataSetFile, shardingDataSource.getConnection(),
"t_order_item", String.format(sql.getSelectGroupWithoutGroupedColumnSql(), 10, 11, 1000, 1109));
......@@ -96,7 +97,8 @@ public final class ShardingTablesOnlyForStatementWithSelectTest extends Abstract
@Test
public void assertSelectWithBindingTableAndConfigTable() throws SQLException, DatabaseUnitException {
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) ? "integrate/dataset/tbl/expect/select/postgresql/SelectWithBindingTableAndConfigTable.xml"
String expectedDataSetFile = PostgreSQL.name().equalsIgnoreCase(currentDbType()) || Oracle.name().equalsIgnoreCase(currentDbType())
? "integrate/dataset/tbl/expect/select/postgresql/SelectWithBindingTableAndConfigTable.xml"
: "integrate/dataset/tbl/expect/select/SelectWithBindingTableAndConfigTable.xml";
assertDataSet(expectedDataSetFile, shardingDataSource.getConnection(),
"t_order_item", String.format(sql.getSelectGroupWithBindingTableAndConfigSql(), 10, 11, 1009, 1108, "'init'"));
......
<dataset>
<t_order_item group_by_derived_0="20" items_count="20" />
<t_order_item group_by_derived_0="20" items_count="20" />
</dataset>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册