Skip to content

Commit

Permalink
优化typeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Apr 13, 2019
1 parent 7cef088 commit ccb4e96
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class EasyOrmSqlBuilder {

public EntityFactory entityFactory;

private static final EasyOrmSqlBuilder instance = new EasyOrmSqlBuilder();
private static final EasyOrmSqlBuilder instance = new EasyOrmSqlBuilder();
protected static final Map<Class, String> simpleName = new HashMap<>();

protected PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
Expand Down Expand Up @@ -106,11 +106,11 @@ public static String getJavaType(Class type) {
return javaType;
}

private final RDBDatabaseMetaData mysql = new MysqlMeta();
private final RDBDatabaseMetaData oracle = new OracleMeta();
private final RDBDatabaseMetaData h2 = new H2Meta();
private final RDBDatabaseMetaData mysql = new MysqlMeta();
private final RDBDatabaseMetaData oracle = new OracleMeta();
private final RDBDatabaseMetaData h2 = new H2Meta();
private final RDBDatabaseMetaData postgresql = new PGMeta();
private final RDBDatabaseMetaData mssql = new MSSQLMeta();
private final RDBDatabaseMetaData mssql = new MSSQLMeta();

private final ConcurrentMap<RDBDatabaseMetaData, Map<String, RDBTableMetaData>> metaCache = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -156,7 +156,7 @@ protected RDBTableMetaData createMeta(String tableName, String resultMapId) {
return cached;
}

RDBTableMetaData rdbTableMetaData = new RDBTableMetaData(){
RDBTableMetaData rdbTableMetaData = new RDBTableMetaData() {
@Override
public String getName() {
//动态切换表名
Expand All @@ -174,6 +174,9 @@ public String getName() {
RDBColumnMetaData column = new RDBColumnMetaData();
column.setJdbcType(JDBCType.valueOf(resultMapping.getJdbcType().name()));
column.setName(resultMapping.getColumn());
if (resultMapping.getTypeHandler() != null) {
column.setProperty("typeHandler", resultMapping.getTypeHandler().getClass().getName());
}
if (!StringUtils.isNullOrEmpty(resultMapping.getProperty())) {
column.setAlias(resultMapping.getProperty());
}
Expand Down Expand Up @@ -248,10 +251,14 @@ public String buildUpdateFields(String resultMapId, String tableName, UpdatePara
appender.add(",", encodeColumn(dialect, columnMetaData.getName())
, "=", ((Sql) value).getSql());
} else {
String typeHandler = columnMetaData.getProperty("typeHandler")
.getValue();

appender.add(",", encodeColumn(dialect, columnMetaData.getName())
, "=", "#{data.", columnMetaData.getAlias(),
",javaType=", EasyOrmSqlBuilder.getJavaType(columnMetaData.getJavaType()),
",jdbcType=", columnMetaData.getJdbcType(),
typeHandler != null ? ",typeHandler=" + typeHandler : "",
"}");
}
});
Expand Down Expand Up @@ -443,6 +450,7 @@ class H2Meta extends H2RDBDatabaseMetaData {
renderMap.put(SqlRender.TYPE.INSERT, new InsertSqlBuilder());
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.H2));
}

@Override
public String getDatabaseName() {
return DataSourceHolder.databaseSwitcher().currentDatabase();
Expand All @@ -455,6 +463,7 @@ class PGMeta extends PGRDBDatabaseMetaData {
renderMap.put(SqlRender.TYPE.INSERT, new InsertSqlBuilder());
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.POSTGRES));
}

@Override
public String getDatabaseName() {
return DataSourceHolder.databaseSwitcher().currentDatabase();
Expand All @@ -467,6 +476,7 @@ class MSSQLMeta extends MSSQLRDBDatabaseMetaData {
renderMap.put(SqlRender.TYPE.INSERT, new InsertSqlBuilder());
renderMap.put(SqlRender.TYPE.UPDATE, new UpdateSqlBuilder(Dialect.MSSQL));
}

@Override
public String getDatabaseName() {
return DataSourceHolder.databaseSwitcher().currentDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public SQL render(RDBTableMetaData metaData, InsertParam param) {

@Override
protected SqlAppender getParamString(String prefix, String paramName, RDBColumnMetaData rdbColumnMetaData) {
String typeHandler = rdbColumnMetaData.getProperty("typeHandler")
.getValue();

return new SqlAppender().add("#{", paramName,
",javaType=", EasyOrmSqlBuilder.getJavaType(rdbColumnMetaData.getJavaType()),
",jdbcType=", rdbColumnMetaData.getJdbcType(), "}");
",jdbcType=", rdbColumnMetaData.getJdbcType(),
typeHandler != null ? ",typeHandler=" + typeHandler : "",
"}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class UpdateSqlBuilder extends SimpleUpdateSqlRender {
public UpdateSqlBuilder(Dialect dialect) {
super(dialect);
}

@Override
public SQL render(RDBTableMetaData metaData, UpdateParam param) {
RDBTableMetaData metaDataNew = metaData.clone();
Expand All @@ -44,10 +45,15 @@ public SQL render(RDBTableMetaData metaData, UpdateParam param) {
.forEach(metaDataNew::removeColumn);
return super.render(metaDataNew, param);
}

@Override
protected SqlAppender getParamString(String paramName, RDBColumnMetaData rdbColumnMetaData) {
String typeHandler = rdbColumnMetaData.getProperty("typeHandler")
.getValue();

return new SqlAppender().add("#{", paramName,
",javaType=", EasyOrmSqlBuilder.getJavaType(rdbColumnMetaData.getJavaType()),
",jdbcType=", rdbColumnMetaData.getJdbcType(), "}");
",jdbcType=", rdbColumnMetaData.getJdbcType(),
typeHandler != null ? ",typeHandler=" + typeHandler : "", "}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hswebframework.ezorm.rdb.executor.SqlExecutor;
import org.hswebframework.web.commons.entity.param.DeleteParamEntity;
import org.hswebframework.web.commons.entity.param.QueryParamEntity;
import org.hswebframework.web.commons.entity.param.UpdateParamEntity;
import org.hswebframework.web.datasource.DataSourceHolder;
import org.hswebframework.web.dict.EnumDict;
import org.junit.Assert;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void init() throws SQLException {
}

@Test
public void testInsert() {
public void testCRUD() {

DataSourceHolder.databaseSwitcher().use("PUBLIC");

Expand All @@ -84,6 +85,11 @@ public void testInsert() {
testDao.query(query);
testDao.countNest(query);
testDao.count(query);
UpdateParamEntity.newUpdate()
.set("name","测试")
.where("id",entity.getId())
.exec(testDao::update);

DeleteParamEntity.newDelete()
.where("id", "1234")
.exec(testDao::delete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<resultMap id="TestResultMap" type="org.hswebframework.web.dao.crud.TestEntity">
<id property="id" column="id" javaType="Long" jdbcType="INTEGER"/>
<id property="name" column="name" javaType="String" jdbcType="VARCHAR"/>

</resultMap>

Expand Down Expand Up @@ -50,7 +51,7 @@
select
<include refid="BasicMapper.buildSelectField"/>
from ${_fullTableName} h_test
left join ${databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
left join ${_databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
<where>
<include refid="BasicMapper.buildWhere"/>
</where>
Expand All @@ -63,7 +64,7 @@
select
count(1)
from ${_fullTableName} h_test
left join ${databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
left join ${_databasePrefix}h_nest_table nest_table on nest_table.id=h_test.id
<where>
<include refid="BasicMapper.buildWhere"/>
</where>
Expand Down

0 comments on commit ccb4e96

Please sign in to comment.