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 package androidx.room.writer 17 18 import androidx.room.ext.L 19 import androidx.room.ext.N 20 import androidx.room.ext.RoomTypeNames 21 import androidx.room.solver.CodeGenScope 22 import com.squareup.javapoet.ClassName 23 import com.squareup.javapoet.FieldSpec 24 import com.squareup.javapoet.MethodSpec 25 import com.squareup.javapoet.TypeSpec 26 import javax.lang.model.element.Modifier 27 28 /** 29 * Creates anonymous classes for RoomTypeNames#SHARED_SQLITE_STMT. 30 */ 31 class PreparedStatementWriter(val queryWriter: QueryWriter) { createAnonymousnull32 fun createAnonymous(classWriter: ClassWriter, dbParam: FieldSpec): TypeSpec { 33 val scope = CodeGenScope(classWriter) 34 @Suppress("RemoveSingleExpressionStringTemplate") 35 return TypeSpec.anonymousClassBuilder("$N", dbParam).apply { 36 superclass(RoomTypeNames.SHARED_SQLITE_STMT) 37 addMethod(MethodSpec.methodBuilder("createQuery").apply { 38 addAnnotation(Override::class.java) 39 returns(ClassName.get("java.lang", "String")) 40 addModifiers(Modifier.PUBLIC) 41 val queryName = scope.getTmpVar("_query") 42 val queryGenScope = scope.fork() 43 queryWriter.prepareQuery(queryName, queryGenScope) 44 addCode(queryGenScope.builder().build()) 45 addStatement("return $L", queryName) 46 }.build()) 47 }.build() 48 } 49 } 50