Skip to content

Commit

Permalink
For #475 2th.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Dec 27, 2017
1 parent 8f06e81 commit ca3f02b
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword;
import io.shardingjdbc.core.parsing.lexer.token.Keyword;
import io.shardingjdbc.core.parsing.lexer.token.Token;
import io.shardingjdbc.core.parsing.parser.clause.TableReferencesClauseParser;
import io.shardingjdbc.core.parsing.parser.exception.SQLParsingException;
import io.shardingjdbc.core.parsing.parser.sql.SQLParser;
import io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingjdbc.core.parsing.parser.token.IndexToken;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -52,18 +55,30 @@ public DDLStatement parse() {
lexerEngine.nextToken();
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword());
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword());
DDLStatement result = new DDLStatement();
if (lexerEngine.equalAny(DefaultKeyword.INDEX)) {
lexerEngine.skipUntil(DefaultKeyword.ON);
parseIndex(result);
} else if (lexerEngine.equalAny(DefaultKeyword.TABLE)) {
lexerEngine.nextToken();
} else {
lexerEngine.unsupportedIfNotSkip(DefaultKeyword.TABLE);
lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName());
} else {
throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX.");
}
DDLStatement result = new DDLStatement();
tableReferencesClauseParser.parse(result, true);
return result;
}

private void parseIndex(final DDLStatement ddlStatement) {
lexerEngine.nextToken();
Token currentToken = lexerEngine.getCurrentToken();
int beginPosition = currentToken.getEndPosition() - currentToken.getLiterals().length();
String literals = currentToken.getLiterals();
lexerEngine.skipUntil(DefaultKeyword.ON);
lexerEngine.nextToken();
String tableName = lexerEngine.getCurrentToken().getLiterals();
ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName));
}

protected abstract Keyword[] getSkippedKeywordsBetweenCreateAndKeyword();

protected abstract Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.token;

