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