package androidx.room.util; import android.database.AbstractWindowedCursor; import android.database.Cursor; import android.database.sqlite.SQLiteConstraintException; import android.os.CancellationSignal; 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.Deprecated; import kotlin.Metadata; import kotlin.Unit; import kotlin.collections.CollectionsKt; import kotlin.io.CloseableKt; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt; /* compiled from: DBUtil.kt */ @Metadata(d1 = {"\u0000D\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0000\u001a\b\u0010\u0000\u001a\u0004\u0018\u00010\u0001\u001a\u000e\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u0005\u001a\u0016\u0010\u0006\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u00052\u0006\u0010\u0007\u001a\u00020\b\u001a\u0010\u0010\t\u001a\u00020\b2\u0006\u0010\n\u001a\u00020\u000bH\u0002\u001a \u0010\f\u001a\u00020\u000b2\u0006\u0010\u0004\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u0010\u001a\u00020\u0011H\u0007\u001a(\u0010\f\u001a\u00020\u000b2\u0006\u0010\u0004\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u0010\u001a\u00020\u00112\b\u0010\u0012\u001a\u0004\u0018\u00010\u0001\u001a\u000e\u0010\u0013\u001a\u00020\u00142\u0006\u0010\u0015\u001a\u00020\u0016ยจ\u0006\u0017"}, d2 = {"createCancellationSignal", "Landroid/os/CancellationSignal;", "dropFtsSyncTriggers", "", "db", "Landroidx/sqlite/db/SupportSQLiteDatabase;", "foreignKeyCheck", "tableName", "", "processForeignKeyCheckFailure", "cursor", "Landroid/database/Cursor;", "query", "Landroidx/room/RoomDatabase;", "sqLiteQuery", "Landroidx/sqlite/db/SupportSQLiteQuery;", "maybeCopy", "", "signal", "readVersion", "", "databaseFile", "Ljava/io/File;", "room-runtime_release"}, k = 2, mv = {1, 7, 1}, xi = 48) /* loaded from: classes2.dex */ public final class DBUtil { @Deprecated(message = "This is only used in the generated code and shouldn't be called directly.") 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) { Intrinsics.checkNotNullParameter(db, "db"); List createListBuilder = CollectionsKt.createListBuilder(); Cursor query = db.query("SELECT name FROM sqlite_master WHERE type = 'trigger'"); try { Cursor cursor = query; while (cursor.moveToNext()) { createListBuilder.add(cursor.getString(0)); } Unit unit = Unit.INSTANCE; CloseableKt.closeFinally(query, null); for (String triggerName : CollectionsKt.build(createListBuilder)) { Intrinsics.checkNotNullExpressionValue(triggerName, "triggerName"); if (StringsKt.startsWith$default(triggerName, "room_fts_content_sync_", false, 2, (Object) null)) { db.execSQL("DROP TRIGGER IF EXISTS " + triggerName); } } } finally { } } 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 { Cursor cursor = query; if (cursor.getCount() > 0) { throw new SQLiteConstraintException(processForeignKeyCheckFailure(cursor)); } 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 { FileChannel fileChannel = channel; ByteBuffer allocate = ByteBuffer.allocate(4); fileChannel.tryLock(60L, 4L, true); fileChannel.position(60L); if (fileChannel.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)).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()).append("\nNumber of rows in violation: "); sb.append(count).append("\nViolation(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).append("\n"); } String sb2 = sb.toString(); Intrinsics.checkNotNullExpressionValue(sb2, "StringBuilder().apply(builderAction).toString()"); return sb2; } }