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

Fix/sql queries backmerge #107

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src-tauri/src/models/storage/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ impl PersistentStorage {
Ok(PersistentStorage { conn, db_path })
}

/// Create a table within the database with an SQL query
pub fn execute_query(&self, query: &str) -> AvailResult<()> {
self.conn.execute(query, ())?;

Ok(())
}

pub fn execute_query_params<T: ToSql>(&self, query: &str, params: Vec<T>) -> AvailResult<()> {
self.conn
.execute(query, params_from_iter(params.into_iter()))?;

Ok(())
}

/// Save a vector of items of the same type to a table within the database with an SQL query
pub fn save<T: ToSql>(&self, data: Vec<T>, query: String) -> AvailResult<()> {
let _insert = self.conn.execute(
Expand Down
74 changes: 30 additions & 44 deletions src-tauri/src/services/local_storage/encrypted_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,18 @@ pub fn get_encrypted_data_by_flavour(
let address = get_address_string()?;
let network = get_network()?;

let query = format!(
"SELECT * FROM encrypted_data WHERE flavour='{}' AND owner='{}' AND network='{}'",
flavour.to_str(),
address,
network
);
let query = "SELECT * FROM encrypted_data WHERE flavour=?1 AND owner=?2 AND network=?3";

handle_encrypted_data_query(&query)
handle_encrypted_data_query_params(query, vec![flavour.to_str(), &address, &network])
}

/// get encrypted data by id
pub fn get_encrypted_data_by_id(id: &str) -> AvailResult<EncryptedData> {
let query = format!("SELECT * FROM encrypted_data WHERE id='{}'", id);
let query = "SELECT * FROM encrypted_data WHERE id=?1";

let encrypted_data = handle_encrypted_data_query(&query)?;
let encrypted_data = handle_encrypted_data_query_params(query, vec![id])?;

if encrypted_data.len() > 0 {
if !encrypted_data.is_empty() {
Ok(encrypted_data[0].clone())
} else {
Err(AvailError::new(
Expand Down Expand Up @@ -351,12 +346,10 @@ pub fn get_encrypted_data_by_nonce(nonce: &str) -> AvailResult<Option<EncryptedD
let address = get_address_string()?;
let network = get_network()?;

let query = format!(
"SELECT * FROM encrypted_data WHERE record_nonce='{}' AND owner='{}' AND network='{}'",
nonce, address, network
);
let query = "SELECT * FROM encrypted_data WHERE record_nonce=?1 AND owner=?2 AND network=?3";

let encrypted_data = handle_encrypted_data_query(&query)?;
let encrypted_data =
handle_encrypted_data_query_params(query, vec![nonce, &address, &network])?;

if !encrypted_data.is_empty() {
Ok(Some(encrypted_data[0].clone()))
Expand All @@ -371,12 +364,9 @@ pub fn get_encrypted_data_by_nonce(nonce: &str) -> AvailResult<Option<EncryptedD
pub fn update_encrypted_data_by_id(id: &str, ciphertext: &str, nonce: &str) -> AvailResult<()> {
let storage = PersistentStorage::new()?;

let query = format!(
"UPDATE encrypted_data SET ciphertext=?1, nonce=?2 WHERE id='{}'",
id
);
let query = "UPDATE encrypted_data SET ciphertext=?1, nonce=?2 WHERE id=?3";

storage.save_mixed(vec![&ciphertext, &nonce], query)?;
storage.save_mixed(vec![&ciphertext, &nonce, &id], query.to_string())?;

Ok(())
}
Expand All @@ -391,12 +381,13 @@ pub fn update_encrypted_data_spent_by_id(
let storage = PersistentStorage::new()?;
let updated_at = Utc::now();

let query = format!(
"UPDATE encrypted_data SET ciphertext=?1, nonce=?2, spent=?3, updated_at=?4 WHERE id='{}'",
id
);
let query =
"UPDATE encrypted_data SET ciphertext=?1, nonce=?2, spent=?3, updated_at=?4 WHERE id=?5";

storage.save_mixed(vec![&ciphertext, &nonce, &spent, &updated_at], query)?;
storage.save_mixed(
vec![&ciphertext, &nonce, &spent, &updated_at, &id],
query.to_string(),
)?;

Ok(())
}
Expand All @@ -410,14 +401,11 @@ pub fn update_encrypted_transaction_state_by_id(
) -> AvailResult<()> {
let storage = PersistentStorage::new()?;

let query = format!(
"UPDATE encrypted_data SET ciphertext=?1, nonce=?2, state=?3 WHERE id='{}'",
id
);
let query = "UPDATE encrypted_data SET ciphertext=?1, nonce=?2, state=?3 WHERE id=?4";

storage.save_mixed(
vec![&ciphertext, &nonce, &transaction_state.to_str()],
query,
vec![&ciphertext, &nonce, &transaction_state.to_str(), &id],
query.to_string(),
)?;

Ok(())
Expand All @@ -433,10 +421,7 @@ pub fn update_encrypted_transaction_confirmed_by_id(
) -> AvailResult<()> {
let storage = PersistentStorage::new()?;

let query = format!(
"UPDATE encrypted_data SET ciphertext=?1, nonce=?2, program_ids=?3, function_ids=?4, state=?5 WHERE id='{}'",
id
);
let query = "UPDATE encrypted_data SET ciphertext=?1, nonce=?2, program_ids=?3, function_ids=?4, state=?5 WHERE id=?6";

storage.save_mixed(
vec![
Expand All @@ -445,8 +430,9 @@ pub fn update_encrypted_transaction_confirmed_by_id(
&program_ids,
&function_ids,
&TransactionState::Confirmed.to_str(),
&id,
],
query,
query.to_string(),
)?;

Ok(())
Expand All @@ -456,11 +442,11 @@ pub fn update_encrypted_transaction_confirmed_by_id(
pub fn update_encrypted_data_synced_on_by_id(id: &str) -> AvailResult<()> {
let storage = PersistentStorage::new()?;

let query = format!("UPDATE encrypted_data SET synced_on=?1 WHERE id='{}'", id);
let query = "UPDATE encrypted_data SET synced_on=?1 WHERE id=?2";

let synced_on = Utc::now();

storage.save_mixed(vec![&synced_on], query)?;
storage.save_mixed(vec![&synced_on, &id], query.to_string())?;

Ok(())
}
Expand All @@ -469,9 +455,9 @@ pub fn update_encrypted_data_synced_on_by_id(id: &str) -> AvailResult<()> {
pub fn delete_encrypted_data_by_id(id: &str) -> AvailResult<()> {
let storage = PersistentStorage::new()?;

let query = format!("DELETE FROM encrypted_data WHERE id='{}'", id);
let query = "DELETE FROM encrypted_data WHERE id=?1";

storage.execute_query(&query)?;
storage.execute_query_params(query, vec![&id])?;

Ok(())
}
Expand All @@ -481,9 +467,9 @@ pub fn delete_encrypted_data_by_address() -> AvailResult<()> {
let storage = PersistentStorage::new()?;
let address = get_address_string()?;

let query = format!("DELETE FROM encrypted_data WHERE owner='{}'", address);
let query = "DELETE FROM encrypted_data WHERE owner=?1";

storage.execute_query(&query)?;
storage.execute_query_params(query, vec![&address])?;

Ok(())
}
Expand All @@ -501,8 +487,8 @@ pub fn delete_user_encrypted_data() -> AvailResult<()> {
_ => {
return Err(AvailError::new(
AvailErrorType::Internal,
"Error deleting encrypted data ".to_string(),
"".to_string(),
"Error deleting Encrypted Data".to_string(),
"Error deleting Encrypted Data".to_string(),
))
}
},
Expand Down
Loading