/** * Inserts values into a table that has an unique id as identifier. * * @param table The affected table. * @param values The values to be inserted/ updated. * @param mId The identifier of the affected row. * @return The number of rows affected on update, the rowId on insert, -1 on error. */ public int doInsertOrUpdate(String table, ContentValues values, Where where) { int result; open(); Cursor oldVersion = get(table, where, null); if (oldVersion.moveToNext() && values.size() != 0) { String whereClause = null; if (where != null) { whereClause = where.toString().replace(" WHERE ", ""); } result = mDb.update(table, values, whereClause, null); } else { String nullColumnHack = null; if (values.size() == 0) { // if no fields are defined on a model instance the nullColumnHack // needs to be utilized in order to insert an empty row. nullColumnHack = Model.PK; } result = (int) mDb.insertOrThrow(table, nullColumnHack, values); } oldVersion.close(); close(); return result; }
/** * Delete one object or a set of objects from a specific table. * * @param table Query table. * @param where {@link Where} clause to find the object. * @return Number of affected rows. */ public int delete(String table, Where where) { open(); int affectedRows = mDb.delete(table, where.toString().replace(" WHERE ", ""), null); close(); return affectedRows; }
/** * Query the database for a specific item. * * @param table Query table. * @param where {@link Where} clause to apply. * @param limit {@link Limit} clause to apply. * @return {@link Cursor} that represents the query result. */ private Cursor get(String table, Where where, Limit limit) { String whereClause = null; if (where != null) { whereClause = where.toString().replace(" WHERE ", ""); Log.i("ANDORM", "where: " + whereClause); } String limitClause = null; if (limit != null) { limitClause = limit.toString().replace(" LIMIT ", ""); } Cursor result = mDb.query(table, null, whereClause, null, null, null, null, limitClause); return result; }