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;
}
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;
}
Aggregations