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 
17 package androidx.room.vo
18 
19 import androidx.room.ext.typeName
20 import javax.lang.model.type.TypeMirror
21 
22 /**
23  * Value object created from processing a @Relation annotation.
24  */
25 class Relation(
26         val entity: Entity,
27         // return type. e..g. String in @Relation List<String>
28         val pojoType: TypeMirror,
29         // field in Pojo that holds these relations (e.g. List<Pet> pets)
30         val field: Field,
31         // the parent field referenced for matching
32         val parentField: Field,
33         // the field referenced for querying. does not need to be in the response but the query
34         // we generate always has it in the response.
35         val entityField: Field,
36         // the projection for the query
37         val projection: List<String>) {
38 
<lambda>null39     val pojoTypeName by lazy { pojoType.typeName() }
40 
createLoadAllSqlnull41     fun createLoadAllSql(): String {
42         val resultFields = projection.toSet() + entityField.columnName
43         return createSelect(resultFields)
44     }
45 
createSelectnull46     private fun createSelect(resultFields: Set<String>): String {
47         return "SELECT ${resultFields.joinToString(",") {"`$it`"}}" +
48                 " FROM `${entity.tableName}`" +
49                 " WHERE `${entityField.columnName}` IN (:args)"
50     }
51 }
52