use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class SqlServerDialectUtils method saveOrUpdateAll.
/**
* @todo 批量保存或修改
* @param sqlToyContext
* @param entities
* @param reflectPropertyHandler
* @param forceUpdateFields
* @param openIdentity
* @param conn
* @param dbType
* @param autoCommit
* @throws Exception
*/
public static Long saveOrUpdateAll(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, final ReflectPropertyHandler reflectPropertyHandler, final String[] forceUpdateFields, Connection conn, final Integer dbType, final Boolean autoCommit) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
boolean isIdentity = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.IDENTITY));
// 2012 默认为false
boolean openIdentity = SqlToyConstants.sqlServerIdentityOpen();
/**
* sqlserver2008 identity 模式必须要先执行set identity_insert tableName on
* 从2012版本后则无需进行设置
*/
if (isIdentity && openIdentity)
DialectUtils.executeSql("SET IDENTITY_INSERT " + entityMeta.getSchemaTable() + " ON", null, null, conn, true);
// sqlserver merge into must end with ";" charater
Long updateCount = DialectUtils.saveOrUpdateAll(sqlToyContext, entities, batchSize, entityMeta, forceUpdateFields, new GenerateSqlHandler() {
public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
String sql = SqlServerDialectUtils.getSaveOrUpdateSql(dbType, entityMeta, entityMeta.getIdStrategy(), forceUpdateFields, null, "isnull", "@mySeqVariable", false);
if (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE))
sql = "DECLARE @mySeqVariable as numeric(20)=NEXT VALUE FOR " + entityMeta.getSequence() + " " + sql;
return sql.concat(";");
}
}, reflectPropertyHandler, conn, autoCommit);
if (isIdentity && openIdentity)
DialectUtils.executeSql("SET IDENTITY_INSERT " + entityMeta.getSchemaTable() + " OFF", null, null, conn, true);
return updateCount;
}
use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class OracleDialect 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 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.ORACLE, entityMeta, pkStrategy, forceUpdateFields, DUAL, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), null);
}
}, emptyCascadeClasses, subTableForceUpdateProps, conn, tableName);
}
use of org.sagacity.sqltoy.dialect.handler.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 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.ORACLE, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPKValue(pkStrategy), tableName);
return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPKValue(pkStrategy), ReturnPkType.PREPARD_ID, insertSql, entity, new GenerateSqlHandler() {
// 通过反调方式提供oracle insert语句
public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
PKStrategy pkStrategy = entityMeta.getIdStrategy();
String sequence = entityMeta.getSequence() + NEXTVAL;
// oracle sequence主键策略
if (pkStrategy != null && pkStrategy.equals(PKStrategy.IDENTITY)) {
pkStrategy = PKStrategy.SEQUENCE;
sequence = entityMeta.getFieldsMeta().get(entityMeta.getIdArray()[0]).getDefaultValue();
}
return DialectUtils.generateInsertSql(DBType.ORACLE, 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);
}
use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class SqlServerDialect 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());
boolean isIdentity = entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.IDENTITY);
// sqlserver2012 开始默认为false
boolean openIdentity = SqlToyConstants.sqlServerIdentityOpen();
if (isIdentity && openIdentity)
DialectUtils.executeSql("SET IDENTITY_INSERT " + entityMeta.getSchemaTable() + " ON", null, null, conn, true);
// sqlserver merge into must end with ";" charater
Long updateCount = DialectUtils.saveAllIgnoreExist(sqlToyContext, entities, batchSize, entityMeta, new GenerateSqlHandler() {
public String generateSql(EntityMeta entityMeta, String[] forceUpdateFields) {
String sql = SqlServerDialectUtils.getSaveIgnoreExistSql(DBType.SQLSERVER, entityMeta, entityMeta.getIdStrategy(), null, "isnull", "@mySeqVariable", false);
if (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE))
sql = "DECLARE @mySeqVariable as numeric(20)=NEXT VALUE FOR " + entityMeta.getSequence() + " " + sql;
return sql.concat(";");
}
}, reflectPropertyHandler, conn, autoCommit);
if (isIdentity && openIdentity)
DialectUtils.executeSql("SET IDENTITY_INSERT " + entityMeta.getSchemaTable() + " OFF", null, null, conn, true);
return updateCount;
}
use of org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler in project sagacity-sqltoy by chenrenfei.
the class DB2Dialect 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, final Connection conn, final String tableName) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
// db2 identity 和sequence 都支持手工赋值
String insertSql = DialectUtils.generateInsertSql(DBType.DB2, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName);
ReturnPkType returnPkType = (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE)) ? ReturnPkType.PREPARD_ID : ReturnPkType.PREPARD_ID;
return DialectUtils.save(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPKValue(entityMeta.getIdStrategy()), returnPkType, insertSql, entity, new GenerateSqlHandler() {
public String generateSql(EntityMeta entityMeta, String[] forceUpdateField) {
return DialectUtils.generateInsertSql(DBType.DB2, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPKValue(entityMeta.getIdStrategy()), null);
}
}, new GenerateSavePKStrategy() {
public SavePKStrategy generate(EntityMeta entityMeta) {
return new SavePKStrategy(entityMeta.getIdStrategy(), isAssignPKValue(entityMeta.getIdStrategy()));
}
}, conn);
}
Aggregations