List of usage examples for android.database.sqlite SQLiteDatabase inTransaction
public boolean inTransaction()
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Delete the specified rowId in this tableId. Deletion respects sync * semantics. If the row is in the SyncState.new_row state, then the row and * its associated file attachments are immediately deleted. Otherwise, the row * is placed into the SyncState.deleted state and will be retained until the * device can delete the record on the server. * <p>//from w w w. ja va 2s. c o m * If you need to immediately delete a record that would otherwise sync to the * server, call updateRowETagAndSyncState(...) to set the row to * SyncState.new_row, and then call this method and it will be immediately * deleted (in this case, unless the record on the server was already deleted, * it will remain and not be deleted during any subsequent synchronizations). * * @param db * @param appName * @param tableId * @param rowId */ public void deleteDataInExistingDBTableWithId(SQLiteDatabase db, String appName, String tableId, String rowId) { SyncState syncState = getSyncState(db, appName, tableId, rowId); boolean dbWithinTransaction = db.inTransaction(); if (syncState == SyncState.new_row) { String[] whereArgs = { rowId }; String whereClause = DataTableColumns.ID + " = ?"; try { if (!dbWithinTransaction) { db.beginTransaction(); } db.delete(tableId, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } File instanceFolder = new File(ODKFileUtils.getInstanceFolder(appName, tableId, rowId)); try { FileUtils.deleteDirectory(instanceFolder); } catch (IOException e) { // TODO Auto-generated catch block WebLogger.getLogger(appName).e(t, "Unable to delete this directory: " + instanceFolder.getAbsolutePath()); WebLogger.getLogger(appName).printStackTrace(e); } } else if (syncState == SyncState.synced || syncState == SyncState.changed) { String[] whereArgs = { rowId }; ContentValues values = new ContentValues(); values.put(DataTableColumns.SYNC_STATE, SyncState.deleted.name()); values.put(DataTableColumns.SAVEPOINT_TIMESTAMP, TableConstants.nanoSecondsFromMillis(System.currentTimeMillis())); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(tableId, values, DataTableColumns.ID + " = ?", whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Drop the given tableId and remove all the files (both configuration and * data attachments) associated with that table. * /* w ww . j a v a2 s. c om*/ * @param db * @param appName * @param tableId */ public void deleteDBTableAndAllData(SQLiteDatabase db, final String appName, final String tableId) { SyncETagsUtils seu = new SyncETagsUtils(); boolean dbWithinTransaction = db.inTransaction(); try { String whereClause = TableDefinitionsColumns.TABLE_ID + " = ?"; String[] whereArgs = { tableId }; if (!dbWithinTransaction) { db.beginTransaction(); } // Drop the table used for the formId db.execSQL("DROP TABLE IF EXISTS \"" + tableId + "\";"); // Delete the server sync ETags associated with this table seu.deleteAllSyncETags(db, tableId); // Delete the table definition for the tableId int count = db.delete(DatabaseConstants.TABLE_DEFS_TABLE_NAME, whereClause, whereArgs); // Delete the column definitions for this tableId db.delete(DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, whereClause, whereArgs); // Delete the uploads for the tableId String uploadWhereClause = InstanceColumns.DATA_TABLE_TABLE_ID + " = ?"; db.delete(DatabaseConstants.UPLOADS_TABLE_NAME, uploadWhereClause, whereArgs); // Delete the values from the 4 key value stores db.delete(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, whereClause, whereArgs); db.delete(DatabaseConstants.KEY_VALULE_STORE_SYNC_TABLE_NAME, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } // And delete the files from the SDCard... String tableDir = ODKFileUtils.getTablesFolder(appName, tableId); try { FileUtils.deleteDirectory(new File(tableDir)); } catch (IOException e1) { e1.printStackTrace(); throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1); } String assetsCsvDir = ODKFileUtils.getAssetsFolder(appName) + "/csv"; try { Collection<File> files = FileUtils.listFiles(new File(assetsCsvDir), new IOFileFilter() { @Override public boolean accept(File file) { String[] parts = file.getName().split("\\."); return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv") && (parts.length == 2 || parts.length == 3 || (parts.length == 4 && parts[parts.length - 2].equals("properties")))); } @Override public boolean accept(File dir, String name) { String[] parts = name.split("\\."); return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv") && (parts.length == 2 || parts.length == 3 || (parts.length == 4 && parts[parts.length - 2].equals("properties")))); } }, new IOFileFilter() { // don't traverse into directories @Override public boolean accept(File arg0) { return false; } // don't traverse into directories @Override public boolean accept(File arg0, String arg1) { return false; } }); FileUtils.deleteDirectory(new File(tableDir)); for (File f : files) { FileUtils.deleteQuietly(f); } } catch (IOException e1) { e1.printStackTrace(); throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1); } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Update the timestamp of the last entirely-successful synchronization * attempt of this table./* w w w .j a va 2 s. c o m*/ * * @param db * @param tableId */ public void updateDBTableLastSyncTime(SQLiteDatabase db, String tableId) { if (tableId == null || tableId.length() <= 0) { throw new IllegalArgumentException(t + ": application name and table name must be specified"); } ContentValues cvTableDef = new ContentValues(); cvTableDef.put(TableDefinitionsColumns.LAST_SYNC_TIME, TableConstants.nanoSecondsFromMillis(System.currentTimeMillis())); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(DatabaseConstants.TABLE_DEFS_TABLE_NAME, cvTableDef, TableDefinitionsColumns.TABLE_ID + "=?", new String[] { tableId }); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Change the conflictType for the given row from null (not in conflict) to * the specified one.// w w w . ja v a 2s .c om * * @param db * @param tableId * @param rowId * @param conflictType * expected to be one of ConflictType.LOCAL_DELETED_OLD_VALUES (0) or * ConflictType.LOCAL_UPDATED_UPDATED_VALUES (1) */ public void placeRowIntoConflict(SQLiteDatabase db, String tableId, String rowId, int conflictType) { String whereClause = String.format("%s = ? AND %s IS NULL", DataTableColumns.ID, DataTableColumns.CONFLICT_TYPE); String[] whereArgs = { rowId }; ContentValues cv = new ContentValues(); cv.put(DataTableColumns.SYNC_STATE, SyncState.in_conflict.name()); cv.put(DataTableColumns.CONFLICT_TYPE, conflictType); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(tableId, cv, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Update the schema and data-modification ETags of a given tableId. * //w w w . java 2s . co m * @param db * @param tableId * @param schemaETag * @param lastDataETag */ public void updateDBTableETags(SQLiteDatabase db, String tableId, String schemaETag, String lastDataETag) { if (tableId == null || tableId.length() <= 0) { throw new IllegalArgumentException(t + ": application name and table name must be specified"); } ContentValues cvTableDef = new ContentValues(); cvTableDef.put(TableDefinitionsColumns.SCHEMA_ETAG, schemaETag); cvTableDef.put(TableDefinitionsColumns.LAST_DATA_ETAG, lastDataETag); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(DatabaseConstants.TABLE_DEFS_TABLE_NAME, cvTableDef, TableDefinitionsColumns.TABLE_ID + "=?", new String[] { tableId }); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Changes the conflictType for the given row from the specified one to null * and set the sync state of this row to the indicated value. In general, you * should first update the local conflict record with its new values, then * call deleteServerConflictRowWithId(...) and then call this method. * //from ww w. j a v a 2s . c om * @param db * @param tableId * @param rowId * @param syncState * @param conflictType */ public void restoreRowFromConflict(SQLiteDatabase db, String tableId, String rowId, SyncState syncState, int conflictType) { String whereClause = String.format("%s = ? AND %s = ?", DataTableColumns.ID, DataTableColumns.CONFLICT_TYPE); String[] whereArgs = { rowId, String.valueOf(conflictType) }; ContentValues cv = new ContentValues(); cv.putNull(DataTableColumns.CONFLICT_TYPE); cv.put(DataTableColumns.SYNC_STATE, syncState.name()); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(tableId, cv, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Insert or update a single table-level metadata KVS entry. * //from w w w.ja v a 2s .com * @param db * @param entry */ public void replaceDBTableMetadata(SQLiteDatabase db, KeyValueStoreEntry entry) { ContentValues values = new ContentValues(); values.put(KeyValueStoreColumns.TABLE_ID, entry.tableId); values.put(KeyValueStoreColumns.PARTITION, entry.partition); values.put(KeyValueStoreColumns.ASPECT, entry.aspect); values.put(KeyValueStoreColumns.VALUE_TYPE, entry.type); values.put(KeyValueStoreColumns.VALUE, entry.value); values.put(KeyValueStoreColumns.KEY, entry.key); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.replace(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, values); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Deletes the server conflict row (if any) for this rowId in this tableId. * /*w w w . ja v a2 s .c om*/ * @param db * @param tableId * @param rowId */ public void deleteServerConflictRowWithId(SQLiteDatabase db, String tableId, String rowId) { // delete the old server-values in_conflict row if it exists String whereClause = String.format("%s = ? AND %s = ? AND %s IN " + "( ?, ? )", DataTableColumns.ID, DataTableColumns.SYNC_STATE, DataTableColumns.CONFLICT_TYPE); String[] whereArgs = { rowId, SyncState.in_conflict.name(), String.valueOf(ConflictType.SERVER_DELETED_OLD_VALUES), String.valueOf(ConflictType.SERVER_UPDATED_UPDATED_VALUES) }; boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.delete(tableId, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Update the ETag and SyncState of a given rowId. There should be exactly one * record for this rowId in thed database (i.e., no conflicts or checkpoints). * //from w ww . j a va2 s . com * @param db * @param tableId * @param rowId * @param rowETag * @param state */ public void updateRowETagAndSyncState(SQLiteDatabase db, String tableId, String rowId, String rowETag, SyncState state) { String whereClause = DataTableColumns.ID + " = ?"; String[] whereArgs = { rowId }; ContentValues cvDataTableVal = new ContentValues(); String sel = "SELECT * FROM " + tableId + " WHERE " + whereClause; String[] selArgs = whereArgs; Cursor cursor = rawQuery(db, sel, selArgs); // There must be only one row in the db if (cursor.getCount() != 1) { throw new IllegalArgumentException( t + ": row id " + rowId + " does not have exactly 1 row in table " + tableId); } cvDataTableVal.put(DataTableColumns.ROW_ETAG, rowETag); cvDataTableVal.put(DataTableColumns.SYNC_STATE, state.name()); boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.update(tableId, cvDataTableVal, whereClause, whereArgs); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * The deletion filter includes all non-null arguments. If all arguments * (except the db) are null, then all properties are removed. * /*from ww w .j av a2s . c om*/ * @param db * @param tableId * @param partition * @param aspect * @param key */ public void deleteDBTableMetadata(SQLiteDatabase db, String tableId, String partition, String aspect, String key) { StringBuilder b = new StringBuilder(); ArrayList<String> selArgs = new ArrayList<String>(); if (tableId != null) { b.append(KeyValueStoreColumns.TABLE_ID).append("=?"); selArgs.add(tableId); } if (partition != null) { if (b.length() != 0) { b.append(" AND "); } b.append(KeyValueStoreColumns.PARTITION).append("=?"); selArgs.add(partition); } if (aspect != null) { if (b.length() != 0) { b.append(" AND "); } b.append(KeyValueStoreColumns.ASPECT).append("=?"); selArgs.add(aspect); } if (key != null) { if (b.length() != 0) { b.append(" AND "); } b.append(KeyValueStoreColumns.KEY).append("=?"); selArgs.add(key); } boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.delete(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, b.toString(), selArgs.toArray(new String[selArgs.size()])); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }