Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #520 from pushtorefresh/user-with-tweets-example
Example of UserWithTweets entity with custom Put/Get/Delete resolvers
- Loading branch information
Showing
10 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/UserWithTweets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.pushtorefresh.storio.sample.db.entities; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Collections.unmodifiableList; | ||
|
||
/** | ||
* Example of entity with linked sub-entities! | ||
* | ||
* It's a User with his tweets. | ||
* | ||
* Main idea of this example is to show you that | ||
* StorIO can solve ORM problems but still be a DAO. | ||
* | ||
* Moreover, we will write GetResolver in such manner | ||
* that you will be able to write use any Query with it, | ||
* but at the same time you could optimize any frequent case | ||
* with JOIN and other SQL things directly in GetResolver. | ||
*/ | ||
public final class UserWithTweets { | ||
|
||
@NonNull | ||
private final User user; | ||
|
||
@NonNull | ||
private final List<Tweet> tweets; | ||
|
||
public UserWithTweets(@NonNull User user, @NonNull List<Tweet> tweets) { | ||
this.user = user; | ||
this.tweets = unmodifiableList(tweets); // We prefer immutable entities | ||
} | ||
|
||
@NonNull | ||
public User user() { | ||
return user; | ||
} | ||
|
||
@NonNull | ||
public List<Tweet> tweets() { | ||
return tweets; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../main/java/com/pushtorefresh/storio/sample/db/resolvers/UserWithTweetsDeleteResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.pushtorefresh.storio.sample.db.resolvers; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.pushtorefresh.storio.sample.db.entities.UserWithTweets; | ||
import com.pushtorefresh.storio.sample.db.tables.TweetsTable; | ||
import com.pushtorefresh.storio.sample.db.tables.UsersTable; | ||
import com.pushtorefresh.storio.sqlite.StorIOSQLite; | ||
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResolver; | ||
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public final class UserWithTweetsDeleteResolver extends DeleteResolver<UserWithTweets> { | ||
|
||
@NonNull | ||
@Override | ||
public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull UserWithTweets userWithTweets) { | ||
// 1 for user and other for his/her tweets | ||
final List<Object> objectsToDelete = new ArrayList<Object>(1 + userWithTweets.tweets().size()); | ||
|
||
objectsToDelete.add(userWithTweets.user()); | ||
objectsToDelete.addAll(userWithTweets.tweets()); | ||
|
||
storIOSQLite | ||
.delete() | ||
.objects(objectsToDelete) | ||
.prepare() | ||
.executeAsBlocking(); | ||
|
||
// BTW, you can save it as static final | ||
final Set<String> affectedTables = new HashSet<String>(2); | ||
|
||
affectedTables.add(UsersTable.TABLE); | ||
affectedTables.add(TweetsTable.TABLE); | ||
|
||
return DeleteResult.newInstance(objectsToDelete.size(), affectedTables); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...src/main/java/com/pushtorefresh/storio/sample/db/resolvers/UserWithTweetsGetResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.pushtorefresh.storio.sample.db.resolvers; | ||
|
||
import android.database.Cursor; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.pushtorefresh.storio.sample.db.entities.Tweet; | ||
import com.pushtorefresh.storio.sample.db.entities.User; | ||
import com.pushtorefresh.storio.sample.db.entities.UserWithTweets; | ||
import com.pushtorefresh.storio.sample.db.tables.TweetsTable; | ||
import com.pushtorefresh.storio.sqlite.StorIOSQLite; | ||
import com.pushtorefresh.storio.sqlite.operations.get.GetResolver; | ||
import com.pushtorefresh.storio.sqlite.queries.Query; | ||
import com.pushtorefresh.storio.sqlite.queries.RawQuery; | ||
|
||
import java.util.List; | ||
|
||
public final class UserWithTweetsGetResolver extends GetResolver<UserWithTweets> { | ||
|
||
// We can even reuse existing get resolvers for our needs | ||
// But, you can always write custom code, of course. | ||
@NonNull | ||
private final GetResolver<User> userGetResolver; | ||
|
||
// Sorry for this hack :( | ||
// We will pass you an instance of StorIO | ||
// into the mapFromCursor() in v2.0.0. | ||
// | ||
// At the moment, you can save this instance in performGet() and then null it at the end | ||
@NonNull | ||
private final ThreadLocal<StorIOSQLite> storIOSQLiteFromPerformGet = new ThreadLocal<StorIOSQLite>(); | ||
|
||
public UserWithTweetsGetResolver(@NonNull GetResolver<User> userGetResolver) { | ||
this.userGetResolver = userGetResolver; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public UserWithTweets mapFromCursor(@NonNull Cursor cursor) { | ||
final StorIOSQLite storIOSQLite = storIOSQLiteFromPerformGet.get(); | ||
|
||
// BTW, you don't need a transaction here | ||
// StorIO will wrap mapFromCursor() into the transaction if needed | ||
try { | ||
// Or you can manually parse cursor (it will be sliiightly faster) | ||
final User user = userGetResolver.mapFromCursor(cursor); | ||
|
||
// Yep, you can reuse StorIO here! | ||
// Or, you can do manual low level requests here | ||
// BTW, if you profiled your app and found that such queries are not very fast | ||
// You can always add some optimized version for particular queries to improve the performance | ||
final List<Tweet> tweetsOfTheUser = storIOSQLite | ||
.get() | ||
.listOfObjects(Tweet.class) | ||
.withQuery(Query.builder() | ||
.table(TweetsTable.TABLE) | ||
.where(TweetsTable.COLUMN_AUTHOR + "=?") | ||
.whereArgs(user.nick()) | ||
.build()) | ||
.prepare() | ||
.executeAsBlocking(); | ||
|
||
return new UserWithTweets(user, tweetsOfTheUser); | ||
} finally { | ||
// Releasing StorIOSQLite reference | ||
storIOSQLiteFromPerformGet.set(null); | ||
} | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Cursor performGet(@NonNull StorIOSQLite storIOSQLite, @NonNull RawQuery rawQuery) { | ||
storIOSQLiteFromPerformGet.set(storIOSQLite); | ||
return storIOSQLite.internal().rawQuery(rawQuery); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Cursor performGet(@NonNull StorIOSQLite storIOSQLite, @NonNull Query query) { | ||
storIOSQLiteFromPerformGet.set(storIOSQLite); | ||
return storIOSQLite.internal().query(query); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...src/main/java/com/pushtorefresh/storio/sample/db/resolvers/UserWithTweetsPutResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.pushtorefresh.storio.sample.db.resolvers; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.pushtorefresh.storio.sample.db.entities.UserWithTweets; | ||
import com.pushtorefresh.storio.sample.db.tables.TweetsTable; | ||
import com.pushtorefresh.storio.sample.db.tables.UsersTable; | ||
import com.pushtorefresh.storio.sqlite.StorIOSQLite; | ||
import com.pushtorefresh.storio.sqlite.operations.put.PutResolver; | ||
import com.pushtorefresh.storio.sqlite.operations.put.PutResult; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public final class UserWithTweetsPutResolver extends PutResolver<UserWithTweets> { | ||
|
||
@NonNull | ||
@Override | ||
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull UserWithTweets userWithTweets) { | ||
// 1 for user and other for his/her tweets | ||
final List<Object> objectsToPut = new ArrayList<Object>(1 + userWithTweets.tweets().size()); | ||
|
||
objectsToPut.add(userWithTweets.user()); | ||
objectsToPut.addAll(userWithTweets.tweets()); | ||
|
||
storIOSQLite | ||
.put() | ||
.objects(objectsToPut) | ||
.prepare() | ||
.executeAsBlocking(); | ||
|
||
// BTW, you can save it as static final | ||
final Set<String> affectedTables = new HashSet<String>(2); | ||
|
||
affectedTables.add(UsersTable.TABLE); | ||
affectedTables.add(TweetsTable.TABLE); | ||
|
||
// Actually, we don't know what to return for such complex operation, so let's return update result | ||
return PutResult.newUpdateResult(objectsToPut.size(), affectedTables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
storio-sample-app/src/test/java/com/pushtorefresh/storio/sample/db/DbOpenHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.pushtorefresh.storio.sample.db; | ||
|
||
import android.database.sqlite.SQLiteDatabase; | ||
|
||
import com.pushtorefresh.storio.contentresolver.BuildConfig; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricGradleTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(RobolectricGradleTestRunner.class) | ||
@Config(constants = BuildConfig.class, sdk = 21) | ||
public final class DbOpenHelperTest { | ||
|
||
@Test | ||
public void shouldCreateDb() { | ||
SQLiteDatabase database = new DbOpenHelper(RuntimeEnvironment.application) | ||
.getWritableDatabase(); | ||
|
||
assertThat(database).isNotNull(); | ||
} | ||
} |