-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use SqlDelight for database implementation instead of Exposed
Expose is a JVM only lib. SqlDelight provides KMP support out of the box. This migrates the internal db implementation to SqlDelight. Work towards resolving issue #3.
- Loading branch information
Showing
15 changed files
with
188 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
lib/src/main/kotlin/me/tb/cashuclient/db/DBBolt11Payment.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,57 @@ | ||
package me.tb.cashuclient.db | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver | ||
import fr.acinq.lightning.utils.getValue | ||
import me.tb.cashuclient.types.Proof | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq | ||
import org.jetbrains.exposed.sql.deleteWhere | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import me.tb.cashulib.Database | ||
|
||
/** | ||
* The default implementation of the [CashuDB] interface, using SQLite. | ||
*/ | ||
public class SQLiteDB : CashuDB { | ||
override fun insertProof(proof: Proof) { | ||
transaction(DBSettings.db) { | ||
SchemaUtils.create(DBProof) | ||
|
||
DBProof.insert { | ||
it[amount] = proof.amount | ||
it[secret] = proof.secret | ||
it[C] = proof.C | ||
it[id] = proof.id | ||
it[script] = null | ||
} | ||
public class SQLiteDB( | ||
private val sqlDriver: SqlDriver = sqlDriver() | ||
) : CashuDB { | ||
|
||
private val database by lazy { | ||
Database.Schema.create(sqlDriver) | ||
Database(sqlDriver) | ||
} | ||
|
||
override fun proofsForAmounts(amounts: List<ULong>): List<Proof> { | ||
return amounts.map { amt -> | ||
val record = database.proofQueries.selectProofByAmount(amt.toLong()).executeAsOne() | ||
Proof( | ||
amount = record.amount.toULong(), | ||
secret = record.secret, | ||
C = record.C, | ||
id = record.id | ||
) | ||
} | ||
} | ||
|
||
override fun insertProof(proof: Proof) { | ||
database.proofQueries.insert( | ||
amount = proof.amount.toLong(), | ||
secret = proof.secret, | ||
C = proof.C, | ||
id = proof.id, | ||
script = proof.script | ||
) | ||
} | ||
|
||
override fun deleteProof(proof: Proof) { | ||
transaction(DBSettings.db) { | ||
SchemaUtils.create(DBProof) | ||
val secretOfProofToDelete = proof.secret | ||
DBProof.deleteWhere { secret eq secretOfProofToDelete } | ||
} | ||
database.proofQueries.deleteById(proof.id) | ||
} | ||
|
||
override fun spendableNoteSizes(): List<ULong> { | ||
return transaction(DBSettings.db) { | ||
SchemaUtils.create(DBProof) | ||
DBProof.selectAll().map { it[DBProof.amount] } | ||
} | ||
return database.proofQueries.selectAll().executeAsList().map { it.amount.toULong() } | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new [SqlDriver] instance that connects to the SQLite database. | ||
*/ | ||
private fun sqlDriver(): SqlDriver { | ||
return JdbcSqliteDriver(url = "jdbc:sqlite:./cashu.sqlite3") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
lib/src/main/sqldelight/me/tb/cashuclient/Bolt11Payment.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- A table to store payment requests made by the mint. | ||
-- Once paid, these will later be used to request tokens. | ||
CREATE TABLE Bolt11Payment( | ||
-- The hash here is really just a secret payment ID used to identify the payment between the mint and the client. | ||
hash TEXT NOT NULL PRIMARY KEY, | ||
pr TEXT NOT NULL, | ||
value INTEGER NOT NULL | ||
); | ||
|
||
-- Using a hash as the identifier for an amount requested and invoice paid, return said amount | ||
getAmountByHash: | ||
SELECT value FROM Bolt11Payment WHERE hash = ?; |
Oops, something went wrong.