Skip to content

Commit

Permalink
refaPara
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Sep 30, 2023
1 parent 225f6d3 commit 7759a1a
Show file tree
Hide file tree
Showing 27 changed files with 154 additions and 156 deletions.
2 changes: 1 addition & 1 deletion sqli-builder/src/main/java/io/xream/sqli/api/Routable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* to find target unit,
* not for db sharding,
* not suggest having Criteria,qr as the request object, the interface
* not suggest having Q,qr as the request object, the interface
* of Routeable maybe meaningless, of cource, your request object clzz can implements
* Routeable
*
Expand Down
14 changes: 7 additions & 7 deletions sqli-builder/src/main/java/io/xream/sqli/builder/QB.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public static <T> QB<T> of(Class<T> clz) {
}

public static X x() {
Q.X resultMapCriteria = new Q.X();
return new X(resultMapCriteria);
Q.X xq = new Q.X();
return new X(xq);
}

public Class<?> getClz() {
Expand Down Expand Up @@ -173,8 +173,8 @@ public SourceBuilder source(Class clzz) {
public SourceBuilder sub(Sub sub) {
X subBuilder = QB.x();
sub.buildBy(subBuilder);
Q.X resultMapCriteria = subBuilder.build();
sourceScriptTemp.setSubQ(resultMapCriteria);
Q.X xq = subBuilder.build();
sourceScriptTemp.setSubQ(xq);
subBuilder.clear();
return this;
}
Expand Down Expand Up @@ -345,11 +345,11 @@ public X sourceScript(String sourceScript, Object...vs) {
public X distinct(String... objs) {
if (objs == null)
throw new IllegalArgumentException("distinct non resultKey");
Q.X resultMapped = get();
Distinct distinct = resultMapped.getDistinct();
Q.X xq = get();
Distinct distinct = xq.getDistinct();
if (Objects.isNull(distinct)) {
distinct = new Distinct();
resultMapped.setDistinct(distinct);
xq.setDistinct(distinct);
}
for (String obj : objs) {
distinct.add(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public String sql(Mappable mappable) {
public String toString() {
return "SourceScript{" +
"source='" + source + '\'' +
", subCriteria=" + subQ +
", subQ=" + subQ +
", joinType=" + joinType +
", joinStr='" + joinStr + '\'' +
", on=" + on +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
package io.xream.sqli.builder.internal;

import io.xream.sqli.builder.*;
import io.xream.sqli.exception.CriteriaSyntaxException;
import io.xream.sqli.exception.QSyntaxException;
import io.xream.sqli.exception.ParsingException;
import io.xream.sqli.exception.SqlBuildException;
import io.xream.sqli.filter.BaseTypeFilter;
import io.xream.sqli.mapping.Mappable;
import io.xream.sqli.parser.BeanElement;
import io.xream.sqli.parser.Parsed;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.support.ResultMapSingleSourceSupport;
import io.xream.sqli.support.XSingleSourceSupport;
import io.xream.sqli.support.TimeSupport;
import io.xream.sqli.util.EnumUtil;
import io.xream.sqli.util.SqliJsonUtil;
Expand All @@ -39,7 +39,7 @@
/**
* @author Sim
*/
public final class DefaultQ2Sql implements Q2Sql, ResultKeyGenerator, SourceScriptOptimizable, ResultMapSingleSourceSupport {
public final class DefaultQ2Sql implements Q2Sql, ResultKeyGenerator, SourceScriptOptimizable, XSingleSourceSupport {

private static Q2Sql instance;

Expand Down Expand Up @@ -358,25 +358,25 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta

boolean flag = false;

Q.X resultMapped = (Q.X) q;
Q.X xq = (Q.X) q;
StringBuilder columnBuilder = new StringBuilder();

Map<String, String> mapperPropertyMap = resultMapped.getMapperPropertyMap();
Map<String, String> mapperPropertyMap = xq.getMapperPropertyMap();

if (Objects.nonNull(resultMapped.getDistinct())) {
if (Objects.nonNull(xq.getDistinct())) {

columnBuilder.append(SqlScript.DISTINCT);
List<String> list = resultMapped.getDistinct().getList();
List<String> list = xq.getDistinct().getList();
int size = list.size();
int i = 0;
StringBuilder distinctColumn = new StringBuilder();
distinctColumn.append(columnBuilder);
for (String resultKey : list) {
addConditonBeforeOptimization(resultKey, sqlSth.conditionSet);
String mapper = mapping(resultKey, resultMapped);
String mapper = mapping(resultKey, xq);
mapperPropertyMap.put(mapper, resultKey);//REDUCE ALIAN NAME
distinctColumn.append(SqlScript.SPACE).append(mapper);
mapper = generate(mapper, resultMapped);
mapper = generate(mapper, xq);
columnBuilder.append(SqlScript.SPACE).append(mapper);
i++;
if (i < size) {
Expand All @@ -388,7 +388,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta
flag = true;
}

List<Reduce> reduceList = resultMapped.getReduceList();
List<Reduce> reduceList = xq.getReduceList();

if (!reduceList.isEmpty()) {

Expand All @@ -399,7 +399,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta
addConditonBeforeOptimization(reduce.getProperty(), sqlSth.conditionSet);
String alianProperty = reduce.getProperty() + SqlScript.UNDER_LINE + reduce.getType().toString().toLowerCase();//property_count
String alianName = alianProperty.replace(SqlScript.DOT, SqlScript.DOLLOR);
resultMapped.getResultKeyAliaMap().put(alianName, alianProperty);
xq.getResultKeyAliaMap().put(alianName, alianProperty);

String value = mapping(reduce.getProperty(), q);

Expand Down Expand Up @@ -428,13 +428,13 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta
Having h = reduce.getHaving();
if (h != null) {
h.setAliaOrFunction(alianName);
resultMapped.getHavingList().add(h);
xq.getHavingList().add(h);
}
flag = true;
}
}

List<String> resultList = resultMapped.getResultKeyList();
List<String> resultList = xq.getResultKeyList();
if (!resultList.isEmpty()) {
if (flag) {
columnBuilder.append(SqlScript.COMMA);
Expand All @@ -445,7 +445,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta
addConditonBeforeOptimization(resultKey, sqlSth.conditionSet);
String mapper = mapping(resultKey, q);
mapperPropertyMap.put(mapper, resultKey);
mapper = generate(mapper, resultMapped);
mapper = generate(mapper, xq);
columnBuilder.append(SqlScript.SPACE).append(mapper);
if (i < size - 1) {
columnBuilder.append(SqlScript.COMMA);
Expand All @@ -455,7 +455,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta

}

List<KV> resultListAssignedAliaList = resultMapped.getResultKeyAssignedAliaList();
List<KV> resultListAssignedAliaList = xq.getResultKeyAssignedAliaList();
if (!resultListAssignedAliaList.isEmpty()) {
if (flag) {
columnBuilder.append(SqlScript.COMMA);
Expand All @@ -468,7 +468,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta
String mapper = mapping(key, q);
mapperPropertyMap.put(mapper, key);
String alian = kv.getV().toString();
resultMapped.getResultKeyAliaMap().put(alian, mapper);
xq.getResultKeyAliaMap().put(alian, mapper);
columnBuilder.append(SqlScript.SPACE).append(mapper).append(SqlScript.AS).append(alian);
if (i < size - 1) {
columnBuilder.append(SqlScript.COMMA);
Expand All @@ -478,13 +478,13 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta

}

List<FunctionResultKey> functionList = resultMapped.getResultFunctionList();
List<FunctionResultKey> functionList = xq.getResultFunctionList();
if (!functionList.isEmpty()) {//
if (flag) {
columnBuilder.append(SqlScript.COMMA);
}

Map<String, String> resultKeyAliaMap = resultMapped.getResultKeyAliaMap();
Map<String, String> resultKeyAliaMap = xq.getResultKeyAliaMap();

int size = functionList.size();
for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -516,7 +516,7 @@ private String resultKey(SqlSth sqlSth, Q q, SqlBuildingAttached sqlBuildingAtta

String script = columnBuilder.toString();
if (SqliStringUtil.isNullOrEmpty(script)) {
throw new CriteriaSyntaxException("Suggest API: find(Criteria criteria), no any resultKey for ResultMapCriteria");
throw new QSyntaxException("Suggest API: find(Q Q), q any resultKey for Q.X");
}

return script;
Expand Down Expand Up @@ -596,14 +596,14 @@ private void having(SqlSth sqlSth, Q q) {
if (!(q instanceof Q.X))
return;

Q.X resultMapped = (Q.X) q;
List<Having> havingList = resultMapped.getHavingList();
Q.X xq = (Q.X) q;
List<Having> havingList = xq.getHavingList();

if (havingList.isEmpty())
return;

if (!q.isTotalRowsIgnored()) {
throw new CriteriaSyntaxException("Reduce with having not support totalRows query, try to builder.paged().ignoreTotalRows()");
throw new QSyntaxException("Reduce with having not support totalRows query, try to builder.paged().ignoreTotalRows()");
}

boolean flag = true;
Expand Down Expand Up @@ -701,7 +701,7 @@ private void sourceScript(SqlSth sb, Q q) {

if (rmc.getSourceScripts().isEmpty()) {// builderSource null
String str = q.sourceScript();
Objects.requireNonNull(str, "Not set sourceScript of ResultMappedBuilder");
Objects.requireNonNull(str, "Not set sourceScript of QB.X");
final String strd = normalizeSql(str);
StringBuilder sbs = new StringBuilder();
mapping((reg) -> strd.split(reg), rmc, sbs);
Expand Down Expand Up @@ -772,13 +772,13 @@ private void filter0(Q q) {
List<Bb> bbList = q.getBbList();

if (q instanceof Q.X) {
Q.X resultMapCriteria = (Q.X) q;//FIXME 判断是虚表
filter(bbList, resultMapCriteria);
Q.X xq = (Q.X) q;//FIXME 判断是虚表
filter(bbList, xq);
for (SourceScript sourceScript : ((Q.X) q).getSourceScripts()) {
List<Bb> bbs = sourceScript.getBbList();
if (bbs == null || bbs.isEmpty())
continue;
filter(bbs, resultMapCriteria);
filter(bbs, xq);
}
} else {
filter(bbList, q);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
/**
* @author Sim
*/
public class CriteriaSyntaxException extends RuntimeException{
public class QSyntaxException extends RuntimeException{

private static final long serialVersionUID = 5749142995547236081L;

public CriteriaSyntaxException(String message){
public QSyntaxException(String message){
super(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public final class BaseTypeFilter {

public static boolean baseTypeSupported = false;

public static boolean isBaseType(String key, Object v, Mappable criteria) {
public static boolean isBaseType(String key, Object v, Mappable m) {
if (! baseTypeSupported)
return false;
String[] arr = key.split("\\.");
String alia = arr[0];
String clzName = criteria.getAliaMap().get(alia);
String clzName = m.getAliaMap().get(alia);
if (clzName == null)
clzName = alia;
Parsed parsed = Parser.get(clzName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private static void sortOnParsing(Parsed parsed, List<BeanElement> elementList)
public static void parse(Class clz) {

if (clz == Q.class || clz == Q.X.class || clz == Void.class)
throw new IllegalArgumentException("parser unsupport Criteria, CriteriaJoinable, ....");
throw new IllegalArgumentException("parser unsupport Q, Q.X, ....");

Parsed parsed = new Parsed(clz);
List<BeanElement> elementList = ParserUtil.parseElementList(clz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
/**
* @author Sim
*/
public interface ResultMapSingleSourceSupport {
public interface XSingleSourceSupport {

default void supportSingleSource(Q.X resultMapCriteria) {
if (resultMapCriteria.getSourceScripts().size() == 1 && resultMapCriteria.getParsed() == null) {
SourceScript sourceScript = resultMapCriteria.getSourceScripts().get(0);
default void supportSingleSource(Q.X xq) {
if (xq.getSourceScripts().size() == 1 && xq.getParsed() == null) {
SourceScript sourceScript = xq.getSourceScripts().get(0);
String source = sourceScript.getSource();
if (source != null) {
Parsed parsed = Parser.get(source);
resultMapCriteria.setParsed(parsed);
resultMapCriteria.setClzz(parsed.getClzz());
xq.setParsed(parsed);
xq.setClzz(parsed.getClzz());
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions sqli-builder/src/main/java/io/xream/sqli/test/SqlGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SqlGenerator {
private static SqlGenerator instance;
private static Q2Sql condToSql;

private static List<KV> resultMapCriteriaList = new ArrayList<>();
private static List<KV> xKvList = new ArrayList<>();


private SqlGenerator(){}
Expand All @@ -54,17 +54,17 @@ public SqlGenerator source(Class<?> clzz) {
return instance;
}

public SqlGenerator build(String traceKey, Q.X resultMapCriteria){
KV kv = new KV(traceKey,resultMapCriteria);
resultMapCriteriaList.add(kv);
public SqlGenerator build(String traceKey, Q.X xq){
KV kv = new KV(traceKey,xq);
xKvList.add(kv);
return instance;
}

public void generate(String fileName){

StringBuilder sb = new StringBuilder();

for (KV kv : resultMapCriteriaList) {
for (KV kv : xKvList) {

SqlBuilt sqlBuilt = new SqlBuilt();

Expand Down Expand Up @@ -105,7 +105,7 @@ private static void write(String fileName, StringBuilder sb) {
}catch (Exception e){
e.printStackTrace();
}finally {
resultMapCriteriaList.clear();
xKvList.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public static String filterSQLKeyword(String mapper) {

public static String getClzName(String alia, Map<String, String> aliaMap) {
if (aliaMap == null)
throw new ParsingException("CriteriaBuilder.builder(Class) not support the key contains '.'");
throw new ParsingException("QB.of(Class) not support the key contains '.'");
String a = aliaMap.get(alia);
if (SqliStringUtil.isNotNull(a))
return a;
Expand Down
2 changes: 1 addition & 1 deletion sqli-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

### API
BaseRepository
ResultMapRepository
RepositoryX
TemporaryRepository
CacheFilter

Expand Down
2 changes: 1 addition & 1 deletion sqli-core/src/main/java/io/xream/sqli/api/RepositoryX.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Map;

/**
* ResultMap API
* X API
* @author Sim
*/
public interface RepositoryX {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* How to update with TemporaryRepository?
* suggest:
* .findToHandle(ResultMapCriteria, map -> {
* .findToHandle(Q.X, map -> {
*
* refresh(
* qr.build()....
Expand Down
Loading

0 comments on commit 7759a1a

Please sign in to comment.