String executeForString(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for string", new StatementOperation<String>() { @Override public String call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnString(0); } }); }
long executeForLong(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for long", new StatementOperation<Long>() { @Override public Long call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnLong(0); } }); }
private static Number nativeGetNumber(long windowPtr, int row, int column) { Value value = WINDOW_DATA.get(windowPtr).value(row, column); switch (value.type) { case Cursor.FIELD_TYPE_NULL: case SQLiteConstants.SQLITE_NULL: return 0; case Cursor.FIELD_TYPE_INTEGER: case Cursor.FIELD_TYPE_FLOAT: return (Number) value.value; case Cursor.FIELD_TYPE_STRING: { try { return Double.parseDouble((String) value.value); } catch (NumberFormatException e) { return 0; } } case Cursor.FIELD_TYPE_BLOB: throw new android.database.sqlite.SQLiteException("could not convert "+value); default: throw new android.database.sqlite.SQLiteException("unknown type: "+value.type); } }
@Implementation(minSdk = LOLLIPOP) protected static String nativeGetString(long windowPtr, int row, int column) { Value val = WINDOW_DATA.get(windowPtr).value(row, column); if (val.type == Cursor.FIELD_TYPE_BLOB) { throw new android.database.sqlite.SQLiteException("Getting string when column is blob. Row " + row + ", col " + column); } Object value = val.value; return value == null ? null : String.valueOf(value); }
@Implementation(minSdk = LOLLIPOP) protected static byte[] nativeGetBlob(long windowPtr, int row, int column) { Value value = WINDOW_DATA.get(windowPtr).value(row, column); switch (value.type) { case Cursor.FIELD_TYPE_NULL: return null; case Cursor.FIELD_TYPE_BLOB: // This matches Android's behavior, which does not match the SQLite spec byte[] blob = (byte[])value.value; return blob == null ? new byte[]{} : blob; case Cursor.FIELD_TYPE_STRING: return ((String)value.value).getBytes(UTF_8); default: throw new android.database.sqlite.SQLiteException("Getting blob when column is non-blob. Row " + row + ", col " + column); } }
private static RuntimeException getSqliteException(final String message, final int baseErrorCode) { // Mapping is from throw_sqlite3_exception in android_database_SQLiteCommon.cpp switch (baseErrorCode) { case SQLiteConstants.SQLITE_ABORT: return new SQLiteAbortException(message); case SQLiteConstants.SQLITE_PERM: return new SQLiteAccessPermException(message); case SQLiteConstants.SQLITE_RANGE: return new SQLiteBindOrColumnIndexOutOfRangeException(message); case SQLiteConstants.SQLITE_TOOBIG: return new SQLiteBlobTooBigException(message); case SQLiteConstants.SQLITE_CANTOPEN: return new SQLiteCantOpenDatabaseException(message); case SQLiteConstants.SQLITE_CONSTRAINT: return new SQLiteConstraintException(message); case SQLiteConstants.SQLITE_NOTADB: // fall through case SQLiteConstants.SQLITE_CORRUPT: return new SQLiteDatabaseCorruptException(message); case SQLiteConstants.SQLITE_BUSY: return new SQLiteDatabaseLockedException(message); case SQLiteConstants.SQLITE_MISMATCH: return new SQLiteDatatypeMismatchException(message); case SQLiteConstants.SQLITE_IOERR: return new SQLiteDiskIOException(message); case SQLiteConstants.SQLITE_DONE: return new SQLiteDoneException(message); case SQLiteConstants.SQLITE_FULL: return new SQLiteFullException(message); case SQLiteConstants.SQLITE_MISUSE: return new SQLiteMisuseException(message); case SQLiteConstants.SQLITE_NOMEM: return new SQLiteOutOfMemoryException(message); case SQLiteConstants.SQLITE_READONLY: return new SQLiteReadOnlyDatabaseException(message); case SQLiteConstants.SQLITE_LOCKED: return new SQLiteTableLockedException(message); case SQLiteConstants.SQLITE_INTERRUPT: return new OperationCanceledException(message); default: return new android.database.sqlite.SQLiteException(message + ", base error code: " + baseErrorCode); } } }
@Test public void shouldCatchSQLiteExceptions() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); when(resolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())) .thenThrow(new SQLiteException()); mockEmptyQuery(); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); }
@Test public void testFailingFindAllPinned() throws Exception { OfflineStore offlineStore = mock(OfflineStore.class); Parse.setLocalDatastore(offlineStore); when(offlineStore.findFromPinAsync(eq(EventuallyPin.PIN_NAME), any(ParseQuery.State.class), any(ParseUser.class))) .thenReturn(Task.forError(new SQLiteException())); ParsePlugins plugins = mock(ParsePlugins.class); ParsePlugins.set(plugins); when(plugins.restClient()).thenReturn(ParseHttpClient.createClient(null)); thrown.expect(SQLiteException.class); ParseTaskUtils.wait(EventuallyPin.findAllPinned()); } }
private void throwIfStatementForbidden(PreparedStatement statement) { if (mOnlyAllowReadOnlyOperations && !statement.mReadOnly) { throw new SQLiteException("Cannot execute this statement because it " + "might modify the database but the connection is read-only."); } }
@Override public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { throw new SQLiteException("Can't downgrade database from version " + oldVersion + " to " + newVersion); }
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { throw new SQLiteException("Can't downgrade database from version " + oldVersion + " to " + newVersion); }
@Override public void onDowngrade(SQLiteDatabase db, WellTableManager helper, int oldVersion, int newVersion) { throw new SQLiteException(mContext.getString(R.string.downgrade, oldVersion, newVersion)); } }
long executeForLong(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for long", new StatementOperation<Long>() { @Override public Long call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnLong(0); } }); }
String executeForString(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for string", new StatementOperation<String>() { @Override public String call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnString(0); } }); }
String executeForString(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for string", new StatementOperation<String>() { @Override public String call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnString(0); } }); }
long executeForLong(final long connectionPtr, final long statementPtr) { return executeStatementOperation(connectionPtr, statementPtr, "execute for long", new StatementOperation<Long>() { @Override public Long call(final SQLiteStatement statement) throws Exception { if (!statement.step()) { throw new SQLiteException(SQLiteConstants.SQLITE_DONE, "No rows returned from query"); } return statement.columnLong(0); } }); }
@Implementation(minSdk = LOLLIPOP) public static String nativeGetString(long windowPtr, int row, int column) { Value val = WINDOW_DATA.get(windowPtr).value(row, column); if (val.type == Cursor.FIELD_TYPE_BLOB) { throw new android.database.sqlite.SQLiteException("Getting string when column is blob. Row " + row + ", col " + column); } Object value = val.value; return value == null ? null : String.valueOf(value); }
@Implementation(minSdk = LOLLIPOP) protected static String nativeGetString(long windowPtr, int row, int column) { Value val = WINDOW_DATA.get(windowPtr).value(row, column); if (val.type == Cursor.FIELD_TYPE_BLOB) { throw new android.database.sqlite.SQLiteException("Getting string when column is blob. Row " + row + ", col " + column); } Object value = val.value; return value == null ? null : String.valueOf(value); }
@Implementation(minSdk = LOLLIPOP) public static String nativeGetString(long windowPtr, int row, int column) { Value val = WINDOW_DATA.get(windowPtr).value(row, column); if (val.type == Cursor.FIELD_TYPE_BLOB) { throw new android.database.sqlite.SQLiteException("Getting string when column is blob. Row " + row + ", col " + column); } Object value = val.value; return value == null ? null : String.valueOf(value); }
@Implementation public static String nativeGetString(long windowPtr, int row, int column) { Value val = WINDOW_DATA.get(windowPtr).value(row, column); if (val.type == Cursor.FIELD_TYPE_BLOB) { throw new android.database.sqlite.SQLiteException("Getting string when column is blob. Row " + row + ", col " + column); } Object value = val.value; return value == null ? null : String.valueOf(value); }