Back to project page androidata.
The source code is released under:
Apache License
If you think the Android project androidata listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.stanidesis.androidata.table; //from w w w .j av a 2 s . c o m import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import android.util.Log; import com.stanidesis.androidata.AndroiData; import com.stanidesis.androidata.AndroiDataUpgradeHelper; import com.stanidesis.androidata.Utils; import com.stanidesis.androidata.column.Column; import com.stanidesis.androidata.column.RowIDColumn; import com.stanidesis.androidata.model.IModelBuilder; import com.stanidesis.androidata.model.IModelUpdateBuilder; import java.util.ArrayList; import java.util.List; /** * Created by Stanley Idesis on 8/25/14. */ public abstract class BaseTable implements ITable { /** * Row Id column */ public static final RowIDColumn COLUMN_ROW_ID = new RowIDColumn(); @Override public RowIDColumn getRowIDColumn() { return COLUMN_ROW_ID; } @Override public void onUpgrade(AndroiDataUpgradeHelper upgradeHelper, int oldVersion, int newVersion) { // Nothing } @Override public Cursor getRow(SQLiteDatabase readOnlyDB, long rowId) { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "SQL Query: '" + getRowIDColumn().getName() + " = ?' with args '" + String.valueOf(rowId) + "'"); } return readOnlyDB.query(true, // Distinct getName(), // Table name null, // All values getRowIDColumn().getName() + " = ?", // Selection new String[] {String.valueOf(rowId)}, // Arguments null, // Group By null, // Having null, // Order By String.valueOf(1)); // Limit to 1 } @Override public Cursor selectAll(SQLiteDatabase readOnlyDB) { String rawSelectAllQuery = "select * from ?"; if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Raw SQL Query: '" + rawSelectAllQuery + "'"); } return readOnlyDB.rawQuery(rawSelectAllQuery, new String[] {getName()}); } @Override public Cursor selectAll(SQLiteDatabase readOnlyDB, long[] ignoreIds) { StringBuilder selectionBuilder = new StringBuilder(); String[] args = new String[ignoreIds.length]; for (int i = 0; i < ignoreIds.length; i++) { long id = ignoreIds[i]; args[i] = String.valueOf(id); selectionBuilder.append(getRowIDColumn().getName()).append(" != ?"); if (i < ignoreIds.length - 1) { selectionBuilder.append(" AND "); } } if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "SQL Query: '" + selectionBuilder.toString() + "' with args '" + Utils.arrayToString(args) + "'"); } return readOnlyDB.query(true, getName(), null, selectionBuilder.toString(), args, null, null, null, null); } @Override public Integer count(SQLiteDatabase readOnlyDB) { String countQuery = "SELECT count(*) FROM " + getName(); if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Raw SQL Query: '" + countQuery + "'"); } int result = 0; Cursor cursor = readOnlyDB.rawQuery(countQuery, null); if (cursor != null && cursor.moveToFirst()) { result = cursor.getInt(0); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return result; } @Override public long insert(SQLiteDatabase writableDB, IModelBuilder builder) { return insert(writableDB, builder.getValues()); } @Override public long insert(SQLiteDatabase writableDB, ContentValues values) { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Inserting Model"); } return writableDB.insert(getName(), null, values); } @Override public boolean update(SQLiteDatabase writableDB, IModelUpdateBuilder builder) { update(writableDB, builder.getValues(), builder.getRowId()); return true; } @Override public void update(SQLiteDatabase writableDB, ContentValues values, long rowId) { if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "Updating Model with row: '" + rowId + "'"); } writableDB.update(getName(), values, getRowIDColumn().getName() + " = ?", new String[]{String.valueOf(rowId)}); } @Override public void update(SQLiteDatabase writableDB, ContentValues values, long[] idsToUpdate) { List<Long> idsToUpdateList = new ArrayList<Long>(); for (long id : idsToUpdate) { idsToUpdateList.add(id); } writableDB.update(getName(), values, getRowIDColumn().getName() + " IN (?)", new String[]{TextUtils.join(", ", idsToUpdateList)}); } @Override public void update(SQLiteDatabase writableDB, ContentValues values) { writableDB.update(getName(), values, getRowIDColumn().getName() + " > -1", null); } @Override public void delete(SQLiteDatabase writableDB, long[] idsToDelete) { List<Long> idsToDeleteList = new ArrayList<Long>(); for (long id : idsToDelete) { idsToDeleteList.add(id); } String commaSeparatedIDs = TextUtils.join(", ", idsToDeleteList); writableDB.execSQL(String.format("DELETE FROM %s WHERE %s IN (%s);", getName(), getRowIDColumn().getName(), commaSeparatedIDs)); } @Override public Cursor find(SQLiteDatabase readOnlyDB, Column[] columns, Object[] equalTo, int limit) { if (columns.length != equalTo.length) { throw new IllegalArgumentException(String.format("Number of columns (%d) does not match number of arguments (%d)", columns.length, equalTo.length)); } StringBuilder sqlStatementBuilder = new StringBuilder(); String [] args = new String[equalTo.length]; for (int i = 0; i < columns.length; i++) { args[i] = String.valueOf(equalTo[i]); sqlStatementBuilder.append(columns[i].getName()).append(" = ?"); if (i < columns.length - 1) { sqlStatementBuilder.append(" AND "); } } if (AndroiData.get().isDebug()) { Log.v(AndroiData.TAG, "SQL Query: '" + sqlStatementBuilder.toString() + "' with args '" + Utils.arrayToString(args) + "'"); } return readOnlyDB.query(true, getName(), null, sqlStatementBuilder.toString(), args, null, null, null, String.valueOf(limit)); } }