use of org.sagacity.sqltoy.callback.GenerateSqlHandler 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);
}
use of org.sagacity.sqltoy.callback.GenerateSqlHandler 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.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class Oracle11gDialect method update.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#update(org.sagacity.sqltoy.
* SqlToyContext , java.io.Serializable, java.lang.String[],
* java.sql.Connection)
*/
@Override
public Long update(SqlToyContext sqlToyContext, Serializable entity, String[] forceUpdateFields, final boolean cascade, final Class[] emptyCascadeClasses, final 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 = entityMeta.getSequence() + NEXTVAL;
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
pkStrategy = PKStrategy.SEQUENCE;
sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
}
return DialectUtils.getSaveOrUpdateSql(sqlToyContext.getUnifyFieldsHandler(), dbType, entityMeta, pkStrategy, forceUpdateFields, VIRTUAL_TABLE, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
}
}, emptyCascadeClasses, subTableForceUpdateProps, conn, dbType, tableName);
}
use of org.sagacity.sqltoy.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class OracleDialect 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 {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = entityMeta.getSequence().concat(".nextval");
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
pkStrategy = PKStrategy.SEQUENCE;
sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
}
boolean isAssignPK = OracleDialectUtils.isAssignPKValue(pkStrategy);
String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPK, tableName);
return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPK, ReturnPkType.PREPARD_ID, insertSql, entity, new GenerateSqlHandler() {
@Override
public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = entityMeta.getSequence().concat(".nextval");
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
pkStrategy = PKStrategy.SEQUENCE;
sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
}
return DialectExtUtils.generateInsertSql(dbType, entityMeta, pkStrategy, NVL_FUNCTION, sequence, OracleDialectUtils.isAssignPKValue(pkStrategy), null);
}
}, new GenerateSavePKStrategy() {
@Override
public SavePKStrategy generate(EntityMeta entityMeta) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
pkStrategy = PKStrategy.SEQUENCE;
}
return new SavePKStrategy(pkStrategy, OracleDialectUtils.isAssignPKValue(pkStrategy));
}
}, conn, dbType);
}
use of org.sagacity.sqltoy.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class TidbDialect 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);
}
Aggregations