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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use sqlx::postgres::{Postgres, PgPoolOptions};
pub mod asset_handler;
pub mod quote_handler;
pub mod transaction_handler;
pub struct PostgresDB {
pub pool: sqlx::Pool<Postgres>,
}
impl PostgresDB {
pub async fn new(connection_string: &str) -> Result<PostgresDB, sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(connection_string).await?;
Ok(PostgresDB{ pool })
}
pub async fn clean(&self) -> Result<(), sqlx::Error> {
sqlx::query!("DROP TABLE IF EXISTS transactions").execute(&self.pool).await?;
sqlx::query!("DROP TABLE IF EXISTS quotes").execute(&self.pool).await?;
sqlx::query!("DROP TABLE IF EXISTS ticker").execute(&self.pool).await?;
sqlx::query!("DROP TYPE IF EXISTS market_data_source").execute(&self.pool).await?;
sqlx::query!("DROP TABLE IF EXISTS assets").execute(&self.pool).await?;
sqlx::query!("DROP TABLE IF EXISTS rounding_digits").execute(&self.pool).await?;
self.init().await
}
pub async fn init(&self) -> Result<(), sqlx::Error> {
sqlx::query!(
"CREATE TABLE IF NOT EXISTS assets (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
wkn TEXT UNIQUE,
isin TEXT UNIQUE,
note TEXT
)").execute(&self.pool).await?;
sqlx::query!(
"CREATE TABLE IF NOT EXISTS transactions (
id SERIAL PRIMARY KEY,
trans_type TEXT NOT NULL,
asset_id INTEGER,
cash_amount FLOAT8 NOT NULL,
cash_currency TEXT NOT NULL,
cash_date DATE NOT NULL,
related_trans INTEGER,
position FLOAT8,
note TEXT,
FOREIGN KEY(asset_id) REFERENCES assets(id),
FOREIGN KEY(related_trans) REFERENCES transactions(id)
)").execute(&self.pool).await?;
sqlx::query!(
"CREATE TABLE IF NOT EXISTS ticker (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
asset_id INTEGER NOT NULL,
source TEXT NOT NULL,
priority INTEGER NOT NULL,
currency TEXT NOT NULL,
factor FLOAT8 NOT NULL DEFAULT 1.0,
FOREIGN KEY(asset_id) REFERENCES assets(id)
)").execute(&self.pool).await?;
sqlx::query!(
"CREATE TABLE IF NOT EXISTS quotes (
id SERIAL PRIMARY KEY,
ticker_id INTEGER NOT NULL,
price FLOAT8 NOT NULL,
time TIMESTAMP WITH TIME ZONE NOT NULL,
volume FLOAT8,
FOREIGN KEY(ticker_id) REFERENCES ticker(id)
)").execute(&self.pool).await?;
sqlx::query!(
"CREATE TABLE IF NOT EXISTS rounding_digits (
id SERIAL PRIMARY KEY,
currency TEXT NOT NULL UNIQUE,
digits INT NOT NULL
)").execute(&self.pool).await?;
Ok(())
}
}