mirror of
https://gitee.com/openharmony/security_asset
synced 2024-11-27 10:00:46 +00:00
Merge branch 'master' of https://gitee.com/openharmony/security_asset
This commit is contained in:
commit
9f927dbf29
@ -44,10 +44,10 @@ fn delete_on_package_removed(user_id: i32, owner: Vec<u8>) -> Result<bool> {
|
||||
cond.insert(column::OWNER, Value::Bytes(owner));
|
||||
cond.insert(column::IS_PERSISTENT, Value::Bool(false));
|
||||
let mut db = Database::build(user_id)?;
|
||||
let _ = db.delete_datas(&cond, None)?;
|
||||
let _ = db.delete_datas(&cond, None, false)?;
|
||||
|
||||
cond.insert(column::IS_PERSISTENT, Value::Bool(true));
|
||||
db.is_data_exists(&cond)
|
||||
db.is_data_exists(&cond, false)
|
||||
}
|
||||
|
||||
fn clear_cryptos(calling_info: &CallingInfo) {
|
||||
|
@ -72,15 +72,15 @@ fn resolve_conflict(
|
||||
match attrs.get(&Tag::ConflictResolution) {
|
||||
Some(Value::Number(num)) if *num == ConflictResolution::Overwrite as u32 => {
|
||||
encrypt(calling, db_data)?;
|
||||
db.replace_datas(query, db_data)
|
||||
db.replace_datas(query, false, db_data)
|
||||
},
|
||||
_ => {
|
||||
let mut condition = query.clone();
|
||||
condition.insert(column::SYNC_TYPE, Value::Number(SyncType::TrustedAccount as u32));
|
||||
condition.insert(column::SYNC_STATUS, Value::Number(SyncStatus::SyncDel as u32));
|
||||
if db.is_data_exists(&condition)? {
|
||||
if db.is_data_exists(&condition, false)? {
|
||||
encrypt(calling, db_data)?;
|
||||
db.replace_datas(&condition, db_data)
|
||||
db.replace_datas(&condition, false, db_data)
|
||||
} else {
|
||||
log_throw_error!(ErrCode::Duplicated, "[FATAL][SA]The specified alias already exists.")
|
||||
}
|
||||
@ -179,7 +179,7 @@ fn local_add(attributes: &AssetMap, calling_info: &CallingInfo) -> Result<()> {
|
||||
|
||||
let query = get_query_condition(calling_info, attributes)?;
|
||||
let mut db = Database::build(calling_info.user_id())?;
|
||||
if db.is_data_exists(&query)? {
|
||||
if db.is_data_exists(&query, false)? {
|
||||
resolve_conflict(calling_info, &mut db, attributes, &query, &mut db_data)
|
||||
} else {
|
||||
encrypt(calling_info, &mut db_data)?;
|
||||
|
@ -52,6 +52,7 @@ fn query_key_attrs(calling_info: &CallingInfo, db_data: &DbMap) -> Result<(Acces
|
||||
&vec![column::ACCESSIBILITY, column::REQUIRE_PASSWORD_SET],
|
||||
db_data,
|
||||
None,
|
||||
true,
|
||||
)?;
|
||||
match results.len() {
|
||||
0 => log_throw_error!(ErrCode::NotFound, "[FATAL][SA]No data that meets the query conditions is found."),
|
||||
|
@ -24,7 +24,7 @@ use asset_db_operator::{
|
||||
types::{column, DbMap, QueryOptions, DB_DATA_VERSION},
|
||||
};
|
||||
use asset_definition::{
|
||||
log_throw_error, throw_error, AssetMap, AuthType, ErrCode, Extension, Result, ReturnType, Tag, Value, SyncStatus
|
||||
log_throw_error, throw_error, AssetMap, AuthType, ErrCode, Extension, Result, ReturnType, Tag, Value
|
||||
};
|
||||
|
||||
use crate::operations::common;
|
||||
@ -52,7 +52,7 @@ fn upgrade_version(db: &mut Database, calling_info: &CallingInfo, db_data: &mut
|
||||
let mut query_data = DbMap::new();
|
||||
query_data.insert_attr(column::ID, db_data.get_num_attr(&column::ID)?);
|
||||
|
||||
let update_num = db.update_datas(&query_data, &update_data)?;
|
||||
let update_num = db.update_datas(&query_data, true, &update_data)?;
|
||||
if update_num == 0 {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]Upgrade asset failed.");
|
||||
}
|
||||
@ -87,7 +87,7 @@ fn exec_crypto(calling_info: &CallingInfo, query: &AssetMap, db_data: &mut DbMap
|
||||
|
||||
fn query_all(calling_info: &CallingInfo, db_data: &mut DbMap, query: &AssetMap) -> Result<Vec<AssetMap>> {
|
||||
let mut db = Database::build(calling_info.user_id())?;
|
||||
let mut results = db.query_datas(&vec![], db_data, None)?;
|
||||
let mut results = db.query_datas(&vec![], db_data, None, true)?;
|
||||
match results.len() {
|
||||
0 => throw_error!(ErrCode::NotFound, "[FATAL]The data to be queried does not exist."),
|
||||
1 => {
|
||||
@ -141,19 +141,11 @@ fn get_query_options(attrs: &AssetMap) -> QueryOptions {
|
||||
|
||||
pub(crate) fn query_attrs(calling_info: &CallingInfo, db_data: &DbMap, attrs: &AssetMap) -> Result<Vec<AssetMap>> {
|
||||
let mut results =
|
||||
Database::build(calling_info.user_id())?.query_datas(&vec![], db_data, Some(&get_query_options(attrs)))?;
|
||||
Database::build(calling_info.user_id())?.query_datas(&vec![], db_data, Some(&get_query_options(attrs)), true)?;
|
||||
if results.is_empty() {
|
||||
return throw_error!(ErrCode::NotFound, "[FATAL]The data to be queried does not exist.");
|
||||
}
|
||||
|
||||
results.retain(|data| {
|
||||
if let Some(Value::Number(status)) = data.get(&column::SYNC_STATUS) {
|
||||
!matches!(status, x if *x == SyncStatus::SyncDel as u32)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
for data in &mut results {
|
||||
data.remove(&column::SECRET);
|
||||
}
|
||||
|
@ -65,30 +65,19 @@ pub(crate) fn remove(query: &AssetMap, calling_info: &CallingInfo) -> Result<()>
|
||||
add_system_attrs(&mut update_db_data)?;
|
||||
add_normal_attrs(&mut update_db_data);
|
||||
|
||||
let mut results =
|
||||
Database::build(calling_info.user_id())?.query_datas(&vec![], &db_data, None)?;
|
||||
if results.is_empty() {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]The data to be deleted does not exist.");
|
||||
}
|
||||
results.retain(|data| {
|
||||
if let Some(Value::Number(status)) = data.get(&column::SYNC_STATUS) {
|
||||
!matches!(status, x if *x == SyncStatus::SyncDel as u32)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
let results = Database::build(calling_info.user_id())?.query_datas(&vec![], &db_data, None, true)?;
|
||||
if results.is_empty() {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]The data to be deleted does not exist.");
|
||||
}
|
||||
|
||||
let update_num = Database::build(calling_info.user_id())?.update_datas(&db_data, &update_db_data)?;
|
||||
let update_num = Database::build(calling_info.user_id())?.update_datas(&db_data, true, &update_db_data)?;
|
||||
if update_num == 0 {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]The data to be deleted does not exist.");
|
||||
}
|
||||
|
||||
let mut reverse_condition = DbMap::new();
|
||||
reverse_condition.insert(column::SYNC_TYPE, Value::Number(SyncType::TrustedAccount as u32));
|
||||
let remove_num = Database::build(calling_info.user_id())?.delete_datas(&db_data, Some(&reverse_condition))?;
|
||||
let remove_num = Database::build(calling_info.user_id())?.delete_datas(&db_data, Some(&reverse_condition), false)?;
|
||||
logi!("Delete num: {}", remove_num);
|
||||
|
||||
common::inform_asset_ext(query, calling_info.user_id());
|
||||
|
@ -94,25 +94,14 @@ pub(crate) fn update(query: &AssetMap, update: &AssetMap, calling_info: &Calling
|
||||
add_system_attrs(update, &mut update_db_data)?;
|
||||
add_normal_attrs(&mut update_db_data);
|
||||
|
||||
let mut results =
|
||||
Database::build(calling_info.user_id())?.query_datas(&vec![], &query_db_data, None)?;
|
||||
if results.is_empty() {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]The asset to update is not found.");
|
||||
}
|
||||
results.retain(|data| {
|
||||
if let Some(Value::Number(status)) = data.get(&column::SYNC_STATUS) {
|
||||
!matches!(status, x if *x == SyncStatus::SyncDel as u32)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
let results = Database::build(calling_info.user_id())?.query_datas(&vec![], &query_db_data, None, true)?;
|
||||
if results.is_empty() {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]The asset to update is not found.");
|
||||
}
|
||||
|
||||
let mut db = Database::build(calling_info.user_id())?;
|
||||
if update.contains_key(&Tag::Secret) {
|
||||
let mut results = db.query_datas(&vec![], &query_db_data, None)?;
|
||||
let mut results = db.query_datas(&vec![], &query_db_data, None, true)?;
|
||||
if results.len() != 1 {
|
||||
return log_throw_error!(
|
||||
ErrCode::NotFound,
|
||||
@ -132,7 +121,7 @@ pub(crate) fn update(query: &AssetMap, update: &AssetMap, calling_info: &Calling
|
||||
}
|
||||
|
||||
// call sql to update
|
||||
let update_num = db.update_datas(&query_db_data, &update_db_data)?;
|
||||
let update_num = db.update_datas(&query_db_data, true, &update_db_data)?;
|
||||
if update_num == 0 {
|
||||
return log_throw_error!(ErrCode::NotFound, "[FATAL]Update asset failed, update 0 asset.");
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ impl Database {
|
||||
query.insert_attr(column::ALIAS, datas.get_bytes_attr(&column::ALIAS)?.clone());
|
||||
query.insert_attr(column::OWNER, datas.get_bytes_attr(&column::OWNER)?.clone());
|
||||
query.insert_attr(column::OWNER_TYPE, datas.get_enum_attr::<OwnerType>(&column::OWNER_TYPE)?);
|
||||
if e.is_data_exists(&query)? {
|
||||
if e.is_data_exists(&query, false)? {
|
||||
log_throw_error!(ErrCode::Duplicated, "[FATAL]The data with the specified alias already exists.")
|
||||
} else {
|
||||
e.insert_row(datas)
|
||||
@ -321,14 +321,15 @@ impl Database {
|
||||
/// datas.insert(column::ALIAS, Value::Bytes(b"alias".to_ver()));
|
||||
/// datas.insert("value", Value::Bytes(b"delete_value".to_vec()));
|
||||
/// let user_id = 100;
|
||||
/// let ret = Database::build(user_id)?.delete_datas(&cond, None);
|
||||
/// let ret = Database::build(user_id)?.delete_datas(&cond, None, false);
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
#[inline(always)]
|
||||
pub fn delete_datas(&mut self, condition: &DbMap, reverse_condition: Option<&DbMap>) -> Result<i32> {
|
||||
pub fn delete_datas(&mut self, condition: &DbMap, reverse_condition: Option<&DbMap>,
|
||||
is_filter_sync: bool) -> Result<i32> {
|
||||
let _lock = self.db_lock.mtx.lock().unwrap();
|
||||
let closure = |e: &Table| e.delete_row(condition, reverse_condition);
|
||||
let closure = |e: &Table| e.delete_row(condition, reverse_condition, is_filter_sync);
|
||||
self.restore_if_exec_fail(closure)
|
||||
}
|
||||
|
||||
@ -349,12 +350,12 @@ impl Database {
|
||||
/// cond.insert(column::ALIAS, Value::Bytes(b"alias".to_ver()));
|
||||
/// let datas = DbMap::from([("alias", Value::Bytes(b"update_value".to_vec()))]);
|
||||
/// let user_id = 100;
|
||||
/// let ret = Database::build(user_id)?.update_datas(&condition, &datas);
|
||||
/// let ret = Database::build(user_id)?.update_datas(&condition, true, &datas);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn update_datas(&mut self, condition: &DbMap, datas: &DbMap) -> Result<i32> {
|
||||
pub fn update_datas(&mut self, condition: &DbMap, is_filter_sync: bool, datas: &DbMap) -> Result<i32> {
|
||||
let _lock = self.db_lock.mtx.lock().unwrap();
|
||||
let closure = |e: &Table| e.update_row(condition, datas);
|
||||
let closure = |e: &Table| e.update_row(condition, is_filter_sync, datas);
|
||||
self.restore_if_exec_fail(closure)
|
||||
}
|
||||
|
||||
@ -372,12 +373,12 @@ impl Database {
|
||||
/// datas.insert(column::OWNER_TYPE, Value::Number(OwnerType::Native as u32));
|
||||
/// datas.insert(column::ALIAS, Value::Bytes(b"alias".to_ver()));
|
||||
/// let user_id = 100;
|
||||
/// let exist = Database::build(user_id)?.is_data_exists(&datas);
|
||||
/// let exist = Database::build(user_id)?.is_data_exists(&datas, false);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn is_data_exists(&mut self, condition: &DbMap) -> Result<bool> {
|
||||
pub fn is_data_exists(&mut self, condition: &DbMap, is_filter_sync: bool) -> Result<bool> {
|
||||
let _lock = self.db_lock.mtx.lock().unwrap();
|
||||
let closure = |e: &Table| e.is_data_exists(condition);
|
||||
let closure = |e: &Table| e.is_data_exists(condition, is_filter_sync);
|
||||
self.restore_if_exec_fail(closure)
|
||||
}
|
||||
|
||||
@ -396,7 +397,7 @@ impl Database {
|
||||
/// cond.insert(column::OWNER_TYPE, Value::Number(OwnerType::Native as u32));
|
||||
/// cond.insert(column::ALIAS, Value::Bytes(b"alias".to_ver()));
|
||||
/// let user_id = 100;
|
||||
/// let ret = Database::build(user_id)?.query_datas(&vec![], &cond, None);
|
||||
/// let ret = Database::build(user_id)?.query_datas(&vec![], &cond, None, false);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn query_datas(
|
||||
@ -404,16 +405,17 @@ impl Database {
|
||||
columns: &Vec<&'static str>,
|
||||
condition: &DbMap,
|
||||
query_options: Option<&QueryOptions>,
|
||||
is_filter_sync: bool,
|
||||
) -> Result<Vec<DbMap>> {
|
||||
let _lock = self.db_lock.mtx.lock().unwrap();
|
||||
let closure = |e: &Table| e.query_row(columns, condition, query_options, COLUMN_INFO);
|
||||
let closure = |e: &Table| e.query_row(columns, condition, query_options, is_filter_sync, COLUMN_INFO);
|
||||
self.restore_if_exec_fail(closure)
|
||||
}
|
||||
|
||||
/// Delete old data and insert new data.
|
||||
pub fn replace_datas(&mut self, condition: &DbMap, datas: &DbMap) -> Result<()> {
|
||||
pub fn replace_datas(&mut self, condition: &DbMap, is_filter_sync: bool, datas: &DbMap) -> Result<()> {
|
||||
let _lock = self.db_lock.mtx.lock().unwrap();
|
||||
let closure = |e: &Table| e.replace_row(condition, datas);
|
||||
let closure = |e: &Table| e.replace_row(condition, is_filter_sync, datas);
|
||||
self.restore_if_exec_fail(closure)
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,18 @@ fn bind_datas(datas: &DbMap, stmt: &Statement, index: &mut i32) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bind_where_datas(datas: &DbMap, stmt: &Statement, index: &mut i32) -> Result<()> {
|
||||
for (key, value) in datas.iter() {
|
||||
if *key == "SyncType" {
|
||||
stmt.bind_data(*index, value)?;
|
||||
*index += 1;
|
||||
}
|
||||
stmt.bind_data(*index, value)?;
|
||||
*index += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn build_sql_columns_not_empty(columns: &Vec<&str>, sql: &mut String) {
|
||||
for i in 0..columns.len() {
|
||||
@ -69,18 +81,26 @@ fn build_sql_columns(columns: &Vec<&str>, sql: &mut String) {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn build_sql_where(conditions: &DbMap, sql: &mut String) {
|
||||
if !conditions.is_empty() {
|
||||
fn build_sql_where(conditions: &DbMap, filter: bool, sql: &mut String) {
|
||||
if !conditions.is_empty() || filter {
|
||||
sql.push_str(" where ");
|
||||
for (i, column_name) in conditions.keys().enumerate() {
|
||||
if *column_name == "SyncType" {
|
||||
sql.push_str("(SyncType & ?) <> 0");
|
||||
} else {
|
||||
sql.push_str(column_name);
|
||||
sql.push_str("=?");
|
||||
if filter {
|
||||
sql.push_str("SyncStatus <> 2");
|
||||
if !conditions.is_empty() {
|
||||
sql.push_str(" and ");
|
||||
}
|
||||
if i != conditions.len() - 1 {
|
||||
sql.push_str(" and ")
|
||||
}
|
||||
if !conditions.is_empty() {
|
||||
for (i, column_name) in conditions.keys().enumerate() {
|
||||
if *column_name == "SyncType" {
|
||||
sql.push_str("(SyncType & ?) = ?");
|
||||
} else {
|
||||
sql.push_str(column_name);
|
||||
sql.push_str("=?");
|
||||
}
|
||||
if i != conditions.len() - 1 {
|
||||
sql.push_str(" and ")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -280,15 +300,16 @@ impl<'a> Table<'a> {
|
||||
/// ```
|
||||
/// // SQL: delete from table_name where id=2
|
||||
/// let condition = &DbMap::from([("id", Value::Number(2)]);
|
||||
/// let ret = table.delete_row(condition, None);
|
||||
/// let ret = table.delete_row(condition, None, false);
|
||||
/// ```
|
||||
pub(crate) fn delete_row(&self, condition: &DbMap, reverse_condition: Option<&DbMap>) -> Result<i32> {
|
||||
pub(crate) fn delete_row(&self, condition: &DbMap, reverse_condition: Option<&DbMap>,
|
||||
is_filter_sync: bool) -> Result<i32> {
|
||||
let mut sql = format!("delete from {}", self.table_name);
|
||||
build_sql_where(condition, &mut sql);
|
||||
build_sql_where(condition, is_filter_sync, &mut sql);
|
||||
build_sql_reverse_condition(reverse_condition, &mut sql);
|
||||
let stmt = Statement::prepare(&sql, self.db)?;
|
||||
let mut index = 1;
|
||||
bind_datas(condition, &stmt, &mut index)?;
|
||||
bind_where_datas(condition, &stmt, &mut index)?;
|
||||
if let Some(datas) = reverse_condition {
|
||||
bind_datas(datas, &stmt, &mut index)?;
|
||||
}
|
||||
@ -305,9 +326,9 @@ impl<'a> Table<'a> {
|
||||
/// // SQL: update table_name set alias='update_value' where id=2
|
||||
/// let condition = &DbMap::from([("id", Value::Number(2)]);
|
||||
/// let datas = &DbMap::from([("alias", Value::Bytes(b"update_value")]);
|
||||
/// let ret = table.update_row(conditions, datas);
|
||||
/// let ret = table.update_row(conditions, false, datas);
|
||||
/// ```
|
||||
pub(crate) fn update_row(&self, condition: &DbMap, datas: &DbMap) -> Result<i32> {
|
||||
pub(crate) fn update_row(&self, condition: &DbMap, is_filter_sync: bool, datas: &DbMap) -> Result<i32> {
|
||||
let mut sql = format!("update {} set ", self.table_name);
|
||||
for (i, column_name) in datas.keys().enumerate() {
|
||||
sql.push_str(column_name);
|
||||
@ -316,11 +337,11 @@ impl<'a> Table<'a> {
|
||||
sql.push(',');
|
||||
}
|
||||
}
|
||||
build_sql_where(condition, &mut sql);
|
||||
build_sql_where(condition, is_filter_sync, &mut sql);
|
||||
let stmt = Statement::prepare(&sql, self.db)?;
|
||||
let mut index = 1;
|
||||
bind_datas(datas, &stmt, &mut index)?;
|
||||
bind_datas(condition, &stmt, &mut index)?;
|
||||
bind_where_datas(condition, &stmt, &mut index)?;
|
||||
stmt.step()?;
|
||||
let count = unsafe { SqliteChanges(self.db.handle as _) };
|
||||
Ok(count)
|
||||
@ -334,13 +355,14 @@ impl<'a> Table<'a> {
|
||||
///
|
||||
/// ```
|
||||
/// // SQL: select alias,blobs from table_name
|
||||
/// let result_set = table.query_datas_with_key_value(&vec!["alias", "blobs"], &vec![]);
|
||||
/// let result_set = table.query_datas_with_key_value(&vec!["alias", "blobs"], false, &vec![]);
|
||||
/// ```
|
||||
pub(crate) fn query_row(
|
||||
&self,
|
||||
columns: &Vec<&'static str>,
|
||||
condition: &DbMap,
|
||||
query_options: Option<&QueryOptions>,
|
||||
is_filter_sync: bool,
|
||||
column_info: &'static [ColumnInfo],
|
||||
) -> Result<Vec<DbMap>> {
|
||||
let mut sql = String::from("select ");
|
||||
@ -350,11 +372,11 @@ impl<'a> Table<'a> {
|
||||
build_sql_columns(columns, &mut sql);
|
||||
sql.push_str(" from ");
|
||||
sql.push_str(self.table_name.as_str());
|
||||
build_sql_where(condition, &mut sql);
|
||||
build_sql_where(condition, is_filter_sync, &mut sql);
|
||||
build_sql_query_options(query_options, &mut sql);
|
||||
let stmt = Statement::prepare(&sql, self.db)?;
|
||||
let mut index = 1;
|
||||
bind_datas(condition, &stmt, &mut index)?;
|
||||
bind_where_datas(condition, &stmt, &mut index)?;
|
||||
let mut result = vec![];
|
||||
while stmt.step()? == SQLITE_ROW {
|
||||
let mut record = DbMap::new();
|
||||
@ -384,14 +406,14 @@ impl<'a> Table<'a> {
|
||||
///
|
||||
/// ```
|
||||
/// // SQL: select count(*) as count from table_name where id=3
|
||||
/// let count = table.count_datas(&DbMap::from([("id", Value::Number(3))]));
|
||||
/// let count = table.count_datas(&DbMap::from([("id", Value::Number(3))]), false);
|
||||
/// ```
|
||||
pub(crate) fn count_datas(&self, condition: &DbMap) -> Result<u32> {
|
||||
pub(crate) fn count_datas(&self, condition: &DbMap, is_filter_sync: bool) -> Result<u32> {
|
||||
let mut sql = format!("select count(*) as count from {}", self.table_name);
|
||||
build_sql_where(condition, &mut sql);
|
||||
build_sql_where(condition, is_filter_sync, &mut sql);
|
||||
let stmt = Statement::prepare(&sql, self.db)?;
|
||||
let mut index = 1;
|
||||
bind_datas(condition, &stmt, &mut index)?;
|
||||
bind_where_datas(condition, &stmt, &mut index)?;
|
||||
stmt.step()?;
|
||||
let count = stmt.query_column_int(0);
|
||||
Ok(count)
|
||||
@ -404,10 +426,10 @@ impl<'a> Table<'a> {
|
||||
/// ```
|
||||
/// // SQL: select count(*) as count from table_name where id=3 and alias='alias'
|
||||
/// let exits = table
|
||||
/// .is_data_exists(&DbMap::from([("id", Value::Number(3)), ("alias", Value::Bytes(b"alias"))]));
|
||||
/// .is_data_exists(&DbMap::from([("id", Value::Number(3)), ("alias", Value::Bytes(b"alias"))]), false);
|
||||
/// ```
|
||||
pub(crate) fn is_data_exists(&self, cond: &DbMap) -> Result<bool> {
|
||||
let ret = self.count_datas(cond);
|
||||
pub(crate) fn is_data_exists(&self, cond: &DbMap, is_filter_sync: bool) -> Result<bool> {
|
||||
let ret = self.count_datas(cond, is_filter_sync);
|
||||
match ret {
|
||||
Ok(count) => Ok(count > 0),
|
||||
Err(e) => Err(e),
|
||||
@ -452,10 +474,10 @@ impl<'a> Table<'a> {
|
||||
self.db.exec(sql.as_str())
|
||||
}
|
||||
|
||||
pub(crate) fn replace_row(&self, condition: &DbMap, datas: &DbMap) -> Result<()> {
|
||||
pub(crate) fn replace_row(&self, condition: &DbMap, is_filter_sync: bool, datas: &DbMap) -> Result<()> {
|
||||
let mut trans = Transaction::new(self.db);
|
||||
trans.begin()?;
|
||||
if self.delete_row(condition, None).is_ok() && self.insert_row(datas).is_ok() {
|
||||
if self.delete_row(condition, None, is_filter_sync).is_ok() && self.insert_row(datas).is_ok() {
|
||||
trans.commit()
|
||||
} else {
|
||||
trans.rollback()
|
||||
|
@ -131,8 +131,8 @@ fn insert_data_with_different_alias() {
|
||||
assert_eq!(count, 1);
|
||||
|
||||
let ret = db
|
||||
.query_datas(&vec![], &DbMap::from([(column::OWNER, Value::Bytes(column::OWNER.as_bytes().to_vec()))]), None)
|
||||
.unwrap();
|
||||
.query_datas(&vec![], &DbMap::from([(column::OWNER, Value::Bytes(column::OWNER.as_bytes().to_vec()))]),
|
||||
None, false).unwrap();
|
||||
assert_eq!(ret.len(), 2);
|
||||
remove_dir();
|
||||
}
|
||||
@ -145,13 +145,13 @@ fn delete_data() {
|
||||
datas.insert(column::OWNER, Value::Bytes(column::OWNER.as_bytes().to_vec()));
|
||||
datas.insert(column::ALIAS, Value::Bytes(column::ALIAS.as_bytes().to_vec()));
|
||||
|
||||
let ret = db.is_data_exists(&datas).unwrap();
|
||||
let ret = db.is_data_exists(&datas, false).unwrap();
|
||||
assert!(ret);
|
||||
|
||||
let count = db.delete_datas(&datas, None).unwrap();
|
||||
let count = db.delete_datas(&datas, None, false).unwrap();
|
||||
assert_eq!(count, 1);
|
||||
|
||||
let ret = db.is_data_exists(&datas).unwrap();
|
||||
let ret = db.is_data_exists(&datas, false).unwrap();
|
||||
assert!(!ret);
|
||||
|
||||
remove_dir();
|
||||
@ -166,10 +166,11 @@ fn update_data() {
|
||||
datas.insert(column::ALIAS, Value::Bytes(column::ALIAS.as_bytes().to_vec()));
|
||||
let update_time: Vec<u8> = vec![2];
|
||||
let count =
|
||||
db.update_datas(&datas, &DbMap::from([(column::UPDATE_TIME, Value::Bytes(update_time.clone()))])).unwrap();
|
||||
db.update_datas(&datas, true,
|
||||
&DbMap::from([(column::UPDATE_TIME, Value::Bytes(update_time.clone()))])).unwrap();
|
||||
assert_eq!(count, 1);
|
||||
|
||||
let res = db.query_datas(&vec![], &datas, None).unwrap();
|
||||
let res = db.query_datas(&vec![], &datas, None, false).unwrap();
|
||||
assert_eq!(res.len(), 1);
|
||||
let query_update_time = res[0].get_bytes_attr(&column::UPDATE_TIME).unwrap();
|
||||
assert_eq!(update_time.len(), query_update_time.len());
|
||||
@ -207,6 +208,7 @@ fn query_ordered_data() {
|
||||
&vec![column::ID, column::ALIAS],
|
||||
&DbMap::from([(column::OWNER, Value::Bytes(column::OWNER.as_bytes().to_vec()))]),
|
||||
Some(&query),
|
||||
false
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(res.len(), 2);
|
||||
@ -240,7 +242,7 @@ fn backup_and_restore() {
|
||||
let mut def = DbMap::from(DB_DATA);
|
||||
add_bytes_column(&mut def);
|
||||
|
||||
db.query_datas(&vec![], &def, None).unwrap();
|
||||
db.query_datas(&vec![], &def, None, false).unwrap();
|
||||
drop(db);
|
||||
remove_dir();
|
||||
}
|
||||
@ -266,7 +268,7 @@ fn query_mismatch_type_data() {
|
||||
let mut db = Database::build(0).unwrap();
|
||||
db.insert_datas(&data).unwrap();
|
||||
|
||||
assert_eq!(ErrCode::FileOperationError, db.query_datas(&vec![], &data, None).unwrap_err().code);
|
||||
assert_eq!(ErrCode::FileOperationError, db.query_datas(&vec![], &data, None, false).unwrap_err().code);
|
||||
drop(db);
|
||||
remove_dir();
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ fn table_restore() {
|
||||
fs::copy("/data/asset_test/0/asset.db.backup", "/data/asset_test/0/asset.db").unwrap();
|
||||
db.open().unwrap();
|
||||
let table = Table::new("table_name", &db);
|
||||
let count = table.count_datas(&DbMap::new()).unwrap();
|
||||
let count = table.count_datas(&DbMap::new(), false).unwrap();
|
||||
assert_eq!(count, 1);
|
||||
db.close();
|
||||
fs::remove_dir_all("/data/asset_test/0").unwrap();
|
||||
@ -119,16 +119,16 @@ fn data_life_circle() {
|
||||
datas.insert_attr("Owner", b"owner1".to_vec());
|
||||
datas.insert_attr("Alias", b"alias1".to_vec());
|
||||
let table = Table::new("table_name", &db);
|
||||
assert!(table.is_data_exists(&datas).unwrap());
|
||||
assert!(table.is_data_exists(&datas, false).unwrap());
|
||||
|
||||
datas.insert_attr("Owner", b"owner1".to_vec());
|
||||
datas.insert_attr("Alias", b"alias2".to_vec());
|
||||
assert!(!table.is_data_exists(&datas).unwrap());
|
||||
assert!(!table.is_data_exists(&datas, false).unwrap());
|
||||
|
||||
datas.insert_attr("Owner", b"owner2".to_vec());
|
||||
datas.insert_attr("Alias", b"alias3".to_vec());
|
||||
assert_eq!(1, table.update_row(&datas, &DbMap::from([("value", Value::Bytes(b"dddd".to_vec()))])).unwrap());
|
||||
assert_eq!(1, table.delete_row(&datas, None).unwrap());
|
||||
assert_eq!(1, table.update_row(&datas, false, &DbMap::from([("value", Value::Bytes(b"dddd".to_vec()))])).unwrap());
|
||||
assert_eq!(1, table.delete_row(&datas, None, false).unwrap());
|
||||
fs::remove_dir_all("/data/asset_test/0").unwrap();
|
||||
}
|
||||
|
||||
@ -220,12 +220,12 @@ fn insert_query_row() {
|
||||
let datas = DbMap::from([("alias", Value::Bytes(b"alias1".to_vec()))]);
|
||||
assert_eq!(table.insert_row(&datas).unwrap(), 1);
|
||||
|
||||
let result_set = table.query_row(&vec![], &DbMap::new(), None, columns).unwrap();
|
||||
let result_set = table.query_row(&vec![], &DbMap::new(), None, false, columns).unwrap();
|
||||
assert_eq!(result_set.len(), 2);
|
||||
|
||||
let count = table.count_datas(&DbMap::new()).unwrap();
|
||||
let count = table.count_datas(&DbMap::new(), false).unwrap();
|
||||
assert_eq!(count, 2);
|
||||
let count = table.count_datas(&DbMap::from([("id", Value::Number(3))])).unwrap();
|
||||
let count = table.count_datas(&DbMap::from([("id", Value::Number(3))]), false).unwrap();
|
||||
assert_eq!(count, 1);
|
||||
|
||||
fs::remove_dir_all("/data/asset_test/0").unwrap();
|
||||
@ -249,10 +249,10 @@ fn update_delete_row() {
|
||||
|
||||
let conditions = DbMap::from([("id", Value::Number(2))]);
|
||||
let datas = DbMap::from([("alias", Value::Bytes(b"test_update".to_vec()))]);
|
||||
assert_eq!(table.update_row(&conditions, &datas).unwrap(), 1);
|
||||
assert!(table.is_data_exists(&datas).unwrap());
|
||||
assert_eq!(table.delete_row(&conditions, None).unwrap(), 1);
|
||||
assert!(!table.is_data_exists(&conditions).unwrap());
|
||||
assert_eq!(table.update_row(&conditions, false, &datas).unwrap(), 1);
|
||||
assert!(table.is_data_exists(&datas, false).unwrap());
|
||||
assert_eq!(table.delete_row(&conditions, None, false).unwrap(), 1);
|
||||
assert!(!table.is_data_exists(&conditions, false).unwrap());
|
||||
|
||||
fs::remove_dir_all("/data/asset_test/0").unwrap();
|
||||
}
|
||||
@ -313,9 +313,9 @@ fn replace_datas() {
|
||||
|
||||
let conditions = DbMap::from([("id", Value::Number(2))]);
|
||||
let datas = DbMap::from([("id", Value::Number(3)), ("alias", Value::Bytes(b"alias3".to_vec()))]);
|
||||
table.replace_row(&conditions, &datas).unwrap();
|
||||
assert!(table.is_data_exists(&datas).unwrap());
|
||||
table.replace_row(&conditions, false, &datas).unwrap();
|
||||
assert!(table.is_data_exists(&datas, false).unwrap());
|
||||
|
||||
assert_eq!(table.count_datas(&conditions).unwrap(), 0);
|
||||
assert_eq!(table.count_datas(&conditions, false).unwrap(), 0);
|
||||
fs::remove_dir_all("/data/asset_test/0").unwrap();
|
||||
}
|
||||
|
@ -105,21 +105,21 @@ impl IAssetPluginCtx for AssetContext {
|
||||
/// Queries the asset database.
|
||||
fn query(&mut self, attributes: &ExtDbMap) -> std::result::Result<Vec<ExtDbMap>, u32> {
|
||||
self.data_base.as_mut().ok_or(ErrCode::InvalidArgument as u32)?
|
||||
.query_datas(&vec![], attributes, None)
|
||||
.query_datas(&vec![], attributes, None, false)
|
||||
.map_err(|e| e.code as u32)
|
||||
}
|
||||
|
||||
/// Removes an asset from the database.
|
||||
fn remove(&mut self, attributes: &ExtDbMap) -> std::result::Result<i32, u32> {
|
||||
self.data_base.as_mut().ok_or(ErrCode::InvalidArgument as u32)?
|
||||
.delete_datas(attributes, None)
|
||||
.delete_datas(attributes, None, false)
|
||||
.map_err(|e| e.code as u32)
|
||||
}
|
||||
|
||||
/// Updates the attributes of an asset in the database.
|
||||
fn update(&mut self, attributes: &ExtDbMap, attrs_to_update: &ExtDbMap) -> std::result::Result<i32, u32> {
|
||||
self.data_base.as_mut().ok_or(ErrCode::InvalidArgument as u32)?
|
||||
.update_datas(attributes, attrs_to_update)
|
||||
.update_datas(attributes, false, attrs_to_update)
|
||||
.map_err(|e| e.code as u32)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user