1 /*
<lambda>null2  * Copyright 2018 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.migration.bundle
18 
19 import androidx.annotation.RestrictTo
20 import androidx.room.migration.bundle.SchemaEqualityUtil.checkSchemaEquality
21 import kotlinx.serialization.SerialName
22 import kotlinx.serialization.Serializable
23 
24 /**
25  * Data class that holds the schema information about an [androidx.room.Fts3] or
26  * [androidx.room.Fts4] entity.
27  */
28 @Serializable
29 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
30 class FtsEntityBundle(
31     @SerialName("tableName") override val tableName: String,
32     @SerialName("createSql") override val createSql: String,
33     @SerialName("fields") override val fields: List<FieldBundle>,
34     @SerialName("primaryKey") override val primaryKey: PrimaryKeyBundle,
35     @SerialName("indices") override val indices: List<IndexBundle> = emptyList(),
36     @SerialName("foreignKeys") override val foreignKeys: List<ForeignKeyBundle> = emptyList(),
37     @SerialName("ftsVersion") val ftsVersion: String,
38     @SerialName("ftsOptions") val ftsOptions: FtsOptionsBundle,
39     @SerialName("contentSyncTriggers") val contentSyncSqlTriggers: List<String>
40 ) : BaseEntityBundle(), SchemaEquality<FtsEntityBundle> {
41 
42     /** Creates the list of SQL queries that are necessary to create this entity. */
43     override fun buildCreateQueries(): List<String> {
44         return buildList {
45             add(createTable())
46             addAll(contentSyncSqlTriggers)
47         }
48     }
49 
50     override fun isSchemaEqual(other: FtsEntityBundle): Boolean {
51         if (tableName != other.tableName) {
52             return false
53         }
54         return checkSchemaEquality(fieldsByColumnName, other.fieldsByColumnName) &&
55             checkSchemaEquality(primaryKey, other.primaryKey) &&
56             checkSchemaEquality(indices, other.indices) &&
57             checkSchemaEquality(foreignKeys, other.foreignKeys) &&
58             ftsVersion == other.ftsVersion &&
59             checkSchemaEquality(ftsOptions, other.ftsOptions)
60     }
61 
62     /** Gets the list of shadow table names corresponding to the FTS virtual table. */
63     val shadowTableNames: List<String> by lazy {
64         val currentTable = this@FtsEntityBundle.tableName
65         buildList { SHADOW_TABLE_NAME_SUFFIXES.forEach { suffix -> add(currentTable + suffix) } }
66     }
67 
68     companion object {
69         private val SHADOW_TABLE_NAME_SUFFIXES =
70             listOf("_content", "_segdir", "_segments", "_stat", "_docsize")
71     }
72 }
73