use of org.xutils.common.util.KeyValue 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.common.util.KeyValue 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;
}
use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.
the class SqlInfoBuilder method buildInsertSqlInfo.
// *********************************************** insert sql ***********************************************
public static SqlInfo buildInsertSqlInfo(TableEntity<?> table, Object entity) throws DbException {
List<KeyValue> keyValueList = entity2KeyValueList(table, entity);
if (keyValueList.size() == 0)
return null;
SqlInfo result = new SqlInfo();
String sql = INSERT_SQL_CACHE.get(table);
if (sql == null) {
StringBuilder builder = new StringBuilder();
builder.append("INSERT INTO ");
builder.append("\"").append(table.getName()).append("\"");
builder.append(" (");
for (KeyValue kv : keyValueList) {
builder.append("\"").append(kv.key).append("\"").append(',');
}
builder.deleteCharAt(builder.length() - 1);
builder.append(") VALUES (");
int length = keyValueList.size();
for (int i = 0; i < length; i++) {
builder.append("?,");
}
builder.deleteCharAt(builder.length() - 1);
builder.append(")");
sql = builder.toString();
result.setSql(sql);
result.addBindArgs(keyValueList);
INSERT_SQL_CACHE.put(table, sql);
} else {
result.setSql(sql);
result.addBindArgs(keyValueList);
}
return result;
}
use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.
the class SqlInfoBuilder method buildReplaceSqlInfo.
// *********************************************** replace sql ***********************************************
public static SqlInfo buildReplaceSqlInfo(TableEntity<?> table, Object entity) throws DbException {
List<KeyValue> keyValueList = entity2KeyValueList(table, entity);
if (keyValueList.size() == 0)
return null;
SqlInfo result = new SqlInfo();
String sql = REPLACE_SQL_CACHE.get(table);
if (sql == null) {
StringBuilder builder = new StringBuilder();
builder.append("REPLACE INTO ");
builder.append("\"").append(table.getName()).append("\"");
builder.append(" (");
for (KeyValue kv : keyValueList) {
builder.append("\"").append(kv.key).append("\"").append(',');
}
builder.deleteCharAt(builder.length() - 1);
builder.append(") VALUES (");
int length = keyValueList.size();
for (int i = 0; i < length; i++) {
builder.append("?,");
}
builder.deleteCharAt(builder.length() - 1);
builder.append(")");
sql = builder.toString();
result.setSql(sql);
result.addBindArgs(keyValueList);
REPLACE_SQL_CACHE.put(table, sql);
} else {
result.setSql(sql);
result.addBindArgs(keyValueList);
}
return result;
}
use of org.xutils.common.util.KeyValue in project xUtils3 by wyouflf.
the class DbFragment method onTestDbClick.
@Event(R.id.btn_test_db)
private void onTestDbClick(View view) {
// 一对多: (本示例的代码)
// 自己在多的一方(child)保存另一方的(parentId), 查找的时候用parentId查parent或child.
// 一对一:
// 在任何一边保存另一边的Id并加上唯一属性: @Column(name = "parentId", property = "UNIQUE")
// 多对多:
// 再建一个关联表, 保存两边的id. 查询分两步: 先查关联表得到id, 再查对应表的属性.
String temp = "";
try {
DbManager db = x.getDb(daoConfig);
Child child = new Child();
child.setName("child's name");
Parent test = db.selector(Parent.class).where("id", "in", new int[] { 1, 3, 6 }).findFirst();
// Parent test = db.selector(Parent.class).where("id", "between", new String[]{"1", "5"}).findFirst();
if (test != null) {
child.setParentId(test.getId());
temp += "first parent:" + test + "\n";
tv_db_result.setText(temp);
}
Parent parent = new Parent();
parent.name = "测试" + System.currentTimeMillis();
parent.setAdmin(true);
parent.setEmail("wyouflf@qq.com");
parent.setTime(new Date());
parent.setDate(new java.sql.Date(new Date().getTime()));
// db.save(parent);
db.saveBindingId(parent);
// 保存对象关联数据库生成的id
db.saveBindingId(child);
List<Child> children = db.selector(Child.class).findAll();
temp += "children size:" + children.size() + "\n";
tv_db_result.setText(temp);
if (children.size() > 0) {
temp += "last children:" + children.get(children.size() - 1) + "\n";
tv_db_result.setText(temp);
}
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
calendar.add(Calendar.HOUR, 3);
List<Parent> list = db.selector(Parent.class).where("id", "<", 54).and("time", ">", calendar.getTime()).orderBy("id").limit(10).findAll();
temp += "find parent size:" + list.size() + "\n";
tv_db_result.setText(temp);
if (list.size() > 0) {
temp += "last parent:" + list.get(list.size() - 1) + "\n";
tv_db_result.setText(temp);
}
// test update
parent.name = "hahaha123";
parent.setEmail("wyouflf@gmail.com");
db.update(parent);
db.update(parent, "name", "email");
db.update(Parent.class, WhereBuilder.b("id", "=", 1).and("isAdmin", "=", true), new KeyValue("name", "test_name"), new KeyValue("isAdmin", false));
Parent entity = child.getParent(db);
temp += "find by id:" + entity.toString() + "\n";
tv_db_result.setText(temp);
List<DbModel> dbModels = db.selector(Parent.class).groupBy("name").select("name", "count(name) as count").findAll();
temp += "group by result:" + dbModels.get(0).getDataMap() + "\n";
tv_db_result.setText(temp);
} catch (Throwable e) {
temp += "error :" + e.getMessage() + "\n";
tv_db_result.setText(temp);
}
}
Aggregations