Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database, Discord-Frontend: Fix Option<T>ness of Bind Parameters #2511

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ pub struct CachedGuildUpsert {
default_message_notifications: i16,
explicit_content_filter: i16,
features: Vec<String>,
icon: String,
icon: Option<String>,
large: bool,
name: String,
owner_id: String,
id: String,
mfa_level: i16,
premium_subscription_count: i64,
premium_subscription_count: Option<i64>,
premium_tier: i16,
verification_level: i16,
}
Expand All @@ -32,13 +32,13 @@ impl CachedGuildUpsert {
default_message_notifications: i16,
explicit_content_filter: i16,
features: Vec<String>,
icon: String,
icon: Option<String>,
large: bool,
name: String,
owner_id: String,
id: String,
mfa_level: i16,
premium_subscription_count: i64,
premium_subscription_count: Option<i64>,
premium_tier: i16,
verification_level: i16,
) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct CachedMemberUpsert {
db_executor: Option<Executor<wtx::Error, ExecutorBuffer, TcpStream>>,
executor_constructor: for<'a> fn(Uri<&'a str>) -> crate::internal::Ret<'a>,
flags: i64,
joined_at: chrono::DateTime<chrono::offset::Utc>,
nick: String,
joined_at: Option<chrono::DateTime<chrono::offset::Utc>>,
nick: Option<String>,
user_id: String,
guild_id: String,
roles: Vec<String>,
Expand All @@ -24,8 +24,8 @@ impl CachedMemberUpsert {
#[must_use = "Queries must be executed after construction"]
pub fn bind(
flags: i64,
joined_at: chrono::DateTime<chrono::offset::Utc>,
nick: String,
joined_at: Option<chrono::DateTime<chrono::offset::Utc>>,
nick: Option<String>,
user_id: String,
guild_id: String,
roles: Vec<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct CachedRoleUpsert {
db_executor: Option<Executor<wtx::Error, ExecutorBuffer, TcpStream>>,
executor_constructor: for<'a> fn(Uri<&'a str>) -> crate::internal::Ret<'a>,
color: i64,
icon: String,
icon: Option<String>,
id: String,
guild_id: String,
flags: i32,
Expand All @@ -27,7 +27,7 @@ impl CachedRoleUpsert {
#[must_use = "Queries must be executed after construction"]
pub fn bind(
color: i64,
icon: String,
icon: Option<String>,
id: String,
guild_id: String,
flags: i32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ use crate::result::IntoCrateResult;
pub struct CachedUserUpsert {
db_executor: Option<Executor<wtx::Error, ExecutorBuffer, TcpStream>>,
executor_constructor: for<'a> fn(Uri<&'a str>) -> crate::internal::Ret<'a>,
avatar: String,
avatar: Option<String>,
id: String,
bot: bool,
name: String,
discriminator: String,
global_name: String,
global_name: Option<String>,
}
impl CachedUserUpsert {
#[must_use = "Queries must be executed after construction"]
pub fn bind(
avatar: String,
avatar: Option<String>,
id: String,
bot: bool,
name: String,
discriminator: String,
global_name: String,
global_name: Option<String>,
) -> Self {
Self {
db_executor: None,
Expand Down
11 changes: 9 additions & 2 deletions database/hartex-database-typedsql/src/codegen/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use proc_macro2::Literal;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::TokenStreamExt;
use sqlparser::ast::ColumnOption;
use sqlparser::ast::Statement;
use syn::File;

Expand Down Expand Up @@ -136,7 +137,13 @@ fn generate_query_struct_token_stream(
.iter()
.map(|placeholder| {
let dtype = if let Some(col) = table.columns.get(placeholder) {
types::sql_type_to_rust_type_token_stream(col.coltype.clone()).unwrap()
let dt = types::sql_type_to_rust_type_token_stream(col.coltype.clone()).unwrap();

if !col.constraints.contains(&ColumnOption::NotNull) {
quote::quote! {Option<#dt>}
} else {
dt
}
} else if let Some(dt) = query.extra_placeholder_tys.get(placeholder) {
types::sql_type_to_rust_type_token_stream(dt.clone()).unwrap()
} else {
Expand Down Expand Up @@ -292,7 +299,7 @@ fn generate_select_query_fns_token_streams(
placeholders.clone(),
));
}
_ => return Ok(vec![])
_ => return Ok(vec![]),
};

Ok(vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ impl Repository<GuildEntity> for CachedGuildRepository {
<GuildFeature as Into<Cow<'static, str>>>::into(feature.clone()).to_string()
})
.collect(),
entity.icon.map(|hash| hash.to_string()).unwrap_or_default(),
entity.icon.map(|hash| hash.to_string()),
entity.large,
entity.name,
entity.owner_id.to_string(),
entity.id.to_string(),
i16::from(<MfaLevel as Into<u8>>::into(entity.mfa_level)),
entity
.premium_subscription_count
.map(|id| id as i64)
.unwrap_or_default(),
entity.premium_subscription_count.map(|id| id as i64),
i16::from(<PremiumTier as Into<u8>>::into(entity.premium_tier)),
i16::from(<VerificationLevel as Into<u8>>::into(
entity.verification_level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ impl Repository<MemberEntity> for CachedMemberRepository {
entity.flags.bits() as i64,
entity
.joined_at
.map(|timestamp| DateTime::from_timestamp(timestamp.as_secs(), 0).unwrap())
.unwrap(),
entity.nick.unwrap_or_default(),
.map(|timestamp| DateTime::from_timestamp(timestamp.as_secs(), 0).unwrap()),
entity.nick,
entity.user_id.to_string(),
entity.guild_id.to_string(),
entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Repository<RoleEntity> for CachedRoleRepository {
async fn upsert(&self, entity: RoleEntity) -> CacheResult<()> {
CachedRoleUpsert::bind(
entity.color as i64,
entity.icon.map(|hash| hash.to_string()).unwrap_or_default(),
entity.icon.map(|hash| hash.to_string()),
entity.id.to_string(),
entity.guild_id.to_string(),
entity.flags.bits() as i32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ impl Repository<UserEntity> for CachedUserRepository {

async fn upsert(&self, entity: UserEntity) -> CacheResult<()> {
CachedUserUpsert::bind(
entity
.avatar
.map(|hash| hash.to_string())
.unwrap_or_default(),
entity.avatar.map(|hash| hash.to_string()),
entity.id.to_string(),
entity.bot,
entity.name,
entity.discriminator.to_string(),
entity.global_name.unwrap_or_default(),
entity.global_name,
)
.executor()
.await?
Expand Down
Loading