/** * Construct a query with raw SQL * * @param properties * @param selection * @param selectionArgs * @return */ public TodorooCursor<TYPE> rawQuery( String selection, String[] selectionArgs, Property<?>... properties) { String[] fields = new String[properties.length]; for (int i = 0; i < properties.length; i++) fields[i] = properties[i].name; return new TodorooCursor<TYPE>( database .getDatabase() .query(table.name, fields, selection, selectionArgs, null, null, null), properties); }
/** * Creates the given item. * * @param database * @param table table name * @param item item model * @return returns true on success. */ public boolean createNew(TYPE item) { item.clearValue(AbstractModel.ID_PROPERTY); long newRow = database.insert(table.name, AbstractModel.ID_PROPERTY.name, item.getMergedValues()); boolean result = newRow >= 0; if (result) { item.setId(newRow); onModelUpdated(item); item.markSaved(); } return result; }
/** * Saves the given item. Will not create a new item! * * @param database * @param table table name * @param item item model * @return returns true on success. */ public boolean saveExisting(TYPE item) { ContentValues values = item.getSetValues(); if (values == null || values.size() == 0) // nothing changed return true; boolean result = database.update( table.name, values, AbstractModel.ID_PROPERTY.eq(item.getId()).toString(), null) > 0; if (result) { onModelUpdated(item); item.markSaved(); } return result; }
/** * Connect to a PostgreSQL database. {@inheritDoc} * * @see com.continuent.tungsten.replicator.database.AbstractDatabase#connect(boolean) */ public void connect(boolean binlog) throws SQLException { // Use superclass method to avoid missing things like loading the // driver class. super.connect(binlog); }
/** * Construct a query with SQL DSL objects * * @param query * @return */ public TodorooCursor<TYPE> query(Query query) { query.from(table); if (debug) Log.i("SQL-" + modelClass.getSimpleName(), query.toString()); // $NON-NLS-1$ Cursor cursor = database.rawQuery(query.toString(), null); return new TodorooCursor<TYPE>(cursor, query.getFields()); }
/** * Sets database accessed by this DAO. Used for dependency-injected initialization by child * classes and unit tests * * @param database */ public void setDatabase(AbstractDatabase database) { if (database == this.database) return; this.database = database; table = database.getTable(modelClass); }
/** * Updates multiple rows of the database based on model set values * * @param item item model * @param criterion * @return returns true on success. */ public int updateMultiple(ContentValues values, Criterion criterion) { if (values.size() == 0) // nothing changed return 0; return database.update(table.name, values, criterion.toString(), null); }
/** * Update all matching a clause to have the values set on template object. * * <p>Example (updates "joe" => "bob" in metadata value1): {code} Metadata item = new Metadata(); * item.setValue(Metadata.VALUE1, "bob"); update(item, Metadata.VALUE1.eq("joe")); {code} * * @param where sql criteria * @param template set fields on this object in order to set them in the db. * @return # of updated items */ public int update(Criterion where, TYPE template) { return database.update(table.name, template.getSetValues(), where.toString(), null); }
/** * Delete all matching a clause * * @param where predicate for deletion * @return # of deleted items */ public int deleteWhere(Criterion where) { return database.delete(table.name, where.toString(), null); }
/** * Delete the given id * * @param database * @param id * @return true if delete was successful */ public boolean delete(long id) { return database.delete(table.name, AbstractModel.ID_PROPERTY.eq(id).toString(), null) > 0; }