Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

dynamic Row #12

Open
soerenmeier opened this issue Apr 20, 2024 · 0 comments
Open

dynamic Row #12

soerenmeier opened this issue Apr 20, 2024 · 0 comments

Comments

@soerenmeier
Copy link
Member

soerenmeier commented Apr 20, 2024

This is ugly

let mut fields =
	String::from(r#""firstname" = $1, "lastname" = $2, "email" = $3"#);

let mut params: Vec<&(dyn ToSql + Sync)> =
	vec![firstname, lastname, &email];

let hashed_password;
if let Some(password) = password {
	hashed_password = hash(password, 12)?;
	fields.push_str(r#", "password" = $4"#);
	params.push(&hashed_password);
}

let raw_rights;
if let Some(rights) = rights {
	raw_rights = RawRights(rights.clone());
	fields.push_str(format!(r#", "rights" = {}"#, params.len() + 1));
	params.push(&raw_rights);
}

let sql = format!(
	"UPDATE \"{}\" SET {} WHERE \"id\" = ${}",
	self.table.name(),
	fields,
	params.len() + 1
);
params.push(id);
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant