Search in sources :

Example 6 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbUtils method findById.

@SuppressWarnings("unchecked")
public <T> T findById(Class<T> entityType, Object idValue) throws DbException {
    if (!tableIsExist(entityType))
        return null;
    Table table = Table.get(this, entityType);
    Selector selector = Selector.from(entityType).where(table.id.getColumnName(), "=", idValue);
    String sql = selector.limit(1).toString();
    long seq = CursorUtils.FindCacheSequence.getSeq();
    findTempCache.setSeq(seq);
    Object obj = findTempCache.get(sql);
    if (obj != null) {
        return (T) obj;
    }
    Cursor cursor = execQuery(sql);
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                T entity = (T) CursorUtils.getEntity(this, cursor, entityType, seq);
                findTempCache.put(sql, entity);
                return entity;
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return null;
}
Also used : Table(com.lidroid.xutils.db.table.Table) Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 7 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbUtils method tableIsExist.

public boolean tableIsExist(Class<?> entityType) throws DbException {
    Table table = Table.get(this, entityType);
    if (table.isCheckedDatabase()) {
        return true;
    }
    Cursor cursor = execQuery("SELECT COUNT(*) AS c FROM sqlite_master WHERE type='table' AND name='" + table.tableName + "'");
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    table.setCheckedDatabase(true);
                    return true;
                }
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return false;
}
Also used : Table(com.lidroid.xutils.db.table.Table) Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 8 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbUtils method getInstance.

private static synchronized DbUtils getInstance(DaoConfig daoConfig) {
    DbUtils dao = daoMap.get(daoConfig.getDbName());
    if (dao == null) {
        dao = new DbUtils(daoConfig);
        daoMap.put(daoConfig.getDbName(), dao);
    } else {
        dao.daoConfig = daoConfig;
    }
    // update the database if needed
    SQLiteDatabase database = dao.database;
    int oldVersion = database.getVersion();
    int newVersion = daoConfig.getDbVersion();
    if (oldVersion != newVersion) {
        if (oldVersion != 0) {
            DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener();
            if (upgradeListener != null) {
                upgradeListener.onUpgrade(dao, oldVersion, newVersion);
            } else {
                try {
                    dao.dropDb();
                } catch (DbException e) {
                    LogUtils.e(e.getMessage(), e);
                }
            }
        }
        database.setVersion(newVersion);
    }
    return dao;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DbException(com.lidroid.xutils.exception.DbException)

Example 9 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbUtils method findAll.

@SuppressWarnings("unchecked")
public <T> List<T> findAll(Selector selector) throws DbException {
    if (!tableIsExist(selector.getEntityType()))
        return null;
    String sql = selector.toString();
    long seq = CursorUtils.FindCacheSequence.getSeq();
    findTempCache.setSeq(seq);
    Object obj = findTempCache.get(sql);
    if (obj != null) {
        return (List<T>) obj;
    }
    List<T> result = new ArrayList<T>();
    Cursor cursor = execQuery(sql);
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                T entity = (T) CursorUtils.getEntity(this, cursor, selector.getEntityType(), seq);
                result.add(entity);
            }
            findTempCache.put(sql, result);
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Example 10 with DbException

use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.

the class DbUtils method findDbModelAll.

public List<DbModel> findDbModelAll(SqlInfo sqlInfo) throws DbException {
    List<DbModel> dbModelList = new ArrayList<DbModel>();
    Cursor cursor = execQuery(sqlInfo);
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                dbModelList.add(CursorUtils.getDbModel(cursor));
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtils.closeQuietly(cursor);
        }
    }
    return dbModelList;
}
Also used : ArrayList(java.util.ArrayList) DbModel(com.lidroid.xutils.db.table.DbModel) Cursor(android.database.Cursor) DbException(com.lidroid.xutils.exception.DbException)

Aggregations

DbException (com.lidroid.xutils.exception.DbException)12 Cursor (android.database.Cursor)8 DbModel (com.lidroid.xutils.db.table.DbModel)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Table (com.lidroid.xutils.db.table.Table)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 DbUtils (com.lidroid.xutils.DbUtils)1 FinderLazyLoader (com.lidroid.xutils.db.sqlite.FinderLazyLoader)1 ForeignLazyLoader (com.lidroid.xutils.db.sqlite.ForeignLazyLoader)1 Child (com.lidroid.xutils.sample.entities.Child)1 Parent (com.lidroid.xutils.sample.entities.Parent)1 OnClick (com.lidroid.xutils.view.annotation.event.OnClick)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1