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
17package androidx.room.vo
18
19import com.squareup.javapoet.ClassName
20import com.squareup.javapoet.TypeName
21import javax.lang.model.element.TypeElement
22import javax.lang.model.type.DeclaredType
23
24data class Dao(
25        val element: TypeElement, val type: DeclaredType,
26        val queryMethods: List<QueryMethod>,
27        val rawQueryMethods: List<RawQueryMethod>,
28        val insertionMethods: List<InsertionMethod>,
29        val deletionMethods: List<DeletionMethod>,
30        val updateMethods: List<UpdateMethod>,
31        val transactionMethods: List<TransactionMethod>,
32        val constructorParamType: TypeName?) {
33    // parsed dao might have a suffix if it is used in multiple databases.
34    private var suffix: String? = null
35
36    fun setSuffix(newSuffix: String) {
37        if (this.suffix != null) {
38            throw IllegalStateException("cannot set suffix twice")
39        }
40        this.suffix = if (newSuffix == "") "" else "_$newSuffix"
41    }
42
43    val typeName: ClassName by lazy { ClassName.get(element) }
44
45    val shortcutMethods: List<ShortcutMethod> by lazy {
46        deletionMethods + updateMethods
47    }
48
49    private val implClassName by lazy {
50        if (suffix == null) {
51            suffix = ""
52        }
53        val path = arrayListOf<String>()
54        var enclosing = element.enclosingElement
55        while (enclosing is TypeElement) {
56            path.add(ClassName.get(enclosing as TypeElement).simpleName())
57            enclosing = enclosing.enclosingElement
58        }
59        path.reversed().joinToString("_") + "${typeName.simpleName()}${suffix}_Impl"
60    }
61
62    val implTypeName: ClassName by lazy {
63        ClassName.get(typeName.packageName(), implClassName)
64    }
65}
66