Home | History | Annotate | Download | only in writer
      1 /*
      2  * Copyright (C) 2016 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.ext.L
     20 import androidx.room.ext.RoomTypeNames
     21 import androidx.room.ext.S
     22 import androidx.room.ext.SupportDbTypeNames
     23 import androidx.room.solver.CodeGenScope
     24 import androidx.room.vo.Entity
     25 import androidx.room.vo.FieldWithIndex
     26 import com.squareup.javapoet.ClassName
     27 import com.squareup.javapoet.MethodSpec
     28 import com.squareup.javapoet.ParameterSpec
     29 import com.squareup.javapoet.ParameterizedTypeName
     30 import com.squareup.javapoet.TypeName
     31 import com.squareup.javapoet.TypeSpec
     32 import javax.lang.model.element.Modifier.PUBLIC
     33 
     34 class EntityDeletionAdapterWriter(val entity: Entity) {
     35     fun createAnonymous(classWriter: ClassWriter, dbParam: String): TypeSpec {
     36         @Suppress("RemoveSingleExpressionStringTemplate")
     37         return TypeSpec.anonymousClassBuilder("$L", dbParam).apply {
     38             superclass(ParameterizedTypeName.get(RoomTypeNames.DELETE_OR_UPDATE_ADAPTER,
     39                     entity.typeName)
     40             )
     41             addMethod(MethodSpec.methodBuilder("createQuery").apply {
     42                 addAnnotation(Override::class.java)
     43                 returns(ClassName.get("java.lang", "String"))
     44                 addModifiers(PUBLIC)
     45                 val query = "DELETE FROM `${entity.tableName}` WHERE " +
     46                         entity.primaryKey.fields.joinToString(" AND ") {
     47                             "`${it.columnName}` = ?"
     48                         }
     49                 addStatement("return $S", query)
     50             }.build())
     51             addMethod(MethodSpec.methodBuilder("bind").apply {
     52                 val bindScope = CodeGenScope(classWriter)
     53                 addAnnotation(Override::class.java)
     54                 val stmtParam = "stmt"
     55                 addParameter(ParameterSpec.builder(SupportDbTypeNames.SQLITE_STMT,
     56                         stmtParam).build())
     57                 val valueParam = "value"
     58                 addParameter(ParameterSpec.builder(entity.typeName, valueParam).build())
     59                 returns(TypeName.VOID)
     60                 addModifiers(PUBLIC)
     61                 val mapped = FieldWithIndex.byOrder(entity.primaryKey.fields)
     62                 FieldReadWriteWriter.bindToStatement(ownerVar = valueParam,
     63                         stmtParamVar = stmtParam,
     64                         fieldsWithIndices = mapped,
     65                         scope = bindScope)
     66                 addCode(bindScope.builder().build())
     67             }.build())
     68         }.build()
     69     }
     70 }
     71