Search in sources :

Example 1 with GenerateSqlHandler

use of org.sagacity.sqltoy.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method oceanBaseSaveOrUpdateAll.

// 针对oceanBase
private static void oceanBaseSaveOrUpdateAll(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";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
            }
            return DialectExtUtils.mergeIgnore(dbType, entityMeta, pkStrategy, "dual", "nvl", sequence, OracleDialectUtils.isAssignPKValue(pkStrategy), tableName);
        }
    }, reflectPropsHandler, conn, dbType, null);
    logger.debug("级联子表:{} 变更记录数:{},新建记录数为:{}", tableName, updateCnt, saveCnt);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 2 with GenerateSqlHandler

use of org.sagacity.sqltoy.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method postgreSaveOrUpdateAll.

// 针对postgresql 数据库
private static void postgreSaveOrUpdateAll(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, "COALESCE", 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 = "nextval('" + entityMeta.getSequence() + "')";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                // 伪造成sequence模式
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = "DEFAULT";
            }
            boolean isAssignPK = PostgreSqlDialectUtils.isAssignPKValue(pkStrategy);
            return DialectExtUtils.insertIgnore(dbType, entityMeta, pkStrategy, "COALESCE", sequence, isAssignPK, tableName);
        }
    }, reflectPropsHandler, conn, dbType, null);
    logger.debug("级联子表:{} 变更记录数:{},新建记录数为:{}", tableName, updateCnt, saveCnt);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 3 with GenerateSqlHandler

use of org.sagacity.sqltoy.callback.GenerateSqlHandler 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);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 4 with GenerateSqlHandler

use of org.sagacity.sqltoy.callback.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.

the class SqliteDialect 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 {
    // sqlite 只提供autoincrement 机制,即identity模式,所以sequence可以忽略
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    boolean isAssignPk = SqliteDialectUtils.isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectExtUtils.generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPk, tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPk, ReturnPkType.PREPARD_ID, 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(), SqliteDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()), null);
        }
    }, new GenerateSavePKStrategy() {

        @Override
        public SavePKStrategy generate(EntityMeta entityMeta) {
            return new SavePKStrategy(entityMeta.getIdStrategy(), SqliteDialectUtils.isAssignPKValue(entityMeta.getIdStrategy()));
        }
    }, conn, dbType);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy)

Example 5 with GenerateSqlHandler

use of org.sagacity.sqltoy.callback.GenerateSqlHandler 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);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.callback.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.callback.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy)

Aggregations

GenerateSqlHandler (org.sagacity.sqltoy.callback.GenerateSqlHandler)20 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)20 GenerateSavePKStrategy (org.sagacity.sqltoy.callback.GenerateSavePKStrategy)18 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)18 PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)14 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)5