-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relations example! #494
Merged
Merged
Relations example! #494
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 33 additions & 0 deletions
33
...o-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/TweetWithUser.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,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; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/entities/User.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,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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserDeleteResolver.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,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<TweetWithUser> { | ||
@NonNull | ||
@Override | ||
public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull TweetWithUser tweetWithUser) { | ||
// We can even reuse StorIO methods | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
storIOSQLite | ||
.delete() | ||
.objects(asList(tweetWithUser.tweet(), tweetWithUser.user())) | ||
.prepare() // BTW: it will use transaction! | ||
.executeAsBlocking(); | ||
|
||
final Set<String> affectedTables = new HashSet<String>(2); | ||
|
||
affectedTables.add(TweetsTable.TABLE); | ||
affectedTables.add(UsersTable.TABLE); | ||
|
||
return DeleteResult.newInstance(2, affectedTables); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserGetResolver.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,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<TweetWithUser> { | ||
|
||
// 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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../src/main/java/com/pushtorefresh/storio/sample/db/resolvers/TweetWithUserPutResolver.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,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<TweetWithUser> { | ||
|
||
@NonNull | ||
@Override | ||
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull TweetWithUser tweetWithUser) { | ||
// We can even reuse StorIO methods | ||
final PutResults<Object> putResults = storIOSQLite | ||
.put() | ||
.objects(asList(tweetWithUser.tweet(), tweetWithUser.user())) | ||
.prepare() // BTW: it will use transaction! | ||
.executeAsBlocking(); | ||
|
||
final Set<String> affectedTables = new HashSet<String>(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); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
storio-sample-app/src/main/java/com/pushtorefresh/storio/sample/db/tables/UsersTable.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,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" | ||
+ ");"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn