1 /*
<lambda>null2  * 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.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 /** Data class that holds the schema information about an [androidx.room.Entity]. */
25 @Serializable
26 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
27 open class EntityBundle(
28     @SerialName("tableName") override val tableName: String,
29     @SerialName("createSql") override val createSql: String,
30     @SerialName("fields") override val fields: List<FieldBundle>,
31     @SerialName("primaryKey") override val primaryKey: PrimaryKeyBundle,
32     @SerialName("indices") override val indices: List<IndexBundle> = emptyList(),
33     @SerialName("foreignKeys") override val foreignKeys: List<ForeignKeyBundle> = emptyList()
34 ) : BaseEntityBundle(), SchemaEquality<EntityBundle> {
35 
36     /** Creates the list of SQL queries that are necessary to create this entity. */
37     override fun buildCreateQueries(): List<String> {
38         return buildList {
39             add(createTable())
40             this@EntityBundle.indices.forEach { indexBundle -> add(indexBundle.create(tableName)) }
41         }
42     }
43 
44     override fun isSchemaEqual(other: EntityBundle): Boolean {
45         if (tableName != other.tableName) {
46             return false
47         }
48         return checkSchemaEquality(fieldsByColumnName, other.fieldsByColumnName) &&
49             checkSchemaEquality(primaryKey, other.primaryKey) &&
50             checkSchemaEquality(indices, other.indices) &&
51             checkSchemaEquality(foreignKeys, other.foreignKeys)
52     }
53 }
54