-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.rs
More file actions
124 lines (103 loc) · 3.19 KB
/
sqlite.rs
File metadata and controls
124 lines (103 loc) · 3.19 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use async_trait::async_trait;
use sqlx::{migrate::MigrateDatabase, sqlite::SqlitePoolOptions, Sqlite, SqlitePool};
use crate::{
core::{Index, MetadataDatabase, NewIndex},
errors::Error,
};
pub(crate) struct Database(SqlitePool);
impl Database {
pub(crate) async fn create() -> Self {
let db_url = "sqlite://data/database.sqlite";
if !Sqlite::database_exists(db_url)
.await
.unwrap_or_else(|e| panic!("Cannot check database existance at {db_url} ({e})"))
{
Sqlite::create_database(db_url)
.await
.unwrap_or_else(|e| panic!("Cannot create database {db_url} ({e})"));
}
let pool = SqlitePoolOptions::new()
.connect(db_url)
.await
.unwrap_or_else(|e| panic!("Cannot connect to database at {db_url} ({e})"));
sqlx::migrate!()
.run(&pool)
.await
.unwrap_or_else(|e| panic!("Cannot run migration on database at {db_url} ({e})"));
Database(pool)
}
}
#[async_trait]
impl MetadataDatabase for Database {
async fn get_indexes(&self) -> Result<Vec<Index>, Error> {
let mut db = self.0.acquire().await?;
Ok(sqlx::query_as!(
Index,
r#"
SELECT
*,
null as "size: _"
FROM indexes
ORDER BY created_at DESC"#,
)
.fetch_all(&mut db)
.await?)
}
async fn get_index(&self, id: &str) -> Result<Option<Index>, Error> {
let mut db = self.0.acquire().await?;
let index = sqlx::query_as!(
Index,
r#"
SELECT
*,
null as "size: _"
FROM indexes
WHERE id = $1
"#,
id,
)
.fetch_optional(&mut db)
.await?;
Ok(index)
}
async fn delete_index(&self, id: &str) -> Result<(), Error> {
let mut db = self.0.acquire().await?;
sqlx::query_as!(Index, r#"DELETE FROM indexes WHERE id = $1"#, id,)
.execute(&mut db)
.await?;
Ok(())
}
async fn create_index(&self, new_index: NewIndex) -> Result<Index, Error> {
let mut db = self.0.acquire().await?;
let Id { id } = sqlx::query_as!(
Id,
r#"INSERT INTO indexes (
id,
name,
fetch_entries_key,
fetch_chains_key,
upsert_entries_key,
insert_chains_key
) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id"#,
new_index.id,
new_index.name,
new_index.fetch_entries_key,
new_index.fetch_chains_key,
new_index.upsert_entries_key,
new_index.insert_chains_key,
)
.fetch_one(&mut db)
.await?;
Ok(sqlx::query_as!(
Index,
r#"SELECT *, null as "size: _" FROM indexes WHERE id = $1"#,
id
)
.fetch_one(&mut db)
.await?)
}
}
struct Id {
// The column is mark as `NOT NULL` but SQLx seems to not understand it.
id: Option<String>,
}