-
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.
Add CashuDB interface and SQLiteDB implementation
- Loading branch information
1 parent
dd310d2
commit 54ba410
Showing
3 changed files
with
64 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.tb.cashuclient.db | ||
|
||
import me.tb.cashuclient.types.Proof | ||
|
||
public interface CashuDB { | ||
public fun insertProof(proof: Proof): Unit | ||
|
||
public fun deleteProof(proof: Proof): Unit | ||
|
||
public fun spendableNoteSizes(): List<ULong> | ||
} |
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,40 @@ | ||
package me.tb.cashuclient.db | ||
|
||
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 | ||
|
||
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 | ||
} | ||
} | ||
} | ||
|
||
override fun deleteProof(proof: Proof) { | ||
transaction(DBSettings.db) { | ||
SchemaUtils.create(DBProof) | ||
val secretOfProofToDelete = proof.secret | ||
DBProof.deleteWhere { secret eq secretOfProofToDelete } | ||
} | ||
} | ||
|
||
override fun spendableNoteSizes(): List<ULong> { | ||
return transaction(DBSettings.db) { | ||
SchemaUtils.create(DBProof) | ||
DBProof.selectAll().map { it[DBProof.amount] } | ||
} | ||
} | ||
} |