Search in sources :

Example 11 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbManagerImpl method getLastAutoIncrementId.

//************************************************ tools ***********************************
private long getLastAutoIncrementId(String tableName) throws DbException {
    long id = -1;
    Cursor cursor = execQuery("SELECT seq FROM sqlite_sequence WHERE name='" + tableName + "' LIMIT 1");
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                id = cursor.getLong(0);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return id;
}
Also used : Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Example 12 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbModelSelector method findAll.

public List<DbModel> findAll() throws DbException {
    TableEntity<?> table = selector.getTable();
    if (!table.tableIsExist())
        return null;
    List<DbModel> result = null;
    Cursor cursor = table.getDb().execQuery(this.toString());
    if (cursor != null) {
        try {
            result = new ArrayList<DbModel>();
            while (cursor.moveToNext()) {
                DbModel entity = CursorUtils.getDbModel(cursor);
                result.add(entity);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return result;
}
Also used : DbModel(org.xutils.db.table.DbModel) Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Example 13 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbModelSelector method findFirst.

public DbModel findFirst() throws DbException {
    TableEntity<?> table = selector.getTable();
    if (!table.tableIsExist())
        return null;
    this.limit(1);
    Cursor cursor = table.getDb().execQuery(this.toString());
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                return CursorUtils.getDbModel(cursor);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Example 14 with DbException

use of org.xutils.ex.DbException 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 15 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class Selector method findFirst.

public T findFirst() throws DbException {
    if (!table.tableIsExist())
        return null;
    this.limit(1);
    Cursor cursor = table.getDb().execQuery(this.toString());
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                return CursorUtils.getEntity(table, cursor);
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Aggregations

DbException (org.xutils.ex.DbException)16 Cursor (android.database.Cursor)8 ColumnEntity (org.xutils.db.table.ColumnEntity)3 ArrayList (java.util.ArrayList)2 ProcessLock (org.xutils.common.util.ProcessLock)2 DbModel (org.xutils.db.table.DbModel)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1 DbManager (org.xutils.DbManager)1 KeyValue (org.xutils.common.util.KeyValue)1 FileLockedException (org.xutils.ex.FileLockedException)1 Parent (org.xutils.sample.db.Parent)1 Event (org.xutils.view.annotation.Event)1