package androidx.room.util; import android.database.AbstractWindowedCursor; import android.database.Cursor; import android.database.sqlite.SQLiteConstraintException; import android.os.CancellationSignal; import androidx.annotation.RestrictTo; import androidx.room.RoomDatabase; import androidx.sqlite.db.SupportSQLiteCompat; import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.sqlite.db.SupportSQLiteQuery; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import kotlin.Unit; import kotlin.collections.CollectionsKt__CollectionsJVMKt; import kotlin.io.CloseableKt; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.SourceDebugExtension; import kotlin.text.StringsKt__StringsJVMKt; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) @SourceDebugExtension({"SMAP\nDBUtil.kt\nKotlin\n*S Kotlin\n*F\n+ 1 DBUtil.kt\nandroidx/room/util/DBUtil\n+ 2 CursorUtil.kt\nandroidx/room/util/CursorUtil\n+ 3 _Collections.kt\nkotlin/collections/CollectionsKt___CollectionsKt\n*L\n1#1,214:1\n145#2,7:215\n145#2,7:224\n1855#3,2:222\n*S KotlinDebug\n*F\n+ 1 DBUtil.kt\nandroidx/room/util/DBUtil\n*L\n100#1:215,7\n121#1:224,7\n107#1:222,2\n*E\n"}) /* loaded from: classes.dex */ public final class DBUtil { public static final Cursor query(RoomDatabase db, SupportSQLiteQuery sqLiteQuery, boolean z) { Intrinsics.checkNotNullParameter(db, "db"); Intrinsics.checkNotNullParameter(sqLiteQuery, "sqLiteQuery"); return query(db, sqLiteQuery, z, null); } public static final Cursor query(RoomDatabase db, SupportSQLiteQuery sqLiteQuery, boolean z, CancellationSignal cancellationSignal) { Intrinsics.checkNotNullParameter(db, "db"); Intrinsics.checkNotNullParameter(sqLiteQuery, "sqLiteQuery"); Cursor query = db.query(sqLiteQuery, cancellationSignal); if (!z || !(query instanceof AbstractWindowedCursor)) { return query; } AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor) query; int count = abstractWindowedCursor.getCount(); return (abstractWindowedCursor.hasWindow() ? abstractWindowedCursor.getWindow().getNumRows() : count) < count ? CursorUtil.copyAndClose(query) : query; } public static final void dropFtsSyncTriggers(SupportSQLiteDatabase db) { List createListBuilder; List build; Intrinsics.checkNotNullParameter(db, "db"); createListBuilder = CollectionsKt__CollectionsJVMKt.createListBuilder(); Cursor query = db.query("SELECT name FROM sqlite_master WHERE type = 'trigger'"); while (query.moveToNext()) { try { createListBuilder.add(query.getString(0)); } finally { } } Unit unit = Unit.INSTANCE; CloseableKt.closeFinally(query, null); build = CollectionsKt__CollectionsJVMKt.build(createListBuilder); for (String triggerName : build) { Intrinsics.checkNotNullExpressionValue(triggerName, "triggerName"); if (StringsKt__StringsJVMKt.startsWith$default(triggerName, "room_fts_content_sync_", false, 2, null)) { db.execSQL("DROP TRIGGER IF EXISTS " + triggerName); } } } public static final void foreignKeyCheck(SupportSQLiteDatabase db, String tableName) { Intrinsics.checkNotNullParameter(db, "db"); Intrinsics.checkNotNullParameter(tableName, "tableName"); Cursor query = db.query("PRAGMA foreign_key_check(`" + tableName + "`)"); try { if (query.getCount() > 0) { throw new SQLiteConstraintException(processForeignKeyCheckFailure(query)); } Unit unit = Unit.INSTANCE; CloseableKt.closeFinally(query, null); } catch (Throwable th) { try { throw th; } catch (Throwable th2) { CloseableKt.closeFinally(query, th); throw th2; } } } public static final int readVersion(File databaseFile) throws IOException { Intrinsics.checkNotNullParameter(databaseFile, "databaseFile"); FileChannel channel = new FileInputStream(databaseFile).getChannel(); try { ByteBuffer allocate = ByteBuffer.allocate(4); channel.tryLock(60L, 4L, true); channel.position(60L); if (channel.read(allocate) != 4) { throw new IOException("Bad database header, unable to read 4 bytes at offset 60"); } allocate.rewind(); int i = allocate.getInt(); CloseableKt.closeFinally(channel, null); return i; } catch (Throwable th) { try { throw th; } catch (Throwable th2) { CloseableKt.closeFinally(channel, th); throw th2; } } } public static final CancellationSignal createCancellationSignal() { return SupportSQLiteCompat.Api16Impl.createCancellationSignal(); } private static final String processForeignKeyCheckFailure(Cursor cursor) { StringBuilder sb = new StringBuilder(); int count = cursor.getCount(); LinkedHashMap linkedHashMap = new LinkedHashMap(); while (cursor.moveToNext()) { if (cursor.isFirst()) { sb.append("Foreign key violation(s) detected in '"); sb.append(cursor.getString(0)); sb.append("'.\n"); } String constraintIndex = cursor.getString(3); if (!linkedHashMap.containsKey(constraintIndex)) { Intrinsics.checkNotNullExpressionValue(constraintIndex, "constraintIndex"); String string = cursor.getString(2); Intrinsics.checkNotNullExpressionValue(string, "cursor.getString(2)"); linkedHashMap.put(constraintIndex, string); } } sb.append("Number of different violations discovered: "); sb.append(linkedHashMap.keySet().size()); sb.append("\n"); sb.append("Number of rows in violation: "); sb.append(count); sb.append("\n"); sb.append("Violation(s) detected in the following constraint(s):\n"); for (Map.Entry entry : linkedHashMap.entrySet()) { String str = (String) entry.getKey(); String str2 = (String) entry.getValue(); sb.append("\tParent Table = "); sb.append(str2); sb.append(", Foreign Key Constraint Index = "); sb.append(str); sb.append("\n"); } String sb2 = sb.toString(); Intrinsics.checkNotNullExpressionValue(sb2, "StringBuilder().apply(builderAction).toString()"); return sb2; } }