List of usage examples for android.database.sqlite SQLiteDatabase inTransaction
public boolean inTransaction()
From source file:com.adguard.android.db.DbHelper.java
@Override public void onCreate(SQLiteDatabase db) { LOG.info("DbHelper.onCreate()"); try {/*ww w.j a v a 2s . co m*/ db.beginTransaction(); createTables(db); fillFilters(db); fillFiltersLocalization(db); enableDefaultFilters(db); db.setTransactionSuccessful(); } finally { if (db.inTransaction()) { db.endTransaction(); } } }
From source file:net.zionsoft.obadiah.model.Bible.java
private boolean removeTranslation(String translationShortName) { SQLiteDatabase db = null; try {// w ww .j av a 2 s . c o m db = mDatabaseHelper.openDatabase(); if (db == null) { Analytics.trackException("Failed to open database."); return false; } db.beginTransaction(); TranslationHelper.removeTranslation(db, translationShortName); db.setTransactionSuccessful(); return true; } finally { if (db != null) { if (db.inTransaction()) { db.endTransaction(); } mDatabaseHelper.closeDatabase(); } } }
From source file:com.hichinaschool.flashcards.libanki.Collection.java
public synchronized void close(boolean save) { if (mDb != null) { if (!mConf.optBoolean("newBury", false)) { boolean mod = mDb.getMod(); mSched.unburyCards();//from w w w . j a v a 2s. c o m mDb.setMod(mod); } try { SQLiteDatabase db = getDb().getDatabase(); if (save) { db.beginTransaction(); try { save(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } else { if (db.inTransaction()) { db.endTransaction(); } lock(); } } catch (RuntimeException e) { AnkiDroidApp.saveExceptionReportFile(e, "closeDB"); } AnkiDatabaseManager.closeDatabase(mPath); mDb = null; mMedia.close(); // Log.i(AnkiDroidApp.TAG, "Collection closed"); } }
From source file:com.ichi2.libanki.Collection.java
public synchronized void close(boolean save) { if (mDb != null) { if (!mConf.optBoolean("newBury", false)) { boolean mod = mDb.getMod(); mSched.unburyCards();/* ww w . ja v a2 s. c o m*/ mDb.setMod(mod); } try { SQLiteDatabase db = getDb().getDatabase(); if (save) { db.beginTransaction(); try { save(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } else { if (db.inTransaction()) { db.endTransaction(); } lock(); } } catch (RuntimeException e) { AnkiDroidApp.sendExceptionReport(e, "closeDB"); } AnkiDatabaseManager.closeDatabase(mPath); mDb = null; mMedia.close(); _closeLog(); Timber.i("Collection closed"); } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Update all rows for the given rowId to SavepointType 'INCOMPLETE' and * remove all but the most recent row. When used with a rowId that has * checkpoints, this updates to the most recent checkpoint and removes any * earlier checkpoints, incomplete or complete savepoints. Otherwise, it has * the general effect of resetting the rowId to an INCOMPLETE state. * // ww w . j ava 2s.com * @param db * @param tableId * @param rowId */ public void saveAsIncompleteMostRecentCheckpointDataInDBTableWithId(SQLiteDatabase db, String tableId, String rowId) { boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } db.execSQL( "UPDATE \"" + tableId + "\" SET " + DataTableColumns.SAVEPOINT_TYPE + "= ? WHERE " + DataTableColumns.ID + "=?", new String[] { SavepointTypeManipulator.incomplete(), rowId }); db.delete(tableId, DataTableColumns.ID + "=? AND " + DataTableColumns.SAVEPOINT_TIMESTAMP + " NOT IN (SELECT MAX(" + DataTableColumns.SAVEPOINT_TIMESTAMP + ") FROM \"" + tableId + "\" WHERE " + DataTableColumns.ID + "=?)", new String[] { rowId, rowId }); if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
private void rawDeleteDataInDBTable(SQLiteDatabase db, String tableId, String whereClause, String[] whereArgs) { boolean dbWithinTransaction = db.inTransaction(); try {/*from ww w . j a v a 2 s . c o m*/ 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
/** * Clean up the KVS row data types. This simplifies the migration process by * enforcing the proper data types regardless of what the values are in the * imported CSV files.//from w w w. j a v a 2s .com * * @param db * @param tableId */ public void enforceTypesDBTableMetadata(SQLiteDatabase db, String tableId) { boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } StringBuilder b = new StringBuilder(); b.setLength(0); //@formatter:off b.append("UPDATE \"").append(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME).append("\" SET ") .append(KeyValueStoreColumns.VALUE_TYPE).append("=? WHERE ") .append(KeyValueStoreColumns.PARTITION).append("=? AND ").append(KeyValueStoreColumns.KEY) .append("=?"); //@formatter:on String sql = b.toString(); String[] fields = new String[3]; // for columns fields[0] = ElementDataType.array.name(); fields[1] = KeyValueStoreConstants.PARTITION_COLUMN; fields[2] = KeyValueStoreConstants.COLUMN_DISPLAY_CHOICES_LIST; db.execSQL(sql, fields); fields[0] = ElementDataType.string.name(); fields[1] = KeyValueStoreConstants.PARTITION_COLUMN; fields[2] = KeyValueStoreConstants.COLUMN_DISPLAY_FORMAT; db.execSQL(sql, fields); fields[0] = ElementDataType.object.name(); fields[1] = KeyValueStoreConstants.PARTITION_COLUMN; fields[2] = KeyValueStoreConstants.COLUMN_DISPLAY_NAME; db.execSQL(sql, fields); fields[0] = ElementDataType.bool.name(); fields[1] = KeyValueStoreConstants.PARTITION_COLUMN; fields[2] = KeyValueStoreConstants.COLUMN_DISPLAY_VISIBLE; db.execSQL(sql, fields); fields[0] = ElementDataType.array.name(); fields[1] = KeyValueStoreConstants.PARTITION_COLUMN; fields[2] = KeyValueStoreConstants.COLUMN_JOINS; db.execSQL(sql, fields); // and for the table... fields[0] = ElementDataType.array.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_COL_ORDER; db.execSQL(sql, fields); fields[0] = ElementDataType.object.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_DISPLAY_NAME; db.execSQL(sql, fields); fields[0] = ElementDataType.array.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_GROUP_BY_COLS; db.execSQL(sql, fields); fields[0] = ElementDataType.string.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_INDEX_COL; db.execSQL(sql, fields); fields[0] = ElementDataType.object.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_SORT_COL; db.execSQL(sql, fields); fields[0] = ElementDataType.object.name(); fields[1] = KeyValueStoreConstants.PARTITION_TABLE; fields[2] = KeyValueStoreConstants.TABLE_SORT_ORDER; db.execSQL(sql, fields); // TODO: color rule groups if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * If the tableId is not recorded in the TableDefinition metadata table, then * create the tableId with the indicated columns. This will synthesize * reasonable metadata KVS entries for table. * /*from www . java 2 s . com*/ * If the tableId is present, then this is a no-op. * * @param db * @param appName * @param tableId * @param columns * @return the ArrayList<ColumnDefinition> of the user columns in the table. */ public ArrayList<ColumnDefinition> createOrOpenDBTableWithColumns(SQLiteDatabase db, String appName, String tableId, List<Column> columns) { boolean dbWithinTransaction = db.inTransaction(); boolean success = false; ArrayList<ColumnDefinition> orderedDefs = ColumnDefinition.buildColumnDefinitions(appName, tableId, columns); try { if (!dbWithinTransaction) { db.beginTransaction(); } if (!hasTableId(db, tableId)) { createDBTableWithColumns(db, appName, tableId, orderedDefs); } if (!dbWithinTransaction) { db.setTransactionSuccessful(); } success = true; return orderedDefs; } finally { if (!dbWithinTransaction) { db.endTransaction(); } if (success == false) { // Get the names of the columns StringBuilder colNames = new StringBuilder(); if (columns != null) { for (Column column : columns) { colNames.append(" ").append(column.getElementKey()).append(","); } if (colNames != null && colNames.length() > 0) { colNames.deleteCharAt(colNames.length() - 1); WebLogger.getLogger(appName).e(t, "createOrOpenDBTableWithColumns: Error while adding table " + tableId + " with columns:" + colNames.toString()); } } else { WebLogger.getLogger(appName).e(t, "createOrOpenDBTableWithColumns: Error while adding table " + tableId + " with columns: null"); } } } }
From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java
/** * Insert or update a list of table-level metadata KVS entries. If clear is * true, then delete the existing set of values for this tableId before * inserting the new values.//from ww w .ja v a 2 s . c om * * @param db * @param tableId * @param metadata * a List<KeyValueStoreEntry> * @param clear * if true then delete the existing set of values for this tableId * before inserting the new ones. */ public void replaceDBTableMetadata(SQLiteDatabase db, String tableId, List<KeyValueStoreEntry> metadata, boolean clear) { boolean dbWithinTransaction = db.inTransaction(); try { if (!dbWithinTransaction) { db.beginTransaction(); } if (clear) { db.delete(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, KeyValueStoreColumns.TABLE_ID + "=?", new String[] { tableId }); } for (KeyValueStoreEntry e : metadata) { ContentValues values = new ContentValues(); if (!tableId.equals(e.tableId)) { throw new IllegalArgumentException( "updateDBTableMetadata: expected all kvs entries to share the same tableId"); } if (e.value == null || e.value.trim().length() == 0) { deleteDBTableMetadata(db, e.tableId, e.partition, e.aspect, e.key); } else { values.put(KeyValueStoreColumns.TABLE_ID, e.tableId); values.put(KeyValueStoreColumns.PARTITION, e.partition); values.put(KeyValueStoreColumns.ASPECT, e.aspect); values.put(KeyValueStoreColumns.KEY, e.key); values.put(KeyValueStoreColumns.VALUE_TYPE, e.type); values.put(KeyValueStoreColumns.VALUE, e.value); db.replace(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, values); } } if (!dbWithinTransaction) { db.setTransactionSuccessful(); } } finally { if (!dbWithinTransaction) { db.endTransaction(); } } }
From source file:net.zionsoft.obadiah.model.Bible.java
private void downloadTranslation(String url, String translationShortName, OnDownloadProgressListener onProgress) throws Exception { ZipInputStream zis = null;//w w w . j a v a 2 s. com SQLiteDatabase db = null; try { db = mDatabaseHelper.openDatabase(); if (db == null) { Analytics.trackException("Failed to open database."); throw new Exception("Failed to open database for writing"); } db.beginTransaction(); TranslationHelper.createTranslationTable(db, translationShortName); zis = new ZipInputStream(NetworkHelper.getStream(url)); final byte buffer[] = new byte[2048]; final ByteArrayOutputStream os = new ByteArrayOutputStream(); int downloaded = 0; int read; ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { os.reset(); while ((read = zis.read(buffer, 0, 2048)) != -1) os.write(buffer, 0, read); final byte[] bytes = os.toByteArray(); String fileName = entry.getName(); fileName = fileName.substring(0, fileName.length() - 5); // removes the trailing ".json" if (fileName.equals("books")) { TranslationHelper.saveBookNames(db, new JSONObject(new String(bytes, "UTF8"))); } else { final String[] parts = fileName.split("-"); final int bookIndex = Integer.parseInt(parts[0]); final int chapterIndex = Integer.parseInt(parts[1]); TranslationHelper.saveVerses(db, translationShortName, bookIndex, chapterIndex, new JSONObject(new String(bytes, "UTF8"))); } onProgress.onProgress(++downloaded / 12); } db.setTransactionSuccessful(); } finally { if (db != null) { if (db.inTransaction()) { db.endTransaction(); } mDatabaseHelper.closeDatabase(); } if (zis != null) { try { zis.close(); } catch (IOException e) { // we can't do much here } } } }