Skip to content

Commit

Permalink
Add method builder.sourceScript(sql, Object...vs); (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed May 20, 2022
1 parent ad02afb commit 62fa7b3
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 28 deletions.
8 changes: 1 addition & 7 deletions sqli-builder/src/main/java/io/xream/sqli/builder/Bb.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public final class Bb {
private String key;
private Object value;
private List<Bb> subList;
private Bb parent;
public Bb(){}
public Bb(boolean isOr){
if (isOr)
Expand Down Expand Up @@ -70,12 +69,7 @@ public List<Bb> getSubList() {
public void setSubList(List<Bb> subList) {
this.subList = subList;
}
public Bb getParent() {
return parent;
}
public void setParent(Bb parent) {
this.parent = parent;
}


@Override
public boolean equals(Object object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
* @author Sim
Expand Down
100 changes: 100 additions & 0 deletions sqli-builder/src/main/java/io/xream/sqli/builder/ConditionParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2020 io.xream.sqli
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package io.xream.sqli.builder;

import java.util.ArrayList;
import java.util.List;

/**
* @author Sim
*/
public interface ConditionParser {


static int parse(int i, List<String> conditionScriptSplittedList,
List<Bb> bbList, List<Object> valueList) {
int size = conditionScriptSplittedList.size();

Bb bb = null;
while (true) {
if (i > size - 1)
return i;
String s = conditionScriptSplittedList.get(i++);

if (s.equals(SqlScript.PLACE_HOLDER)) { // ?
Object v = valueList.remove(0);
if (v == null || (v instanceof List && ((List) v).isEmpty())) {
bbList.remove(bbList.size()-1);
}
bb.setValue(v);
}

String u = s.toUpperCase();
if (s.startsWith("(")) {
bb = new Bb();
bb.setKey("");
bb.setC(Op.AND);
bb.setP(Op.SUB);
bbList.add(bb);
List<Bb> subList = new ArrayList<>();
bb.setSubList(subList);
i = parse(i, conditionScriptSplittedList,
subList, valueList);
continue;
}

else if (s.startsWith(")")) {
return i;
}

else if (u.startsWith("AND") || u.startsWith("OR")) {

String next = conditionScriptSplittedList.get(i);
if (next.equals("(")){
bb = new Bb();
bb.setKey("");
bb.setC(Op.valueOf(u));
bb.setP(Op.SUB);
bbList.add(bb);
List<Bb> subList = new ArrayList<>();
bb.setSubList(subList);
i++;
i = parse(i, conditionScriptSplittedList,
subList, valueList);
continue;
}

bb = new Bb();
bb.setKey("");
bb.setC(Op.valueOf(u));
bb.setP(Op.X);
bbList.add(bb);
} else {
if (bb == null) {
bb = new Bb();
bb.setKey("");
bb.setP(Op.X);
bb.setC(Op.AND);
bbList.add(bb);
}
bb.setKey(bb.getKey() + " " + s);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public interface ConditionToSql extends Mapper, SqlNormalizer, UnsafeSyntaxFilte
default void buildConditionSql(StringBuilder sb, List<Bb> bbList, Mappable mappable) {
if (bbList == null || bbList.isEmpty())
return;

for (Bb bb : bbList) {

Op p = bb.getP();
Expand All @@ -63,7 +62,10 @@ default void buildConditionSql(StringBuilder sb, List<Bb> bbList, Mappable mappa

String mapper = null;
if (p == Op.X){
final String str = normalizeSql(bb.getKey());
String key = bb.getKey();
if (SqliStringUtil.isNullOrEmpty(key))
continue;
final String str = normalizeSql(key);
StringBuilder sbx = new StringBuilder();
mapping((reg)->str.split(reg), mappable,sbx);
mapper = sbx.toString();
Expand Down Expand Up @@ -162,7 +164,7 @@ default void filter(List<Bb> bbList, Mappable mappable) {
if (bb.getSubList().isEmpty()) {
ite.remove();
}
}else if (p == Op.EQ
} else if (p == Op.EQ
|| p == Op.NE
|| p == Op.GT
|| p == Op.GTE
Expand Down
17 changes: 17 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 @@ -174,6 +174,10 @@ public void paged(Paged paged) {
}
}

public List<Object> getSourceScriptValueList() {
return null;
}


@Override
public String toString() {
Expand All @@ -198,6 +202,7 @@ public static final class ResultMapCriteria extends Criteria implements ResultMa
private List<Bb> aggrList;
private Distinct distinct;
private String sourceScript;
private List<Object> sourceScriptValueList;
private List<SourceScript> sourceScripts;
private List<Reduce> reduceList;
private List<Having> havingList;
Expand Down Expand Up @@ -280,6 +285,18 @@ public void setSourceScript(String sourceScript) {
this.sourceScript = normalizeSql(sourceScript);
}

@Override
public List<Object> getSourceScriptValueList() {
return sourceScriptValueList;
}

public void setSourceScriptValueList(Object[] vs) {
this.sourceScriptValueList = new ArrayList<>();
for (Object v : vs) {
this.sourceScriptValueList.add(v);
}
}

public List<String> getResultKeyList() {
return resultKeyList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.xream.sqli.page.Paged;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.util.BeanUtil;
import io.xream.sqli.util.SqliStringUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -155,6 +156,12 @@ public SourceScriptBuilder source(String source) {
return this;
}

@Override
public SourceScriptBuilder source(Class clzz) {
sourceScriptTemp.setSource(BeanUtil.getByFirstLower(clzz.getSimpleName()));
return this;
}

@Override
public SourceScriptBuilder sub(Sub sub) {
ResultMapBuilder subBuilder = CriteriaBuilder.resultMapBuilder();
Expand Down Expand Up @@ -220,8 +227,18 @@ public ConditionBuilder more() {
return builder(bbList);
}

@Override
public ResultMapBuilder build() {
return getInstance();
}

};

private ResultMapBuilder instance;
protected ResultMapBuilder getInstance(){
return this.instance;
}

public SourceScriptBuilder sourceBuilder() {
sourceScriptTemp = new SourceScript();
get().getSourceScripts().add(sourceScriptTemp);
Expand All @@ -245,6 +262,7 @@ public Criteria.ResultMapCriteria build() {

public ResultMapBuilder(Criteria criteria) {
super(criteria);
instance = this;
}

public ResultMapBuilder resultKey(String resultKey) {
Expand Down Expand Up @@ -294,7 +312,17 @@ public ResultMapBuilder resultKeyFunction(ResultKeyAlia resultKeyAlia, String fu
public ResultMapBuilder sourceScript(String sourceScript) {
if (SqliStringUtil.isNullOrEmpty(sourceScript))
return this;
sourceScript = normalizeSql(sourceScript);
get().setSourceScript(sourceScript);
return this;
}

public ResultMapBuilder sourceScript(String sourceScript, Object...vs) {
if (SqliStringUtil.isNullOrEmpty(sourceScript))
return this;
sourceScript = normalizeSql(sourceScript);
get().setSourceScript(sourceScript);
get().setSourceScriptValueList(vs);
return this;
}

Expand Down
9 changes: 9 additions & 0 deletions sqli-builder/src/main/java/io/xream/sqli/builder/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ public enum Op {
public String sql(){
return op;
}

public static Op valueOfSql(String str) {
String t = str.trim();
for (Op op : values()) {
if (op.sql().equals(str))
return op;
}
return NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public String sql(Mappable mappable) {
);
}
}

buildConditionSql(sb, bbList, mappable);

return sb.toString();
Expand Down

0 comments on commit 62fa7b3

Please sign in to comment.