use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method wrapDecryptHandler.
/**
* @TODO 构造加解密处理器
* @param sqlToyContext
* @param resultType
* @return
*/
private DecryptHandler wrapDecryptHandler(final SqlToyContext sqlToyContext, Type resultType) {
// 只针对POJO 实体类
if (resultType == null || resultType.equals(Map.class) || resultType.equals(HashMap.class) || resultType.equals(List.class)) {
return null;
}
FieldsSecureProvider fieldsSecureProvider = sqlToyContext.getFieldsSecureProvider();
if (fieldsSecureProvider == null) {
return null;
}
EntityMeta entityMeta = null;
if (sqlToyContext.isEntity((Class) resultType)) {
entityMeta = sqlToyContext.getEntityMeta((Class) resultType);
}
if (entityMeta == null || entityMeta.getSecureColumns() == null) {
return null;
}
return new DecryptHandler(fieldsSecureProvider, entityMeta.getSecureColumns());
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method wrapTreeTableRoute.
/**
* @todo 构造树形表的节点路径、节点层级、节点类别(是否叶子节点)
* @param sqlToyContext
* @param treeModel
* @param dataSource
* @return
*/
public boolean wrapTreeTableRoute(final SqlToyContext sqlToyContext, final TreeTableModel treeModel, final DataSource dataSource) {
if (treeModel == null || StringUtil.isBlank(treeModel.getPidField())) {
throw new IllegalArgumentException("请检查pidField赋值是否正确!");
}
if (StringUtil.isBlank(treeModel.getLeafField()) || StringUtil.isBlank(treeModel.getNodeRouteField()) || StringUtil.isBlank(treeModel.getNodeLevelField())) {
throw new IllegalArgumentException("请检查isLeafField\nodeRouteField\nodeLevelField 赋值是否正确!");
}
try {
if (null != treeModel.getEntity()) {
EntityMeta entityMeta = null;
if (treeModel.getEntity() instanceof Type) {
entityMeta = sqlToyContext.getEntityMeta((Class) treeModel.getEntity());
} else {
entityMeta = sqlToyContext.getEntityMeta(treeModel.getEntity().getClass());
}
// 兼容填写fieldName,统一转化为columnName
// pid
String columnName = entityMeta.getColumnName(treeModel.getPidField());
if (columnName != null) {
treeModel.pidField(columnName);
}
// leafField
columnName = entityMeta.getColumnName(treeModel.getLeafField());
if (columnName != null) {
treeModel.isLeafField(columnName);
}
// nodeLevel
columnName = entityMeta.getColumnName(treeModel.getNodeLevelField());
if (columnName != null) {
treeModel.nodeLevelField(columnName);
}
// nodeRoute
columnName = entityMeta.getColumnName(treeModel.getNodeRouteField());
if (columnName != null) {
treeModel.nodeRouteField(columnName);
}
HashMap<String, String> columnMap = new HashMap<String, String>();
for (FieldMeta column : entityMeta.getFieldsMeta().values()) {
columnMap.put(column.getColumnName().toUpperCase(), "");
}
if (!columnMap.containsKey(treeModel.getNodeRouteField().toUpperCase())) {
throw new IllegalArgumentException("树形表:节点路径字段名称:" + treeModel.getNodeRouteField() + "不正确,请检查!");
}
if (!columnMap.containsKey(treeModel.getLeafField().toUpperCase())) {
throw new IllegalArgumentException("树形表:是否叶子节点字段名称:" + treeModel.getLeafField() + "不正确,请检查!");
}
if (!columnMap.containsKey(treeModel.getNodeLevelField().toUpperCase())) {
throw new IllegalArgumentException("树形表:节点等级字段名称:" + treeModel.getNodeLevelField() + "不正确,请检查!");
}
FieldMeta idMeta = (FieldMeta) entityMeta.getFieldMeta(entityMeta.getIdArray()[0]);
// 如未定义则使用主键(update 2020-10-16)
if (StringUtil.isBlank(treeModel.getIdField())) {
treeModel.idField(idMeta.getColumnName());
} else {
// 别名转换
columnName = entityMeta.getColumnName(treeModel.getIdField());
if (columnName != null) {
treeModel.idField(columnName);
}
}
if (StringUtil.isBlank(treeModel.getTableName())) {
treeModel.table(entityMeta.getSchemaTable(null, null));
}
// 通过实体对象取值给rootId和idValue赋值
if (!(treeModel.getEntity() instanceof Type)) {
// update 2020-10-19 从手工设定的字段中取值(原本从主键中取值)
if (null == treeModel.getRootId()) {
Object pidValue = BeanUtil.getProperty(treeModel.getEntity(), StringUtil.toHumpStr(treeModel.getPidField(), false));
treeModel.rootId(pidValue);
}
if (null == treeModel.getIdValue()) {
Object idValue = BeanUtil.getProperty(treeModel.getEntity(), StringUtil.toHumpStr(treeModel.getIdField(), false));
treeModel.setIdValue(idValue);
}
}
// 类型,默认值为false
if (idMeta.getType() == java.sql.Types.INTEGER || idMeta.getType() == java.sql.Types.DECIMAL || idMeta.getType() == java.sql.Types.DOUBLE || idMeta.getType() == java.sql.Types.FLOAT || idMeta.getType() == java.sql.Types.NUMERIC) {
treeModel.idTypeIsChar(false);
// update 2016-12-05 节点路径默认采取主键值直接拼接,更加直观科学
// treeModel.setAppendZero(true);
} else if (idMeta.getType() == java.sql.Types.VARCHAR || idMeta.getType() == java.sql.Types.CHAR) {
treeModel.idTypeIsChar(true);
}
}
SqlExecuteStat.start(treeModel.getTableName(), "wrapTreeTableRoute", true);
return (Boolean) DataSourceUtils.processDataSource(sqlToyContext, dataSource, new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(SqlUtil.wrapTreeTableRoute(sqlToyContext.getTypeHandler(), treeModel, conn, dbType));
}
});
} catch (Exception e) {
logger.error("封装树形表节点路径操作:wrapTreeTableRoute发生错误,{}", e.getMessage());
e.printStackTrace();
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class EntityManager method getEntityMeta.
/**
* @todo <b>获取Entity类的对应数据库表信息,如:查询、修改、插入sql、对象属性跟表字段之间的关系等信息</b>
* @param sqlToyContext
* @param voClass
* @return
*/
public EntityMeta getEntityMeta(SqlToyContext sqlToyContext, Class voClass) {
if (voClass == null) {
return null;
}
Class entityClass = BeanUtil.getEntityClass(voClass);
String className = entityClass.getName();
EntityMeta entityMeta = entitysMetaMap.get(className);
// 增加在使用对象时动态解析的功能,因此可以不用配置packagesToScan和annotatedClasses
if (entityMeta == null) {
entityMeta = parseEntityMeta(sqlToyContext, entityClass, true);
if (entityMeta == null) {
throw new IllegalArgumentException("您传入的对象:[".concat(className).concat(" ]不是一个@SqlToyEntity实体POJO对象,sqltoy实体对象必须使用 @SqlToyEntity/@Entity/@Id 等注解来标识!"));
}
}
return entityMeta;
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class MySqlDialect method save.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#save(org.sagacity.sqltoy.
* SqlToyContext , java.io.Serializable, java.util.List, java.sql.Connection)
*/
@Override
public Object save(SqlToyContext sqlToyContext, Serializable entity, Connection conn, final Integer dbType, final String dialect, final String tableName) throws Exception {
// mysql只支持identity,sequence 值忽略,mysql identity可以手工插入
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
boolean isAssignPK = MySqlDialectUtils.isAssignPKValue(entityMeta.getIdStrategy());
String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName);
ReturnPkType returnPkType = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE)) ? ReturnPkType.GENERATED_KEYS : ReturnPkType.PREPARD_ID;
return DialectUtils.save(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, returnPkType, insertSql, entity, new GenerateSqlHandler() {
@Override
public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
return DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), MySqlDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()), null);
}
}, new GenerateSavePKStrategy() {
@Override
public SavePKStrategy generate(EntityMeta entityMeta) {
return new SavePKStrategy(entityMeta.getIdStrategy(), MySqlDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()));
}
}, conn, dbType);
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class MySqlDialect method saveAllIgnoreExist.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#saveAllNotExist(org.sagacity.sqltoy.
* SqlToyContext, java.util.List,
* org.sagacity.sqltoy.callback.ReflectPropsHandler, java.sql.Connection,
* java.lang.Boolean)
*/
@Override
public Long saveAllIgnoreExist(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropsHandler reflectPropsHandler, Connection conn, final Integer dbType, final String dialect, final Boolean autoCommit, final String tableName) throws Exception {
// mysql只支持identity,sequence 值忽略
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
boolean isAssignPK = MySqlDialectUtils.isAssignPKValue(entityMeta.getIdStrategy());
String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName).replaceFirst("(?i)insert ", "insert ignore ");
return DialectUtils.saveAll(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, insertSql, entities, batchSize, reflectPropsHandler, conn, dbType, autoCommit);
}
Aggregations