Search in sources :

Example 76 with EntityMeta

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

the class SapIQDialectUtils method saveAll.

/**
 * @todo 批量保存处理
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param reflectPropertyHandler
 * @param openIdentity
 * @param conn
 * @param tableName
 * @throws Exception
 */
public static Long saveAll(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, ReflectPropertyHandler reflectPropertyHandler, boolean openIdentity, Connection conn, String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    String insertSql = DialectUtils.generateInsertSql(DBType.SYBASE_IQ, entityMeta, entityMeta.getIdStrategy(), null, "@mySeqVariable", false, tableName);
    if (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE))
        insertSql = "DECLARE @mySeqVariable decimal(20) select @mySeqVariable=" + entityMeta.getSequence() + ".NEXTVAL " + insertSql;
    if (sqlToyContext.isDebug())
        out.println("batch insert sql:" + insertSql);
    return saveAll(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), false, insertSql, entities, batchSize, reflectPropertyHandler, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 77 with EntityMeta

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

the class PostgreSqlDialectUtils method save.

/**
 * @todo 保存单条对象记录
 * @param sqlToyContext
 * @param entity
 * @param conn
 * @param tableName
 * @return
 * @throws Exception
 */
public static Object save(SqlToyContext sqlToyContext, Serializable entity, Connection conn, String tableName) throws Exception {
    // 只支持sequence模式
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    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() + ")";
    }
    boolean isAssignPK = isAssignPKValue(pkStrategy);
    String insertSql = DialectUtils.generateInsertSql(DBType.POSTGRESQL, entityMeta, pkStrategy, NVL_FUNCTION, sequence, isAssignPK, tableName);
    return DialectUtils.save(sqlToyContext, entityMeta, pkStrategy, isAssignPK, ReturnPkType.GENERATED_KEYS, insertSql, entity, new GenerateSqlHandler() {

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

        public SavePKStrategy generate(EntityMeta entityMeta) {
            return new SavePKStrategy(entityMeta.getIdStrategy(), isAssignPKValue(entityMeta.getIdStrategy()));
        }
    }, 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 78 with EntityMeta

use of org.sagacity.sqltoy.config.model.EntityMeta 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;
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) GenerateSqlHandler(org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)

Example 79 with EntityMeta

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

the class SqlServerDialectUtils method saveAll.

/**
 * @todo 批量保存处理
 * @param sqlToyContext
 * @param entities
 * @param reflectPropertyHandler
 * @param conn
 * @param dbType
 * @param autoCommit
 * @throws Exception
 */
public static Long saveAll(SqlToyContext sqlToyContext, List<?> entities, ReflectPropertyHandler reflectPropertyHandler, Connection conn, Integer dbType, final Boolean autoCommit) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = generateInsertSql(dbType, entityMeta, entityMeta.getIdStrategy(), "isnull", "@mySeqVariable", isAssignPK);
    if (entityMeta.getIdStrategy() != null && entityMeta.getIdStrategy().equals(PKStrategy.SEQUENCE))
        insertSql = "DECLARE @mySeqVariable as numeric(20)=NEXT VALUE FOR " + entityMeta.getSequence() + " " + insertSql;
    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);
    Long updateCount = saveAll(sqlToyContext, entityMeta, entityMeta.getIdStrategy(), isAssignPK, insertSql, entities, reflectPropertyHandler, conn, autoCommit);
    if (isIdentity && openIdentity)
        DialectUtils.executeSql("SET IDENTITY_INSERT " + entityMeta.getSchemaTable() + " OFF", null, null, conn, true);
    return updateCount;
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Example 80 with EntityMeta

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

the class DB2Dialect 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 {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
    boolean isAssignPK = isAssignPKValue(entityMeta.getIdStrategy());
    String insertSql = DialectUtils.generateInsertSql(DBType.DB2, 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);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta)

Aggregations

EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)141 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)36 PKStrategy (org.sagacity.sqltoy.config.model.PKStrategy)28 GenerateSavePKStrategy (org.sagacity.sqltoy.callback.GenerateSavePKStrategy)24 ArrayList (java.util.ArrayList)23 Serializable (java.io.Serializable)22 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)22 List (java.util.List)20 GenerateSqlHandler (org.sagacity.sqltoy.callback.GenerateSqlHandler)20 GenerateSqlHandler (org.sagacity.sqltoy.dialect.handler.GenerateSqlHandler)13 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)12 IOException (java.io.IOException)11 SQLException (java.sql.SQLException)11 HashMap (java.util.HashMap)11 TableCascadeModel (org.sagacity.sqltoy.config.model.TableCascadeModel)10 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)10 OneToManyModel (org.sagacity.sqltoy.config.model.OneToManyModel)8 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)8 Type (java.lang.reflect.Type)6 PreparedStatement (java.sql.PreparedStatement)6