blob: 88a660d6506c28d5ffdc7857ebe7a350a0803909 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
use crate::models::actualbudget::{
account::Account, category::Category, date::Date, transaction::Transaction,
zero_budget::ZeroBudget,
};
use sqlite::Connection;
pub fn get_first_transaction_date(connection: &Connection) -> Date {
let mut statement = connection
.prepare("select min(date) as min_date from transactions;")
.unwrap();
match statement.next() {
Ok(sqlite::State::Row) => Date::from_date_row(&statement, "min_date"),
err => panic!("Could not find first transaction date: {:?}", err),
}
}
pub fn get_accounts(connection: &Connection) -> Vec<Account> {
let mut statement = connection.prepare("select * from accounts;").unwrap();
let mut accounts = Vec::<Account>::new();
while let Ok(sqlite::State::Row) = statement.next() {
accounts.push(Account::from_row(&statement));
}
accounts
}
pub fn get_categories(connection: &Connection) -> Vec<Category> {
let mut statement = connection.prepare("select * from categories;").unwrap();
let mut categories = Vec::<Category>::new();
while let Ok(sqlite::State::Row) = statement.next() {
categories.push(Category::from_row(&statement));
}
categories
}
pub fn get_zero_budgets(connection: &Connection) -> Vec<ZeroBudget> {
let mut statement = connection.prepare("select * from zero_budgets;").unwrap();
let mut zero_budgets = Vec::<ZeroBudget>::new();
while let Ok(sqlite::State::Row) = statement.next() {
zero_budgets.push(ZeroBudget::from_row(&statement));
}
zero_budgets
}
pub fn get_category_transfers_account(connection: &Connection) -> Account {
let mut statement = connection
.prepare("select * from accounts where name = 'Category transfers';")
.unwrap();
match statement.next() {
Ok(sqlite::State::Row) => Account::from_row(&statement),
_ => panic!("Could not determine category transfers account"),
}
}
pub fn get_transactions(connection: &Connection) -> Vec<Transaction> {
let mut statement = connection.prepare("select * from transactions;").unwrap();
let mut transactions = Vec::<Transaction>::new();
while let Ok(sqlite::State::Row) = statement.next() {
transactions.push(Transaction::from_row(&statement));
}
transactions
}
|