package androidx.room;

import androidx.media3.common.C;
import androidx.room.RoomDatabase;
import java.util.Iterator;
import java.util.List;
import kotlin.Pair;
import kotlin.Result;
import kotlin.collections.builders.ListBuilder;

/* JADX INFO: loaded from: classes.dex */
public abstract class c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public boolean f4007a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public boolean f4008b;

    public static final void a(c cVar, k3.a aVar) throws Exception {
        Object objB;
        f0 f0Var = (f0) cVar;
        e eVar = f0Var.f4083c;
        RoomDatabase.JournalMode journalMode = eVar.f4066g;
        RoomDatabase.JournalMode journalMode2 = RoomDatabase.JournalMode.f3997c;
        if (journalMode == journalMode2) {
            ua.b.t("PRAGMA journal_mode = WAL", aVar);
        } else {
            ua.b.t("PRAGMA journal_mode = TRUNCATE", aVar);
        }
        if (eVar.f4066g == journalMode2) {
            ua.b.t("PRAGMA synchronous = NORMAL", aVar);
        } else {
            ua.b.t("PRAGMA synchronous = FULL", aVar);
        }
        b(aVar);
        k3.c cVarJ = aVar.J("PRAGMA user_version");
        try {
            cVarJ.G();
            int i10 = (int) cVarJ.getLong(0);
            ua.b.h(cVarJ, null);
            if (i10 != f0Var.f4084d.getVersion()) {
                ua.b.t("BEGIN EXCLUSIVE TRANSACTION", aVar);
                try {
                    if (i10 == 0) {
                        cVar.d(aVar);
                    } else {
                        cVar.e(aVar, i10, ((f0) cVar).f4084d.getVersion());
                    }
                    ua.b.t("PRAGMA user_version = " + ((f0) cVar).f4084d.getVersion(), aVar);
                    objB = kd.e.f29109a;
                } catch (Throwable th) {
                    objB = kotlin.a.b(th);
                }
                if (!(objB instanceof Result.Failure)) {
                    ua.b.t("END TRANSACTION", aVar);
                }
                Throwable thA = Result.a(objB);
                if (thA != null) {
                    ua.b.t("ROLLBACK TRANSACTION", aVar);
                    throw thA;
                }
            }
            cVar.f(aVar);
        } finally {
        }
    }

    public static void b(k3.a aVar) throws Exception {
        k3.c cVarJ = aVar.J("PRAGMA busy_timeout");
        try {
            cVarJ.G();
            long j10 = cVarJ.getLong(0);
            ua.b.h(cVarJ, null);
            if (j10 < C.DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION_MS) {
                ua.b.t("PRAGMA busy_timeout = 3000", aVar);
            }
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                ua.b.h(cVarJ, th);
                throw th2;
            }
        }
    }

    public final void c(k3.a aVar) throws Exception {
        if (((f0) this).f4083c.f4066g == RoomDatabase.JournalMode.f3997c) {
            ua.b.t("PRAGMA synchronous = NORMAL", aVar);
        } else {
            ua.b.t("PRAGMA synchronous = FULL", aVar);
        }
    }

    public final void d(k3.a aVar) throws Exception {
        nb.g.i(aVar, "connection");
        k3.c cVarJ = aVar.J("SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'");
        try {
            boolean z10 = false;
            if (cVarJ.G()) {
                if (cVarJ.getLong(0) == 0) {
                    z10 = true;
                }
            }
            ua.b.h(cVarJ, null);
            f0 f0Var = (f0) this;
            n0 n0Var = f0Var.f4084d;
            n0Var.createAllTables(aVar);
            if (!z10) {
                m0 m0VarOnValidateSchema = n0Var.onValidateSchema(aVar);
                if (!m0VarOnValidateSchema.f4115a) {
                    throw new IllegalStateException(("Pre-packaged database has an invalid schema: " + m0VarOnValidateSchema.f4116b).toString());
                }
            }
            g(aVar);
            n0Var.onCreate(aVar);
            Iterator it = f0Var.f4085e.iterator();
            while (it.hasNext()) {
                ((j0) it.next()).onCreate(aVar);
            }
        } finally {
        }
    }

    public final void e(k3.a aVar, int i10, int i11) throws Exception {
        nb.g.i(aVar, "connection");
        f0 f0Var = (f0) this;
        e eVar = f0Var.f4083c;
        List listA = androidx.room.util.d.a(eVar.f4063d, i10, i11);
        n0 n0Var = f0Var.f4084d;
        if (listA != null) {
            n0Var.onPreMigrate(aVar);
            Iterator it = listA.iterator();
            while (it.hasNext()) {
                ((g3.a) it.next()).migrate(aVar);
            }
            m0 m0VarOnValidateSchema = n0Var.onValidateSchema(aVar);
            if (m0VarOnValidateSchema.f4115a) {
                n0Var.onPostMigrate(aVar);
                g(aVar);
                return;
            } else {
                throw new IllegalStateException(("Migration didn't properly handle: " + m0VarOnValidateSchema.f4116b).toString());
            }
        }
        if (androidx.room.util.d.b(eVar, i10, i11)) {
            throw new IllegalStateException(("A migration from " + i10 + " to " + i11 + " was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* functions.").toString());
        }
        if (eVar.f4078s) {
            k3.c cVarJ = aVar.J("SELECT name, type FROM sqlite_master WHERE type = 'table' OR type = 'view'");
            try {
                ListBuilder listBuilder = new ListBuilder();
                while (cVarJ.G()) {
                    String strA = cVarJ.A(0);
                    if (!kotlin.text.h.j0(strA, "sqlite_", false) && !nb.g.c(strA, "android_metadata")) {
                        listBuilder.add(new Pair(strA, Boolean.valueOf(nb.g.c(cVarJ.A(1), "view"))));
                    }
                }
                listBuilder.m();
                ua.b.h(cVarJ, null);
                Iterator it2 = listBuilder.iterator();
                while (true) {
                    ld.a aVar2 = (ld.a) it2;
                    if (!aVar2.hasNext()) {
                        break;
                    }
                    Pair pair = (Pair) aVar2.next();
                    String str = (String) pair.getFirst();
                    if (((Boolean) pair.getSecond()).booleanValue()) {
                        ua.b.t("DROP VIEW IF EXISTS " + str, aVar);
                    } else {
                        ua.b.t("DROP TABLE IF EXISTS " + str, aVar);
                    }
                }
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    ua.b.h(cVarJ, th);
                    throw th2;
                }
            }
        } else {
            n0Var.dropAllTables(aVar);
        }
        Iterator it3 = f0Var.f4085e.iterator();
        while (it3.hasNext()) {
            ((j0) it3.next()).onDestructiveMigration(aVar);
        }
        n0Var.createAllTables(aVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x0024  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f(k3.a aVar) throws Exception {
        boolean z10;
        Object objB;
        m0 m0VarOnValidateSchema;
        nb.g.i(aVar, "connection");
        k3.c cVarJ = aVar.J("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'room_master_table'");
        try {
            if (cVarJ.G()) {
                z10 = cVarJ.getLong(0) != 0;
            }
            ua.b.h(cVarJ, null);
            if (z10) {
                cVarJ = aVar.J("SELECT identity_hash FROM room_master_table WHERE id = 42 LIMIT 1");
                try {
                    String strA = cVarJ.G() ? cVarJ.A(0) : null;
                    ua.b.h(cVarJ, null);
                    n0 n0Var = ((f0) this).f4084d;
                    if (!nb.g.c(n0Var.getIdentityHash(), strA) && !nb.g.c(n0Var.getLegacyIdentityHash(), strA)) {
                        throw new IllegalStateException(("Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: " + n0Var.getIdentityHash() + ", found: " + strA).toString());
                    }
                } finally {
                }
            } else {
                ua.b.t("BEGIN EXCLUSIVE TRANSACTION", aVar);
                try {
                    m0VarOnValidateSchema = ((f0) this).f4084d.onValidateSchema(aVar);
                } catch (Throwable th) {
                    objB = kotlin.a.b(th);
                }
                if (!m0VarOnValidateSchema.f4115a) {
                    throw new IllegalStateException(("Pre-packaged database has an invalid schema: " + m0VarOnValidateSchema.f4116b).toString());
                }
                ((f0) this).f4084d.onPostMigrate(aVar);
                g(aVar);
                objB = kd.e.f29109a;
                if (!(objB instanceof Result.Failure)) {
                    ua.b.t("END TRANSACTION", aVar);
                }
                Throwable thA = Result.a(objB);
                if (thA != null) {
                    ua.b.t("ROLLBACK TRANSACTION", aVar);
                    throw thA;
                }
            }
            f0 f0Var = (f0) this;
            f0Var.f4084d.onOpen(aVar);
            Iterator it = f0Var.f4085e.iterator();
            while (it.hasNext()) {
                ((j0) it.next()).onOpen(aVar);
            }
            this.f4007a = true;
        } finally {
            try {
                throw th;
            } finally {
            }
        }
    }

    public final void g(k3.a aVar) throws Exception {
        ua.b.t("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", aVar);
        String identityHash = ((f0) this).f4084d.getIdentityHash();
        nb.g.i(identityHash, "hash");
        ua.b.t("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + identityHash + "')", aVar);
    }
}
