Search in sources :

Example 1 with FieldMeta

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

the class DB2DialectUtils method getSaveOrUpdateSql.

/**
 * @todo 处理加工对象基于db2 的merge into 语句
 * @param dbType
 * @param entityMeta
 * @param pkStrategy
 * @param forceUpdateFields
 * @param fromTable
 * @param isNullFunction
 * @param sequence
 * @param isAssignPK
 * @return
 */
public static String getSaveOrUpdateSql(Integer dbType, EntityMeta entityMeta, PKStrategy pkStrategy, String[] forceUpdateFields, String fromTable, String isNullFunction, String sequence, boolean isAssignPK, String tableName) {
    String realTable = (tableName == null) ? entityMeta.getSchemaTable() : tableName;
    // 在无主键的情况下产生insert sql语句
    if (entityMeta.getIdArray() == null) {
        return DialectUtils.generateInsertSql(dbType, entityMeta, pkStrategy, isNullFunction, sequence, isAssignPK, realTable);
    } else {
        int columnSize = entityMeta.getFieldsArray().length;
        FieldMeta fieldMeta;
        StringBuilder sql = new StringBuilder(columnSize * 30 + 100);
        String columnName;
        sql.append("merge into ");
        sql.append(realTable);
        sql.append(" ta ");
        sql.append(" using (select ");
        int fieldType;
        for (int i = 0; i < columnSize; i++) {
            fieldMeta = entityMeta.getFieldMeta(entityMeta.getFieldsArray()[i]);
            fieldType = fieldMeta.getType();
            columnName = fieldMeta.getColumnName();
            if (i > 0)
                sql.append(",");
            if (fieldType == java.sql.Types.VARCHAR)
                sql.append("cast(? as varchar(" + fieldMeta.getLength() + "))");
            else if (fieldType == java.sql.Types.CHAR)
                sql.append("cast(? as char(" + fieldMeta.getLength() + "))");
            else if (fieldType == java.sql.Types.DATE)
                sql.append("cast(? as date)");
            else if (fieldType == java.sql.Types.NUMERIC)
                sql.append("cast(? as numeric)");
            else if (fieldType == java.sql.Types.DECIMAL)
                sql.append("cast(? as decimal)");
            else if (fieldType == java.sql.Types.INTEGER || fieldType == java.sql.Types.BIGINT || fieldType == java.sql.Types.TINYINT)
                sql.append("cast(? as integer)");
            else if (fieldType == java.sql.Types.TIMESTAMP)
                sql.append("cast(? as timestamp)");
            else if (fieldType == java.sql.Types.DOUBLE)
                sql.append("cast(? as double)");
            else if (fieldType == java.sql.Types.FLOAT)
                sql.append("cast(? as float)");
            else if (fieldType == java.sql.Types.TIME)
                sql.append("cast(? as time)");
            else if (fieldType == java.sql.Types.CLOB)
                sql.append("cast(? as clob(" + fieldMeta.getLength() + "))");
            else if (fieldType == java.sql.Types.BOOLEAN)
                sql.append("cast(? as boolean)");
            else if (fieldType == java.sql.Types.BINARY)
                sql.append("cast(? as BINARY LARGE OBJECT(" + fieldMeta.getLength() + "))");
            else if (fieldType == java.sql.Types.BLOB)
                sql.append("cast(? as blob(" + fieldMeta.getLength() + "))");
            else
                sql.append("?");
            sql.append(" as ");
            sql.append(columnName);
        }
        if (StringUtil.isNotBlank(fromTable))
            sql.append(" from ").append(fromTable);
        sql.append(") tv on (");
        StringBuilder idColumns = new StringBuilder();
        // 组织on部分的主键条件判断
        for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
            columnName = entityMeta.getColumnName(entityMeta.getIdArray()[i]);
            if (i > 0) {
                sql.append(" and ");
                idColumns.append(",");
            }
            sql.append(" ta.").append(columnName).append("=tv.").append(columnName);
            idColumns.append("ta.").append(columnName);
        }
        sql.append(" ) ");
        // 排除id的其他字段信息
        StringBuilder insertRejIdCols = new StringBuilder();
        StringBuilder insertRejIdColValues = new StringBuilder();
        // 是否全部字段都是ID主键(复合主键),匹配上则无需进行更新,只需将未匹配上的插入即可
        boolean allIds = (entityMeta.getRejectIdFieldArray() == null);
        if (!allIds) {
            // update 操作
            sql.append(" when matched then update set ");
            int rejectIdColumnSize = entityMeta.getRejectIdFieldArray().length;
            // 需要被强制修改的字段
            HashMap<String, String> fupc = new HashMap<String, String>();
            if (forceUpdateFields != null) {
                for (String field : forceUpdateFields) fupc.put(entityMeta.getColumnName(field), "1");
            }
            // update 只针对非主键字段进行修改
            for (int i = 0; i < rejectIdColumnSize; i++) {
                fieldMeta = entityMeta.getFieldMeta(entityMeta.getRejectIdFieldArray()[i]);
                columnName = fieldMeta.getColumnName();
                if (i > 0) {
                    sql.append(",");
                    insertRejIdCols.append(",");
                    insertRejIdColValues.append(",");
                }
                sql.append(" ta.").append(columnName).append("=");
                // 强制修改
                if (fupc.containsKey(columnName)) {
                    sql.append("tv.").append(columnName);
                } else {
                    sql.append(isNullFunction);
                    sql.append("(tv.").append(columnName);
                    sql.append(",ta.").append(columnName);
                    sql.append(")");
                }
                insertRejIdCols.append(columnName);
                // 存在默认值
                if (StringUtil.isNotBlank(fieldMeta.getDefaultValue())) {
                    insertRejIdColValues.append(isNullFunction);
                    insertRejIdColValues.append("(tv.").append(columnName).append(",");
                    DialectUtils.processDefaultValue(insertRejIdColValues, dbType, fieldMeta.getType(), fieldMeta.getDefaultValue());
                    insertRejIdColValues.append(")");
                } else
                    insertRejIdColValues.append("tv.").append(columnName);
            }
        }
        // 主键未匹配上则进行插入操作
        sql.append(" when not matched then insert (");
        String idsColumnStr = idColumns.toString();
        // 不考虑只有一个字段且还是主键的情况
        if (allIds) {
            sql.append(idsColumnStr.replaceAll("ta.", ""));
            sql.append(") values (");
            sql.append(idsColumnStr.replaceAll("ta.", "tv."));
        } else {
            sql.append(insertRejIdCols.toString());
            // sequence方式主键
            if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                sql.append(",");
                sql.append(columnName);
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                if (isAssignPK) {
                    sql.append(isNullFunction);
                    sql.append("(tv.").append(columnName).append(",");
                    sql.append(sequence).append(") ");
                } else {
                    sql.append(sequence);
                }
            } else if (pkStrategy.equals(PKStrategy.IDENTITY)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                if (isAssignPK) {
                    sql.append(",");
                    sql.append(columnName);
                }
                sql.append(") values (");
                // identity 模式insert无需写插入该字段语句
                sql.append(insertRejIdColValues);
                if (isAssignPK) {
                    sql.append(",").append("tv.").append(columnName);
                }
            } else {
                sql.append(",");
                sql.append(idsColumnStr.replaceAll("ta.", ""));
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                sql.append(idsColumnStr.replaceAll("ta.", "tv."));
            }
        }
        sql.append(")");
        return sql.toString();
    }
}
Also used : HashMap(java.util.HashMap) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta)

