Generalized function name
This commit is contained in:
parent
e6737d3b4e
commit
7b1cbcb0f0
21
src/db.rs
21
src/db.rs
@ -1,8 +1,7 @@
|
|||||||
|
use rusqlite::types::Value;
|
||||||
use rusqlite::{Connection, Result};
|
use rusqlite::{Connection, Result};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use rusqlite::types::Value;
|
|
||||||
|
|
||||||
pub struct Db {
|
pub struct Db {
|
||||||
conn: Mutex<Connection>,
|
conn: Mutex<Connection>,
|
||||||
}
|
}
|
||||||
@ -14,10 +13,13 @@ impl Db {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_users(&self) -> Result<(Vec<String>, Vec<Vec<String>>)> {
|
pub fn get_table(&self, table: &str) -> Result<(Vec<String>, Vec<Vec<String>>)> {
|
||||||
let conn = self.conn.lock().unwrap();
|
let conn = self.conn.lock().unwrap();
|
||||||
|
|
||||||
let mut stmt = conn.prepare("SELECT * FROM users")?;
|
// WARNING: table name is interpolated → trust only internal input
|
||||||
|
let query = format!("SELECT * FROM {table}");
|
||||||
|
|
||||||
|
let mut stmt = conn.prepare(&query)?;
|
||||||
|
|
||||||
let columns = stmt
|
let columns = stmt
|
||||||
.column_names()
|
.column_names()
|
||||||
@ -28,7 +30,7 @@ impl Db {
|
|||||||
let col_count = stmt.column_count();
|
let col_count = stmt.column_count();
|
||||||
|
|
||||||
let rows_iter = stmt.query_map([], move |row| {
|
let rows_iter = stmt.query_map([], move |row| {
|
||||||
let mut r = Vec::new();
|
let mut r = Vec::with_capacity(col_count);
|
||||||
|
|
||||||
for i in 0..col_count {
|
for i in 0..col_count {
|
||||||
let val: Value = row.get(i)?;
|
let val: Value = row.get(i)?;
|
||||||
@ -37,8 +39,8 @@ impl Db {
|
|||||||
Value::Null => "<null>".to_string(),
|
Value::Null => "<null>".to_string(),
|
||||||
Value::Integer(i) => i.to_string(),
|
Value::Integer(i) => i.to_string(),
|
||||||
Value::Real(f) => f.to_string(),
|
Value::Real(f) => f.to_string(),
|
||||||
Value::Text(t) => t, // already String
|
Value::Text(t) => t,
|
||||||
Value::Blob(_) => "<blob>".to_string(),
|
Value::Blob(b) => format!("<blob {} bytes>", b.len()),
|
||||||
};
|
};
|
||||||
|
|
||||||
r.push(s);
|
r.push(s);
|
||||||
@ -47,10 +49,7 @@ impl Db {
|
|||||||
Ok(r)
|
Ok(r)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mut rows = Vec::new();
|
let rows = rows_iter.collect::<Result<Vec<_>>>()?;
|
||||||
for r in rows_iter {
|
|
||||||
rows.push(r?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((columns, rows))
|
Ok((columns, rows))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use askama::Template; // REQUIRED
|
use askama::Template;
|
||||||
|
|
||||||
use axum::{extract::State, response::Html};
|
use axum::{extract::State, response::Html};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -14,10 +14,11 @@ pub struct TableTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn index(State(db): State<Arc<Db>>) -> Html<String> {
|
pub async fn index(State(db): State<Arc<Db>>) -> Html<String> {
|
||||||
let (columns, rows) = db.get_users().unwrap();
|
let table_name = "users";
|
||||||
|
let (columns, rows) = db.get_table(table_name).unwrap();
|
||||||
|
|
||||||
let tmpl = TableTemplate {
|
let tmpl = TableTemplate {
|
||||||
table_name: "users".into(),
|
table_name: table_name.into(),
|
||||||
columns,
|
columns,
|
||||||
rows,
|
rows,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user