From e0e32d7a05095ec4ccfa98402ab97f44e773dc84 Mon Sep 17 00:00:00 2001 From: Rohan Dhruva Date: Tue, 19 Dec 2023 03:11:02 -0700 Subject: [PATCH] Allow custom SupportSQLiteOpenHelper.Callback in the SqlNormalizedCacheFactory (#5488) * Allow custom SupportSQLiteOpenHelper.Callback in the SqlNormalizedCacheFactory * Use getSchema instead of schema * Only expose onConfigure for now * Update api file --------- Co-authored-by: Rohan Dhruva --- .../api/android/apollo-normalized-cache-sqlite.api | 5 +++-- .../cache/normalized/sql/SqlNormalizedCacheFactory.kt | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libraries/apollo-normalized-cache-sqlite/api/android/apollo-normalized-cache-sqlite.api b/libraries/apollo-normalized-cache-sqlite/api/android/apollo-normalized-cache-sqlite.api index bcbd6fce132..9d5cd4f473d 100644 --- a/libraries/apollo-normalized-cache-sqlite/api/android/apollo-normalized-cache-sqlite.api +++ b/libraries/apollo-normalized-cache-sqlite/api/android/apollo-normalized-cache-sqlite.api @@ -24,8 +24,9 @@ public final class com/apollographql/apollo3/cache/normalized/sql/SqlNormalizedC public fun (Landroid/content/Context;)V public fun (Landroid/content/Context;Ljava/lang/String;)V public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;)V - public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Z)V - public synthetic fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;)V + public fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;Z)V + public synthetic fun (Landroid/content/Context;Ljava/lang/String;Landroidx/sqlite/db/SupportSQLiteOpenHelper$Factory;Lkotlin/jvm/functions/Function1;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Ljava/lang/String;)V public synthetic fun (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun create ()Lcom/apollographql/apollo3/cache/normalized/api/NormalizedCache; diff --git a/libraries/apollo-normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/apollo3/cache/normalized/sql/SqlNormalizedCacheFactory.kt b/libraries/apollo-normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/apollo3/cache/normalized/sql/SqlNormalizedCacheFactory.kt index 07f4d344e94..07361365adb 100644 --- a/libraries/apollo-normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/apollo3/cache/normalized/sql/SqlNormalizedCacheFactory.kt +++ b/libraries/apollo-normalized-cache-sqlite/src/androidMain/kotlin/com/apollographql/apollo3/cache/normalized/sql/SqlNormalizedCacheFactory.kt @@ -1,6 +1,7 @@ package com.apollographql.apollo3.cache.normalized.sql import android.content.Context +import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteOpenHelper import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory import com.apollographql.apollo3.cache.normalized.api.NormalizedCacheFactory @@ -18,6 +19,8 @@ actual class SqlNormalizedCacheFactory internal constructor( /** * @param [name] Name of the database file, or null for an in-memory database (as per Android framework implementation). * @param [factory] Factory class to create instances of [SupportSQLiteOpenHelper] + * @param [configure] Optional callback, called when the database connection is being configured, to enable features such as + * write-ahead logging or foreign key support. It should not modify the database except to configure it. * @param [useNoBackupDirectory] Sets whether to use a no backup directory or not. */ @JvmOverloads @@ -25,6 +28,7 @@ actual class SqlNormalizedCacheFactory internal constructor( context: Context, name: String? = "apollo.db", factory: SupportSQLiteOpenHelper.Factory = FrameworkSQLiteOpenHelperFactory(), + configure: ((SupportSQLiteDatabase) -> Unit)? = null, useNoBackupDirectory: Boolean = false, ) : this( AndroidSqliteDriver( @@ -32,6 +36,12 @@ actual class SqlNormalizedCacheFactory internal constructor( context.applicationContext, name, factory, + object : AndroidSqliteDriver.Callback(getSchema()) { + override fun onConfigure(db: SupportSQLiteDatabase) { + super.onConfigure(db) + configure?.invoke(db) + } + }, useNoBackupDirectory = useNoBackupDirectory ), )