use of org.sagacity.sqltoy.config.model.EntityMeta 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);
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class MySqlDialect 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 {
// mysql只支持identity,sequence 值忽略
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
String insertSql = DialectUtils.generateInsertSql(DBType.MYSQL, entityMeta, entityMeta.getIdStrategy(), NVL_FUNCTION, "NEXTVAL FOR " + entityMeta.getSequence(), isAssignPK, tableName);
return DialectUtils.saveAll(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, insertSql, entities, batchSize, reflectPropertyHandler, conn, autoCommit);
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class SqliteDialect 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(entityMeta.getSchemaTable());
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 PostgreSqlDialect method loadAll.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#loadAll(org.sagacity.sqltoy.
* SqlToyContext, java.util.List, java.util.List,
* org.sagacity.sqltoy.lock.LockMode, java.sql.Connection)
*/
@Override
public List<?> loadAll(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);
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(") ");
}
if (lockMode != null) {
switch(lockMode) {
case UPGRADE_NOWAIT:
loadSql.append(" for update nowait ");
break;
case UPGRADE:
loadSql.append(" for update ");
break;
}
}
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 PostgreSqlDialect method load.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#load(org.sagacity.sqltoy.
* SqlToyContext, java.io.Serializable, java.util.List,
* org.sagacity.sqltoy.lock.LockMode, java.sql.Connection)
*/
@Override
public Serializable load(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:
loadSql = loadSql + " for update nowait";
break;
case UPGRADE:
loadSql = loadSql + " for update";
break;
}
}
return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Aggregations