TableInfo.java
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.room.util;
import android.database.Cursor;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.room.ColumnInfo;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* A data class that holds the information about a table.
* <p>
* It directly maps to the result of {@code PRAGMA table_info(<table_name>)}. Check the
* <a href="http://www.sqlite.org/pragma.html#pragma_table_info">PRAGMA table_info</a>
* documentation for more details.
* <p>
* Even though SQLite column names are case insensitive, this class uses case sensitive matching.
*
* @hide
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
@SuppressWarnings({"WeakerAccess", "unused", "TryFinallyCanBeTryWithResources",
"SimplifiableIfStatement"})
// if you change this class, you must change TableInfoWriter.kt
public class TableInfo {
/**
* The table name.
*/
public final String name;
/**
* Unmodifiable map of columns keyed by column name.
*/
public final Map<String, Column> columns;
public final Set<ForeignKey> foreignKeys;
/**
* Sometimes, Index information is not available (older versions). If so, we skip their
* verification.
*/
@Nullable
public final Set<Index> indices;
@SuppressWarnings("unused")
public TableInfo(String name, Map<String, Column> columns, Set<ForeignKey> foreignKeys,
Set<Index> indices) {
this.name = name;
this.columns = Collections.unmodifiableMap(columns);
this.foreignKeys = Collections.unmodifiableSet(foreignKeys);
this.indices = indices == null ? null : Collections.unmodifiableSet(indices);
}
/**
* For backward compatibility with dbs created with older versions.
*/
@SuppressWarnings("unused")
public TableInfo(String name, Map<String, Column> columns, Set<ForeignKey> foreignKeys) {
this(name, columns, foreignKeys, Collections.<Index>emptySet());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TableInfo tableInfo = (TableInfo) o;
if (name != null ? !name.equals(tableInfo.name) : tableInfo.name != null) return false;
if (columns != null ? !columns.equals(tableInfo.columns) : tableInfo.columns != null) {
return false;
}
if (foreignKeys != null ? !foreignKeys.equals(tableInfo.foreignKeys)
: tableInfo.foreignKeys != null) {
return false;
}
if (indices == null || tableInfo.indices == null) {
// if one us is missing index information, seems like we couldn't acquire the
// information so we better skip.
return true;
}
return indices.equals(tableInfo.indices);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (columns != null ? columns.hashCode() : 0);
result = 31 * result + (foreignKeys != null ? foreignKeys.hashCode() : 0);
// skip index, it is not reliable for comparison.
return result;
}
@Override
public String toString() {
return "TableInfo{"
+ "name='" + name + '