Search in sources :

Example 26 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedPutObject method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null result of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResult executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        final PutResolver<T> putResolver;
        if (explicitPutResolver != null) {
            putResolver = explicitPutResolver;
        } else {
            final SQLiteTypeMapping<T> typeMapping = lowLevel.typeMapping((Class<T>) object.getClass());
            if (typeMapping == null) {
                throw new IllegalStateException("Object does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + ", " + "db was not affected by this operation, please add type mapping for this type");
            }
            putResolver = typeMapping.putResolver();
        }
        final PutResult putResult = putResolver.performPut(storIOSQLite, object);
        if (putResult.wasInserted() || putResult.wasUpdated()) {
            lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
        }
        return putResult;
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 27 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldPassSQLWithArgsToExecSQL.

@Test
public void shouldPassSQLWithArgsToExecSQL() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE users").args("arg1", "arg2").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()), eq(new String[] { "arg1", "arg2" }));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 28 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method notifyAboutChangesShouldNotAcceptNullAsChanges.

@Test
public void notifyAboutChangesShouldNotAcceptNullAsChanges() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    assertThat(lowLevel).isNotNull();
    try {
        //noinspection ConstantConditions
        lowLevel.notifyAboutChanges(null);
        failBecauseExceptionWasNotThrown(NullPointerException.class);
    } catch (NullPointerException expected) {
        assertThat(expected).hasMessage("Changes can not be null");
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 29 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldCloseSQLiteOpenHelper.

@Test
public void shouldCloseSQLiteOpenHelper() throws IOException {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    // Should not call close before explicit call to close
    verify(sqLiteOpenHelper, times(0)).close();
    storIOSQLite.close();
    // Should call close on SQLiteOpenHelper
    verify(sqLiteOpenHelper).close();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 30 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldPassSQLWithoutArgsToExecSQL.

@Test
public void shouldPassSQLWithoutArgsToExecSQL() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
    when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE IF EXISTS someTable").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)45 Test (org.junit.Test)37 StorIOException (com.pushtorefresh.storio.StorIOException)23 TestSubscriber (rx.observers.TestSubscriber)16 ContentValues (android.content.ContentValues)13 NonNull (android.support.annotation.NonNull)12 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)9 Query (com.pushtorefresh.storio.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 DeleteQuery (com.pushtorefresh.storio.sqlite.queries.DeleteQuery)6 WorkerThread (android.support.annotation.WorkerThread)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Changes (com.pushtorefresh.storio.sqlite.Changes)4 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio.sqlite.queries.RawQuery)4 HashSet (java.util.HashSet)4 UpdateQuery (com.pushtorefresh.storio.sqlite.queries.UpdateQuery)3 HashMap (java.util.HashMap)3 SQLiteTypeMapping (com.pushtorefresh.storio.sqlite.SQLiteTypeMapping)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2