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