Search in sources :

Example 1 with StorIOSQLite

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

the class CarRelationsGetResolver method mapFromCursor.

@Override
@NonNull
public Car mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
    final Car car = super.mapFromCursor(storIOSQLite, cursor);
    final List<Person> persons = storIOSQLite.get().listOfObjects(Person.class).withQuery(RawQuery.builder().query("SELECT " + PersonTable.NAME + ".*" + " FROM " + PersonTable.NAME + " JOIN " + PersonCarRelationTable.TABLE + " ON " + PersonTable.ID_COLUMN + " = " + PersonCarRelationTable.COLUMN_PERSON_ID + " AND " + PersonCarRelationTable.COLUMN_CAR_ID + " = ?").args(car.id()).build()).withGetResolver(// without relations to prevent cycling
    personStorIOSQLiteGetResolver).prepare().executeAsBlocking();
    return new Car(car.id(), car.mark(), persons);
}
Also used : Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) Person(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person) NonNull(android.support.annotation.NonNull)

Example 2 with StorIOSQLite

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

the class CarRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Car object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long carId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_CAR_ID + " = ?").whereArgs(carId).build()).prepare().executeAsBlocking();
        final List<Person> persons = object.persons();
        if (persons != null) {
            storIOSQLite.put().objects(persons).withPutResolver(personStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(persons.size());
            for (Person person : persons) {
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_CAR_ID, carId);
                cv.put(COLUMN_PERSON_ID, person.id());
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) Person(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 3 with StorIOSQLite

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

the class PersonRelationsGetResolver method mapFromCursor.

@Override
@NonNull
public Person mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
    final Person person = super.mapFromCursor(storIOSQLite, cursor);
    final List<Car> cars = storIOSQLite.get().listOfObjects(Car.class).withQuery(RawQuery.builder().query("SELECT " + CarTable.NAME + ".*" + " FROM " + CarTable.NAME + " JOIN " + PersonCarRelationTable.TABLE + " ON " + CarTable.ID_COLUMN + " = " + PersonCarRelationTable.COLUMN_CAR_ID + " AND " + PersonCarRelationTable.COLUMN_PERSON_ID + " = ?").args(person.id()).build()).withGetResolver(carStorIOSQLiteGetResolver).prepare().executeAsBlocking();
    return new Person(person.id(), person.name(), cars);
}
Also used : Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) Person(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person) NonNull(android.support.annotation.NonNull)

Example 4 with StorIOSQLite

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

the class PersonRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Person object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long personId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_PERSON_ID + " = ?").whereArgs(personId).build()).prepare().executeAsBlocking();
        final List<Car> cars = object.cars();
        if (cars != null) {
            final PutResults<Car> carsPutResults = storIOSQLite.put().objects(cars).withPutResolver(carStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(cars.size());
            for (Car car : cars) {
                final PutResult carPutResult = carsPutResults.results().get(car);
                // noinspection ConstantConditions
                final long carId = carPutResult.wasInserted() ? carPutResult.insertedId() : car.id();
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_PERSON_ID, personId);
                cv.put(PersonCarRelationTable.COLUMN_CAR_ID, carId);
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 5 with StorIOSQLite

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

the class CarRelationsDeleteResolver method performDelete.

@Override
@NonNull
public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull Car object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final DeleteResult deleteResult = super.performDelete(storIOSQLite, object);
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(PersonCarRelationTable.COLUMN_CAR_ID + " = ?").whereArgs(object.id()).build()).prepare().executeAsBlocking();
        lowLevel.setTransactionSuccessful();
        return deleteResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) DeleteResult(com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult) NonNull(android.support.annotation.NonNull)

Aggregations

Test (org.junit.Test)34 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)32 NonNull (android.support.annotation.NonNull)18 StorIOException (com.pushtorefresh.storio3.StorIOException)18 ContentValues (android.content.ContentValues)13 Query (com.pushtorefresh.storio3.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 TestObserver (io.reactivex.observers.TestObserver)8 DeleteQuery (com.pushtorefresh.storio3.sqlite.queries.DeleteQuery)7 TestSubscriber (io.reactivex.subscribers.TestSubscriber)7 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)4 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)4 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)4 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)3 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)3 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)3 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)3 User (com.pushtorefresh.storio3.sample.db.entities.User)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2