optimize sqlite call

This commit is contained in:
vms 2019-08-18 18:16:22 +02:00
parent 167fc19b6a
commit b2fdcd889e
2 changed files with 22 additions and 32 deletions

View File

@ -1,24 +1,19 @@
use crate::errors::{err_msg, AppResult};
use crate::sqlite;
pub fn query(query: &str) -> AppResult<String> {
let response = sqlite::call(query.as_bytes());
pub fn query(bytes: &str) -> AppResult<String> {
let response = sqlite::call(bytes.as_bytes());
// Decode query result to a utf8 string
let result_str = std::str::from_utf8(&response);
// Log if there's an error
if result_str.is_err() {
log::error!("unable to decode result from bytes: {:#x?}", response);
match String::from_utf8(response) {
Ok(string) => Ok(string),
Err(err) => {
log::error!("unable to decode result from bytes: {:#x?}", bytes);
Err(err_msg(&format!(
"unable to decode result from bytes {:#x?}: {}",
bytes, err
)))
}
}
// Wrap error with a better message, and return Result
result_str
.map_err(|e| {
err_msg(&format!(
"unable to decode result from bytes {:#x?}: {}",
response, e
))
})
.map(|s| s.to_string())
}

View File

@ -1,24 +1,19 @@
use crate::errors::{err_msg, AppResult};
use crate::sqlite;
pub fn query(query: &str) -> AppResult<String> {
let response = sqlite::call(query.as_bytes());
pub fn query(bytes: &str) -> AppResult<String> {
let response = sqlite::call(bytes.as_bytes());
// Decode query result to a utf8 string
let result_str = std::str::from_utf8(&response);
// Log if there's an error
if result_str.is_err() {
log::error!("unable to decode result from bytes: {:#x?}", response);
match String::from_utf8(response) {
Ok(string) => Ok(string),
Err(err) => {
log::error!("unable to decode result from bytes: {:#x?}", bytes);
Err(err_msg(&format!(
"unable to decode result from bytes {:#x?}: {}",
bytes, err
)))
}
}
// Wrap error with a better message, and return Result
result_str
.map_err(|e| {
err_msg(&format!(
"unable to decode result from bytes {:#x?}: {}",
response, e
))
})
.map(|s| s.to_string())
}