Skip to content

Commit

Permalink
简化ORM引擎中querySpace支持
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 17, 2023
1 parent 8973d01 commit 6985b2c
Show file tree
Hide file tree
Showing 46 changed files with 576 additions and 50 deletions.
16 changes: 11 additions & 5 deletions docs/theory/pros-and-cons-of-orm-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

近二十年以来,软件框架技术领域的一个最重要的进展就是认识到框架的中立性(framework agnostic)。业务代码开发本质上是对业务信息的一种表达,这种表达原则上应该独立于任何软件框架,甚至是独立于任何技术因素。**框架的作用是辅助我们用最自然、最直观的方式来表达业务信息,同时能够满足性能和其他技术规范要求****最理想的框架,应该是在开发业务代码时完全意识不到它存在的框架**

这样做的好处在于我们可以避免业务域和纯技术域之间的信息渗透和污染,使得业务代码的测试、技术框架的升级甚至更换变得异常简单。
这样做的好处在于我们可以**避免业务域和纯技术域之间的信息渗透和污染**,使得业务代码的测试、技术框架的升级甚至更换变得异常简单。

### POJO和轻量级框架

Expand All @@ -51,7 +51,7 @@
}
```

在新的Web框架设计中,一般都是接收POJO对象,返回POJO对象,除了函数上的注解之外没有任何框架依赖的痕迹
在新的Web框架设计中,一般都是接收POJO对象,返回POJO对象,除了函数上的注解之外没有任何框架依赖的痕迹,而注解本身又只是一些纯粹的描述性信息

```javascript

Expand All @@ -67,16 +67,22 @@

> 前端Redux和Vuex框架本质上也都是将action规范化为针对单个POJO对象的单参数函数。
在Dao访问层,现在大量的ORM框架都可以识别JPA注解或者MyBatis注解,使得替换ORM框架成为一件相对简单的事情
在Dao访问层,现在大量的ORM框架都可以识别JPA注解,这使得同一个实体定义可以应用于多种ORM框架

### 虚拟DOM和Hooks

在前端领域越来越多的业务逻辑也开始采用框架中立的表达形式。

例如,虚拟DOM概念的引入使得前端框架可以脱离浏览器运行环境,这造就了React Native这种多端统一开发技术和各类小程序框架的繁荣。虚拟DOM本质上就是普通的JavaScript对象,不同的运行时都可以创建并且翻译同样的虚拟DOM。
例如,虚拟DOM概念的引入使得前端框架可以脱离浏览器运行环境,这造就了React Native这种多端统一的开发技术和各类小程序框架的繁荣。虚拟DOM本质上就是普通的JavaScript对象,不同的运行时都可以创建并且翻译同样的虚拟DOM。

Hooks概念的发展使得前端界面逻辑的表达可以摆脱组件对象的形式束缚,只需要引入最小化的Hooks假定,就可以将业务逻辑抽象到框架无关的纯逻辑函数中。借助于Hooks这样的抽象,Headless的组件库逐渐开始占据主流,并且可以用同一套核心代码适配React/Vue/Angular等多种基础框架。

## 二. 框架进行了哪些自动推导?

一个框架如果具有本质上的优越性,那么它一定是相比于其他可选方案更充分的利用了某些信息,并且基于这些信息自动推导完成了大量的工作。
一个框架如果具有本质上的优越性,那么它一定是**相比于其他可选方案更充分的利用了某些信息,并且基于这些信息自动推导完成了大量的工作**


### 注解 vs. XML配置



