1 /*
2  * Copyright 2022 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 
17 package androidx.room.writer
18 
19 import androidx.room.compiler.codegen.XCodeBlock
20 import androidx.room.ext.RoomTypeNames
21 import androidx.room.vo.DataClass
22 import androidx.room.vo.ShortcutEntity
23 
24 class EntityUpsertAdapterWriter
25 private constructor(val tableName: String, val dataClass: DataClass) {
26     companion object {
createnull27         fun create(entity: ShortcutEntity): EntityUpsertAdapterWriter {
28             return EntityUpsertAdapterWriter(
29                 tableName = entity.tableName,
30                 dataClass = entity.dataClass
31             )
32         }
33     }
34 
createConcretenull35     fun createConcrete(entity: ShortcutEntity, typeWriter: TypeWriter): XCodeBlock {
36         val upsertAdapter = RoomTypeNames.UPSERT_ADAPTER.parametrizedBy(dataClass.typeName)
37         val insertHelper = EntityInsertAdapterWriter.create(entity, "").createAnonymous(typeWriter)
38         val updateHelper = EntityUpdateAdapterWriter.create(entity, "").createAnonymous(typeWriter)
39         return XCodeBlock.ofNewInstance(
40             typeName = upsertAdapter,
41             argsFormat = "%L, %L",
42             args = arrayOf(insertHelper, updateHelper)
43         )
44     }
45 }
46