From e06af49ca8d02fcc2b22f8706ff289b47546859d Mon Sep 17 00:00:00 2001 From: Artem Zinnatullin Date: Thu, 20 Aug 2015 03:21:15 +0300 Subject: [PATCH] Relations example! --- .../storio/sample/db/DbModule.java | 29 +++++++++++--- .../storio/sample/db/DbOpenHelper.java | 17 ++------ .../sample/db/{entity => entities}/Tweet.java | 11 ++--- .../sample/db/entities/TweetWithUser.java | 33 +++++++++++++++ .../storio/sample/db/entities/User.java | 40 +++++++++++++++++++ .../TweetWithUserDeleteResolver.java | 35 ++++++++++++++++ .../resolvers/TweetWithUserGetResolver.java | 33 +++++++++++++++ .../resolvers/TweetWithUserPutResolver.java | 40 +++++++++++++++++++ .../TweetsTable.java} | 17 ++++++-- .../storio/sample/db/tables/UsersTable.java | 32 +++++++++++++++ .../provider/SampleContentProvider.java | 10 ++--- .../sample/provider/meta/TweetMeta.java | 20 +++++----- .../storio/sample/sample_code/Relations.java | 38 ++++++++++++++++++ .../sample/ui/adapter/TweetsAdapter.java | 2 +- .../sample/ui/fragment/TweetsFragment.java | 6 +-- 15 files changed, 318 insertions(+), 45 deletions(-) rename storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/{entity => entities}/Tweet.java (87%) create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/TweetWithUser.java create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/User.java create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserDeleteResolver.java create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserGetResolver.java create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserPutResolver.java rename storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/{table/TweetTableMeta.java => tables/TweetsTable.java} (65%) create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/UsersTable.java create mode 100644 storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/sample_code/Relations.java diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbModule.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbModule.java index f40f86e32..b377e3a0d 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbModule.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbModule.java @@ -4,10 +4,18 @@ import android.database.sqlite.SQLiteOpenHelper; import android.support.annotation.NonNull; -import com.pushtorefresh.storio.sample.db.entity.Tweet; -import com.pushtorefresh.storio.sample.db.entity.TweetStorIOSQLiteDeleteResolver; -import com.pushtorefresh.storio.sample.db.entity.TweetStorIOSQLiteGetResolver; -import com.pushtorefresh.storio.sample.db.entity.TweetStorIOSQLitePutResolver; +import com.pushtorefresh.storio.sample.db.entities.Tweet; +import com.pushtorefresh.storio.sample.db.entities.TweetStorIOSQLiteDeleteResolver; +import com.pushtorefresh.storio.sample.db.entities.TweetStorIOSQLiteGetResolver; +import com.pushtorefresh.storio.sample.db.entities.TweetStorIOSQLitePutResolver; +import com.pushtorefresh.storio.sample.db.entities.TweetWithUser; +import com.pushtorefresh.storio.sample.db.entities.User; +import com.pushtorefresh.storio.sample.db.entities.UserStorIOSQLiteDeleteResolver; +import com.pushtorefresh.storio.sample.db.entities.UserStorIOSQLiteGetResolver; +import com.pushtorefresh.storio.sample.db.entities.UserStorIOSQLitePutResolver; +import com.pushtorefresh.storio.sample.db.resolvers.TweetWithUserDeleteResolver; +import com.pushtorefresh.storio.sample.db.resolvers.TweetWithUserGetResolver; +import com.pushtorefresh.storio.sample.db.resolvers.TweetWithUserPutResolver; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; @@ -24,6 +32,7 @@ public class DbModule { // It's thread safe and so on, so just share it. // But if you need you can have multiple instances of StorIO // (SQLite or ContentResolver) with different settings such as type mapping, logging and so on. + // But keep in mind that different instances of StorIOSQLite won't share notifications! @Provides @NonNull @Singleton @@ -35,13 +44,23 @@ public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelp .getResolver(new TweetStorIOSQLiteGetResolver()) .deleteResolver(new TweetStorIOSQLiteDeleteResolver()) .build()) + .addTypeMapping(User.class, SQLiteTypeMapping.builder() + .putResolver(new UserStorIOSQLitePutResolver()) + .getResolver(new UserStorIOSQLiteGetResolver()) + .deleteResolver(new UserStorIOSQLiteDeleteResolver()) + .build()) + .addTypeMapping(TweetWithUser.class, SQLiteTypeMapping.builder() + .putResolver(new TweetWithUserPutResolver()) + .getResolver(new TweetWithUserGetResolver()) + .deleteResolver(new TweetWithUserDeleteResolver()) + .build()) .build(); } @Provides @NonNull @Singleton - public SQLiteOpenHelper provideSQSqLiteOpenHelper(@NonNull Context context) { + public SQLiteOpenHelper provideSQLiteOpenHelper(@NonNull Context context) { return new DbOpenHelper(context); } } diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbOpenHelper.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbOpenHelper.java index 4513f7980..5c124f301 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbOpenHelper.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/DbOpenHelper.java @@ -5,7 +5,8 @@ import android.database.sqlite.SQLiteOpenHelper; import android.support.annotation.NonNull; -import com.pushtorefresh.storio.sample.db.table.TweetTableMeta; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; +import com.pushtorefresh.storio.sample.db.tables.UsersTable; public class DbOpenHelper extends SQLiteOpenHelper { @@ -13,20 +14,10 @@ public DbOpenHelper(@NonNull Context context) { super(context, "sample_db", null, 1); } - // Better than static final field -> allows VM to unload useless String - // Because you need this string only once per application life on the device - @NonNull - private static String getCreateTweetTableQuery() { - return "CREATE TABLE " + TweetTableMeta.TABLE + "(" - + TweetTableMeta.COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " - + TweetTableMeta.COLUMN_AUTHOR + " TEXT NOT NULL, " - + TweetTableMeta.COLUMN_CONTENT + " TEXT NOT NULL" - + ");"; - } - @Override public void onCreate(@NonNull SQLiteDatabase db) { - db.execSQL(getCreateTweetTableQuery()); + db.execSQL(TweetsTable.getCreateTableQuery()); + db.execSQL(UsersTable.getCreateTableQuery()); } @Override diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entity/Tweet.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/Tweet.java similarity index 87% rename from storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entity/Tweet.java rename to storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/Tweet.java index 8ec178587..fa7b67e80 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entity/Tweet.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/Tweet.java @@ -1,8 +1,9 @@ -package com.pushtorefresh.storio.sample.db.entity; +package com.pushtorefresh.storio.sample.db.entities; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; @@ -12,22 +13,22 @@ // This annotation will trigger annotation processor // Which will generate type mapping code in compile time, // You just need to link it in your code. -@StorIOSQLiteType(table = "tweets") +@StorIOSQLiteType(table = TweetsTable.TABLE) public class Tweet { /** * If object was not inserted into db, id will be null */ @Nullable - @StorIOSQLiteColumn(name = "_id", key = true) + @StorIOSQLiteColumn(name = TweetsTable.COLUMN_ID, key = true) Long id; @NonNull - @StorIOSQLiteColumn(name = "author") + @StorIOSQLiteColumn(name = TweetsTable.COLUMN_AUTHOR) String author; @NonNull - @StorIOSQLiteColumn(name = "content") + @StorIOSQLiteColumn(name = TweetsTable.COLUMN_CONTENT) String content; // leave default constructor for AutoGenerated code! diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/TweetWithUser.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/TweetWithUser.java new file mode 100644 index 000000000..26c8f08ed --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/TweetWithUser.java @@ -0,0 +1,33 @@ +package com.pushtorefresh.storio.sample.db.entities; + +import android.support.annotation.NonNull; + +/** + * Example of entity with another entity linked together! + * + * But our Annotation Processor can not handle such things, + * so we need to wrote our own PutResolver, GetResolver and DeleteResolver + */ +public class TweetWithUser { + + @NonNull + private final Tweet tweet; + + @NonNull + private final User user; + + public TweetWithUser(@NonNull Tweet tweet, @NonNull User user) { + this.tweet = tweet; + this.user = user; + } + + @NonNull + public Tweet tweet() { + return tweet; + } + + @NonNull + public User user() { + return user; + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/User.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/User.java new file mode 100644 index 000000000..ab242b724 --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/User.java @@ -0,0 +1,40 @@ +package com.pushtorefresh.storio.sample.db.entities; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.pushtorefresh.storio.sample.db.tables.UsersTable; +import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; +import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; + +// This annotation will trigger annotation processor +// Which will generate type mapping code in compile time, +// You just need to link it in your code. +@StorIOSQLiteType(table = UsersTable.TABLE) +public class User { + + /** + * If object was not inserted into db, id will be null + */ + @Nullable + @StorIOSQLiteColumn(name = UsersTable.COLUMN_ID, key = true) + Long id; + + // For example: "artem_zin", without "@". + @NonNull + @StorIOSQLiteColumn(name = UsersTable.COLUMN_NICK) + String nick; + + // leave default constructor for AutoGenerated code! + User() { + + } + + @NonNull + public static User newUser(@Nullable Long id, @NonNull String nick) { + User user = new User(); + user.id = id; + user.nick = nick; + return user; + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserDeleteResolver.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserDeleteResolver.java new file mode 100644 index 000000000..bf46a9c14 --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserDeleteResolver.java @@ -0,0 +1,35 @@ +package com.pushtorefresh.storio.sample.db.resolvers; + +import android.support.annotation.NonNull; + +import com.pushtorefresh.storio.sample.db.entities.TweetWithUser; +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.HashSet; +import java.util.Set; + +import static java.util.Arrays.asList; + +public class TweetWithUserDeleteResolver extends DeleteResolver { + @NonNull + @Override + public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull TweetWithUser tweetWithUser) { + // We can even reuse StorIO methods + storIOSQLite + .delete() + .objects(asList(tweetWithUser.tweet(), tweetWithUser.user())) + .prepare() // BTW: it will use transaction! + .executeAsBlocking(); + + final Set affectedTables = new HashSet(2); + + affectedTables.add(TweetsTable.TABLE); + affectedTables.add(UsersTable.TABLE); + + return DeleteResult.newInstance(2, affectedTables); + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserGetResolver.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserGetResolver.java new file mode 100644 index 000000000..be31094b0 --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserGetResolver.java @@ -0,0 +1,33 @@ +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.TweetWithUser; +import com.pushtorefresh.storio.sample.db.entities.User; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; +import com.pushtorefresh.storio.sample.db.tables.UsersTable; +import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; + +public class TweetWithUserGetResolver extends DefaultGetResolver { + + // We expect that cursor will contain both Tweet and User: SQL JOIN + @NonNull + @Override + public TweetWithUser mapFromCursor(@NonNull Cursor cursor) { + final Tweet tweet = Tweet.newTweet( + cursor.getLong(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_ID)), + cursor.getString(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_AUTHOR)), + cursor.getString(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_CONTENT)) + ); + + final User user = User.newUser( + cursor.getLong(cursor.getColumnIndexOrThrow(UsersTable.COLUMN_ID)), + cursor.getString(cursor.getColumnIndexOrThrow(UsersTable.COLUMN_NICK)) + ); + + return new TweetWithUser(tweet, user); + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserPutResolver.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserPutResolver.java new file mode 100644 index 000000000..666414b6a --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserPutResolver.java @@ -0,0 +1,40 @@ +package com.pushtorefresh.storio.sample.db.resolvers; + +import android.support.annotation.NonNull; + +import com.pushtorefresh.storio.sample.db.entities.TweetWithUser; +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 com.pushtorefresh.storio.sqlite.operations.put.PutResults; + +import java.util.HashSet; +import java.util.Set; + +import static java.util.Arrays.asList; + +public class TweetWithUserPutResolver extends PutResolver { + + @NonNull + @Override + public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull TweetWithUser tweetWithUser) { + // We can even reuse StorIO methods + final PutResults putResults = storIOSQLite + .put() + .objects(asList(tweetWithUser.tweet(), tweetWithUser.user())) + .prepare() // BTW: it will use transaction! + .executeAsBlocking(); + + final Set affectedTables = new HashSet(2); + + affectedTables.add(TweetsTable.TABLE); + affectedTables.add(UsersTable.TABLE); + + // Actually, it's not very clear what PutResult should we return hereā€¦ + // Because there is no table for this pair of tweet and user + // So, let's just return Update Result + return PutResult.newUpdateResult(putResults.numberOfUpdates(), affectedTables); + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/table/TweetTableMeta.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/TweetsTable.java similarity index 65% rename from storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/table/TweetTableMeta.java rename to storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/TweetsTable.java index 7e21caa6b..3e98a735d 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/table/TweetTableMeta.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/TweetsTable.java @@ -1,4 +1,4 @@ -package com.pushtorefresh.storio.sample.db.table; +package com.pushtorefresh.storio.sample.db.tables; import android.support.annotation.NonNull; @@ -6,7 +6,7 @@ // We suggest to store table meta such as table name, columns names, queries, etc in separate class // Because it makes code of the Entity itself cleaner and easier to read/understand/support -public class TweetTableMeta { +public class TweetsTable { @NonNull public static final String TABLE = "tweets"; @@ -33,7 +33,18 @@ public class TweetTableMeta { .build(); // This is just class with Meta Data, we don't need instances - private TweetTableMeta() { + private TweetsTable() { throw new IllegalStateException("No instances please"); } + + // Better than static final field -> allows VM to unload useless String + // Because you need this string only once per application life on the device + @NonNull + public static String getCreateTableQuery() { + return "CREATE TABLE " + TABLE + "(" + + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " + + COLUMN_AUTHOR + " TEXT NOT NULL, " + + COLUMN_CONTENT + " TEXT NOT NULL" + + ");"; + } } diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/UsersTable.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/UsersTable.java new file mode 100644 index 000000000..3ed42c9d8 --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/UsersTable.java @@ -0,0 +1,32 @@ +package com.pushtorefresh.storio.sample.db.tables; + +import android.support.annotation.NonNull; + +// We suggest to store table meta such as table name, columns names, queries, etc in separate class +// Because it makes code of the Entity itself cleaner and easier to read/understand/support +public class UsersTable { + + @NonNull + public static final String TABLE = "users"; + + @NonNull + public static final String COLUMN_ID = "_id"; + + @NonNull + public static final String COLUMN_NICK = "nick"; + + // This is just class with Meta Data, we don't need instances + private UsersTable() { + throw new IllegalStateException("No instances please"); + } + + // Better than static final field -> allows VM to unload useless String + // Because you need this string only once per application life on the device + @NonNull + public static String getCreateTableQuery() { + return "CREATE TABLE " + TABLE + "(" + + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " + + COLUMN_NICK + " TEXT NOT NULL UNIQUE" + + ");"; + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/SampleContentProvider.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/SampleContentProvider.java index d349a70f1..897bb8f7a 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/SampleContentProvider.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/SampleContentProvider.java @@ -10,7 +10,7 @@ import android.support.annotation.NonNull; import com.pushtorefresh.storio.sample.SampleApp; -import com.pushtorefresh.storio.sample.db.table.TweetTableMeta; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; import javax.inject.Inject; @@ -47,7 +47,7 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel return sqLiteOpenHelper .getReadableDatabase() .query( - TweetTableMeta.TABLE, + TweetsTable.TABLE, projection, selection, selectionArgs, @@ -75,7 +75,7 @@ public Uri insert(Uri uri, ContentValues values) { insertedId = sqLiteOpenHelper .getWritableDatabase() .insert( - TweetTableMeta.TABLE, + TweetsTable.TABLE, null, values ); @@ -101,7 +101,7 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele numberOfRowsAffected = sqLiteOpenHelper .getWritableDatabase() .update( - TweetTableMeta.TABLE, + TweetsTable.TABLE, values, selection, selectionArgs @@ -128,7 +128,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) { numberOfRowsDeleted = sqLiteOpenHelper .getWritableDatabase() .delete( - TweetTableMeta.TABLE, + TweetsTable.TABLE, selection, selectionArgs ); diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/meta/TweetMeta.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/meta/TweetMeta.java index da1117e98..8fdc14c32 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/meta/TweetMeta.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/provider/meta/TweetMeta.java @@ -14,8 +14,8 @@ import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery; import com.pushtorefresh.storio.contentresolver.queries.InsertQuery; import com.pushtorefresh.storio.contentresolver.queries.UpdateQuery; -import com.pushtorefresh.storio.sample.db.entity.Tweet; -import com.pushtorefresh.storio.sample.db.table.TweetTableMeta; +import com.pushtorefresh.storio.sample.db.entities.Tweet; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; import com.pushtorefresh.storio.sample.provider.SampleContentProvider; public class TweetMeta { @@ -38,7 +38,7 @@ protected InsertQuery mapToInsertQuery(@NonNull Tweet object) { protected UpdateQuery mapToUpdateQuery(@NonNull Tweet tweet) { return UpdateQuery.builder() .uri(CONTENT_URI) - .where(TweetTableMeta.COLUMN_ID + " = ?") + .where(TweetsTable.COLUMN_ID + " = ?") .whereArgs(tweet.id()) .build(); } @@ -48,9 +48,9 @@ protected UpdateQuery mapToUpdateQuery(@NonNull Tweet tweet) { protected ContentValues mapToContentValues(@NonNull Tweet object) { ContentValues contentValues = new ContentValues(); - contentValues.put(TweetTableMeta.COLUMN_ID, object.id()); - contentValues.put(TweetTableMeta.COLUMN_AUTHOR, object.author()); - contentValues.put(TweetTableMeta.COLUMN_CONTENT, object.content()); + contentValues.put(TweetsTable.COLUMN_ID, object.id()); + contentValues.put(TweetsTable.COLUMN_AUTHOR, object.author()); + contentValues.put(TweetsTable.COLUMN_CONTENT, object.content()); return contentValues; } @@ -62,9 +62,9 @@ protected ContentValues mapToContentValues(@NonNull Tweet object) { @Override public Tweet mapFromCursor(@NonNull Cursor cursor) { return Tweet.newTweet( - cursor.getLong(cursor.getColumnIndexOrThrow(TweetTableMeta.COLUMN_ID)), - cursor.getString(cursor.getColumnIndexOrThrow(TweetTableMeta.COLUMN_AUTHOR)), - cursor.getString(cursor.getColumnIndexOrThrow(TweetTableMeta.COLUMN_CONTENT)) + cursor.getLong(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_ID)), + cursor.getString(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_AUTHOR)), + cursor.getString(cursor.getColumnIndexOrThrow(TweetsTable.COLUMN_CONTENT)) ); } }; @@ -76,7 +76,7 @@ public Tweet mapFromCursor(@NonNull Cursor cursor) { protected DeleteQuery mapToDeleteQuery(@NonNull Tweet tweet) { return DeleteQuery.builder() .uri(CONTENT_URI) - .where(TweetTableMeta.COLUMN_ID + " = ?") + .where(TweetsTable.COLUMN_ID + " = ?") .whereArgs(tweet.id()) .build(); } diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/sample_code/Relations.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/sample_code/Relations.java new file mode 100644 index 000000000..d0888af16 --- /dev/null +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/sample_code/Relations.java @@ -0,0 +1,38 @@ +package com.pushtorefresh.storio.sample.sample_code; + +import android.support.annotation.NonNull; + +import com.pushtorefresh.storio.sample.db.entities.TweetWithUser; +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.queries.RawQuery; + +import java.util.List; + +/** + * Examples with relations (SQL JOIN) + */ +public class Relations { + + @NonNull + private final StorIOSQLite storIOSQLite; + + public Relations(@NonNull StorIOSQLite storIOSQLite) { + this.storIOSQLite = storIOSQLite; + } + + public List tweetWithUserGet() { + return storIOSQLite + .get() + .listOfObjects(TweetWithUser.class) + .withQuery(RawQuery.builder() + .query("SELECT * FROM " + TweetsTable.TABLE + + " JOIN " + UsersTable.TABLE + + " ON " + TweetsTable.TABLE + "." + TweetsTable.COLUMN_AUTHOR + + " = " + UsersTable.TABLE + "." + UsersTable.COLUMN_NICK) + .build()) + .prepare() + .executeAsBlocking(); + } +} diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/adapter/TweetsAdapter.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/adapter/TweetsAdapter.java index 204a9f79d..06780c5de 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/adapter/TweetsAdapter.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/adapter/TweetsAdapter.java @@ -9,7 +9,7 @@ import android.widget.TextView; import com.pushtorefresh.storio.sample.R; -import com.pushtorefresh.storio.sample.db.entity.Tweet; +import com.pushtorefresh.storio.sample.db.entities.Tweet; import java.util.List; diff --git a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/fragment/TweetsFragment.java b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/fragment/TweetsFragment.java index 2c8914e2b..c566356a2 100644 --- a/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/fragment/TweetsFragment.java +++ b/storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/ui/fragment/TweetsFragment.java @@ -12,8 +12,8 @@ import com.pushtorefresh.storio.sample.R; import com.pushtorefresh.storio.sample.SampleApp; -import com.pushtorefresh.storio.sample.db.entity.Tweet; -import com.pushtorefresh.storio.sample.db.table.TweetTableMeta; +import com.pushtorefresh.storio.sample.db.entities.Tweet; +import com.pushtorefresh.storio.sample.db.tables.TweetsTable; import com.pushtorefresh.storio.sample.ui.DividerItemDecoration; import com.pushtorefresh.storio.sample.ui.UiStateController; import com.pushtorefresh.storio.sample.ui.adapter.TweetsAdapter; @@ -94,7 +94,7 @@ void reloadData() { final Subscription subscription = storIOSQLite .get() .listOfObjects(Tweet.class) - .withQuery(TweetTableMeta.QUERY_ALL) + .withQuery(TweetsTable.QUERY_ALL) .prepare() .createObservable() // it will be subscribed to changes in tweets table! .delay(1, SECONDS) // for better User Experience :) Actually, StorIO is so fast that we need to delay emissions (it's a joke, or not)