虽然对于rowId
,@Insert
返回Long
或其集合,而对于受影响的行数,@Delete
或@Update
返回Int
,那么@Upsert
可以返回什么?
参考:https://developer.android.com/training/data-storage/room/accessing-data
虽然对于rowId
,@Insert
返回Long
或其集合,而对于受影响的行数,@Delete
或@Update
返回Int
,那么@Upsert
可以返回什么?
参考:https://developer.android.com/training/data-storage/room/accessing-data
使用@Upsert
批注调用的函数/方法可以返回
rowid
和 thus the PRIMARY KEY of the inserted rowrowid
still exists 和 can be usedUPSERT
(at least currently) but uses an an SQLite INSERT OR REPLACE
(和 hence returns the same values that an @Insert
does).通过编码和编译就可以很容易地确定
例如,使用
@Upsert
fun upsert(parent: PartCargo): Int
顶级编译失败,Not sure how to h和le upsert method's return type.
同时,
@Upsert
fun upsert(parent: PartCargo): Long
@Upsert
fun upsert(parent: PartCargo)
和
编译成功.
然而,
@Upsert
fun upsert(parent: PartCargo): LongArray
失败,错误为Upsert method accepts a single parameter but the return type is a collection of elements.
同时,
@Upsert
fun upsert(parents: List<PartCargo>): LongArray
编译成功.
Additional个
can you add a reference about Room's implementation of Upsert that behind the scene only do insert 和 replace before I accept this answer?
如果有参考文献,则不需要.实际上在"幕后"做了什么很容易确定.
The "behind the scenes", is the annotation processer generating java code. This java generated java code forms part of the package 和 is what is actually used at runtime.
The 2 core/primary annotations are the @Database
和 @Dao
annotations. These determine the resultant classes that contain the respective generated java. The classes will be the name of the class/interface being processed suffixed with _Impl.
生成的Java放在文件夹的子文件夹中,通过Android Studio中的Android View显示为Java(生成的).通常会有两个同名的子文件夹.其中一个将被视为包含已生成的各个类.
例如,上述代码摘录摘自以下内容:
The extracts were from the @Dao
annotated interface interface DetailsDAO ....
和 hence the DetailsDAO_Impl
class that has been generated.
搜索Upsert找到:-
It is then just a matter of ascertaining the respective code utilised 和 the SQL invoked. For the above the eventual respective code is:-
this.__insertionAdapterOfPartCargo = new EntityInsertionAdapter<PartCargo>(__db) {
@Override
@NonNull
protected String createQuery() {
return "INSERT OR REPLACE INTO `parts` (`tId`,`detailsTId`,`id`,`name`) VALUES (nullif(?, 0),?,?,?)";
}