Skip to content

Commit

Permalink
fix: DbConnection cannot be extended (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianIOHK authored Jul 4, 2024
1 parent 4a61989 commit 82b03cc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import org.hyperledger.identus.walletsdk.domain.models.PlutoError
/**
* DbConnection class represents a connection to the database.
*/
actual class DbConnection actual constructor() {
actual var driver: SqlDriver? = null
actual suspend fun connectDb(context: Any?): SqlDriver {
actual class DbConnectionImpl actual constructor() : DbConnection {
actual override var driver: SqlDriver? = null
actual override suspend fun connectDb(context: Any?): SqlDriver {
val androidContext: Context = (context as? Context) ?: throw PlutoError.DatabaseContextError()
val driver = AndroidSqliteDriver(SdkPlutoDb.Schema, androidContext, "prism.db")
this.driver = driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.cash.sqldelight.ColumnAdapter
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.db.AfterVersion
import java.util.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -37,7 +38,6 @@ import org.hyperledger.identus.walletsdk.pluto.backup.models.BackupV0_0_1
import org.hyperledger.identus.walletsdk.pluto.data.DbConnection
import org.hyperledger.identus.walletsdk.pluto.data.isConnected
import org.hyperledger.identus.walletsdk.pollux.models.CredentialRequestMeta
import java.util.UUID
import org.hyperledger.identus.walletsdk.pluto.data.AvailableClaims as AvailableClaimsDB
import org.hyperledger.identus.walletsdk.pluto.data.DID as DIDDB
import org.hyperledger.identus.walletsdk.pluto.data.DIDPair as DIDPairDB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package org.hyperledger.identus.walletsdk.pluto.data

import app.cash.sqldelight.db.SqlDriver

interface DbConnection {
var driver: SqlDriver?

suspend fun connectDb(context: Any?): SqlDriver
}

/**
* DbConnection class represents a connection to the database.
*/
expect class DbConnection() {
var driver: SqlDriver?
expect class DbConnectionImpl() : DbConnection {
override var driver: SqlDriver?

/**
* Establishes a connection to the database.
Expand All @@ -15,7 +21,7 @@ expect class DbConnection() {
*
* @return The SdkPlutoDb instance representing the connection to the database.
*/
suspend fun connectDb(context: Any?): SqlDriver
override suspend fun connectDb(context: Any?): SqlDriver
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import org.hyperledger.identus.walletsdk.SdkPlutoDb

actual class DbConnection actual constructor() {
actual var driver: SqlDriver? = null
actual suspend fun connectDb(context: Any?): SqlDriver {
actual class DbConnectionImpl actual constructor() : DbConnection {
actual override var driver: SqlDriver? = null
actual override suspend fun connectDb(context: Any?): SqlDriver {
val driver = JdbcSqliteDriver("jdbc:sqlite:prism.db")
SdkPlutoDb.Schema.create(driver)
this.driver = driver
Expand Down
2 changes: 2 additions & 0 deletions sampleapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ dependencies {
implementation("androidx.room:room-runtime:2.4.2")
ksp("androidx.room:room-compiler:2.4.2")

implementation("app.cash.sqldelight:sqlite-driver:2.0.1")

// Unit Tests
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
@file:Suppress("ktlint:standard:import-ordering")

package org.hyperledger.identus.walletsdk.sampleapp

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.hyperledger.identus.walletsdk.SdkPlutoDb
import org.hyperledger.identus.walletsdk.apollo.ApolloImpl
import org.hyperledger.identus.walletsdk.castor.CastorImpl
import org.hyperledger.identus.walletsdk.castor.resolvers.PrismDIDApiResolver
Expand All @@ -26,6 +31,7 @@ import org.hyperledger.identus.walletsdk.mercury.MercuryImpl
import org.hyperledger.identus.walletsdk.mercury.resolvers.DIDCommWrapper
import org.hyperledger.identus.walletsdk.pluto.PlutoImpl
import org.hyperledger.identus.walletsdk.pluto.data.DbConnection
import org.hyperledger.identus.walletsdk.pluto.data.DbConnectionImpl
import org.hyperledger.identus.walletsdk.pollux.PolluxImpl
import java.net.UnknownHostException
import java.util.Base64
Expand Down Expand Up @@ -90,7 +96,17 @@ class Sdk {
}

private fun createPluto(): Pluto {
return PlutoImpl(DbConnection())
val customDbConnection = object : DbConnection {
override var driver: SqlDriver? = null

override suspend fun connectDb(context: Any?): SqlDriver {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
SdkPlutoDb.Schema.create(driver)
this.driver = driver
return driver
}
}
return PlutoImpl(DbConnectionImpl())
}

private fun createApollo(): Apollo {
Expand Down

0 comments on commit 82b03cc

Please sign in to comment.