db agnostic

This commit is contained in:
2024-05-06 21:43:48 +02:00
parent de97e4520c
commit a323e5f2a1
15 changed files with 351 additions and 319 deletions

View File

@@ -3,7 +3,7 @@ pub mod trips;
pub mod crud {
use async_trait::async_trait;
use crate::{models::Error, sqlite, Context};
use crate::{db, models::Error, Context};
#[async_trait]
pub trait Create: Sized {
@@ -15,7 +15,7 @@ pub mod crud {
async fn create(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
container: Self::Container,
info: Self::Info,
) -> Result<Self::Id, Error>;
@@ -28,13 +28,13 @@ pub mod crud {
async fn findall(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
container: Self::Container,
) -> Result<Vec<Self>, Error>;
async fn find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Self::Reference,
) -> Result<Option<Self>, Error>;
}
@@ -46,7 +46,7 @@ pub mod crud {
async fn update(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Self::Reference,
update: Self::UpdateElement,
) -> Result<Option<Self>, Error>;
@@ -84,7 +84,7 @@ pub mod crud {
async fn delete_all<'c>(
ctx: &Context,
pool: &'c sqlite::Pool,
pool: &'c db::Pool,
container: Self::Container,
ids: Vec<Self::Id>,
) -> Result<bool, Error> {
@@ -112,7 +112,7 @@ pub mod crud {
async fn set(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Self::Reference,
value: bool,
) -> Result<(), crate::Error>;

View File

@@ -20,10 +20,10 @@ use crate::{
route::{self, Toggle},
view::{self, View},
},
htmx,
db, htmx,
models::{user::User, Error},
routing::get_referer,
sqlite, AppState, Context, RequestError,
AppState, Context, RequestError,
};
use async_trait::async_trait;
@@ -140,16 +140,16 @@ impl crud::Read for Todo {
async fn findall(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
container: Container,
) -> Result<Vec<Self>, Error> {
let trip_id_param = container.trip_id.to_string();
let user_id = ctx.user.id.to_string();
let todos: Vec<Todo> = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Todo,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Todo,
},
pool,
TodoRow,
@@ -177,16 +177,16 @@ impl crud::Read for Todo {
#[tracing::instrument]
async fn find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Reference,
) -> Result<Option<Self>, Error> {
let trip_id_param = reference.container.trip_id.to_string();
let todo_id_param = reference.id.0.to_string();
let user_id = ctx.user.id.to_string();
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Todo,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Todo,
},
pool,
TodoRow,
@@ -228,7 +228,7 @@ impl crud::Create for Todo {
async fn create(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
container: Self::Container,
info: Self::Info,
) -> Result<Self::Id, Error> {
@@ -238,9 +238,9 @@ impl crud::Create for Todo {
let id_param = id.to_string();
let trip_id_param = container.trip_id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Todo,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Todo,
},
pool,
r#"
@@ -305,7 +305,7 @@ impl crud::Update for Todo {
#[tracing::instrument]
async fn update(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Self::Reference,
update_element: Self::UpdateElement,
) -> Result<Option<Self>, Error> {
@@ -317,9 +317,9 @@ impl crud::Update for Todo {
let done = state == State::Done.into();
let result = crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
TodoRow,
@@ -351,9 +351,9 @@ impl crud::Update for Todo {
let todo_id_param = reference.id.to_string();
let result = crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Todo,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Todo,
},
pool,
TodoRow,
@@ -400,9 +400,9 @@ impl crud::Delete for Todo {
let trip_id_param = reference.container.trip_id.to_string();
let results = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Todo,
&db::QueryClassification {
query_type: db::QueryType::Delete,
component: db::Component::Todo,
},
&mut *(db.acquire().await?),
r#"
@@ -945,7 +945,7 @@ impl crud::Toggle for StateUpdate {
async fn set(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
reference: Self::Reference,
value: bool,
) -> Result<(), crate::Error> {

View File

@@ -1,68 +1,12 @@
use std::fmt;
use std::time;
use base64::Engine as _;
use sha2::{Digest, Sha256};
use tracing::Instrument;
use std::fmt;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::ConnectOptions;
pub use sqlx::{Pool as SqlitePool, Sqlite};
use std::str::FromStr as _;
pub use sqlx::Type;
use crate::StartError;
pub mod postgres;
pub mod sqlite;
pub type Pool = sqlx::Pool<sqlx::Sqlite>;
pub fn int_to_bool(value: i32) -> bool {
match value {
0 => false,
1 => true,
_ => panic!("got invalid boolean from sqlite"),
}
}
#[tracing::instrument]
pub async fn init_database_pool(url: &str) -> Result<Pool, StartError> {
async {
SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.log_statements(log::LevelFilter::Debug)
.log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100))
.pragma("foreign_keys", "1"),
)
.await
}
.instrument(tracing::info_span!("packager::sql::pool"))
.await
.map_err(Into::into)
}
#[tracing::instrument]
pub async fn migrate(url: &str) -> Result<(), StartError> {
async {
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.pragma("foreign_keys", "0")
.log_statements(log::LevelFilter::Debug),
)
.await?;
sqlx::migrate!().run(&pool).await
}
.instrument(tracing::info_span!("packager::sql::migrate"))
.await?;
Ok(())
}
pub enum QueryType {
Insert,
Update,
@@ -171,7 +115,7 @@ macro_rules! query_all {
use tracing::Instrument as _;
use futures::TryStreamExt as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: Result<Vec<$struct_into>, Error> = sqlx::query_as!(
$struct_row,
$query,
@@ -197,7 +141,7 @@ macro_rules! query_one {
{
use tracing::Instrument as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: Result<Option<$struct_into>, Error> = sqlx::query_as!(
$struct_row,
$query,
@@ -221,7 +165,7 @@ macro_rules! query_exists {
{
use tracing::Instrument as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: bool = sqlx::query!(
$query,
$( $args )*
@@ -250,7 +194,7 @@ macro_rules! execute {
{
use tracing::Instrument as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: Result<sqlx::sqlite::SqliteQueryResult, Error> = sqlx::query!(
$query,
$( $args )*
@@ -298,7 +242,7 @@ macro_rules! execute_returning {
use tracing::Instrument as _;
use futures::TryFutureExt as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: Result<$t, Error> = sqlx::query!(
$query,
$( $args )*
@@ -323,7 +267,7 @@ macro_rules! execute_returning_uuid {
use tracing::Instrument as _;
use futures::TryFutureExt as _;
async {
$crate::sqlite::sqlx_query($class, $query, &[]);
$crate::db::sqlx_query($class, $query, &[]);
let result: Result<Uuid, Error> = sqlx::query!(
$query,
$( $args )*

1
src/db/postgres.rs Normal file
View File

@@ -0,0 +1 @@

59
src/db/sqlite.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::time;
use tracing::Instrument;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::ConnectOptions;
pub use sqlx::{Pool as SqlitePool, Sqlite};
use std::str::FromStr as _;
pub use sqlx::Type;
use crate::StartError;
pub fn int_to_bool(value: i32) -> bool {
match value {
0 => false,
1 => true,
_ => panic!("got invalid boolean from sqlite"),
}
}
#[tracing::instrument]
pub async fn init_database_pool(url: &str) -> Result<sqlx::Pool<sqlx::Sqlite>, StartError> {
async {
SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.log_statements(log::LevelFilter::Debug)
.log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100))
.pragma("foreign_keys", "1"),
)
.await
}
.instrument(tracing::info_span!("packager::sql::pool"))
.await
.map_err(Into::into)
}
#[tracing::instrument]
pub async fn migrate(url: &str) -> Result<(), StartError> {
async {
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str(url)?
.pragma("foreign_keys", "0")
.log_statements(log::LevelFilter::Debug),
)
.await?;
sqlx::migrate!().run(&pool).await
}
.instrument(tracing::info_span!("packager::sql::migrate"))
.await?;
Ok(())
}

View File

@@ -24,7 +24,7 @@ pub struct Cell<'a> {
}
impl<'a> Cell<'a> {
fn render(self, is_edit: bool) -> Markup {
fn render(self, _is_edit: bool) -> Markup {
match self.cell_type {
CellType::Text(text) => html!(
td

View File

@@ -5,12 +5,12 @@ use std::fmt;
pub mod auth;
pub mod cli;
pub mod components;
pub mod db;
pub mod elements;
pub mod error;
pub mod htmx;
pub mod models;
pub mod routing;
pub mod sqlite;
pub mod telemetry;
mod view;
@@ -19,7 +19,7 @@ pub use error::{AuthError, CommandError, Error, RequestError, StartError};
#[derive(Clone, Debug)]
pub struct AppState {
pub database_pool: sqlite::Pool,
pub database_pool: db::Pool,
pub client_state: ClientState,
pub auth_config: auth::Config,
}

View File

@@ -4,7 +4,7 @@ use std::process::ExitCode;
use std::str::FromStr;
use packager::{
auth, cli, models, routing, sqlite, telemetry, AppState, ClientState, Error, StartError,
auth, cli, db, models, routing, telemetry, AppState, ClientState, Error, StartError,
};
use tokio::net::TcpListener;
@@ -59,12 +59,12 @@ async fn main() -> MainResult {
Box::pin(async move {
match args.command {
cli::Command::Serve(serve_args) => {
if let Err(e) = sqlite::migrate(&args.database_url).await {
if let Err(e) = db::sqlite::migrate(&args.database_url).await {
return <_ as Into<Error>>::into(e).into();
}
let database_pool =
match sqlite::init_database_pool(&args.database_url).await {
match db::sqlite::init_database_pool(&args.database_url).await {
Ok(pool) => pool,
Err(e) => return <_ as Into<Error>>::into(e).into(),
};
@@ -152,11 +152,14 @@ async fn main() -> MainResult {
cli::Command::Admin(admin_command) => match admin_command {
cli::Admin::User(cmd) => match cmd {
cli::UserCommand::Create(user) => {
let database_pool =
match sqlite::init_database_pool(&args.database_url).await {
Ok(pool) => pool,
Err(e) => return <_ as Into<Error>>::into(e).into(),
};
let database_pool = match db::sqlite::init_database_pool(
&args.database_url,
)
.await
{
Ok(pool) => pool,
Err(e) => return <_ as Into<Error>>::into(e).into(),
};
let id = match models::user::create(
&database_pool,
@@ -188,7 +191,7 @@ async fn main() -> MainResult {
},
},
cli::Command::Migrate => {
if let Err(e) = sqlite::migrate(&args.database_url).await {
if let Err(e) = db::sqlite::migrate(&args.database_url).await {
return <_ as Into<Error>>::into(e).into();
}

View File

@@ -1,5 +1,5 @@
use super::Error;
use crate::{sqlite, Context};
use crate::{db, Context};
use uuid::Uuid;
@@ -9,13 +9,13 @@ pub struct Inventory {
impl Inventory {
#[tracing::instrument]
pub async fn load(ctx: &Context, pool: &sqlite::Pool) -> Result<Self, Error> {
pub async fn load(ctx: &Context, pool: &db::Pool) -> Result<Self, Error> {
let user_id = ctx.user.id.to_string();
let mut categories = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
DbCategoryRow,
@@ -69,15 +69,15 @@ impl Category {
#[tracing::instrument]
pub async fn _find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
) -> Result<Option<Category>, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
DbCategoryRow,
@@ -97,14 +97,14 @@ impl Category {
}
#[tracing::instrument]
pub async fn save(ctx: &Context, pool: &sqlite::Pool, name: &str) -> Result<Uuid, Error> {
pub async fn save(ctx: &Context, pool: &db::Pool, name: &str) -> Result<Uuid, Error> {
let id = Uuid::new_v4();
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Inventory,
},
pool,
"INSERT INTO inventory_items_categories
@@ -136,14 +136,14 @@ impl Category {
pub async fn populate_items(
&mut self,
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
) -> Result<(), Error> {
let id = self.id.to_string();
let user_id = ctx.user.id.to_string();
let items = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
DbInventoryItemsRow,
@@ -232,14 +232,18 @@ impl TryFrom<DbInventoryItemRow> for InventoryItem {
impl InventoryItem {
#[tracing::instrument]
pub async fn find(ctx: &Context, pool: &sqlite::Pool, id: Uuid) -> Result<Option<Self>, Error> {
pub async fn find(
ctx: &Context,
pool: &db::Pool,
id: Uuid,
) -> Result<Option<Self>, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
DbInventoryItemRow,
@@ -273,14 +277,14 @@ impl InventoryItem {
#[tracing::instrument]
pub async fn name_exists(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
name: &str,
) -> Result<bool, Error> {
let user_id = ctx.user.id.to_string();
crate::query_exists!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
"SELECT id
@@ -295,13 +299,13 @@ impl InventoryItem {
}
#[tracing::instrument]
pub async fn delete(ctx: &Context, pool: &sqlite::Pool, id: Uuid) -> Result<bool, Error> {
pub async fn delete(ctx: &Context, pool: &db::Pool, id: Uuid) -> Result<bool, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
let results = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Delete,
component: db::Component::Inventory,
},
pool,
"DELETE FROM inventory_items
@@ -319,7 +323,7 @@ impl InventoryItem {
#[tracing::instrument]
pub async fn update(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
name: &str,
weight: u32,
@@ -329,9 +333,9 @@ impl InventoryItem {
let id_param = id.to_string();
crate::execute_returning_uuid!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Inventory,
},
pool,
"UPDATE inventory_items AS item
@@ -354,7 +358,7 @@ impl InventoryItem {
#[tracing::instrument]
pub async fn save(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
name: &str,
category_id: Uuid,
weight: u32,
@@ -365,9 +369,9 @@ impl InventoryItem {
let category_id_param = category_id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Inventory,
},
pool,
"INSERT INTO inventory_items
@@ -389,15 +393,15 @@ impl InventoryItem {
#[tracing::instrument]
pub async fn get_category_max_weight(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
category_id: Uuid,
) -> Result<i64, Error> {
let user_id = ctx.user.id.to_string();
let category_id_param = category_id.to_string();
let weight = crate::execute_returning!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
"
@@ -455,15 +459,15 @@ impl Item {
#[tracing::instrument]
pub async fn _get_category_total_picked_weight(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
category_id: Uuid,
) -> Result<i64, Error> {
let user_id = ctx.user.id.to_string();
let category_id_param = category_id.to_string();
crate::execute_returning!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Inventory,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Inventory,
},
pool,
"

View File

@@ -8,7 +8,7 @@ use super::{
inventory,
};
use crate::{sqlite, Context};
use crate::{db, Context};
use serde::{Deserialize, Serialize};
use time;
@@ -38,7 +38,7 @@ use uuid::Uuid;
// }
// }
#[derive(sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)]
#[derive(db::sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)]
pub enum TripState {
Init,
Planning,
@@ -156,7 +156,7 @@ impl TripCategory {
#[tracing::instrument]
pub async fn find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
category_id: Uuid,
) -> Result<Option<TripCategory>, Error> {
@@ -222,9 +222,9 @@ impl TripCategory {
}
let mut rows = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
Row,
@@ -346,7 +346,7 @@ impl TripItem {
#[tracing::instrument]
pub async fn find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
item_id: Uuid,
) -> Result<Option<Self>, Error> {
@@ -354,9 +354,9 @@ impl TripItem {
let item_id_param = item_id.to_string();
let trip_id_param = trip_id.to_string();
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
DbTripsItemsRow,
@@ -389,7 +389,7 @@ impl TripItem {
#[tracing::instrument]
pub async fn set_state(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
item_id: Uuid,
key: TripItemStateKey,
@@ -401,9 +401,9 @@ impl TripItem {
let result = match key {
TripItemStateKey::Pick => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips_items
@@ -420,9 +420,9 @@ impl TripItem {
}
TripItemStateKey::Pack => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips_items
@@ -439,9 +439,9 @@ impl TripItem {
}
TripItemStateKey::Ready => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips_items
@@ -533,12 +533,12 @@ pub enum TripAttribute {
impl Trip {
#[tracing::instrument]
pub async fn all(ctx: &Context, pool: &sqlite::Pool) -> Result<Vec<Trip>, Error> {
pub async fn all(ctx: &Context, pool: &db::Pool) -> Result<Vec<Trip>, Error> {
let user_id = ctx.user.id.to_string();
let mut trips = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
DbTripRow,
@@ -566,15 +566,15 @@ impl Trip {
#[tracing::instrument]
pub async fn find(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
) -> Result<Option<Self>, Error> {
let trip_id_param = trip_id.to_string();
let user_id = ctx.user.id.to_string();
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
DbTripRow,
@@ -600,7 +600,7 @@ impl Trip {
#[tracing::instrument]
pub async fn trip_type_remove(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
type_id: Uuid,
) -> Result<bool, Error> {
@@ -609,9 +609,9 @@ impl Trip {
let type_id_param = type_id.to_string();
let results = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Delete,
component: db::Component::Trips,
},
pool,
"DELETE FROM trips_to_trips_types AS ttt
@@ -635,7 +635,7 @@ impl Trip {
#[tracing::instrument]
pub async fn trip_type_add(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
type_id: Uuid,
) -> Result<(), Error> {
@@ -645,9 +645,9 @@ impl Trip {
let type_id_param = type_id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
pool,
"INSERT INTO
@@ -673,16 +673,16 @@ impl Trip {
#[tracing::instrument]
pub async fn set_state(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
new_state: &TripState,
) -> Result<bool, Error> {
let user_id = ctx.user.id.to_string();
let trip_id_param = id.to_string();
let result = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -700,16 +700,16 @@ impl Trip {
#[tracing::instrument]
pub async fn set_comment(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
new_comment: &str,
) -> Result<bool, Error> {
let user_id = ctx.user.id.to_string();
let trip_id_param = id.to_string();
let result = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -727,7 +727,7 @@ impl Trip {
#[tracing::instrument]
pub async fn set_attribute(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
attribute: TripAttribute,
value: &str,
@@ -737,9 +737,9 @@ impl Trip {
let result = match attribute {
TripAttribute::Name => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -754,9 +754,9 @@ impl Trip {
TripAttribute::DateStart => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -770,9 +770,9 @@ impl Trip {
}
TripAttribute::DateEnd => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -786,9 +786,9 @@ impl Trip {
}
TripAttribute::Location => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -802,9 +802,9 @@ impl Trip {
}
TripAttribute::TempMin => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -818,9 +818,9 @@ impl Trip {
}
TripAttribute::TempMax => {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips
@@ -844,7 +844,7 @@ impl Trip {
#[tracing::instrument]
pub async fn save(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
name: &str,
date_start: time::Date,
date_end: time::Date,
@@ -861,9 +861,9 @@ impl Trip {
let mut transaction = pool.begin().await?;
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
&mut *transaction,
"INSERT INTO trips
@@ -882,9 +882,9 @@ impl Trip {
if let Some(copy_from_trip_id) = copy_from {
let copy_from_trip_id_param = copy_from_trip_id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
&mut *transaction,
r#"INSERT INTO trips_items (
@@ -912,9 +912,9 @@ impl Trip {
.await?;
} else {
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
&mut *transaction,
r#"INSERT INTO trips_items (
@@ -949,15 +949,15 @@ impl Trip {
#[tracing::instrument]
pub async fn find_total_picked_weight(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
trip_id: Uuid,
) -> Result<i64, Error> {
let user_id = ctx.user.id.to_string();
let trip_id_param = trip_id.to_string();
let weight = crate::execute_returning!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
"
@@ -1018,7 +1018,11 @@ impl Trip {
}
#[tracing::instrument]
pub async fn load_todos(&mut self, ctx: &Context, pool: &sqlite::Pool) -> Result<(), Error> {
pub async fn load_todos(
&mut self,
ctx: &Context,
pool: &db::Pool,
) -> Result<(), Error> {
self.todos = Some(
crate::components::trips::todos::Todo::findall(
ctx,
@@ -1034,14 +1038,14 @@ impl Trip {
pub async fn load_trips_types(
&mut self,
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
) -> Result<(), Error> {
let user_id = ctx.user.id.to_string();
let id = self.id.to_string();
let types = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
TripTypeRow,
@@ -1078,7 +1082,7 @@ impl Trip {
pub async fn sync_trip_items_with_inventory(
&mut self,
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
) -> Result<(), Error> {
// we need to get all items that are part of the inventory but not
// part of the trip items
@@ -1107,9 +1111,9 @@ impl Trip {
}
let unsynced_items: Vec<Uuid> = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
Row,
@@ -1137,9 +1141,9 @@ impl Trip {
for unsynced_item in &unsynced_items {
let item_id = unsynced_item.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
pool,
"
@@ -1175,7 +1179,7 @@ impl Trip {
pub async fn load_categories(
&mut self,
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
) -> Result<(), Error> {
let mut categories: Vec<TripCategory> = vec![];
// we can ignore the return type as we collect into `categories`
@@ -1241,9 +1245,9 @@ impl Trip {
}
let rows = crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
Row,
@@ -1408,12 +1412,12 @@ impl TryFrom<TripTypeRow> for TripType {
impl TripsType {
#[tracing::instrument]
pub async fn all(ctx: &Context, pool: &sqlite::Pool) -> Result<Vec<Self>, Error> {
pub async fn all(ctx: &Context, pool: &db::Pool) -> Result<Vec<Self>, Error> {
let user_id = ctx.user.id.to_string();
crate::query_all!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::Trips,
},
pool,
DbTripsTypesRow,
@@ -1429,14 +1433,14 @@ impl TripsType {
}
#[tracing::instrument]
pub async fn save(ctx: &Context, pool: &sqlite::Pool, name: &str) -> Result<Uuid, Error> {
pub async fn save(ctx: &Context, pool: &db::Pool, name: &str) -> Result<Uuid, Error> {
let user_id = ctx.user.id.to_string();
let id = Uuid::new_v4();
let id_param = id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::Trips,
},
pool,
"INSERT INTO trips_types
@@ -1455,7 +1459,7 @@ impl TripsType {
#[tracing::instrument]
pub async fn set_name(
ctx: &Context,
pool: &sqlite::Pool,
pool: &db::Pool,
id: Uuid,
new_name: &str,
) -> Result<bool, Error> {
@@ -1463,9 +1467,9 @@ impl TripsType {
let id_param = id.to_string();
let result = crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Update,
component: sqlite::Component::Trips,
&db::QueryClassification {
query_type: db::QueryType::Update,
component: db::Component::Trips,
},
pool,
"UPDATE trips_types

View File

@@ -1,7 +1,7 @@
use super::Error;
use uuid::Uuid;
use crate::sqlite;
use crate::db;
#[derive(Debug, Clone)]
pub struct User {
@@ -42,9 +42,9 @@ impl User {
name: &str,
) -> Result<Option<Self>, Error> {
crate::query_one!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Select,
component: sqlite::Component::User,
&db::QueryClassification {
query_type: db::QueryType::Select,
component: db::Component::User,
},
pool,
DbUserRow,
@@ -57,14 +57,14 @@ impl User {
}
#[tracing::instrument]
pub async fn create(pool: &sqlite::Pool, user: NewUser<'_>) -> Result<Uuid, Error> {
pub async fn create(pool: &db::Pool, user: NewUser<'_>) -> Result<Uuid, Error> {
let id = Uuid::new_v4();
let id_param = id.to_string();
crate::execute!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Insert,
component: sqlite::Component::User,
&db::QueryClassification {
query_type: db::QueryType::Insert,
component: db::Component::User,
},
pool,
"INSERT INTO users