started sqlite logic
This commit is contained in:
44
src/data_layer.rs
Normal file
44
src/data_layer.rs
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user