use of com.lidroid.xutils.db.table.Table 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.db.table.Table in project xUtils by wyouflf.
the class DbUtils method saveOrUpdateWithoutTransaction.
//***************************** private operations with out transaction *****************************
private void saveOrUpdateWithoutTransaction(Object entity) throws DbException {
Table table = Table.get(this, entity.getClass());
Id id = table.id;
if (id.isAutoIncrement()) {
if (id.getColumnValue(entity) != null) {
execNonQuery(SqlInfoBuilder.buildUpdateSqlInfo(this, entity));
} else {
saveBindingIdWithoutTransaction(entity);
}
} else {
execNonQuery(SqlInfoBuilder.buildReplaceSqlInfo(this, entity));
}
}
use of com.lidroid.xutils.db.table.Table 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.db.table.Table in project xUtils by wyouflf.
the class ForeignLazyLoader method getFirstFromDb.
public T getFirstFromDb() throws DbException {
T entity = null;
Table table = foreignColumn.getTable();
if (table != null) {
entity = table.db.findFirst(Selector.from(foreignColumn.getForeignEntityType()).where(foreignColumn.getForeignColumnName(), "=", columnValue));
}
return entity;
}
Aggregations