Skip to content

Commit

Permalink
refactor(rust): use explicit column lists -- partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetsuro authored and etorreborre committed Dec 11, 2023
1 parent bfcfaff commit 6d9da66
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ impl PolicySqlxDatabase {
#[async_trait]
impl PoliciesRepository for PolicySqlxDatabase {
async fn get_policy(&self, resource: &Resource, action: &Action) -> Result<Option<Policy>> {
let query = query_as("SELECT * FROM policy WHERE resource=$1 and action=$2")
.bind(resource.to_sql())
.bind(action.to_sql());
let query = query_as(
"SELECT resource, action, expression FROM policy WHERE resource=$1 and action=$2",
)
.bind(resource.to_sql())
.bind(action.to_sql());
let row: Option<PolicyRow> = query
.fetch_optional(&self.database.pool)
.await
Expand Down Expand Up @@ -63,7 +65,8 @@ impl PoliciesRepository for PolicySqlxDatabase {
}

async fn get_policies_by_resource(&self, resource: &Resource) -> Result<Vec<(Action, Policy)>> {
let query = query_as("SELECT * FROM policy where resource = $1").bind(resource.to_sql());
let query = query_as("SELECT resource, action, expression FROM policy where resource = $1")
.bind(resource.to_sql());
let row: Vec<PolicyRow> = query.fetch_all(&self.database.pool).await.into_core()?;
row.into_iter()
.map(|r| r.policy().map(|e| (r.action(), e)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CredentialsRepository for CredentialsSqlxDatabase {
}

async fn get_credential(&self, name: &str) -> Result<Option<NamedCredential>> {
let query = query_as("SELECT * FROM credential WHERE name=$1").bind(name.to_sql());
let query = query_as("SELECT name, issuer_identifier, issuer_change_history, credential FROM credential WHERE name=$1").bind(name.to_sql());
let row: Option<CredentialRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -57,7 +57,9 @@ impl CredentialsRepository for CredentialsSqlxDatabase {
}

async fn get_credentials(&self) -> Result<Vec<NamedCredential>> {
let query = query_as("SELECT * FROM credential");
let query = query_as(
"SELECT name, issuer_identifier, issuer_change_history, credential FROM credential",
);
let row: Vec<CredentialRow> = query.fetch_all(&self.database.pool).await.into_core()?;
row.iter().map(|r| r.named_credential()).collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
let mut transaction = self.database.begin().await.into_core()?;

// get the named identity
let query1 = query_as("SELECT * FROM named_identity WHERE name=$1").bind(name.to_sql());
let query1 = query_as(
"SELECT identifier, name, vault_name, is_default FROM named_identity WHERE name=$1",
)
.bind(name.to_sql());
let row: Option<NamedIdentityRow> =
query1.fetch_optional(&mut *transaction).await.into_core()?;
let named_identity = row.map(|r| r.named_identity()).transpose()?;
Expand Down Expand Up @@ -119,7 +122,10 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
}

async fn get_identifier(&self, name: &str) -> Result<Option<Identifier>> {
let query = query_as("SELECT * FROM named_identity WHERE name=$1").bind(name.to_sql());
let query = query_as(
"SELECT identifier, name, vault_name, is_default FROM named_identity WHERE name=$1",
)
.bind(name.to_sql());
let row: Option<NamedIdentityRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -132,7 +138,7 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
identifier: &Identifier,
) -> Result<Option<String>> {
let query =
query_as("SELECT * FROM named_identity WHERE identifier=$1").bind(identifier.to_sql());
query_as("SELECT identifier, name, vault_name, is_default FROM named_identity WHERE identifier=$1").bind(identifier.to_sql());
let row: Option<NamedIdentityRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -141,7 +147,10 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
}

async fn get_named_identity(&self, name: &str) -> Result<Option<NamedIdentity>> {
let query = query_as("SELECT * FROM named_identity WHERE name=$1").bind(name.to_sql());
let query = query_as(
"SELECT identifier, name, vault_name, is_default FROM named_identity WHERE name=$1",
)
.bind(name.to_sql());
let row: Option<NamedIdentityRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -154,7 +163,7 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
identifier: &Identifier,
) -> Result<Option<NamedIdentity>> {
let query =
query_as("SELECT * FROM named_identity WHERE identifier=$1").bind(identifier.to_sql());
query_as("SELECT identifier, name, vault_name, is_default FROM named_identity WHERE identifier=$1").bind(identifier.to_sql());
let row: Option<NamedIdentityRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -163,7 +172,7 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {
}

async fn get_named_identities(&self) -> Result<Vec<NamedIdentity>> {
let query = query_as("SELECT * FROM named_identity");
let query = query_as("SELECT identifier, name, vault_name, is_default FROM named_identity");
let row: Vec<NamedIdentityRow> = query.fetch_all(&self.database.pool).await.into_core()?;
row.iter().map(|r| r.named_identity()).collect()
}
Expand Down Expand Up @@ -202,7 +211,7 @@ impl IdentitiesRepository for IdentitiesSqlxDatabase {

async fn get_default_named_identity(&self) -> Result<Option<NamedIdentity>> {
let query =
query_as("SELECT * FROM named_identity WHERE is_default=$1").bind(true.to_sql());
query_as("SELECT identifier, name, vault_name, is_default FROM named_identity WHERE is_default=$1").bind(true.to_sql());
let row: Option<NamedIdentityRow> = query
.fetch_optional(&self.database.pool)
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl NodesRepository for NodesSqlxDatabase {
}

async fn get_nodes(&self) -> Result<Vec<NodeInfo>> {
let query = query_as("SELECT * FROM node");
let query = query_as("SELECT name, identifier, verbosity, is_default, is_authority, tcp_listener_address, pid FROM node");
let rows: Vec<NodeRow> = query.fetch_all(&self.database.pool).await.into_core()?;
rows.iter().map(|r| r.node_info()).collect()
}

async fn get_node(&self, node_name: &str) -> Result<Option<NodeInfo>> {
let query = query_as("SELECT * FROM node WHERE name = ?").bind(node_name.to_sql());
let query = query_as("SELECT name, identifier, verbosity, is_default, is_authority, tcp_listener_address, pid FROM node WHERE name = ?").bind(node_name.to_sql());
let row: Option<NodeRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -65,13 +65,13 @@ impl NodesRepository for NodesSqlxDatabase {
}

async fn get_nodes_by_identifier(&self, identifier: &Identifier) -> Result<Vec<NodeInfo>> {
let query = query_as("SELECT * FROM node WHERE identifier = ?").bind(identifier.to_sql());
let query = query_as("SELECT name, identifier, verbosity, is_default, is_authority, tcp_listener_address, pid FROM node WHERE identifier = ?").bind(identifier.to_sql());
let rows: Vec<NodeRow> = query.fetch_all(&self.database.pool).await.into_core()?;
rows.iter().map(|r| r.node_info()).collect()
}

async fn get_default_node(&self) -> Result<Option<NodeInfo>> {
let query = query_as("SELECT * FROM node WHERE is_default = ?").bind(true.to_sql());
let query = query_as("SELECT name, identifier, verbosity, is_default, is_authority, tcp_listener_address, pid FROM node WHERE is_default = ?").bind(true.to_sql());
let row: Option<NodeRow> = query
.fetch_optional(&self.database.pool)
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,21 @@ impl ProjectsRepository for ProjectsSqlxDatabase {
async fn get_project_by_name(&self, name: &str) -> Result<Option<Project>> {
let mut transaction = self.database.begin().await.into_core()?;

let query = query_as("SELECT * FROM project WHERE project_name=$1").bind(name.to_sql());
let query = query_as("SELECT project_id, project_name, is_default, space_id, space_name, identifier, access_route, authority_identity, authority_access_route, version, running, operation_id FROM project WHERE project_name=$1").bind(name.to_sql());
let row: Option<ProjectRow> = query.fetch_optional(&mut *transaction).await.into_core()?;
let project = match row.map(|r| r.project()).transpose()? {
Some(mut project) => {
// get the project users emails
let query2 = query_as("SELECT * FROM user_project WHERE project_id=$1")
.bind(project.id.to_sql());
let query2 =
query_as("SELECT project_id, user_email FROM user_project WHERE project_id=$1")
.bind(project.id.to_sql());
let rows: Vec<UserProjectRow> =
query2.fetch_all(&mut *transaction).await.into_core()?;
let users = rows.into_iter().map(|r| r.user_email).collect();
project.users = users;

// get the project users roles
let query3 = query_as("SELECT * FROM user_role WHERE project_id=$1")
let query3 = query_as("SELECT user_id, project_id, user_email, role, scope FROM user_role WHERE project_id=$1")
.bind(project.id.to_sql());
let rows: Vec<UserRoleRow> =
query3.fetch_all(&mut *transaction).await.into_core()?;
Expand All @@ -175,15 +176,17 @@ impl ProjectsRepository for ProjectsSqlxDatabase {
project.user_roles = user_roles;

// get the project okta configuration
let query4 = query_as("SELECT * FROM okta_config WHERE project_id=$1")
let query4 = query_as("SELECT project_id, tenant_base_url, client_id, certificate, attributes FROM okta_config WHERE project_id=$1")
.bind(project.id.to_sql());
let row: Option<OktaConfigRow> =
query4.fetch_optional(&mut *transaction).await.into_core()?;
project.okta_config = row.map(|r| r.okta_config()).transpose()?;

// get the project confluent configuration
let query5 = query_as("SELECT * FROM confluent_config WHERE project_id=$1")
.bind(project.id.to_sql());
let query5 = query_as(
"SELECT project_id, bootstrap_server FROM confluent_config WHERE project_id=$1",
)
.bind(project.id.to_sql());
let row: Option<ConfluentConfigRow> =
query5.fetch_optional(&mut *transaction).await.into_core()?;
project.confluent_config = row.map(|r| r.confluent_config());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ impl SpacesRepository for SpacesSqlxDatabase {
async fn get_space_by_name(&self, name: &str) -> Result<Option<Space>> {
let mut transaction = self.database.begin().await.into_core()?;

let query1 = query_as("SELECT * FROM space WHERE space_name=$1").bind(name.to_sql());
let query1 = query_as("SELECT space_id, space_name FROM space WHERE space_name=$1")
.bind(name.to_sql());
let row: Option<SpaceRow> = query1.fetch_optional(&mut *transaction).await.into_core()?;
let space = match row.map(|r| r.space()) {
Some(mut space) => {
let query2 =
query_as("SELECT * FROM user_space WHERE space_id=$1").bind(space.id.to_sql());
query_as("SELECT space_id, user_email FROM user_space WHERE space_id=$1")
.bind(space.id.to_sql());
let rows: Vec<UserSpaceRow> =
query2.fetch_all(&mut *transaction).await.into_core()?;
let users = rows.into_iter().map(|r| r.user_email).collect();
Expand All @@ -102,12 +104,12 @@ impl SpacesRepository for SpacesSqlxDatabase {
async fn get_spaces(&self) -> Result<Vec<Space>> {
let mut transaction = self.database.begin().await.into_core()?;

let query = query_as("SELECT * FROM space");
let query = query_as("SELECT space_id, space_name FROM space");
let row: Vec<SpaceRow> = query.fetch_all(&mut *transaction).await.into_core()?;

let mut spaces = vec![];
for space_row in row {
let query2 = query_as("SELECT * FROM user_space WHERE space_id=$1")
let query2 = query_as("SELECT space_id, user_email FROM user_space WHERE space_id=$1")
.bind(space_row.space_id.to_sql());
let rows: Vec<UserSpaceRow> = query2.fetch_all(&mut *transaction).await.into_core()?;
let users = rows.into_iter().map(|r| r.user_email).collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl TrustContextsRepository for TrustContextsSqlxDatabase {
}

async fn get_default_trust_context(&self) -> Result<Option<NamedTrustContext>> {
let query = query_as("SELECT * FROM trust_context WHERE is_default=$1").bind(true.to_sql());
let query = query_as("SELECT name, trust_context_id, is_default, credential, authority_change_history, authority_route FROM trust_context WHERE is_default=$1").bind(true.to_sql());
let row: Option<NamedTrustContextRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -100,7 +100,7 @@ impl TrustContextsRepository for TrustContextsSqlxDatabase {
}

async fn get_trust_context(&self, name: &str) -> Result<Option<NamedTrustContext>> {
let query = query_as("SELECT * FROM trust_context WHERE name=$1").bind(name.to_sql());
let query = query_as("SELECT name, trust_context_id, is_default, credential, authority_change_history, authority_route FROM trust_context WHERE name=$1").bind(name.to_sql());
let row: Option<NamedTrustContextRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -109,7 +109,7 @@ impl TrustContextsRepository for TrustContextsSqlxDatabase {
}

async fn get_trust_contexts(&self) -> Result<Vec<NamedTrustContext>> {
let query = query_as("SELECT * FROM trust_context");
let query = query_as("SELECT name, trust_context_id, is_default, credential, authority_change_history, authority_route FROM trust_context");
let rows: Vec<NamedTrustContextRow> =
query.fetch_all(&self.database.pool).await.into_core()?;
rows.iter().map(|u| u.named_trust_context()).collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl UsersRepository for UsersSqlxDatabase {
}

async fn get_user(&self, email: &str) -> Result<Option<UserInfo>> {
let query = query_as("SELECT * FROM user WHERE email=$1").bind(email.to_sql());
let query = query_as("SELECT email, sub, nickname, name, picture, updated_at, email_verified, is_default FROM user WHERE email=$1").bind(email.to_sql());
let row: Option<UserRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -84,7 +84,7 @@ impl UsersRepository for UsersSqlxDatabase {
}

async fn get_users(&self) -> Result<Vec<UserInfo>> {
let query = query_as("SELECT * FROM user");
let query = query_as("SELECT email, sub, nickname, name, picture, updated_at, email_verified, is_default FROM user");
let rows: Vec<UserRow> = query.fetch_all(&self.database.pool).await.into_core()?;
Ok(rows.iter().map(|u| u.user()).collect())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl VaultsRepository for VaultsSqlxDatabase {
let mut transaction = self.database.begin().await.into_core()?;

// get the named vault
let query1 = query_as("SELECT * FROM vault WHERE name=$1").bind(name.to_sql());
let query1 = query_as("SELECT name, path, is_default, is_kms FROM vault WHERE name=$1")
.bind(name.to_sql());
let row: Option<VaultRow> = query1.fetch_optional(&mut *transaction).await.into_core()?;
let named_vault = row.map(|r| r.named_vault()).transpose()?;

Expand Down Expand Up @@ -107,7 +108,8 @@ impl VaultsRepository for VaultsSqlxDatabase {
}

async fn is_default(&self, name: &str) -> Result<bool> {
let query = query_as("SELECT * FROM vault WHERE name = $1").bind(name.to_sql());
let query = query_as("SELECT name, path, is_default, is_kms FROM vault WHERE name = $1")
.bind(name.to_sql());
let row: Option<VaultRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -116,13 +118,14 @@ impl VaultsRepository for VaultsSqlxDatabase {
}

async fn get_named_vaults(&self) -> Result<Vec<NamedVault>> {
let query = query_as("SELECT * FROM vault");
let query = query_as("SELECT name, path, is_default, is_kms FROM vault");
let rows: Vec<VaultRow> = query.fetch_all(&self.database.pool).await.into_core()?;
rows.iter().map(|r| r.named_vault()).collect()
}

async fn get_named_vault(&self, name: &str) -> Result<Option<NamedVault>> {
let query = query_as("SELECT * FROM vault WHERE name = $1").bind(name.to_sql());
let query = query_as("SELECT name, path, is_default, is_kms FROM vault WHERE name = $1")
.bind(name.to_sql());
let row: Option<VaultRow> = query
.fetch_optional(&self.database.pool)
.await
Expand All @@ -131,7 +134,9 @@ impl VaultsRepository for VaultsSqlxDatabase {
}

async fn get_default_vault(&self) -> Result<Option<NamedVault>> {
let query = query_as("SELECT * FROM vault WHERE is_default = $1").bind(true.to_sql());
let query =
query_as("SELECT name, path, is_default, is_kms FROM vault WHERE is_default = $1")
.bind(true.to_sql());
let row: Option<VaultRow> = query
.fetch_optional(&self.database.pool)
.await
Expand Down

0 comments on commit 6d9da66

Please sign in to comment.