use crate::pool::{backend, pool, Backend};
use larust_core::AppError;
use sqlx::Row;
pub async fn table_names() -> Result<Vec<String>, AppError> {
let pool = pool()?;
let sql = match backend() {
Backend::Sqlite => {
"SELECT name FROM sqlite_master WHERE type = 'table' \
AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\' ORDER BY name"
}
Backend::MySql => {
"SELECT table_name FROM information_schema.tables \
WHERE table_schema = DATABASE() ORDER BY table_name"
}
Backend::Postgres => {
"SELECT table_name FROM information_schema.tables \
WHERE table_schema = 'public' ORDER BY table_name"
}
};
let rows = sqlx::query(sql)
.fetch_all(pool)
.await
.map_err(|e| AppError::Internal(Box::new(e)))?;
Ok(rows.iter().map(|row| row.get::<String, _>(0)).collect())
}