Split out db logic - #24
Closed
eupn wants to merge 4 commits into
Closed
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the stats database handling by extracting SQLite logic out of the CashCode driver into a dedicated CashCodeDb module, and wires the shared DB handle into both the bill acceptor and coin acceptor paths.
Changes:
- Added a new
CashCodeDbmodule to own DB connection setup and persistence routines. - Updated
CashCodeand the bill acceptor initialization to use an injectedCashCodeDbinstead of opening the DB internally. - Added coin persistence (and a
payment_log) by recording accepted coin events into the stats DB.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main.rs | Opens a shared CashCodeDb and passes it into bill/coin acceptor initialization; records coins on acceptance. |
| src/cashcode.rs | Removes embedded SQLite logic and delegates bill stats operations to CashCodeDb. |
| src/cashcode_db.rs | Introduces a new DB wrapper with schema init and helpers to record bills/coins and compute totals. |
Comments suppressed due to low confidence (1)
src/cashcode_db.rs:79
record_coinperforms multiple writes (updatingaccepted_coinsand inserting intopayment_log) without a transaction. Using a transaction here avoids partial updates if one of the statements fails mid-way.
pub fn record_coin(&self, value: i32) -> Result<(), CashCodeError> {
let db = self.conn.lock().unwrap();
db.execute(
"INSERT INTO accepted_coins (nominal, quantity) VALUES (?1, 1)
ON CONFLICT(nominal) DO UPDATE SET quantity = quantity + 1",
[value],
)?;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| #[derive(Clone)] | ||
| pub struct CashCodeDb { | ||
| pub conn: Arc<Mutex<Connection>>, |
Comment on lines
+50
to
+56
| pub fn record_bill(&self, nominal: BillNominal) -> Result<(), CashCodeError> { | ||
| let db = self.conn.lock().unwrap(); | ||
| db.execute( | ||
| "INSERT INTO accepted_bills (nominal, quantity) VALUES (?1, 1) | ||
| ON CONFLICT(nominal) DO UPDATE SET quantity = quantity + 1", | ||
| [nominal.value()], | ||
| )?; |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.