Example 2 with FieldMeta

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

the class DialectUtils method getSaveIgnoreExistSql.

/**
 * @todo 处理加工对象基于db2、oracle、informix、sybase数据库的saveIgnoreExist
 * @param dbType
 * @param entityMeta
 * @param pkStrategy
 * @param fromTable
 * @param isNullFunction
 * @param sequence
 * @param isAssignPK
 * @param tableName
 * @return
 */
public static String getSaveIgnoreExistSql(Integer dbType, EntityMeta entityMeta, PKStrategy pkStrategy, String fromTable, String isNullFunction, String sequence, boolean isAssignPK, String tableName) {
    // 在无主键的情况下产生insert sql语句
    String realTable = (tableName == null) ? entityMeta.getSchemaTable() : tableName;
    if (entityMeta.getIdArray() == null) {
        return generateInsertSql(dbType, entityMeta, pkStrategy, isNullFunction, sequence, isAssignPK, realTable);
    } else {
        boolean isSupportNUL = StringUtil.isBlank(isNullFunction) ? false : true;
        int columnSize = entityMeta.getFieldsArray().length;
        StringBuilder sql = new StringBuilder(columnSize * 30 + 100);
        String columnName;
        sql.append("merge into ");
        sql.append(realTable);
        sql.append(" ta ");
        sql.append(" using (select ");
        for (int i = 0; i < columnSize; i++) {
            columnName = entityMeta.getColumnName(entityMeta.getFieldsArray()[i]);
            if (i > 0)
                sql.append(",");
            sql.append("? as ");
            sql.append(columnName);
        }
        if (StringUtil.isNotBlank(fromTable))
            sql.append(" from ").append(fromTable);
        sql.append(") tv on (");
        StringBuilder idColumns = new StringBuilder();
        // 组织on部分的主键条件判断
        for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
            columnName = entityMeta.getColumnName(entityMeta.getIdArray()[i]);
            if (i > 0) {
                sql.append(" and ");
                idColumns.append(",");
            }
            sql.append(" ta.").append(columnName).append("=tv.").append(columnName);
            idColumns.append("ta.").append(columnName);
        }
        sql.append(" ) ");
        // 排除id的其他字段信息
        StringBuilder insertRejIdCols = new StringBuilder();
        StringBuilder insertRejIdColValues = new StringBuilder();
        // 是否全部是ID,匹配上则无需进行更新,只需将未匹配上的插入即可
        boolean allIds = (entityMeta.getRejectIdFieldArray() == null);
        if (!allIds) {
            int rejectIdColumnSize = entityMeta.getRejectIdFieldArray().length;
            FieldMeta fieldMeta;
            // update 只针对非主键字段进行修改
            for (int i = 0; i < rejectIdColumnSize; i++) {
                fieldMeta = entityMeta.getFieldMeta(entityMeta.getRejectIdFieldArray()[i]);
                columnName = fieldMeta.getColumnName();
                if (i > 0) {
                    insertRejIdCols.append(",");
                    insertRejIdColValues.append(",");
                }
                insertRejIdCols.append(columnName);
                // 存在默认值
                if (isSupportNUL && StringUtil.isNotBlank(fieldMeta.getDefaultValue())) {
                    insertRejIdColValues.append(isNullFunction);
                    insertRejIdColValues.append("(tv.").append(columnName).append(",");
                    processDefaultValue(insertRejIdColValues, dbType, fieldMeta.getType(), fieldMeta.getDefaultValue());
                    insertRejIdColValues.append(")");
                } else
                    insertRejIdColValues.append("tv.").append(columnName);
            }
        }
        // 主键未匹配上则进行插入操作
        sql.append(" when not matched then insert (");
        String idsColumnStr = idColumns.toString();
        // 不考虑只有一个字段且还是主键的情况
        if (allIds) {
            sql.append(idsColumnStr.replaceAll("ta.", ""));
            sql.append(") values (");
            sql.append(idsColumnStr.replaceAll("ta.", "tv."));
        } else {
            sql.append(insertRejIdCols.toString());
            // sequence方式主键
            if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                sql.append(",");
                sql.append(columnName);
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                if (isAssignPK && isSupportNUL) {
                    sql.append(isNullFunction);
                    sql.append("(tv.").append(columnName).append(",");
                    sql.append(sequence).append(") ");
                } else {
                    sql.append(sequence);
                }
            } else if (pkStrategy.equals(PKStrategy.IDENTITY)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                if (isAssignPK) {
                    sql.append(",");
                    sql.append(columnName);
                }
                sql.append(") values (");
                // identity 模式insert无需写插入该字段语句
                sql.append(insertRejIdColValues);
                if (isAssignPK) {
                    sql.append(",").append("tv.").append(columnName);
                }
            } else {
                sql.append(",");
                sql.append(idsColumnStr.replaceAll("ta.", ""));
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                sql.append(idsColumnStr.replaceAll("ta.", "tv."));
            }
        }
        sql.append(")");
        return sql.toString();
    }
}
Also used : FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta)

Example 3 with FieldMeta

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

the class SqlServerDialectUtils method getSaveOrUpdateSql.

/**
 * @todo sqlserver 相对特殊不支持timestamp类型的插入,所以单独提供sql生成功能
 * @param entityMeta
 * @param pkStrategy
 * @param forceUpdateFields
 * @param fromTable
 * @param isNullFunction
 * @param sequence
 * @param isAssignPK
 * @return
 */
public static String getSaveOrUpdateSql(Integer dbType, EntityMeta entityMeta, PKStrategy pkStrategy, String[] forceUpdateFields, String fromTable, String isNullFunction, String sequence, boolean isAssignPK) {
    // 在无主键的情况下产生insert sql语句
    if (entityMeta.getIdArray() == null) {
        return generateInsertSql(dbType, entityMeta, pkStrategy, isNullFunction, sequence, isAssignPK);
    } else {
        int columnSize = entityMeta.getFieldsArray().length;
        StringBuilder sql = new StringBuilder(columnSize * 30 + 100);
        String columnName;
        sql.append("merge into ");
        sql.append(entityMeta.getSchemaTable());
        sql.append(" ta ");
        sql.append(" using (select ");
        for (int i = 0; i < columnSize; i++) {
            columnName = entityMeta.getColumnName(entityMeta.getFieldsArray()[i]);
            if (i > 0)
                sql.append(",");
            sql.append("? as ");
            sql.append(columnName);
        }
        if (StringUtil.isNotBlank(fromTable))
            sql.append(" from ").append(fromTable);
        sql.append(") tv on (");
        StringBuilder idColumns = new StringBuilder();
        // 组织on部分的主键条件判断
        for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
            columnName = entityMeta.getColumnName(entityMeta.getIdArray()[i]);
            if (i > 0) {
                sql.append(" and ");
                idColumns.append(",");
            }
            sql.append(" ta.").append(columnName).append("=tv.").append(columnName);
            idColumns.append("ta.").append(columnName);
        }
        sql.append(" ) ");
        // 排除id的其他字段信息
        StringBuilder insertRejIdCols = new StringBuilder();
        StringBuilder insertRejIdColValues = new StringBuilder();
        // 是否全部是ID,匹配上则无需进行更新,只需将未匹配上的插入即可
        boolean allIds = (entityMeta.getRejectIdFieldArray() == null);
        if (!allIds) {
            // update 操作
            sql.append(" when matched then update set ");
            int rejectIdColumnSize = entityMeta.getRejectIdFieldArray().length;
            // 需要被强制修改的字段
            HashMap<String, String> fupc = new HashMap<String, String>();
            if (forceUpdateFields != null) {
                for (String field : forceUpdateFields) fupc.put(entityMeta.getColumnName(field), "1");
            }
            FieldMeta fieldMeta;
            // update 只针对非主键字段进行修改
            boolean isStart = true;
            for (int i = 0; i < rejectIdColumnSize; i++) {
                fieldMeta = entityMeta.getFieldMeta(entityMeta.getRejectIdFieldArray()[i]);
                columnName = fieldMeta.getColumnName();
                if (i > 0) {
                    sql.append(",");
                }
                sql.append(" ta.").append(columnName).append("=");
                // 强制修改
                if (fupc.containsKey(columnName)) {
                    sql.append("tv.").append(columnName);
                } else {
                    sql.append(isNullFunction);
                    sql.append("(tv.").append(columnName);
                    sql.append(",ta.").append(columnName);
                    sql.append(")");
                }
                // sqlserver不支持timestamp类型的数据进行插入赋值
                if (fieldMeta.getType() != java.sql.Types.TIMESTAMP) {
                    if (!isStart) {
                        insertRejIdCols.append(",");
                        insertRejIdColValues.append(",");
                    }
                    insertRejIdCols.append(columnName);
                    isStart = false;
                    // 存在默认值
                    if (StringUtil.isNotBlank(fieldMeta.getDefaultValue())) {
                        insertRejIdColValues.append(isNullFunction);
                        insertRejIdColValues.append("(tv.").append(columnName).append(",");
                        DialectUtils.processDefaultValue(insertRejIdColValues, dbType, fieldMeta.getType(), fieldMeta.getDefaultValue());
                        insertRejIdColValues.append(")");
                    } else
                        insertRejIdColValues.append("tv.").append(columnName);
                }
            }
        }
        // 主键未匹配上则进行插入操作
        sql.append(" when not matched then insert (");
        String idsColumnStr = idColumns.toString();
        // 不考虑只有一个字段且还是主键的情况
        if (allIds) {
            sql.append(idsColumnStr.replaceAll("ta.", ""));
            sql.append(") values (");
            sql.append(idsColumnStr.replaceAll("ta.", "tv."));
        } else {
            sql.append(insertRejIdCols.toString());
            // sequence方式主键
            if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                sql.append(",");
                sql.append(columnName);
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                if (isAssignPK) {
                    sql.append(isNullFunction);
                    sql.append("(tv.").append(columnName).append(",");
                    sql.append(sequence).append(") ");
                } else {
                    sql.append(sequence);
                }
            } else if (pkStrategy.equals(PKStrategy.IDENTITY)) {
                columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
                if (isAssignPK) {
                    sql.append(",");
                    sql.append(columnName);
                }
                sql.append(") values (");
                // identity 模式insert无需写插入该字段语句
                sql.append(insertRejIdColValues);
                if (isAssignPK) {
                    sql.append(",").append("tv.").append(columnName);
                }
            } else {
                sql.append(",");
                sql.append(idsColumnStr.replaceAll("ta.", ""));
                sql.append(") values (");
                sql.append(insertRejIdColValues).append(",");
                sql.append(idsColumnStr.replaceAll("ta.", "tv."));
            }
        }
        sql.append(")");
        return sql.toString();
    }
}
Also used : HashMap(java.util.HashMap) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta)

Example 4 with FieldMeta

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

the class DialectUtils method generateInsertSql.

/**
 * @todo 产生对象对应的insert sql语句
 * @param dbType
 * @param entityMeta
 * @param pkStrategy
 * @param isNullFunction
 * @param sequence
 * @param isAssignPK
 * @param tableName
 * @return
 */
public static String generateInsertSql(Integer dbType, EntityMeta entityMeta, PKStrategy pkStrategy, String isNullFunction, String sequence, boolean isAssignPK, String tableName) {
    int columnSize = entityMeta.getFieldsArray().length;
    StringBuilder sql = new StringBuilder(columnSize * 20 + 30);
    StringBuilder values = new StringBuilder(columnSize * 2 - 1);
    sql.append("insert into ");
    sql.append(tableName == null ? entityMeta.getSchemaTable() : tableName);
    sql.append(" (");
    FieldMeta fieldMeta;
    String field;
    boolean isStart = true;
    boolean isSupportNUL = StringUtil.isBlank(isNullFunction) ? false : true;
    for (int i = 0; i < columnSize; i++) {
        field = entityMeta.getFieldsArray()[i];
        fieldMeta = entityMeta.getFieldMeta(field);
        if (fieldMeta.isPK()) {
            // identity主键策略,且支持主键手工赋值
            if (pkStrategy.equals(PKStrategy.IDENTITY)) {
                // 目前只有mysql支持
                if (isAssignPK) {
                    if (!isStart) {
                        sql.append(",");
                        values.append(",");
                    }
                    sql.append(fieldMeta.getColumnName());
                    values.append("?");
                    isStart = false;
                }
            } else // sequence 策略,oracle12c之后的identity机制统一转化为sequence模式
            if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
                if (!isStart) {
                    sql.append(",");
                    values.append(",");
                }
                sql.append(fieldMeta.getColumnName());
                if (isAssignPK && isSupportNUL) {
                    values.append(isNullFunction);
                    values.append("(?,").append(sequence).append(")");
                } else
                    values.append(sequence);
                isStart = false;
            } else {
                if (!isStart) {
                    sql.append(",");
                    values.append(",");
                }
                sql.append(fieldMeta.getColumnName());
                values.append("?");
                isStart = false;
            }
        } else {
            if (!isStart) {
                sql.append(",");
                values.append(",");
            }
            sql.append(fieldMeta.getColumnName());
            if (isSupportNUL && StringUtil.isNotBlank(fieldMeta.getDefaultValue())) {
                values.append(isNullFunction);
                values.append("(?,");
                processDefaultValue(values, dbType, fieldMeta.getType(), fieldMeta.getDefaultValue());
                values.append(")");
            } else
                values.append("?");
            isStart = false;
        }
    }
    sql.append(") values (");
    sql.append(values);
    sql.append(")");
    return sql.toString();
}
Also used : FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta)