import io.shardingjdbc.core.util.SQLUtil;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Index token.
*
* @author caohao
*/
@RequiredArgsConstructor
@ToString
public final class IndexToken implements SQLToken {

@Getter
private final int beginPosition;

@Getter
private final String originalLiterals;

private final String tableName;

/**
* Get index name.
*
* @return index name
*/
public String getIndexName() {
return SQLUtil.getExactlyValue(originalLiterals).toLowerCase();
}

/**
* Get table name.
*
* @return table name
*/
public String getTableName() {
return SQLUtil.getExactlyValue(tableName).toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected final Map<DatabaseType, ShardingDataSource> getDataSources() throws SQ

@Before
public void initDDLTables() throws SQLException {
if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP")) {
if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP TABLE")) {
if (getSql().contains("TEMP")) {
executeSql("CREATE TEMPORARY TABLE t_temp_log(id int, status varchar(10))");
} else {
Expand All @@ -108,7 +108,7 @@ public void initDDLTables() throws SQLException {

@After
public void cleanupDdlTables() throws SQLException {
if (getSql().startsWith("CREATE") || getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE")) {
if (getSql().startsWith("CREATE TABLE") || getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE")) {
if (getSql().contains("TEMP")) {
executeSql("DROP TABLE t_temp_log");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public final class Assert {
@XmlElement(name = "table-token")
private List<TableToken> tableTokens;

@XmlElement(name = "index-token")
private IndexToken indexToken;

@XmlElement(name = "items-token")
private ItemsToken itemsToken;

Expand Down Expand Up @@ -88,6 +91,9 @@ public List<SQLToken> getSqlTokens() {
if (null != tableTokens) {
result.addAll(tableTokens);
}
if (null != indexToken) {
result.add(indexToken);
}
if (null != offsetToken) {
result.add(offsetToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.jaxb;

import lombok.Getter;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public class IndexToken extends SQLToken {

@XmlAttribute(name = "original-literals")
private String originalLiterals;

@XmlAttribute(name = "table-name")
private String tableName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression;
import io.shardingjdbc.core.parsing.parser.jaxb.Value;
import io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken;
import io.shardingjdbc.core.parsing.parser.token.IndexToken;
import io.shardingjdbc.core.parsing.parser.token.ItemsToken;
import io.shardingjdbc.core.parsing.parser.token.MultipleInsertValuesToken;
import io.shardingjdbc.core.parsing.parser.token.OffsetToken;
Expand All @@ -27,10 +28,8 @@
import java.util.LinkedList;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class ParserAssertHelper {
Expand Down Expand Up @@ -88,34 +87,34 @@ public static void assertSqlTokens(final List<io.shardingjdbc.core.parsing.parse
if (null == expected || expected.size() == 0) {
return;
}
List<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> filteredSqlTokens = filterSqlToken(expected, isPreparedStatement);
Iterator<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> sqlTokenIterator = filteredSqlTokens.iterator();
List<SQLToken> expectedSqlTokens = buildExpectedSqlTokens(expected, isPreparedStatement);
for (SQLToken each : actual) {
SQLToken sqlToken = buildExpectedSQLToken(sqlTokenIterator.next(), isPreparedStatement);
assertTrue(EqualsBuilder.reflectionEquals(sqlToken, each, "originalLiterals"));
if (sqlToken instanceof TableToken) {
assertThat(((TableToken) each).getTableName(), is(((TableToken) sqlToken).getTableName()));
for (SQLToken sqlToken : expectedSqlTokens) {
if (each.getBeginPosition() == sqlToken.getBeginPosition()) {
assertTrue(EqualsBuilder.reflectionEquals(sqlToken, each));
}
}
}
assertFalse(sqlTokenIterator.hasNext());
}

private static List<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> filterSqlToken(final List<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> sqlTokens,
private static List<SQLToken> buildExpectedSqlTokens(final List<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> sqlTokens,
final boolean isPreparedStatement) {
List<io.shardingjdbc.core.parsing.parser.jaxb.SQLToken> result = new ArrayList<>(sqlTokens.size());
List<SQLToken> result = new ArrayList<>(sqlTokens.size());
for (io.shardingjdbc.core.parsing.parser.jaxb.SQLToken each : sqlTokens) {
if (isPreparedStatement && (each instanceof io.shardingjdbc.core.parsing.parser.jaxb.OffsetToken
|| each instanceof io.shardingjdbc.core.parsing.parser.jaxb.RowCountToken)) {
continue;
}
result.add(each);
result.add(buildExpectedSQLToken(each, isPreparedStatement));
}
return result;
}

private static SQLToken buildExpectedSQLToken(final io.shardingjdbc.core.parsing.parser.jaxb.SQLToken sqlToken, final boolean isPreparedStatement) {
if (sqlToken instanceof io.shardingjdbc.core.parsing.parser.jaxb.TableToken) {
return new TableToken(sqlToken.getBeginPosition(), ((io.shardingjdbc.core.parsing.parser.jaxb.TableToken) sqlToken).getOriginalLiterals());
} if (sqlToken instanceof io.shardingjdbc.core.parsing.parser.jaxb.IndexToken) {
return new IndexToken(sqlToken.getBeginPosition(), ((io.shardingjdbc.core.parsing.parser.jaxb.IndexToken) sqlToken).getOriginalLiterals(), ((io.shardingjdbc.core.parsing.parser.jaxb.IndexToken) sqlToken).getTableName());
} else if (sqlToken instanceof io.shardingjdbc.core.parsing.parser.jaxb.ItemsToken) {
ItemsToken itemsToken = new ItemsToken(sqlToken.getBeginPosition());
itemsToken.getItems().addAll(((io.shardingjdbc.core.parsing.parser.jaxb.ItemsToken) sqlToken).getItems());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.shardingjdbc.core.parsing.parser.jaxb.helper;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import io.shardingjdbc.core.constant.AggregationType;
import io.shardingjdbc.core.constant.OrderType;
import io.shardingjdbc.core.parsing.parser.context.OrderItem;
import io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem;
import io.shardingjdbc.core.parsing.parser.context.table.Table;
import io.shardingjdbc.core.parsing.parser.context.table.Tables;
import io.shardingjdbc.core.parsing.parser.jaxb.Assert;
import io.shardingjdbc.core.parsing.parser.jaxb.GroupByColumn;
import io.shardingjdbc.core.parsing.parser.jaxb.OrderByColumn;
import io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.List;

Expand All @@ -24,13 +26,13 @@ public static String[] getParameters(final String parameters) {
return parameters.split(",");
}

public static io.shardingjdbc.core.parsing.parser.context.table.Tables getTables(final io.shardingjdbc.core.parsing.parser.jaxb.Tables tables) {
io.shardingjdbc.core.parsing.parser.context.table.Tables result = new io.shardingjdbc.core.parsing.parser.context.table.Tables();
public static Tables getTables(final io.shardingjdbc.core.parsing.parser.jaxb.Tables tables) {
Tables result = new Tables();
if (null == tables) {
return result;
}
for (io.shardingjdbc.core.parsing.parser.jaxb.Table each : tables.getTables()) {
io.shardingjdbc.core.parsing.parser.context.table.Table table = new io.shardingjdbc.core.parsing.parser.context.table.Table(each.getName(), Optional.fromNullable(each.getAlias()));
Table table = new Table(each.getName(), Optional.fromNullable(each.getAlias()));
result.add(table);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.Sets;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
import io.shardingjdbc.core.constant.ShardingProperties;
import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
Expand All @@ -31,6 +32,7 @@
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -62,7 +64,7 @@ public static Collection init() {
}

@Test
public void assertWithDataSource() throws SQLException, URISyntaxException, IOException {
public void assertWithDataSource() throws SQLException, URISyntaxException, IOException, NoSuchFieldException, IllegalAccessException {
File yamlFile = new File(YamlShardingIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
Expand All @@ -75,6 +77,14 @@ public DataSource apply(final String key) {
}
}), yamlFile);
}
if (filePath.contains("WithProps.yaml")) {
Field field = dataSource.getClass().getDeclaredField("shardingProperties");
if (!field.isAccessible()) {
field.setAccessible(true);
}
ShardingProperties shardingProperties = (ShardingProperties) field.get(dataSource);
//assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW));
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
Expand Down
4 changes: 3 additions & 1 deletion sharding-jdbc-core/src/test/resources/parser/create.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@
<tables>
<table name="t_log" />
</tables>
<index-token begin-position="13" original-literals="t_log_index" table-name="t_log" />
<table-tokens>
<table-token begin-position="30" original-literals="t_log" />
<table-token begin-position="28" original-literals="t_log" />
</table-tokens>
</assert>
<assert id="assertCreateUniqueIndex">
<tables>
<table name="t_log" />
</tables>
<table-tokens>
<index-token begin-position="13" original-literals="t_log_index" table-name="t_log" />
<table-token begin-position="37" original-literals="t_log" />
</table-tokens>
</assert>
Expand Down
2 changes: 1 addition & 1 deletion sharding-jdbc-core/src/test/resources/parser/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
</assert>
<assert id="assertSelectWithBindingTable" parameters="1,2,9,10">
<tables>
<table name="T_order" alias="o"/>
<table name="t_order" alias="o"/>
<table name="t_order_item" alias="i"/>
</tables>
<table-tokens>
Expand Down
2 changes: 1 addition & 1 deletion sharding-jdbc-core/src/test/resources/sql/ddl/create.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<sql id="assertCreateGlobalTemporaryTable" value="CREATE GLOBAL TEMPORARY TABLE t_temp_log(id int, status varchar(10))" type="Oracle,PostgreSQL" />
<sql id="assertCreateLocalTempTable" value="CREATE LOCAL TEMP TABLE t_temp_log(id int, status varchar(10))" type="PostgreSQL" />
<sql id="assertCreateUnloggedTable" value="CREATE UNLOGGED TABLE t_log(id int, status varchar(10))" type="PostgreSQL" />
<sql id="assertCreateIndex" value="CREATE INDEX t_order_index ON t_log (id)" />
<sql id="assertCreateIndex" value="CREATE INDEX t_log_index ON t_log (id)" />
<sql id="assertCreateUniqueIndex" value="CREATE UNIQUE INDEX t_order_index ON t_log (id)" />
</sqls>
2 changes: 1 addition & 1 deletion sharding-jdbc-core/src/test/resources/sql/dql/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<sql id="assertSelectIterator" value="SELECT t.* FROM t_order_item t WHERE t.item_id IN (%s, %s)" />
<sql id="assertSelectNoShardingTable" value="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 ORDER BY i.item_id" />
<sql id="assertSelectLikeWithCount" value="SELECT count(0) as orders_count FROM `t_order` o WHERE o.status LIKE CONCAT('%%', %s, '%%') AND o.`user_id` IN (%s, %s) AND o.`order_id` BETWEEN %s AND %s" type="MySQL,H2" />
<sql id="assertSelectWithBindingTable" value="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" />
<sql id="assertSelectWithBindingTable" value="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" />
<sql id="assertSelectWithBindingTableAndConfigTable" value="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 %s AND %s AND c.status = %s ORDER BY i.item_id" />
<sql id="assertSelectCountWithBindingTable" value="SELECT COUNT(*) AS items_count FROM t_order o, t_order_item i WHERE o.user_id = i.user_id AND o.order_id = i.order_id AND o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s" />
<sql id="assertSelectCountWithBindingTableWithJoin" value="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" />
Expand Down

0 comments on commit ca3f02b

Please sign in to comment.