Skip to content

Commit

Permalink
refactor(rust): use explicit column lists
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewLeboffe authored and etorreborre committed Dec 11, 2023
1 parent 6d9da66 commit 34ac413
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ impl ModelStateRepository for ModelStateSqlxDatabase {
}

async fn load(&self) -> Result<ModelState> {
let query1 = query_as("SELECT * FROM tcp_outlet_status");
let query1 =
query_as("SELECT alias, socket_addr, worker_addr, payload FROM tcp_outlet_status");
let result: Vec<TcpOutletStatusRow> =
query1.fetch_all(&self.database.pool).await.into_core()?;
let tcp_outlets = result
.into_iter()
.map(|r| r.tcp_outlet_status())
.collect::<Result<Vec<_>>>()?;

let query2 = query_as("SELECT * FROM incoming_service");
let query2 = query_as("SELECT invitation_id, enabled, name FROM incoming_service");
let result: Vec<PersistentIncomingServiceRow> =
query2.fetch_all(&self.database.pool).await.into_core()?;
let incoming_services = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ impl ChangeHistorySqlxDatabase {
impl ChangeHistoryRepository for ChangeHistorySqlxDatabase {
async fn update_identity(&self, identity: &Identity) -> Result<()> {
let mut transaction = self.database.begin().await.into_core()?;
let query1 = query_as("SELECT * FROM identity WHERE identifier=$1")
.bind(identity.identifier().to_sql());
let query1 =
query_as("SELECT identifier, change_history FROM identity WHERE identifier=$1")
.bind(identity.identifier().to_sql());
let row: Option<ChangeHistoryRow> =
query1.fetch_optional(&mut *transaction).await.into_core()?;

Expand Down Expand Up @@ -96,8 +97,8 @@ impl ChangeHistoryRepository for ChangeHistorySqlxDatabase {
}

async fn get_change_history(&self, identifier: &Identifier) -> Result<Option<ChangeHistory>> {
let query =
query_as("SELECT * FROM identity WHERE identifier=$1").bind(identifier.to_sql());
let query = query_as("SELECT identifier, change_history FROM identity WHERE identifier=$1")
.bind(identifier.to_sql());
let row: Option<ChangeHistoryRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -106,7 +107,7 @@ impl ChangeHistoryRepository for ChangeHistorySqlxDatabase {
}

async fn get_change_histories(&self) -> Result<Vec<ChangeHistory>> {
let query = query_as("SELECT * FROM identity");
let query = query_as("SELECT identifier, change_history FROM identity");
let row: Vec<ChangeHistoryRow> = query.fetch_all(&self.database.pool).await.into_core()?;
row.iter().map(|r| r.change_history()).collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl IdentityAttributesSqlxDatabase {
#[async_trait]
impl IdentityAttributesRepository for IdentityAttributesSqlxDatabase {
async fn get_attributes(&self, identity: &Identifier) -> Result<Option<AttributesEntry>> {
let query = query_as("SELECT * FROM identity_attributes WHERE identifier=$1")
let query = query_as("SELECT identifier, attributes, added, expires, attested_by FROM identity_attributes WHERE identifier=$1")
.bind(identity.to_sql());
let identity_attributes: Option<IdentityAttributesRow> = query
.fetch_optional(&self.database.pool)
Expand All @@ -46,7 +46,9 @@ impl IdentityAttributesRepository for IdentityAttributesSqlxDatabase {
}

async fn list_attributes_by_identifier(&self) -> Result<Vec<(Identifier, AttributesEntry)>> {
let query = query_as("SELECT * FROM identity_attributes");
let query = query_as(
"SELECT identifier, attributes, added, expires, attested_by FROM identity_attributes",
);
let result: Vec<IdentityAttributesRow> =
query.fetch_all(&self.database.pool).await.into_core()?;
result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl PurposeKeysRepository for PurposeKeysSqlxDatabase {
identifier: &Identifier,
purpose: Purpose,
) -> Result<Option<PurposeKeyAttestation>> {
let query = query_as("SELECT * FROM purpose_key WHERE identifier=$1 and purpose=$2")
let query = query_as("SELECT identifier, purpose, purpose_key_attestation FROM purpose_key WHERE identifier=$1 and purpose=$2")
.bind(identifier.to_sql())
.bind(purpose.to_sql());
let row: Option<PurposeKeyRow> = query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Encode<'_, Sqlite> for SqlxType {
/// }
///
/// let timestamp = TimestampInSeconds(10000000);
/// let query = query_as("SELECT * FROM identity WHERE created_at >= $1").bind(timestamp.as_sql());
/// let query = query_as("SELECT identifier, change_history FROM identity WHERE created_at >= $1").bind(timestamp.as_sql());
///
///
pub trait ToSqlxType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ impl SecretsRepository for SecretsSqlxDatabase {
handle: &SigningSecretKeyHandle,
) -> Result<Option<SigningSecret>> {
let mut transaction = self.database.begin().await.into_core()?;
let query1 = query_as("SELECT * FROM signing_secret WHERE handle=?").bind(handle.to_sql());
let query1 =
query_as("SELECT handle, secret_type, secret FROM signing_secret WHERE handle=?")
.bind(handle.to_sql());
let row: Option<SigningSecretRow> =
query1.fetch_optional(&mut *transaction).await.into_core()?;
let secret = row.map(|r| r.signing_secret()).transpose()?;
Expand All @@ -83,7 +85,9 @@ impl SecretsRepository for SecretsSqlxDatabase {
&self,
handle: &SigningSecretKeyHandle,
) -> Result<Option<SigningSecret>> {
let query = query_as("SELECT * FROM signing_secret WHERE handle=?").bind(handle.to_sql());
let query =
query_as("SELECT handle, secret_type, secret FROM signing_secret WHERE handle=?")
.bind(handle.to_sql());
let row: Option<SigningSecretRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -92,7 +96,7 @@ impl SecretsRepository for SecretsSqlxDatabase {
}

async fn get_signing_secret_handles(&self) -> Result<Vec<SigningSecretKeyHandle>> {
let query = query_as("SELECT * FROM signing_secret");
let query = query_as("SELECT handle, secret_type, secret FROM signing_secret");
let rows: Vec<SigningSecretRow> = query.fetch_all(&self.database.pool).await.into_core()?;
Ok(rows
.iter()
Expand All @@ -116,7 +120,8 @@ impl SecretsRepository for SecretsSqlxDatabase {
handle: &X25519SecretKeyHandle,
) -> Result<Option<X25519SecretKey>> {
let mut transaction = self.database.begin().await.into_core()?;
let query1 = query_as("SELECT * FROM x25519_secret WHERE handle=?").bind(handle.to_sql());
let query1 = query_as("SELECT handle, secret FROM x25519_secret WHERE handle=?")
.bind(handle.to_sql());
let row: Option<X25519SecretRow> =
query1.fetch_optional(&mut *transaction).await.into_core()?;
let secret = row.map(|r| r.x25519_secret()).transpose()?;
Expand All @@ -136,7 +141,8 @@ impl SecretsRepository for SecretsSqlxDatabase {
&self,
handle: &X25519SecretKeyHandle,
) -> Result<Option<X25519SecretKey>> {
let query = query_as("SELECT * FROM x25519_secret WHERE handle=?").bind(handle.to_sql());
let query = query_as("SELECT handle, secret FROM x25519_secret WHERE handle=?")
.bind(handle.to_sql());
let row: Option<X25519SecretRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -145,7 +151,7 @@ impl SecretsRepository for SecretsSqlxDatabase {
}

async fn get_x25519_secret_handles(&self) -> Result<Vec<X25519SecretKeyHandle>> {
let query = query_as("SELECT * FROM x25519_secret");
let query = query_as("SELECT handle, secret FROM x25519_secret");
let rows: Vec<X25519SecretRow> = query.fetch_all(&self.database.pool).await.into_core()?;
Ok(rows
.iter()
Expand Down

0 comments on commit 34ac413

Please sign in to comment.