Skip to content

Split out db logic - #24

Closed
eupn wants to merge 4 commits into
mainfrom
split-db-logic
Closed

Split out db logic#24
eupn wants to merge 4 commits into
mainfrom
split-db-logic

Conversation

@eupn

@eupn eupn commented May 17, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@eupn
eupn requested review from Copilot and gmglbn-0 May 17, 2026 19:14
@eupn eupn self-assigned this May 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CashCodeDb module to own DB connection setup and persistence routines.
  • Updated CashCode and the bill acceptor initialization to use an injected CashCodeDb instead 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_coin performs multiple writes (updating accepted_coins and inserting into payment_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.

Comment thread src/cashcode_db.rs

#[derive(Clone)]
pub struct CashCodeDb {
pub conn: Arc<Mutex<Connection>>,
Comment thread src/cashcode_db.rs
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()],
)?;
@eupn eupn closed this Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants