Search in sources :

Example 6 with ColumnEntity

use of org.xutils.db.table.ColumnEntity in project xUtils3 by wyouflf.

the class SqlInfoBuilder method entity2KeyValueList.

public static List<KeyValue> entity2KeyValueList(TableEntity<?> table, Object entity) {
    Collection<ColumnEntity> columns = table.getColumnMap().values();
    List<KeyValue> keyValueList = new ArrayList<KeyValue>(columns.size());
    for (ColumnEntity column : columns) {
        KeyValue kv = column2KeyValue(entity, column);
        if (kv != null) {
            keyValueList.add(kv);
        }
    }
    return keyValueList;
}
Also used : ColumnEntity(org.xutils.db.table.ColumnEntity) KeyValue(org.xutils.common.util.KeyValue) ArrayList(java.util.ArrayList)

Example 7 with ColumnEntity

use of org.xutils.db.table.ColumnEntity in project xUtils3 by wyouflf.

the class SqlInfoBuilder method buildUpdateSqlInfo.

//*********************************************** update sql ***********************************************
public static SqlInfo buildUpdateSqlInfo(TableEntity<?> table, Object entity, String... updateColumnNames) throws DbException {
    List<KeyValue> keyValueList = entity2KeyValueList(table, entity);
    if (keyValueList.size() == 0)
        return null;
    HashSet<String> updateColumnNameSet = null;
    if (updateColumnNames != null && updateColumnNames.length > 0) {
        updateColumnNameSet = new HashSet<String>(updateColumnNames.length);
        Collections.addAll(updateColumnNameSet, updateColumnNames);
    }
    ColumnEntity id = table.getId();
    Object idValue = id.getColumnValue(entity);
    if (idValue == null) {
        throw new DbException("this entity[" + table.getEntityType() + "]'s id value is null");
    }
    SqlInfo result = new SqlInfo();
    StringBuilder builder = new StringBuilder("UPDATE ");
    builder.append("\"").append(table.getName()).append("\"");
    builder.append(" SET ");
    for (KeyValue kv : keyValueList) {
        if (updateColumnNameSet == null || updateColumnNameSet.contains(kv.key)) {
            builder.append("\"").append(kv.key).append("\"").append("=?,");
            result.addBindArg(kv);
        }
    }
    builder.deleteCharAt(builder.length() - 1);
    builder.append(" WHERE ").append(WhereBuilder.b(id.getName(), "=", idValue));
    result.setSql(builder.toString());
    return result;
}
Also used : KeyValue(org.xutils.common.util.KeyValue) ColumnEntity(org.xutils.db.table.ColumnEntity) DbException(org.xutils.ex.DbException)

Aggregations

ColumnEntity (org.xutils.db.table.ColumnEntity)7 DbException (org.xutils.ex.DbException)3 KeyValue (org.xutils.common.util.KeyValue)2 ArrayList (java.util.ArrayList)1