use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialect method update.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#update(org.sagacity.sqltoy.
* SqlToyContext, java.io.Serializable, java.lang.String[], boolean,
* java.lang.Class[], java.util.HashMap, java.sql.Connection)
*/
@Override
public Long update(SqlToyContext sqlToyContext, Serializable entity, String[] forceUpdateFields, boolean cascade, Class[] emptyCascadeClasses, HashMap<Class, String[]> subTableForceUpdateProps, Connection conn, final Integer dbType, final String dialect, final String tableName) throws Exception {
return DialectUtils.update(sqlToyContext, entity, NVL_FUNCTION, forceUpdateFields, cascade, (cascade == false) ? null : new GenerateSqlHandler() {
@Override
public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = "nextval('" + entityMeta.getSequence() + "')";
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
// 伪造成sequence模式
pkStrategy = PKStrategy.SEQUENCE;
sequence = "DEFAULT";
}
boolean isAssignPK = PostgreSqlDialectUtils.isAssignPKValue(pkStrategy);
return PostgreSqlDialectUtils.getSaveOrUpdateSql(dbType, entityMeta, pkStrategy, isAssignPK, sequence, forceUpdateFields, null);
}
}, emptyCascadeClasses, subTableForceUpdateProps, conn, dbType, tableName);
}
use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialectUtils method saveAll.
/**
* @todo 批量保存对象入数据库
* @param sqlToyContext
* @param entities
* @param batchSize
* @param reflectPropsHandler
* @param conn
* @param dbType
* @param autoCommit
* @param tableName
* @return
* @throws Exception
*/
public static Long saveAll(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropsHandler reflectPropsHandler, Connection conn, final Integer dbType, final Boolean autoCommit, String tableName) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = "nextval('" + entityMeta.getSequence() + "')";
// identity模式用关键词default 代替
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
// 伪造成sequence模式
pkStrategy = PKStrategy.SEQUENCE;
sequence = "DEFAULT";
}
boolean isAssignPK = isAssignPKValue(pkStrategy);
String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPK, tableName);
return DialectUtils.saveAll(sqlToyContext, entityMeta, pkStrategy, isAssignPK, insertSql, entities, batchSize, reflectPropsHandler, conn, dbType, autoCommit);
}
use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialectUtils method save.
/**
* @todo 保存单条对象记录
* @param sqlToyContext
* @param entity
* @param conn
* @param dbType
* @param tableName
* @return
* @throws Exception
*/
public static Object save(SqlToyContext sqlToyContext, Serializable entity, Connection conn, final Integer dbType, String tableName) throws Exception {
// 只支持sequence模式
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = "nextval('" + entityMeta.getSequence() + "')";
// 从10版本开始支持identity
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
// 伪造成sequence模式
pkStrategy = PKStrategy.SEQUENCE;
sequence = "DEFAULT";
}
boolean isAssignPK = isAssignPKValue(pkStrategy);
String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPK, tableName);
return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPK, ReturnPkType.GENERATED_KEYS, insertSql, entity, new GenerateSqlHandler() {
@Override
public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = "nextval('" + entityMeta.getSequence() + "')";
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
// 伪造成sequence模式
pkStrategy = PKStrategy.SEQUENCE;
sequence = "DEFAULT";
}
return DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
}
}, new GenerateSavePKStrategy() {
@Override
public SavePKStrategy generate(EntityMeta entityMeta) {
return new SavePKStrategy(entityMeta.getIdStrategy(), isAssignPKValue(entityMeta.getIdStrategy()));
}
}, conn, dbType);
}
use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.
the class ClickHouseDialectUtils method save.
/**
* @todo 保存对象
* @param sqlToyContext
* @param entityMeta
* @param insertSql
* @param entity
* @param conn
* @param dbType
* @return
* @throws Exception
*/
public static Object save(SqlToyContext sqlToyContext, final EntityMeta entityMeta, final String insertSql, Serializable entity, final Connection conn, final Integer dbType) throws Exception {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
ReturnPkType returnPkType = (pkStrategy != null && pkStrategy.equals(PKStrategy.SEQUENCE)) ? ReturnPkType.GENERATED_KEYS : ReturnPkType.PREPARD_ID;
final boolean isIdentity = (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY));
final boolean isSequence = (pkStrategy != null && pkStrategy.equals(PKStrategy.SEQUENCE));
String[] reflectColumns;
boolean isAssignPK = isAssignPKValue(pkStrategy);
if ((isIdentity && !isAssignPK) || (isSequence && !isAssignPK)) {
reflectColumns = entityMeta.getRejectIdFieldArray();
} else {
reflectColumns = entityMeta.getFieldsArray();
}
// 构造全新的新增记录参数赋值反射(覆盖之前的)
ReflectPropsHandler handler = DialectUtils.getAddReflectHandler(null, sqlToyContext.getUnifyFieldsHandler());
handler = DialectUtils.getSecureReflectHandler(handler, sqlToyContext.getFieldsSecureProvider(), sqlToyContext.getDesensitizeProvider(), entityMeta.getSecureFields());
Object[] fullParamValues = BeanUtil.reflectBeanToAry(entity, reflectColumns, null, handler);
boolean needUpdatePk = false;
int pkIndex = entityMeta.getIdIndex();
// 是否存在业务ID
boolean hasBizId = (entityMeta.getBusinessIdGenerator() == null) ? false : true;
int bizIdColIndex = hasBizId ? entityMeta.getFieldIndex(entityMeta.getBusinessIdField()) : 0;
// 标识符
String signature = entityMeta.getBizIdSignature();
Integer[] relatedColumn = entityMeta.getBizIdRelatedColIndex();
String[] relatedColumnNames = entityMeta.getBizIdRelatedColumns();
int relatedColumnSize = (relatedColumn == null) ? 0 : relatedColumn.length;
// 主键采用assign方式赋予,则调用generator产生id并赋予其值
if (entityMeta.getIdStrategy() != null && null != entityMeta.getIdGenerator()) {
int bizIdLength = entityMeta.getBizIdLength();
int idLength = entityMeta.getIdLength();
Object[] relatedColValue = null;
String businessIdType = hasBizId ? entityMeta.getColumnJavaType(entityMeta.getBusinessIdField()) : "";
if (relatedColumn != null) {
relatedColValue = new Object[relatedColumnSize];
for (int meter = 0; meter < relatedColumnSize; meter++) {
relatedColValue[meter] = fullParamValues[relatedColumn[meter]];
if (StringUtil.isBlank(relatedColValue[meter])) {
throw new IllegalArgumentException("对象:" + entityMeta.getEntityClass().getName() + " 生成业务主键依赖的关联字段:" + relatedColumnNames[meter] + " 值为null!");
}
}
}
if (StringUtil.isBlank(fullParamValues[pkIndex])) {
// id通过generator机制产生,设置generator产生的值
fullParamValues[pkIndex] = entityMeta.getIdGenerator().getId(entityMeta.getTableName(), signature, entityMeta.getBizIdRelatedColumns(), relatedColValue, null, entityMeta.getIdType(), idLength, entityMeta.getBizIdSequenceSize());
needUpdatePk = true;
}
if (hasBizId && StringUtil.isBlank(fullParamValues[bizIdColIndex])) {
fullParamValues[bizIdColIndex] = entityMeta.getBusinessIdGenerator().getId(entityMeta.getTableName(), signature, entityMeta.getBizIdRelatedColumns(), relatedColValue, null, businessIdType, bizIdLength, entityMeta.getBizIdSequenceSize());
// 回写业务主键值
BeanUtil.setProperty(entity, entityMeta.getBusinessIdField(), fullParamValues[bizIdColIndex]);
}
}
SqlExecuteStat.showSql("执行单记录插入", insertSql, null);
final Object[] paramValues = fullParamValues;
final Integer[] paramsType = entityMeta.getFieldsTypeArray();
PreparedStatement pst = null;
Object result = SqlUtil.preparedStatementProcess(null, pst, null, new PreparedStatementResultHandler() {
@Override
public void execute(Object obj, PreparedStatement pst, ResultSet rs) throws SQLException, IOException {
if (isIdentity || isSequence) {
if (returnPkType.equals(ReturnPkType.GENERATED_KEYS)) {
pst = conn.prepareStatement(insertSql, PreparedStatement.RETURN_GENERATED_KEYS);
} else if (returnPkType.equals(ReturnPkType.PREPARD_ID)) {
pst = conn.prepareStatement(insertSql, new String[] { entityMeta.getColumnName(entityMeta.getIdArray()[0]) });
} else {
pst = conn.prepareStatement(insertSql);
}
} else {
pst = conn.prepareStatement(insertSql);
}
SqlUtil.setParamsValue(sqlToyContext.getTypeHandler(), conn, dbType, pst, paramValues, paramsType, 0);
ResultSet keyResult = null;
if ((isIdentity || isSequence) && returnPkType.equals(ReturnPkType.RESULT_GET)) {
keyResult = pst.executeQuery();
} else {
pst.execute();
}
if (isIdentity || isSequence) {
if (!returnPkType.equals(ReturnPkType.RESULT_GET)) {
keyResult = pst.getGeneratedKeys();
}
if (keyResult != null) {
List result = new ArrayList();
while (keyResult.next()) {
result.add(keyResult.getObject(1));
}
if (result.size() == 1) {
this.setResult(result.get(0));
} else {
this.setResult(result.toArray());
}
}
}
}
});
// 无主键直接返回null
if (entityMeta.getIdArray() == null) {
return null;
}
if (result == null) {
result = fullParamValues[pkIndex];
}
// 回置到entity 主键值
if (needUpdatePk || isIdentity || isSequence) {
BeanUtil.setProperty(entity, entityMeta.getIdArray()[0], result);
}
return result;
}
use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.
the class DialectUtils method dmSaveOrUpdateAll.
// 针对达梦数据库
private static void dmSaveOrUpdateAll(SqlToyContext sqlToyContext, final EntityMeta entityMeta, List<?> entities, ReflectPropsHandler reflectPropsHandler, final String[] forceUpdateFields, Connection conn, final Integer dbType) throws Exception {
int batchSize = sqlToyContext.getBatchSize();
final String tableName = entityMeta.getSchemaTable(null, dbType);
Long updateCnt = updateAll(sqlToyContext, entities, batchSize, forceUpdateFields, reflectPropsHandler, "nvl", conn, dbType, null, tableName, true);
// 如果修改的记录数量跟总记录数量一致,表示全部是修改
if (updateCnt >= entities.size()) {
logger.debug("级联子表{}修改记录数为:{}", tableName, updateCnt);
return;
}
Long saveCnt = saveAllIgnoreExist(sqlToyContext, entities, batchSize, entityMeta, new GenerateSqlHandler() {
@Override
public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = entityMeta.getSequence() + ".nextval";
return DialectExtUtils.mergeIgnore(dbType, entityMeta, pkStrategy, "dual", "nvl", sequence, DMDialectUtils.isAssignPKValue(pkStrategy), tableName);
}
}, reflectPropsHandler, conn, dbType, null);
logger.debug("级联子表:{} 变更记录数:{},新建记录数为:{}", tableName, updateCnt, saveCnt);
}
Aggregations