Search in sources :

Example 16 with ReflectPropertyHandler

use of org.sagacity.sqltoy.callback.ReflectPropertyHandler in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method saveAllIgnoreExist.

/**
 * @todo 执行批量保存或修改操作
 * @param sqlToyContext
 * @param sql
 * @param entities
 * @param entityMeta
 * @param reflectPropertyHandler
 * @param conn
 * @param autoCommit
 * @throws Exception
 */
public static Long saveAllIgnoreExist(SqlToyContext sqlToyContext, List<?> entities, final int batchSize, EntityMeta entityMeta, GenerateSqlHandler generateSqlHandler, ReflectPropertyHandler reflectPropertyHandler, Connection conn, Boolean autoCommit) throws Exception {
    // 构造全新的新增记录参数赋值反射(覆盖之前的)
    ReflectPropertyHandler handler = getAddReflectHandler(sqlToyContext, reflectPropertyHandler);
    List<Object[]> paramValues = BeanUtil.reflectBeansToInnerAry(entities, entityMeta.getFieldsArray(), null, handler, false, 0);
    int pkIndex = entityMeta.getIdIndex();
    // 是否存在业务ID
    boolean hasBizId = (entityMeta.getBusinessIdGenerator() == null) ? false : true;
    int bizIdColIndex = hasBizId ? entityMeta.getFieldIndex(entityMeta.getBusinessIdField()) : 0;
    // 标识符
    String signature = entityMeta.getBizIdSignature();
    Integer relatedColumn = entityMeta.getBizIdRelatedColIndex();
    // 无主键以及多主键以及assign或通过generator方式产生主键策略
    if (null != entityMeta.getIdStrategy() && null != entityMeta.getIdGenerator()) {
        int bizIdLength = entityMeta.getBizIdLength();
        int idLength = entityMeta.getIdLength();
        Object[] rowData;
        Object relatedColValue = null;
        int idJdbcType = entityMeta.getIdType();
        int businessIdType = hasBizId ? entityMeta.getColumnType(entityMeta.getBusinessIdField()) : 0;
        for (int i = 0; i < paramValues.size(); i++) {
            rowData = (Object[]) paramValues.get(i);
            // 关联字段赋值
            if (relatedColumn != null) {
                relatedColValue = rowData[relatedColumn];
            }
            if (StringUtil.isBlank(rowData[pkIndex])) {
                rowData[pkIndex] = entityMeta.getIdGenerator().getId(entityMeta.getTableName(), signature, relatedColValue, null, idJdbcType, idLength);
                // 回写主键值
                BeanUtils.setProperty(entities.get(i), entityMeta.getIdArray()[0], rowData[pkIndex]);
            }
            if (hasBizId && StringUtil.isBlank(rowData[bizIdColIndex])) {
                rowData[bizIdColIndex] = entityMeta.getBusinessIdGenerator().getId(entityMeta.getTableName(), signature, relatedColValue, null, businessIdType, bizIdLength);
                // 回写业务主键值
                BeanUtils.setProperty(entities.get(i), entityMeta.getBusinessIdField(), rowData[bizIdColIndex]);
            }
        }
    }
    String saveAllNotExistSql = generateSqlHandler.generateSql(entityMeta, null);
    if (sqlToyContext.isDebug())
        out.println("saveAllNotExistSql=" + saveAllNotExistSql);
    return SqlUtil.batchUpdateByJdbc(saveAllNotExistSql, paramValues, batchSize, null, entityMeta.getFieldsTypeArray(), autoCommit, conn);
}
Also used : ReflectPropertyHandler(org.sagacity.sqltoy.callback.ReflectPropertyHandler)

Aggregations

ReflectPropertyHandler (org.sagacity.sqltoy.callback.ReflectPropertyHandler)16 ArrayList (java.util.ArrayList)9 List (java.util.List)7 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 OneToManyModel (org.sagacity.sqltoy.config.model.OneToManyModel)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Map (java.util.Map)3 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)3 Type (java.lang.reflect.Type)2 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)2 Test (org.junit.Test)1 SqlType (org.sagacity.sqltoy.config.model.SqlType)1 GenerateSavePKStrategy (org.sagacity.sqltoy.dialect.handler.GenerateSavePKStrategy)1 ReturnPkType (org.sagacity.sqltoy.dialect.model.ReturnPkType)1 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)1 DBType (org.sagacity.sqltoy.utils.DataSourceUtils.DBType)1