use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.
the class DbUtils method dropDb.
public void dropDb() throws DbException {
Cursor cursor = execQuery("SELECT name FROM sqlite_master WHERE type='table' AND name<>'sqlite_sequence'");
if (cursor != null) {
try {
while (cursor.moveToNext()) {
try {
String tableName = cursor.getString(0);
execNonQuery("DROP TABLE " + tableName);
Table.remove(this, tableName);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
}
} catch (Throwable e) {
throw new DbException(e);
} finally {
IOUtils.closeQuietly(cursor);
}
}
}
use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.
the class DbUtils method getLastAutoIncrementId.
//************************************************ tools ***********************************
private long getLastAutoIncrementId(String tableName) throws DbException {
long id = -1;
Cursor cursor = execQuery("SELECT seq FROM sqlite_sequence WHERE name='" + tableName + "'");
if (cursor != null) {
try {
if (cursor.moveToNext()) {
id = cursor.getLong(0);
}
} catch (Throwable e) {
throw new DbException(e);
} finally {
IOUtils.closeQuietly(cursor);
}
}
return id;
}
use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.
the class DbUtils method findFirst.
@SuppressWarnings("unchecked")
public <T> T findFirst(Selector selector) throws DbException {
if (!tableIsExist(selector.getEntityType()))
return null;
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, selector.getEntityType(), 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 Finder method setValue2Entity.
@Override
public void setValue2Entity(Object entity, Cursor cursor, int index) {
Object value = null;
Class<?> columnType = columnField.getType();
Object finderValue = TableUtils.getColumnOrId(entity.getClass(), this.valueColumnName).getColumnValue(entity);
if (columnType.equals(FinderLazyLoader.class)) {
value = new FinderLazyLoader(this, finderValue);
} else if (columnType.equals(List.class)) {
try {
value = new FinderLazyLoader(this, finderValue).getAllFromDb();
} catch (DbException e) {
LogUtils.e(e.getMessage(), e);
}
} else {
try {
value = new FinderLazyLoader(this, finderValue).getFirstFromDb();
} catch (DbException e) {
LogUtils.e(e.getMessage(), e);
}
}
if (setMethod != null) {
try {
setMethod.invoke(entity, value);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
} else {
try {
this.columnField.setAccessible(true);
this.columnField.set(entity, value);
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
}
}
use of com.lidroid.xutils.exception.DbException in project xUtils by wyouflf.
the class DbUtils method findDbModelAll.
public List<DbModel> findDbModelAll(DbModelSelector selector) throws DbException {
if (!tableIsExist(selector.getEntityType()))
return null;
List<DbModel> dbModelList = new ArrayList<DbModel>();
Cursor cursor = execQuery(selector.toString());
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