Search in sources :

Example 11 with KeyValue

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;
}
Also used : ColumnEntity(org.xutils.db.table.ColumnEntity) KeyValue(org.xutils.common.util.KeyValue) ArrayList(java.util.ArrayList)

Example 12 with KeyValue

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;
}
Also used : KeyValue(org.xutils.common.util.KeyValue) ColumnEntity(org.xutils.db.table.ColumnEntity) DbException(org.xutils.ex.DbException)

Example 13 with KeyValue

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;
}
Also used : KeyValue(org.xutils.common.util.KeyValue)

Example 14 with KeyValue

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;
}
Also used : KeyValue(org.xutils.common.util.KeyValue)

Example 15 with KeyValue

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);
    }
}
Also used : KeyValue(org.xutils.common.util.KeyValue) Parent(org.xutils.sample.db.Parent) Calendar(java.util.Calendar) DbManager(org.xutils.DbManager) Date(java.util.Date) DbModel(org.xutils.db.table.DbModel) Child(org.xutils.sample.db.Child) Event(org.xutils.view.annotation.Event)

Aggregations

KeyValue (org.xutils.common.util.KeyValue)15 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 ColumnEntity (org.xutils.db.table.ColumnEntity)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 DbManager (org.xutils.DbManager)1 ColumnConverter (org.xutils.db.converter.ColumnConverter)1 DbModel (org.xutils.db.table.DbModel)1 DbException (org.xutils.ex.DbException)1 BodyItemWrapper (org.xutils.http.body.BodyItemWrapper)1