✨ finalize ui
This commit is contained in:
parent
5ce50b76f5
commit
7ba30045a0
35 changed files with 4498 additions and 235 deletions
93
src/lib.rs
93
src/lib.rs
|
@ -1,5 +1,7 @@
|
|||
#![feature(const_vec_string_slice)]
|
||||
|
||||
use rocket::Data;
|
||||
use sqlx::FromRow;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
pub mod asset;
|
||||
|
@ -46,3 +48,94 @@ macro_rules! get_pg {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn transpose<T: Clone>(matrix: Vec<Vec<T>>) -> Vec<Vec<T>> {
|
||||
if matrix.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let row_len = matrix[0].len();
|
||||
let mut transposed = vec![Vec::with_capacity(matrix.len()); row_len];
|
||||
|
||||
for row in matrix {
|
||||
for (i, elem) in row.into_iter().enumerate() {
|
||||
transposed[i].push(elem);
|
||||
}
|
||||
}
|
||||
|
||||
transposed
|
||||
}
|
||||
|
||||
// TODO : More types
|
||||
#[derive(Clone)]
|
||||
pub enum DatabaseType {
|
||||
TEXT(String),
|
||||
INTEGER(i32),
|
||||
}
|
||||
|
||||
impl DatabaseType {
|
||||
pub fn type_name(&self) -> &str {
|
||||
match self {
|
||||
Self::TEXT(_) => "TEXT",
|
||||
Self::INTEGER(_) => "INTEGER",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_integer(self) -> i32 {
|
||||
match self {
|
||||
DatabaseType::INTEGER(result) => return result,
|
||||
_ => panic!("Wrong DB type"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_text(self) -> String {
|
||||
match self {
|
||||
DatabaseType::TEXT(result) => return result,
|
||||
_ => panic!("Wrong DB type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn batch_insert(table: &str, values: &[String], entries: Vec<Vec<DatabaseType>>) {
|
||||
assert_eq!(values.len(), entries.first().unwrap().len());
|
||||
|
||||
let mut cmd = format!("INSERT INTO {table} (");
|
||||
|
||||
cmd.push_str(&values.join(", "));
|
||||
|
||||
cmd.push_str(") SELECT * FROM UNNEST(");
|
||||
|
||||
let entries = transpose(entries);
|
||||
|
||||
for i in 0..values.len() {
|
||||
let t = entries.get(i).unwrap().first().unwrap().type_name();
|
||||
if i == (entries.len() - 1) {
|
||||
cmd.push_str(&format!("${}::{t}[]", i + 1));
|
||||
} else {
|
||||
cmd.push_str(&format!("${}::{t}[],", i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.push_str(")");
|
||||
|
||||
log::debug!("Executing batch query: {cmd}");
|
||||
|
||||
let mut query = sqlx::query(&cmd);
|
||||
|
||||
for e in entries {
|
||||
let first = e.first().unwrap();
|
||||
match first {
|
||||
DatabaseType::TEXT(_) => {
|
||||
query = query.bind(e.into_iter().map(|x| x.as_text()).collect::<Vec<_>>());
|
||||
}
|
||||
DatabaseType::INTEGER(_) => {
|
||||
query = query.bind(e.into_iter().map(|x| x.as_integer()).collect::<Vec<_>>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query.execute(get_pg!()).await.unwrap();
|
||||
}
|
||||
|
||||
// TODO : LibraryIndex
|
||||
// index, cleanup, add_one, remove_one, get, exists, query
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue