started sqlite logic

This commit is contained in:
2025-10-17 08:49:29 -04:00
parent 120cc3e583
commit d5e34d1534
7 changed files with 122 additions and 0 deletions

44
src/data_layer.rs Normal file
View File

@@ -0,0 +1,44 @@
use sqlite::{Connection, State};
use crate::entities::*;
pub fn setup(con: &Connection) {
let _ = con.execute(
"
CREATE TABLE IF NOT EXISTS Accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS TransactionTypes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS Transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
type_id INTEGER NOT NULL,
amount FLOAT NOT NULL,
date DATE NOT NULL,
description TEXT,
FOREIGN KEY(account_id) REFERENCES Accounts(id),
FOREIGN KEY(type_id) REFERENCES TransactionTypes(id)
);
");
}
//pub fn
pub fn get_accounts(con: &Connection) -> Vec<Account> {
let query = "SELECT * FROM Accounts";
let mut statement = con.prepare(query).unwrap();
let mut vec = Vec::<Account>::new();
while let Ok(State::Row) = statement.next() {
vec.push(Account::new(statement.read::<i64, _>("id").unwrap(), statement.read::<String, _>("name").unwrap(), statement.read::<i64, _>("type").unwrap().try_into().unwrap()));
}
return vec;
}