use of org.sagacity.sqltoy.config.model.EntityMeta 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.config.model.EntityMeta 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.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class SqlServerDialect method loadAll.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#loadAll(java.util.List,
* java.util.List, java.sql.Connection)
*/
@Override
public List<?> loadAll(final SqlToyContext sqlToyContext, List<?> entities, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
if (null == entities || entities.isEmpty())
return null;
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
// 判断是否存在主键
if (null == entityMeta.getIdArray())
throw new Exception(entities.get(0).getClass().getName() + " Entity Object hasn't primary key,cann't use load method!");
StringBuilder loadSql = new StringBuilder();
loadSql.append("select * from ");
loadSql.append(tableName == null ? entityMeta.getSchemaTable() : tableName);
if (lockMode != null) {
switch(lockMode) {
case UPGRADE_NOWAIT:
case UPGRADE:
loadSql.append(" with (rowlock xlock) ");
break;
}
}
loadSql.append(" where ");
String field;
for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
field = entityMeta.getIdArray()[i];
if (i > 0)
loadSql.append(" and ");
loadSql.append(entityMeta.getColumnName(field));
loadSql.append(" in (:").append(field).append(") ");
}
return DialectUtils.loadAll(sqlToyContext, loadSql.toString(), entities, cascadeTypes, conn);
}
use of org.sagacity.sqltoy.config.model.EntityMeta 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.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class DB2Dialect method load.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#load(java.io.Serializable,
* java.util.List, java.sql.Connection)
*/
@Override
public Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
// 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
String loadSql = sqlToyConfig.getSql();
if (lockMode != null) {
switch(lockMode) {
case UPGRADE_NOWAIT:
case UPGRADE:
loadSql = loadSql + " for update with rs";
break;
}
}
return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Aggregations