20 changes: 20 additions & 0 deletions nop-commons/src/main/java/io/nop/commons/diff/DiffValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class DiffValue implements IDiffValue {

private List<IDiffValue> elementDiffs;

private String keyProp;

private Map<String, IDiffValue> keyedElementDiffs;

public DiffValue(DiffType diffType, Object oldValue, Object newValue) {
this.diffType = diffType;
this.oldValue = oldValue;
Expand Down Expand Up @@ -89,4 +93,20 @@ public List<IDiffValue> getElementDiffs() {
public void setElementDiffs(List<IDiffValue> elementDiffs) {
this.elementDiffs = elementDiffs;
}

public String getKeyProp() {
return keyProp;
}

public void setKeyProp(String keyProp) {
this.keyProp = keyProp;
}

public Map<String, IDiffValue> getKeyedElementDiffs() {
return keyedElementDiffs;
}

public void setKeyedElementDiffs(Map<String, IDiffValue> keyedElementDiffs) {
this.keyedElementDiffs = keyedElementDiffs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@FunctionalInterface
public interface IEqualsChecker<T> {
IEqualsChecker<Object> OBJECT_EQUALS = Objects::equals;

// 对象的toString结果值相等
IEqualsChecker<Object> STRING_EQUALS = new IEqualsChecker<Object>() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion nop-core/src/main/java/io/nop/core/lang/sql/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void dump(String title) {
if (LOG.isInfoEnabled()) {
StringBuilder buf = new StringBuilder();
SqlFormatter.formatSql(buf, this);
LOG.info("title={},name={},sql=\n{}", title, name, buf);
LOG.info("title={},querySpace={},name={},sql=\n{}", title, querySpace, name, buf);
}
}

Expand Down
4 changes: 2 additions & 2 deletions nop-core/src/main/java/io/nop/core/lang/sql/SqlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static List<SQL> splitSql(IMarkedString str, Supplier<SQL.SqlBuilder> cre

int pos = CharSequenceHelper.indexOf(str.getTextSequence(), 0, ';');
if (pos < 0) {
return Collections.singletonList(SQL.begin().append(str).end());
return Collections.singletonList(creator.get().append(str).end());
}

List<SQL> ret = new ArrayList<>();
Expand All @@ -117,7 +117,7 @@ public static List<SQL> splitSql(IMarkedString str, Supplier<SQL.SqlBuilder> cre
}
char c = statement.charAt(i);
if (c == ';') {
SQL.SqlBuilder part = SQL.begin();
SQL.SqlBuilder part = creator.get();
part.appendRange(str, startPos, i);
ret.add(part.end());
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@
package io.nop.core.reflect.bean;

import io.nop.api.core.beans.FieldSelectionBean;
import io.nop.commons.functional.IEqualsChecker;
import io.nop.core.lang.eval.DisabledEvalScope;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.core.reflect.ReflectionManager;

public class BeanDiffOptions {
private IBeanModelManager beanModelManager = ReflectionManager.instance();
private FieldSelectionBean selection;
private IEvalScope scope;
private IEvalScope scope = DisabledEvalScope.INSTANCE;
private boolean includeSame;
private boolean onlySerializable;

private IEqualsChecker equalsChecker = IEqualsChecker.OBJECT_EQUALS;

public IEqualsChecker getEqualsChecker() {
return equalsChecker;
}

public void setEqualsChecker(IEqualsChecker equalsChecker) {
this.equalsChecker = equalsChecker;
}

public IBeanModelManager getBeanModelManager() {
return beanModelManager;
}
Expand Down
41 changes: 39 additions & 2 deletions nop-core/src/main/java/io/nop/core/reflect/bean/BeanDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import io.nop.commons.diff.IDiffValue;
import io.nop.commons.util.CollectionHelper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -146,12 +149,46 @@ private boolean isSameClass(IBeanModel bm1, IBeanModel bm2, Object src, Object t
}

private IDiffValue diffArray(Object src, Object target, FieldSelectionBean selection, BeanDiffOptions options) {
return null;
return diffCollectionWithAdapter(src, target, selection, options, ArrayBeanCollectionAdapter.INSTANCE);
}

private IDiffValue diffCollection(Object src, Object target, FieldSelectionBean selection,
BeanDiffOptions options) {
return null;
return diffCollectionWithAdapter(src, target, selection, options, BeanCollectionAdapter.INSTANCE);
}

private IDiffValue diffCollectionWithAdapter(Object src, Object target, FieldSelectionBean selection,
BeanDiffOptions options, IBeanCollectionAdapter adapter) {
int srcLen = adapter.getSize(src);
int targetLen = adapter.getSize(target);
if (srcLen == 0) {
if (targetLen == 0) {
return DiffValue.same(src, target);
}

// 全部新增
return DiffValue.replace(src, target);
} else if (targetLen == 0) {
// 全部删除
return DiffValue.replace(src, target);
} else {
List<IDiffValue> diffs = new ArrayList<>();
Iterator<?> it1 = adapter.iterator(src);
Iterator<?> it2 = adapter.iterator(target);
while (it1.hasNext() && it2.hasNext()) {
Object v1 = it1.next();
Object v2 = it2.next();

IDiffValue diff = diffObject(v1, v2, selection, options);
if (diff == null)
diff = DiffValue.same(v1, v2);
diffs.add(diff);
}

DiffValue ret = new DiffValue(DiffType.update, src, target);
ret.setElementDiffs(diffs);
return ret;
}
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion nop-dao/src/main/java/io/nop/dao/dialect/IDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import io.nop.commons.text.CharacterCase;
import io.nop.commons.type.StdDataType;
import io.nop.commons.type.StdSqlType;
import io.nop.dataset.binder.IDataParameterBinder;
import io.nop.dao.dialect.exception.ISQLExceptionTranslator;
import io.nop.dao.dialect.function.ISQLFunction;
import io.nop.dao.dialect.lock.LockOption;
import io.nop.dao.dialect.model.DialectModel;
import io.nop.dao.dialect.model.SqlDataTypeModel;
import io.nop.dao.dialect.pagination.IPaginationHandler;
import io.nop.dataset.binder.IDataParameterBinder;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -37,6 +37,8 @@ public interface IDialect extends IComponentModel {

SQLDataType stdToNativeSqlType(StdSqlType sqlType, int precision, int scale);

IDataParameterBinder getGeometryDataParameterBinder();

ISQLExceptionTranslator getSQLExceptionTranslator();

IPaginationHandler getPaginationHandler();
Expand Down
21 changes: 21 additions & 0 deletions nop-dao/src/main/java/io/nop/dao/dialect/impl/DialectImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.nop.dataset.binder.DataParameterBinders;
import io.nop.dataset.binder.IDataParameterBinder;
import io.nop.dataset.impl.AutoConvertDataParameterBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringReader;
Expand All @@ -64,6 +66,7 @@
import static io.nop.dao.DaoErrors.ERR_DIALECT_TPL_PARAM_NO_ARG;

public class DialectImpl implements IDialect {
static final Logger LOG = LoggerFactory.getLogger(DialectImpl.class);
private final DialectModel dialectModel;
private final ISQLExceptionTranslator exceptionTranslator;
private final IPaginationHandler paginationHandler;
Expand All @@ -75,6 +78,8 @@ public class DialectImpl implements IDialect {

private final boolean convertStringToNull = CFG_AUTO_CONVERT_EMPTY_STRING_TO_NULL.get();

private final IDataParameterBinder geometryParameterBinder;

public DialectImpl(DialectModel dialectModel) {
this.dialectModel = dialectModel;
this.exceptionTranslator = new DialectSQLExceptionTranslator(dialectModel);
Expand All @@ -89,6 +94,14 @@ public DialectImpl(DialectModel dialectModel) {
if (dialectModel.getRename() != null) {
this.renameMap.putAll(dialectModel.getRename());
}

IDataParameterBinder binder = null;
try {
binder = newInstance(dialectModel.getGeometryParameterBinder(), null);
} catch (Exception e) {
LOG.warn("nop.err.dao.load-geometry-parameter-binder-fail", e);
}
this.geometryParameterBinder = binder;
}

static <T> T newInstance(String className, T defaultImpl) {
Expand Down Expand Up @@ -126,6 +139,11 @@ String buildFunctionSql(String funcName) {
return getSelectFromDualSql(sql);
}

@Override
public IDataParameterBinder getGeometryDataParameterBinder() {
return geometryParameterBinder;
}

public DialectModel getDialectModel() {
return dialectModel;
}
Expand Down Expand Up @@ -498,6 +516,9 @@ public IDataParameterBinder getDataParameterBinder(StdDataType stdType, StdSqlTy
if (convertStringToNull && sqlType == StdSqlType.VARCHAR)
return DataParameterBinders.STRING_EX;

if (sqlType == StdSqlType.GEOMETRY)
return getGeometryDataParameterBinder();

IDataParameterBinder binder = DataParameterBinders.getDefaultBinder(sqlType.getName());
if (binder == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [141:6:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [144:6:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [87:10:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [90:10:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [32:6:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [35:6:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public abstract class _DialectModel extends io.nop.core.resource.component.Abstr
*/
private KeyedList<io.nop.dao.dialect.model.ISqlFunctionModel> _functions = KeyedList.emptyList();

/**
*
* xml name: geometryParameterBinder
* JDBC使用这个类存取GEOMETRY类型数据。 IDataParameterBinder类型
*/
private java.lang.String _geometryParameterBinder ;

/**
*
* xml name: jdbcUrlPattern
Expand Down Expand Up @@ -392,6 +399,25 @@ public boolean hasFunctions(){
return !this._functions.isEmpty();
}

/**
*
* xml name: geometryParameterBinder
* JDBC使用这个类存取GEOMETRY类型数据。 IDataParameterBinder类型
*/

public java.lang.String getGeometryParameterBinder(){
return _geometryParameterBinder;
}


public void setGeometryParameterBinder(java.lang.String value){
checkAllowChange();

this._geometryParameterBinder = value;

}


/**
*
* xml name: jdbcUrlPattern
Expand Down Expand Up @@ -685,6 +711,7 @@ protected void outputJson(IJsonHandler out){
out.put("errorCodes",this.getErrorCodes());
out.put("features",this.getFeatures());
out.put("functions",this.getFunctions());
out.put("geometryParameterBinder",this.getGeometryParameterBinder());
out.put("jdbcUrlPattern",this.getJdbcUrlPattern());
out.put("keywordQuote",this.getKeywordQuote());
out.put("keywordUnderscore",this.getKeywordUnderscore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [57:6:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [60:6:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [135:10:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [138:10:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [104:10:0:0]/nop/schema/orm/dialect.xdef <p>
* generate from [107:10:0:0]/nop/schema/orm/dialect.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Loading

0 comments on commit 6985b2c

Please sign in to comment.