Skip to content

Commit

Permalink
Conditions结构优化
Browse files Browse the repository at this point in the history
  • Loading branch information
maxiaoguang64 committed Apr 9, 2018
1 parent bff358a commit 5d14141
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.context.condition;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.LinkedList;
import java.util.List;

/**
* And conditions.
*
* @author maxiaoguang
*/
@RequiredArgsConstructor
@Getter
@ToString
public class AndConditions {

private final List<Condition> conditions = new LinkedList<>();

/**
* Add condition.
*
* @param condition condition
*/
public void add(final Condition condition) {
conditions.add(condition);
}

/**
* Find condition via column.
*
* @param column column
* @return found condition
*/
public Optional<Condition> find(final Column column) {
Condition result = null;
for (Condition each : conditions) {
if (Objects.equal(each.getColumn(), column)) {
result = each;
}
}
return Optional.fromNullable(result);
}

/**
* Get condition via index.
*
* @param index index of conditions
* @return found condition
*/
public Optional<Condition> get(int index) {
Condition result = null;
if (size() > index) {
result = conditions.get(index);
}
return Optional.fromNullable(result);
}

/**
* Adjust and conditions is empty or not.
*
* @return and conditions is empty or not
*/
public boolean isEmpty() {
return conditions.isEmpty();
}

/**
* Returns the number of conditions in this.
*
* @return the number of conditions in this
*/
public int size() {
return conditions.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@

package io.shardingjdbc.core.parsing.parser.context.condition;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.LinkedList;
import java.util.List;

/**
* Conditions collection.
*
Expand All @@ -37,10 +33,10 @@
@ToString
public final class Conditions {

private final List<List<Condition>> orConditions = new LinkedList<>();
private final OrConditions orConditions = new OrConditions();

public Conditions(final Conditions conditions) {
orConditions.addAll(conditions.orConditions);
orConditions.getAndConditions().addAll(conditions.orConditions.getAndConditions());
}

/**
Expand All @@ -49,18 +45,10 @@ public Conditions(final Conditions conditions) {
* @param condition condition
* @param shardingRule databases and tables sharding rule
*/
// TODO adjust before add condition, eg: if condition exist = operator and include same column, should remove condition (tow equal condition should found nothing)
public void add(final Condition condition, final ShardingRule shardingRule) {
// TODO self-join has problem, table name maybe use alias
if (shardingRule.isShardingColumn(condition.getColumn())) {
List<Condition> firstAndConditions;
if (orConditions.isEmpty()) {
firstAndConditions = new LinkedList<>();
orConditions.add(firstAndConditions);
} else {
firstAndConditions = orConditions.get(0);
}
firstAndConditions.add(condition);
orConditions.add(condition);
}
}

Expand All @@ -82,15 +70,6 @@ public Optional<Condition> find(final Column column) {
* @return found condition
*/
public Optional<Condition> find(final Column column, int index) {
Condition result = null;
if (orConditions.size() > index) {
List<Condition> andConditions = orConditions.get(index);
for (Condition each : andConditions) {
if (Objects.equal(each.getColumn(), column)) {
result = each;
}
}
}
return Optional.fromNullable(result);
return orConditions.find(column, index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.context.condition;

import com.google.common.base.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.LinkedList;
import java.util.List;

/**
* Or conditions.
*
* @author maxiaoguang
*/
@RequiredArgsConstructor
@Getter
@ToString
public class OrConditions {

private final List<AndConditions> andConditions = new LinkedList<>();

/**
* Add condition.
*
* @param condition condition
*/
public void add(final Condition condition) {
AndConditions firstAndConditions;
if (isEmpty()) {
firstAndConditions = new AndConditions();
andConditions.add(firstAndConditions);
} else {
firstAndConditions = andConditions.get(0);
}
firstAndConditions.add(condition);
}

/**
* Find condition via column.
*
* @param column column
* @param index index of and conditions
* @return found condition
*/
public Optional<Condition> find(final Column column, int index) {
Optional<AndConditions> andConditions = get(index);
if (andConditions.isPresent()) {
return andConditions.get().find(column);
} else {
return Optional.fromNullable(null);
}
}

/**
* Get and conditions via index.
*
* @param index index of and conditions
* @return found and conditions
*/
public Optional<AndConditions> get(int index) {
AndConditions result = null;
if (size() > index) {
result = andConditions.get(index);
}
return Optional.fromNullable(result);
}

/**
* Adjust or conditions is empty or not.
*
* @return or conditions is empty or not
*/
public boolean isEmpty() {
return andConditions.isEmpty();
}

/**
* Returns the number of and conditions in this.
*
* @return the number of and conditions in this
*/
public int size() {
return andConditions.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, fin
*/
public void assertSQLStatement() {
tableAssert.assertTables(actual.getTables(), expected.getTables());
conditionAssert.assertOrConditions(actual.getConditions(), expected.getOrConditions());
conditionAssert.assertOrConditions(actual.getConditions().getOrConditions(), expected.getOrConditions());
tokenAssert.assertTokens(actual.getSqlTokens(), expected.getTokens());
indexAssert.assertParametersIndex(actual.getParametersIndex(), expected.getParameters().size());
if (actual instanceof SelectStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@

package io.shardingjdbc.core.parsing.integrate.asserts.condition;

import com.google.common.base.Optional;
import io.shardingjdbc.core.parsing.integrate.asserts.SQLStatementAssertMessage;
import io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedAndConditions;
import io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedCondition;
import io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedConditions;
import io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedOrConditions;
import io.shardingjdbc.core.parsing.integrate.jaxb.condition.ExpectedValue;
import io.shardingjdbc.core.parsing.parser.context.condition.AndConditions;
import io.shardingjdbc.core.parsing.parser.context.condition.Condition;
import io.shardingjdbc.core.parsing.parser.context.condition.Conditions;
import io.shardingjdbc.core.parsing.parser.context.condition.OrConditions;
import lombok.RequiredArgsConstructor;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* Condition assert.
Expand All @@ -50,20 +49,20 @@ public final class ConditionAssert {
* @param actual actual conditions
* @param expected expected conditions
*/
public void assertOrConditions(final Conditions actual, final List<ExpectedConditions> expected) {
assertThat(assertMessage.getFullAssertMessage("Or conditions size assertion error: "), actual.getOrConditions().size(), is(expected.size()));
public void assertOrConditions(final OrConditions actual, final ExpectedOrConditions expected) {
assertThat(assertMessage.getFullAssertMessage("Or conditions size assertion error: "), actual.size(), is(expected.getAndConditions().size()));
int count = 0;
for (ExpectedConditions andConditions : expected) {
assertAndConditions(actual.getOrConditions().get(count), andConditions.getConditions());
for (ExpectedAndConditions andConditions : expected.getAndConditions()) {
assertAndConditions(actual.get(count).get(), andConditions);
count++;
}
}

public void assertAndConditions(final List<Condition> actual, final List<ExpectedCondition> expected) {
assertThat(assertMessage.getFullAssertMessage("And conditions size assertion error: "), actual.size(), is(expected.size()));
public void assertAndConditions(final AndConditions actual, final ExpectedAndConditions expected) {
assertThat(assertMessage.getFullAssertMessage("And conditions size assertion error: "), actual.size(), is(expected.getConditions().size()));
int count = 0;
for (ExpectedCondition each : expected) {
Condition condition = actual.get(count);
for (ExpectedCondition each : expected.getConditions()) {
Condition condition = actual.get(count).get();
assertCondition(condition, each);
count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedConditions {
public final class ExpectedAndConditions {

@XmlElement(name = "condition")
private List<ExpectedCondition> conditions = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.integrate.jaxb.condition;

import lombok.Getter;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import java.util.LinkedList;
import java.util.List;

/**
* Expected and conditions.
*
* @author maxiaoguang
*/
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedOrConditions {

@XmlElement(name = "and-conditions")
private List<ExpectedAndConditions> andConditions = new LinkedList<>();
}
Loading

0 comments on commit 5d14141

Please sign in to comment.