Example 5 with FieldMeta

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

the class EntityManager method parseFieldMeta.

/**
 * @todo 解析对象属性跟数据库表字段的信息
 * @param entityMeta
 * @param field
 * @param hasAbstractVO
 * @param fieldNameMap
 * @param rejectIdFieldList
 * @param loadNamedWhereSql
 * @param loadArgWhereSql
 * @throws Exception
 */
private void parseFieldMeta(SqlToyContext sqlToyContext, EntityMeta entityMeta, Field field, boolean hasAbstractVO, HashMap fieldNameMap, List<String> rejectIdFieldList, StringBuilder loadNamedWhereSql, StringBuilder loadArgWhereSql) throws Exception {
    Column column = field.getAnnotation(Column.class);
    if (column == null)
        return;
    // 字段的详细配置信息,字段名称,字段对应数据库表字段,字段默认值,字段类型
    FieldMeta fieldMeta = new FieldMeta(field.getName(), column.name(), StringUtil.isNotBlank(column.defaultValue()) ? column.defaultValue() : null, column.type(), column.nullable(), Long.valueOf(column.length()).intValue(), column.precision(), column.scale());
    // 字段是否自增
    fieldMeta.setAutoIncrement(column.autoIncrement());
    entityMeta.addFieldMeta(fieldMeta);
    // 对于数据库字段处理,仅仅适用于abstractVO中定义的属性
    if (!hasAbstractVO || (hasAbstractVO && fieldNameMap.get(field.getName()).equals("1"))) {
        // 判断字段是否为主键
        Id id = field.getAnnotation(Id.class);
        if (id != null) {
            fieldMeta.setPK(true);
            // 主键生成策略
            entityMeta.setIdStrategy(PKStrategy.getPKStrategy(id.strategy().toLowerCase()));
            entityMeta.setSequence(id.sequence());
            String idGenerator = id.generator();
            if (StringUtil.isNotBlank(idGenerator)) {
                processIdGenerator(sqlToyContext, entityMeta, idGenerator);
                entityMeta.setIdGenerator(idGenerators.get(idGenerator));
            }
            if (loadNamedWhereSql.length() > 1) {
                loadNamedWhereSql.append(" and ");
                loadArgWhereSql.append(" and ");
            } else {
                loadNamedWhereSql.append(" where ");
                loadArgWhereSql.append(" where ");
            }
            loadNamedWhereSql.append(column.name()).append("=:").append(field.getName());
            loadArgWhereSql.append(column.name()).append("=?");
        } else {
            rejectIdFieldList.add(field.getName());
        }
        // 业务主键策略配置解析
        BusinessId bizId = field.getAnnotation(BusinessId.class);
        if (bizId != null) {
            String bizGenerator = bizId.generator();
            entityMeta.setBizIdLength(bizId.length());
            entityMeta.setBizIdSignature(bizId.signature());
            // 生成业务主键关联的字段(主键值生成需要其他字段的值进行组合,入交易业务ID组合交易类别码等)
            if (bizId.relatedColumn() != null)
                entityMeta.setBizIdRelatedColumn(bizId.relatedColumn());
            processIdGenerator(sqlToyContext, entityMeta, bizGenerator);
            // 如果是业务主键跟ID重叠,则ID以业务主键策略生成
            if (id != null) {
                entityMeta.setIdGenerator(idGenerators.get(bizGenerator));
            } else {
                entityMeta.setBusinessIdField(field.getName());
                entityMeta.setBusinessIdGenerator(idGenerators.get(bizGenerator));
            }
        }
    }
}
Also used : Column(org.sagacity.sqltoy.config.annotation.Column) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) Id(org.sagacity.sqltoy.config.annotation.Id) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId) BusinessId(org.sagacity.sqltoy.config.annotation.BusinessId)

Aggregations

FieldMeta (org.sagacity.sqltoy.config.model.FieldMeta)29 HashSet (java.util.HashSet)8 HashMap (java.util.HashMap)7 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)6 Test (org.junit.jupiter.api.Test)3 BusinessId (org.sagacity.sqltoy.config.annotation.BusinessId)2 Column (org.sagacity.sqltoy.config.annotation.Column)2 Id (org.sagacity.sqltoy.config.annotation.Id)2 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)2 Serializable (java.io.Serializable)1 Type (java.lang.reflect.Type)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)1 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)1 PartitionKey (org.sagacity.sqltoy.config.annotation.PartitionKey)1