提交 c5f334e2 编写于 作者: C codefairy08

#1865 bug fixs

上级 142a3ce2
......@@ -112,17 +112,9 @@ public final class OrConditionFiller implements SQLStatementFiller<OrConditionSe
if (null == condition.getColumn()) {
continue;
}
if (condition.getColumn().getOwner().isPresent() && sqlStatement.getTables().getTableNames().contains(condition.getColumn().getOwner().get())) {
String owner = condition.getColumn().getOwner().get();
sqlStatement.addSQLToken(new TableToken(condition.getColumn().getStartIndex(),
0, SQLUtil.getExactlyValue(owner), SQLUtil.getLeftDelimiter(owner), SQLUtil.getRightDelimiter(owner)));
}
addTableTokenForColumn(sqlStatement, condition.getColumn());
if (condition.getExpression() instanceof ColumnSegment) {
ColumnSegment rightColumn = (ColumnSegment) condition.getExpression();
if (rightColumn.getOwner().isPresent() && sqlStatement.getTables().getTableNames().contains(rightColumn.getOwner().get())) {
String owner = rightColumn.getOwner().get();
sqlStatement.addSQLToken(new TableToken(rightColumn.getStartIndex(), 0, SQLUtil.getExactlyValue(owner), SQLUtil.getLeftDelimiter(owner), SQLUtil.getRightDelimiter(owner)));
}
addTableTokenForColumn(sqlStatement, (ColumnSegment) condition.getExpression());
needSharding = true;
continue;
}
......@@ -141,6 +133,16 @@ public final class OrConditionFiller implements SQLStatementFiller<OrConditionSe
return result;
}
private void addTableTokenForColumn(final SQLStatement sqlStatement, final ColumnSegment column) {
if (column.getOwner().isPresent()) {
String owner = column.getOwner().get();
Optional<Table> logicTable = sqlStatement.getTables().find(owner);
if (logicTable.isPresent() && !logicTable.get().getAlias().isPresent()) {
sqlStatement.addSQLToken(new TableToken(column.getStartIndex(), 0, SQLUtil.getExactlyValue(owner), SQLUtil.getLeftDelimiter(owner), SQLUtil.getRightDelimiter(owner)));
}
}
}
private void fillResult(final SQLStatement sqlStatement, final ShardingRule shardingRule, final OrCondition orCondition, final List<ConditionSegment> shardingCondition, final String sql) {
if (shardingCondition.isEmpty()) {
return;
......
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.core.parsing.antlr.filler.impl.dql;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parsing.antlr.filler.SQLStatementFiller;
import org.apache.shardingsphere.core.parsing.antlr.sql.segment.SQLSegment;
......@@ -32,7 +31,6 @@ import org.apache.shardingsphere.core.parsing.parser.context.selectitem.Aggregat
import org.apache.shardingsphere.core.parsing.parser.context.selectitem.AggregationSelectItem;
import org.apache.shardingsphere.core.parsing.parser.context.selectitem.CommonSelectItem;
import org.apache.shardingsphere.core.parsing.parser.context.selectitem.StarSelectItem;
import org.apache.shardingsphere.core.parsing.parser.context.table.Table;
import org.apache.shardingsphere.core.parsing.parser.sql.SQLStatement;
import org.apache.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import org.apache.shardingsphere.core.parsing.parser.token.AggregationDistinctToken;
......@@ -40,6 +38,8 @@ import org.apache.shardingsphere.core.parsing.parser.token.TableToken;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.util.SQLUtil;
import com.google.common.base.Optional;
/**
* Select item filler.
*
......@@ -79,11 +79,7 @@ public final class SelectItemFiller implements SQLStatementFiller {
selectStatement.setContainStar(true);
Optional<String> owner = selectItemSegment.getOwner();
selectStatement.getItems().add(new StarSelectItem(owner.orNull()));
if (!owner.isPresent()) {
return;
}
Optional<Table> table = selectStatement.getTables().find(owner.get());
if (table.isPresent() && !table.get().getAlias().isPresent()) {
if (owner.isPresent() && !selectStatement.getTables().isTableAlias(owner.get())) {
selectStatement.addSQLToken(new TableToken(selectItemSegment.getStartIndex(),
0, SQLUtil.getExactlyValue(owner.get()), SQLUtil.getLeftDelimiter(owner.get()), SQLUtil.getRightDelimiter(owner.get())));
}
......@@ -91,7 +87,7 @@ public final class SelectItemFiller implements SQLStatementFiller {
private void fillColumnSelectItemSegment(final ColumnSelectItemSegment selectItemSegment, final SelectStatement selectStatement) {
Optional<String> owner = selectItemSegment.getOwner();
if (owner.isPresent() && selectStatement.getTables().getTableNames().contains(owner.get())) {
if (owner.isPresent() && !selectStatement.getTables().isTableAlias(owner.get())) {
selectStatement.addSQLToken(new TableToken(selectItemSegment.getStartIndex(),
0, SQLUtil.getExactlyValue(owner.get()), SQLUtil.getLeftDelimiter(owner.get()), SQLUtil.getRightDelimiter(owner.get())));
}
......
......@@ -129,4 +129,15 @@ public final class Tables {
}
return Optional.absent();
}
/**
* Is table alias.
*
* @param alias alias name
* @return boolean
*/
public boolean isTableAlias(final String alias) {
Optional<Table> tableFromName = findTableFromAlias(alias);
return tableFromName.isPresent();
}
}
......@@ -21,6 +21,25 @@
</or-condition>
</parser-result>
<parser-result sql-case-id="select_with_same_table_name_and_alias_column_with_owner" parameters="1, 1">
<tables>
<table name="t_order" alias="t_order" />
</tables>
<tokens>
<table-token start-index="52" table-name="t_order" />
</tokens>
<or-condition>
<and-condition>
<condition column-name="user_id" table-name="t_order" operator="EQUAL">
<value index="0" literal="1" type="int" />
</condition>
<condition column-name="order_id" table-name="t_order" operator="EQUAL">
<value index="1" literal="1" type="int" />
</condition>
</and-condition>
</or-condition>
</parser-result>
<parser-result sql-case-id="select_not_equal_with_single_table" parameters="1">
<tables>
<table name="t_order_item" />
......
......@@ -8,6 +8,11 @@
<assertion parameters="12:int, 1000:int" expected-data-file="empty_order.xml" />
</dql-test-case>
<dql-test-case sql-case-id="select_with_same_table_name_and_alias_column_with_owner">
<assertion parameters="10:int, 1000:int" expected-data-file="select_equal_with_single_table.xml" />
<assertion parameters="12:int, 1000:int" expected-data-file="empty_order.xml" />
</dql-test-case>
<dql-test-case sql-case-id="select_not_equal_with_single_table">
<assertion parameters="100001:int" expected-data-file="select_not_equal_with_single_table.xml" />
</dql-test-case>
......
......@@ -2,6 +2,7 @@
<sql-cases>
<sql-case id="select_constant_without_table" value="SELECT 1 as a" />
<sql-case id="select_with_same_table_name_and_alias" value="SELECT t_order.* FROM t_order t_order WHERE user_id = ? AND order_id = ?" />
<sql-case id="select_with_same_table_name_and_alias_column_with_owner" value="SELECT t_order.order_id,t_order.user_id,status FROM t_order t_order WHERE t_order.user_id = ? AND order_id = ?" db-types="MySQL,H2"/>
<sql-case id="select_not_equal_with_single_table" value="SELECT * FROM t_order_item WHERE item_id &lt;&gt; ? ORDER BY item_id" />
<sql-case id="select_exclamation_equal_with_single_table" value="SELECT * FROM t_order_item WHERE item_id != ? ORDER BY item_id" />
<sql-case id="select_not_in_with_single_table" value="SELECT * FROM t_order_item WHERE item_id IS NOT NULL AND item_id NOT IN (?, ?) ORDER BY item_id" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册