-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Order entities by insertion #6408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,6 +284,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep | |
SELECT *, i.$ROW_ID | ||
FROM $list e, ${getRowIdTableName(list)} i | ||
WHERE e._id = i._id | ||
ORDER BY i.$ROW_ID | ||
""".trimIndent(), | ||
null | ||
) | ||
|
@@ -301,6 +302,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep | |
SELECT *, i.$ROW_ID | ||
FROM $list e, ${getRowIdTableName(list)} i | ||
WHERE e._id = i._id AND $selectionColumn = ? | ||
ORDER BY i.$ROW_ID | ||
""".trimIndent(), | ||
arrayOf(selectionArg) | ||
) | ||
|
@@ -325,7 +327,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep | |
|
||
writableDatabase.execSQL( | ||
""" | ||
CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it; | ||
CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it ORDER BY _id; | ||
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. As far as I understand it, 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. There is a potential issue with this assumption: if we delete some entities, their IDs might be reused. While this shouldn't occur under normal circumstances, once we reach the maximum possible ID value of 9223372036854775807, IDs will start to be reused. However, this number is large enough that we don't need to worry about it for now I guess. Do you agree @lognaturel? 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. Ahh the table gets recreated every time we add/delete an entity so it won't be and issue. 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. Although, as mentioned above, this is not an issue at the moment, it might be a good idea to add a test that serves as documentation hmm? 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. Preserving order is a nice to have more than a must have so I think it's ok to leave edge cases untested. It's only a nice to have because the order is not necessarily meaningful currently. Longer-term, we'll need to be able to let users specify a custom sort order. |
||
""".trimIndent() | ||
) | ||
} | ||
|
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.
I don't think we strictly need the
ORDER BY
here and in the otherqueryWithAttachedRowId
overload and I can't work out a way to test it (I think the query is structured so that it always happens to works out). I feel safer having it there though so.