Search in sources :

Example 11 with GenerateSqlHandler

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

the class Oracle12Dialect 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 String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.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.ORACLE12, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPKValue(pkStrategy), ReturnPkType.PREPARD_ID, insertSql, entity, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
            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.generateInsertSql(DBType.ORACLE12, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
        }
    }, new GenerateSavePKStrategy() {

        public SavePKStrategy generate(EntityMeta entityMeta) {
            PKStrategy pkStrategy = entityMeta.getIdStrategy();
            if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
                pkStrategy = PKStrategy.SEQUENCE;
            }
            return new SavePKStrategy(pkStrategy, isAssignPKValue(pkStrategy));
        }
    }, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy) PKStrategy(org.sagacity.sqltoy.config.model.PKStrategy) GenerateSavePKStrategy(org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy) SavePKStrategy(org.sagacity.sqltoy.dialect.model.SavePKStrategy)

Example 12 with GenerateSqlHandler

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

the class SybaseIQDialect method saveOrUpdateAll.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#saveOrUpdateAll(org.sagacity.sqltoy
	 * .SqlToyContext, java.util.List, java.sql.Connection)
	 */
@Override
public Long saveOrUpdateAll(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, final ReflectPropertyHandler reflectPropertyHandler, final String[] forceUpdateFields, Connection conn, final Boolean autoCommit, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    // sybase iq 只支持identity模式
    boolean isIdentity = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.IDENTITY));
    boolean isOpenIdentity = (isIdentity && SqlToyConstants.sybaseIQIdentityOpen());
    if (isOpenIdentity)
        DialectUtils.executeSql("SET TEMPORARY OPTION IDENTITY_INSERT='" + entityMeta.getSchemaTable() + "'", null, null, conn, true);
    Long updateCount = DialectUtils.saveOrUpdateAll(sqlToyContext, entities, batchSize, entityMeta, forceUpdateFields, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
            return DialectUtils.getSaveOrUpdateSql(DBType.SYBASE_IQ, entityMeta, entityMeta.getIdStrategy(), forceUpdateFields, null, NVL_FUNCTION, entityMeta.getSequence() + ".NEXTVAL", isAssignPKValue(entityMeta.getIdStrategy()), tableName);
        }
    }, reflectPropertyHandler, conn, autoCommit);
    if (isOpenIdentity)
        DialectUtils.executeSql("SET TEMPORARY OPTION IDENTITY_INSERT=''", null, null, conn, true);
    return updateCount;
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)

Example 13 with GenerateSqlHandler

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

the class SybaseIQDialect method saveAllIgnoreExist.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#saveAllNotExist(org.sagacity.sqltoy.
	 * SqlToyContext, java.util.List,
	 * org.sagacity.sqltoy.callback.ReflectPropertyHandler, java.sql.Connection,
	 * java.lang.Boolean)
	 */
@Override
public Long saveAllIgnoreExist(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropertyHandler reflectPropertyHandler, Connection conn, final Boolean autoCommit, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    // sybase iq 只支持identity模式
    boolean isIdentity = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.IDENTITY));
    boolean isOpenIdentity = (isIdentity && SqlToyConstants.sybaseIQIdentityOpen());
    if (isOpenIdentity)
        DialectUtils.executeSql("SET TEMPORARY OPTION IDENTITY_INSERT='" + entityMeta.getSchemaTable() + "'", null, null, conn, true);
    Long updateCount = DialectUtils.saveAllIgnoreExist(sqlToyContext, entities, batchSize, entityMeta, new GenerateSqlHandler() {

        public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
            return DialectUtils.getSaveIgnoreExistSql(DBType.SYBASE_IQ, entityMeta, entityMeta.getIdStrategy(), null, NVL_FUNCTION, entityMeta.getSequence() + ".NEXTVAL", isAssignPKValue(entityMeta.getIdStrategy()), tableName);
        }
    }, reflectPropertyHandler, conn, autoCommit);
    if (isOpenIdentity)
        DialectUtils.executeSql("SET TEMPORARY OPTION IDENTITY_INSERT=''", null, null, conn, true);
    return updateCount;
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)

Aggregations

EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)13 GenerateSqlHandler (org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)13 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)8 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)8 PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)6 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)2