Search in sources :

Example 1 with PKStrategy

use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.

the class Oracle12Dialect 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[] forceCascadeClass, final HashMap<Class, String[]> subTableForceUpdateProps, Connection conn, final String tableName) throws Exception {
    return DialectUtils.update(sqlToyContext, entity, NVL_FUNCTION, forceUpdateFields, cascade, (cascade == false) ? null : new GenerateSqlHandler() {

        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(DBType.ORACLE12, entityMeta, pkStrategy, forceUpdateFields, "dual", NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
        }
    }, forceCascadeClass, subTableForceUpdateProps, conn, tableName);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 2 with PKStrategy

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 String tableName) throws Exception {
    return DialectUtils.update(sqlToyContext, entity, NVL_FUNCTION, forceUpdateFields, cascade, (cascade == false) ? null : new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            String sequence = "nextval('" + entityMeta.getSequence() + "')";
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
                sequence = "nextval(" + entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue() + ")";
            }
            return PostgreSqlDialectUtils.getSaveOrUpdateSql(DBType.POSTGRESQL, entityMeta, pkStrategy, sequence, forceUpdateFields, null);
        }
    }, emptyCascadeClasses, subTableForceUpdateProps, conn, tableName);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 3 with PKStrategy

use of org.sagacity.sqltoy.config.model.PKStrategy in project sagacity-sqltoy by chenrenfei.

the class OracleDialect method saveAll.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#saveAll(org.sagacity.sqltoy.
	 * SqlToyContext , java.util.List,
	 * org.sagacity.core.utils.callback.ReflectPropertyHandler, java.sql.Connection)
	 */
@Override
public Long saveAll(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropertyHandler reflectPropertyHandler, Connection conn, final Boolean autoCommit, final String tableName) throws Exception {
    // oracle12c 开始支持identity机制
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    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();
    }
    String insertSql = DialectUtils.generateInsertSql(DBType.ORACLE, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), tableName);
    return DialectUtils.saveAll(sqlToyContext, entityMeta, pkStrategy, isAssignPKValue(pkStrategy), insertSql, entities, batchSize, reflectPropertyHandler, conn, autoCommit);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy)

Example 4 with PKStrategy

use of org.sagacity.sqltoy.config.model.PKStrategy 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 5 with PKStrategy

use of org.sagacity.sqltoy.config.model.PKStrategy 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)

Aggregations

PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)30 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)28 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)25 GenerateSavePKStrategy (org.sagacity.sqltoy.callback.GenerateSavePKStrategy)17 GenerateSqlHandler (org.sagacity.sqltoy.callback.GenerateSqlHandler)14 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)8 GenerateSqlHandler (org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)6 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)2 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)1 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)1