1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package androidx.room 17 18 import kotlin.reflect.KClass 19 20 /** 21 * Marks a method in a [Dao] annotated class as an update method. 22 * 23 * The implementation of the method will update its parameters in the database if they already 24 * exists (checked by primary keys). If they don't already exists, this option will not change the 25 * database. 26 * 27 * All of the parameters of the Update method must either be classes annotated with [Entity] or 28 * collections/array of it. 29 * 30 * Example: 31 * ``` 32 * @Dao 33 * public interface MusicDao { 34 * @Update 35 * fun updateSong(song: Song) 36 * 37 * @Update 38 * fun updateSongs(songs: List<Song>): Int 39 * } 40 * ``` 41 * 42 * If the target entity is specified via [entity] then the parameters can be of arbitrary POJO types 43 * that will be interpreted as partial entities. For example: 44 * ``` 45 * @Entity 46 * data class Playlist ( 47 * @PrimaryKey(autoGenerate = true) 48 * val playlistId: Long, 49 * val name: String, 50 * @ColumnInfo(defaultValue = "") 51 * val description: String, 52 * @ColumnInfo(defaultValue = "normal") 53 * val category: String, 54 * @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP") 55 * val createdTime: String, 56 * @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP") 57 * val lastModifiedTime: String 58 * ) 59 * 60 * data class PlaylistCategory ( 61 * val playlistId: Long, 62 * val category: String, 63 * val lastModifiedTime: String 64 * ) 65 * 66 * @Dao 67 * public interface PlaylistDao { 68 * @Update(entity = Playlist::class) 69 * fun updateCategory(varargs category: Category) 70 * } 71 * ``` 72 * 73 * @see [Insert] 74 * @see [Delete] 75 */ 76 @Target(AnnotationTarget.FUNCTION) 77 @Retention(AnnotationRetention.BINARY) 78 public annotation class Update( 79 80 /** 81 * The target entity of the update method. 82 * 83 * When this is declared, the update method parameters are interpreted as partial entities when 84 * the type of the parameter differs from the target. The POJO class that represents the entity 85 * must contain a subset of the fields of the target entity along with its primary keys. 86 * 87 * Only the columns represented by the partial entity fields will be updated if an entity with 88 * equal primary key is found. 89 * 90 * By default the target entity is interpreted by the method parameters. 91 * 92 * @return the target entity of the update method or none if the method should use the parameter 93 * type entities. 94 */ 95 val entity: KClass<*> = Any::class, 96 97 /** 98 * What to do if a conflict happens. 99 * 100 * Use [OnConflictStrategy.ABORT] (default) to roll back the transaction on conflict. Use 101 * [OnConflictStrategy.REPLACE] to replace the existing rows with the new rows. Use 102 * [OnConflictStrategy.IGNORE] to keep the existing rows. 103 * 104 * @return How to handle conflicts. Defaults to [OnConflictStrategy.ABORT]. 105 */ 106 @get:OnConflictStrategy val onConflict: Int = OnConflictStrategy.ABORT 107 ) 108