Update DieselRegistry insert_node to not use raw SQL
When trying to insert a node using the diesel registry en error
was returned due to the raw SQL. Using diesel to do the query fixes
this issue.
//! Provides the "insert node" operation for the `DieselRegistry`.
use diesel::{
- dsl::{delete, insert_into, sql_query, update},
+ dsl::{delete, insert_into, update},
prelude::*,
};
@@ -43,19 +43,16 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::pg::PgCo
check_node_required_fields_are_not_empty(&node)?;
self.conn.transaction::<(), _, _>(|| {
- // Verify that the node's endpoints are unique. This requires a raw SQL query because of
- // the complicated `WHERE` statement.
+ // Verify that the node's endpoints are unique.
let filters = node
.endpoints
.iter()
- .map(|endpoint| format!("endpoint = {}", endpoint))
- .collect::<Vec<_>>()
- .join(" OR ");
- let duplicate_endpoint = sql_query(format!(
- "SELECT * FROM splinter_nodes_endpoints WHERE identity <> {} AND ({})",
- node.identity, filters
- ))
- .get_result::<NodeEndpointsModel>(self.conn)
+ .map(|endpoint| endpoint.to_string())
+ .collect::<Vec<_>>();
+
+ let duplicate_endpoint = splinter_nodes_endpoints::table
+ .filter(splinter_nodes_endpoints::endpoint.eq_any(filters))
+ .first::<NodeEndpointsModel>(self.conn)
.optional()
.map_err(|err| {
RegistryError::general_error_with_source(
@@ -63,6 +60,7 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::pg::PgCo
Box::new(err),
)
})?;
+
if let Some(endpoint) = duplicate_endpoint {
return Err(RegistryError::from(InvalidNodeError::DuplicateEndpoint(
endpoint.endpoint,
@@ -183,19 +181,16 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::sqlite::
check_node_required_fields_are_not_empty(&node)?;
self.conn.transaction::<(), _, _>(|| {
- // Verify that the node's endpoints are unique. This requires a raw SQL query because of
- // the complicated `WHERE` statement.
+ // Verify that the node's endpoints are unique.
let filters = node
.endpoints
.iter()
- .map(|endpoint| format!("endpoint = {}", endpoint))
- .collect::<Vec<_>>()
- .join(" OR ");
- let duplicate_endpoint = sql_query(format!(
- "SELECT * FROM splinter_nodes_endpoints WHERE identity <> {} AND ({})",
- node.identity, filters
- ))
- .get_result::<NodeEndpointsModel>(self.conn)
+ .map(|endpoint| endpoint.to_string())
+ .collect::<Vec<_>>();
+
+ let duplicate_endpoint = splinter_nodes_endpoints::table
+ .filter(splinter_nodes_endpoints::endpoint.eq_any(filters))
+ .first::<NodeEndpointsModel>(self.conn)
.optional()
.map_err(|err| {
RegistryError::general_error_with_source(
@@ -203,6 +198,7 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::sqlite::
Box::new(err),
)
})?;
+
if let Some(endpoint) = duplicate_endpoint {
return Err(RegistryError::from(InvalidNodeError::DuplicateEndpoint(
endpoint.endpoint,
