Skip to content

Commit

Permalink
Add builder API: InRequired (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Oct 21, 2022
1 parent fc48f99 commit becdfe3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<maven.test.skip>true</maven.test.skip>
<sqli.version>2.0.0</sqli.version>
<jackson.version>2.11.3</jackson.version>
<jackson.version>2.13.4.2</jackson.version>
<sl4j.version>1.7.29</sl4j.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ConditionBuilder implements SqlNormalizer {
private ConditionBuilder instance;

private transient List<Bb> bbList;
protected transient boolean isAbort;
private transient boolean isOr;

private transient List<Bb> tempList;
Expand Down Expand Up @@ -133,6 +134,13 @@ public ConditionBuilder in(String property, List<? extends Object> list){
return doIn(Op.IN,property,list);
}

public ConditionBuilder inRequired(String property, List<? extends Object> list){
if (list == null || list.isEmpty()){
this.instance.isAbort = true;
}
return doIn(Op.IN,property,list);
}

public ConditionBuilder nin(String property, List<? extends Object> list){
return doIn(Op.NOT_IN,property,list);
}
Expand Down Expand Up @@ -241,7 +249,6 @@ private ConditionBuilder doLike(Op p, String property, String likeWalue){
return instance;
}


private ConditionBuilder doIn(Op p, String property, List<? extends Object> list){

if (list == null || list.isEmpty()){
Expand Down
31 changes: 31 additions & 0 deletions sqli-builder/src/main/java/io/xream/sqli/builder/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class Criteria implements Mappable,CriteriaCondition, Paged, Routable,Ser
private List<Sort> sortList;
private List<KV> fixedSortList;
private List<Bb> bbList = new ArrayList<>();
private boolean isAbort;

@JsonIgnore
private transient Parsed parsed;
Expand All @@ -69,6 +70,14 @@ public void setClzz(Class<?> clz) {
this.clzz = clz;
}

public boolean isAbort() {
return isAbort;
}

public void setAbort(boolean abort) {
isAbort = abort;
}

public Parsed getParsed() {
return parsed;
}
Expand Down Expand Up @@ -375,6 +384,28 @@ public void setRepositoryClzz(Class repositoryClzz) {
this.repositoryClzz = repositoryClzz;
}

@Override
public boolean isAbort(){
if (super.isAbort())
return true;
if (this.sourceScripts == null)
return false;
return isSubAbort(this.sourceScripts);
}

private boolean isSubAbort(List<SourceScript> sourceScripts){
for (SourceScript ss : sourceScripts) {
Criteria.ResultMapCriteria sub = ss.getSubCriteria();
if (sub == null)
continue;
if (sub.sourceScripts == null)
continue;
if (sub.isSubAbort(sub.sourceScripts))
return true;
}
return false;
}

@Override
public String toString() {
return "ResultMapCriteria{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected Criteria get() {
}

public Criteria build(){
this.criteria.setAbort(isAbort);
return this.criteria;
}

Expand Down Expand Up @@ -257,7 +258,7 @@ protected Criteria.ResultMapCriteria get() {

@Override
public Criteria.ResultMapCriteria build() {
return (Criteria.ResultMapCriteria) super.get();
return (Criteria.ResultMapCriteria) super.build();
}

public ResultMapBuilder(Criteria criteria) {
Expand Down Expand Up @@ -451,6 +452,10 @@ public ResultMapBuilder in(String property, List<? extends Object> list) {
return (ResultMapBuilder) super.in(property,list);
}

public ResultMapBuilder inRequired(String property, List<? extends Object> list) {
return (ResultMapBuilder) super.inRequired(property,list);
}

public ResultMapBuilder nin(String property, List<? extends Object> list) {
return (ResultMapBuilder) super.nin(property,list);
}
Expand Down Expand Up @@ -479,6 +484,10 @@ public ResultMapBuilder endSub(){
return (ResultMapBuilder) super.endSub();
}

public ResultMapBuilder bool(Bool conditon, Then then){
return (ResultMapBuilder) super.bool(conditon, then);
}

public void clear(){
super.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public RefreshBuilder in(String property, List<? extends Object> list) {
return (RefreshBuilder) super.in(property,list);
}

public RefreshBuilder inRequired(String property, List<? extends Object> list) {
if (list.isEmpty()) {
refreshCondition.setAbort(true);
}
return (RefreshBuilder) super.in(property,list);
}

public RefreshBuilder nin(String property, List<? extends Object> list) {
return (RefreshBuilder) super.nin(property,list);
}
Expand Down Expand Up @@ -178,6 +185,10 @@ public RefreshBuilder endSub(){
return (RefreshBuilder) super.endSub();
}

public RefreshBuilder bool(Bool condition, Then then){
return (RefreshBuilder) super.bool(condition, then);
}

public RefreshBuilder sourceScript(String sourceScript) {
this.refreshCondition.setSourceScript(sourceScript);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class RefreshCondition<T> implements Mappable,CriteriaCondition, R
private String sourceScript;//FIXME

private List<Bb> bbList = new ArrayList<>();
private boolean isAbort;
private Object routeKey;
private transient int limit;
@JsonIgnore
Expand Down Expand Up @@ -73,6 +74,14 @@ public List<Object> getValueList() {
return this.valueList;
}

public boolean isAbort() {
return isAbort;
}

public void setAbort(boolean abort) {
isAbort = abort;
}

@Override
public List<Bb> getBbList() {
return bbList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.xream.sqli.util.SqliExceptionUtil;
import io.xream.sqli.util.SqliLoggerProxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -127,6 +128,9 @@ public <T> boolean refresh(T t) {
@Override
public <T> boolean refresh(RefreshCondition<T> refreshCondition) {

if (refreshCondition.isAbort())
return false;

boolean flag = dao.refreshByCondition(refreshCondition);

if (!flag) return flag;
Expand Down Expand Up @@ -198,6 +202,14 @@ public <T> List<T> list(Object conditionObj) {
@Override
public <T> Page<T> find(Criteria criteria) {

if (criteria.isAbort()) {
Page page = new Page<>();
page.setClzz(criteria.getClzz());
page.setRows(criteria.getRows());
page.setPage(criteria.getPage());
return page;
}

Class clz = criteria.getClzz();
Parsed parsed = Parser.get(clz);

Expand All @@ -214,6 +226,9 @@ public <T> Page<T> find(Criteria criteria) {
@Override
public <T> List<T> list(Criteria criteria) {

if (criteria.isAbort())
return new ArrayList<>();

Class clz = criteria.getClzz();
Parsed parsed = Parser.get(clz);

Expand Down Expand Up @@ -300,18 +315,31 @@ public <T> T getOne(T condition) {
}

@Override
public Page<Map<String, Object>> find(Criteria.ResultMapCriteria resultMapped) {
return dao.find(resultMapped);
public Page<Map<String, Object>> find(Criteria.ResultMapCriteria criteria) {
if (criteria.isAbort()) {
Page page = new Page<>();
page.setClzz(criteria.getClzz());
page.setRows(criteria.getRows());
page.setPage(criteria.getPage());
return page;
}
return dao.find(criteria);
}

@Override
public List<Map<String, Object>> list(Criteria.ResultMapCriteria resultMapped) {
return dao.list(resultMapped);
public List<Map<String, Object>> list(Criteria.ResultMapCriteria criteria) {

if (criteria.isAbort())
return new ArrayList<>();

return dao.list(criteria);
}

@Override
public <K> List<K> listPlainValue(Class<K> clzz, Criteria.ResultMapCriteria resultMapped){
return dao.listPlainValue(clzz,resultMapped);
public <K> List<K> listPlainValue(Class<K> clzz, Criteria.ResultMapCriteria criteria){
if (criteria.isAbort())
return new ArrayList<>();
return dao.listPlainValue(clzz,criteria);
}


Expand All @@ -322,12 +350,16 @@ public List<Map<String, Object>> list(String sql, List<Object> conditionSet) {

@Override
public <T> void findToHandle(Criteria criteria, RowHandler<T> handler) {
if (criteria.isAbort())
return;
this.dao.findToHandle(criteria,handler);
}

@Override
public void findToHandle(Criteria.ResultMapCriteria resultMapCriteria, RowHandler<Map<String, Object>> handler) {
this.dao.findToHandle(resultMapCriteria,handler);
public void findToHandle(Criteria.ResultMapCriteria criteria, RowHandler<Map<String, Object>> handler) {
if (criteria.isAbort())
return;
this.dao.findToHandle(criteria,handler);
}

}

0 comments on commit becdfe3

Please sign in to comment.