1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "relational_store_utils.h" 17 #include "rdb_open_callback.h" 18 #include "rdb_store_config.h" 19 #include "rdb_store.h" 20 #include "rdb_helper.h" 21 #include "abs_rdb_predicates.h" 22 #include "logger.h" 23 #include "rdb_errno.h" 24 #include "rdb_open_callback.h" 25 #include "rdb_sql_utils.h" 26 #include "rdb_store_config.h" 27 #include "unistd.h" 28 #include "js_ability.h" 29 #include "native_log.h" 30 #include "value_object.h" 31 #include "rdb_common.h" 32 #include "native_log.h" 33 #include "relational_store_impl_rdbstore.h" 34 35 #ifndef PATH_SPLIT 36 #define PATH_SPLIT '/' 37 #endif 38 39 using ContextParam = OHOS::AppDataMgrJsKit::JSUtils::ContextParam; 40 using RdbConfig = OHOS::AppDataMgrJsKit::JSUtils::RdbConfig; 41 42 using namespace OHOS::FFI; 43 44 namespace OHOS { 45 namespace Relational { RdbStoreObserverImpl(int64_t id,FuncType type,int32_t mode)46 RdbStoreObserverImpl::RdbStoreObserverImpl(int64_t id, FuncType type, int32_t mode) 47 { 48 callbackId = id; 49 funcType = type; 50 mode_ = mode; 51 switch (type) { 52 case NoParam: { 53 auto cFunc = reinterpret_cast<void(*)()>(callbackId); 54 func = CJLambda::Create(cFunc); 55 break; 56 } 57 case ParamArrStr: { 58 auto cFunc = reinterpret_cast<void(*)(CArrStr arr)>(callbackId); 59 carrStrFunc = [ lambda = CJLambda::Create(cFunc)](const std::vector<std::string> &devices) -> 60 void { lambda(VectorToCArrStr(devices)); }; 61 break; 62 } 63 case ParamChangeInfo: { 64 auto cFunc = reinterpret_cast<void(*)(CArrRetChangeInfo arr)>(callbackId); 65 changeInfoFunc = [ lambda = CJLambda::Create(cFunc)](const DistributedRdb::Origin &origin, 66 const PrimaryFields &fields, DistributedRdb::RdbStoreObserver::ChangeInfo &&changeInfo) -> 67 void { lambda(ToCArrRetChangeInfo(origin, fields, std::move(changeInfo))); }; 68 break; 69 } 70 } 71 } 72 SyncObserverImpl(int64_t id)73 SyncObserverImpl::SyncObserverImpl(int64_t id) 74 { 75 callbackId = id; 76 auto cFunc = reinterpret_cast<void(*)(CProgressDetails details)>(callbackId); 77 func = [ lambda = CJLambda::Create(cFunc)](const DistributedRdb::Details &details) -> 78 void { lambda(ToCProgressDetails(details)); }; 79 } 80 81 class DefaultOpenCallback : public NativeRdb::RdbOpenCallback { 82 public: OnCreate(NativeRdb::RdbStore & rdbStore)83 int OnCreate(NativeRdb::RdbStore &rdbStore) override 84 { 85 return RelationalStoreJsKit::OK; 86 } 87 OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)88 int OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override 89 { 90 return RelationalStoreJsKit::OK; 91 } 92 }; 93 RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)94 RdbStoreImpl::RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore) 95 { 96 rdbStore_ = rdbStore; 97 } 98 GetClassType()99 OHOS::FFI::RuntimeType* RdbStoreImpl::GetClassType() 100 { 101 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("RdbStoreImpl"); 102 return &runtimeType; 103 } 104 ConvertFromValueBucket(ValuesBucket valuesBucket)105 NativeRdb::ValuesBucket ConvertFromValueBucket(ValuesBucket valuesBucket) 106 { 107 int64_t mapSize = valuesBucket.size; 108 NativeRdb::ValuesBucket nativeValuesBucket = NativeRdb::ValuesBucket(); 109 110 if (valuesBucket.value == nullptr || valuesBucket.key == nullptr) { 111 return nativeValuesBucket; 112 } 113 for (int64_t i = 0; i < mapSize; i++) { 114 NativeRdb::ValueObject valueObject = ValueTypeToValueObject(valuesBucket.value[i]); 115 if (valuesBucket.key[i] == nullptr) { 116 return nativeValuesBucket; 117 } 118 std::string keyStr = valuesBucket.key[i]; 119 nativeValuesBucket.Put(keyStr, valueObject); 120 } 121 return nativeValuesBucket; 122 } 123 ConvertFromValueBucketEx(ValuesBucketEx valuesBucket)124 NativeRdb::ValuesBucket ConvertFromValueBucketEx(ValuesBucketEx valuesBucket) 125 { 126 int64_t mapSize = valuesBucket.size; 127 NativeRdb::ValuesBucket nativeValuesBucket = NativeRdb::ValuesBucket(); 128 129 if (valuesBucket.value == nullptr || valuesBucket.key == nullptr) { 130 return nativeValuesBucket; 131 } 132 for (int64_t i = 0; i < mapSize; i++) { 133 NativeRdb::ValueObject valueObject = ValueTypeExToValueObject(valuesBucket.value[i]); 134 if (valuesBucket.key[i] == nullptr) { 135 return nativeValuesBucket; 136 } 137 std::string keyStr = valuesBucket.key[i]; 138 nativeValuesBucket.Put(keyStr, valueObject); 139 } 140 return nativeValuesBucket; 141 } 142 Query(RdbPredicatesImpl & predicates,char ** column,int64_t columnSize)143 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::Query(RdbPredicatesImpl &predicates, char** column, 144 int64_t columnSize) 145 { 146 if (column == nullptr) { 147 return nullptr; 148 } 149 std::vector<std::string> columnsVector = std::vector<std::string>(); 150 for (int64_t i = 0; i < columnSize; i++) { 151 if (column[i] == nullptr) { 152 return nullptr; 153 } 154 columnsVector.push_back(std::string(column[i])); 155 } 156 auto resultSet = rdbStore_->Query(*(predicates.GetPredicates()), columnsVector); 157 return resultSet; 158 } 159 RemoteQuery(char * device,RdbPredicatesImpl & predicates,char ** column,int64_t columnSize)160 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::RemoteQuery(char* device, RdbPredicatesImpl &predicates, 161 char** column, int64_t columnSize) 162 { 163 if (column == nullptr) { 164 return nullptr; 165 } 166 std::vector<std::string> columnsVector; 167 for (int64_t i = 0; i < columnSize; i++) { 168 if (column[i] == nullptr) { 169 return nullptr; 170 } 171 columnsVector.push_back(std::string(column[i])); 172 } 173 int32_t errCode; 174 if (predicates.GetPredicates() == nullptr) { 175 return nullptr; 176 } 177 auto resultSet = rdbStore_->RemoteQuery(std::string(device), *(predicates.GetPredicates()), columnsVector, 178 errCode); 179 return resultSet; 180 } 181 Update(ValuesBucket valuesBucket,RdbPredicatesImpl & predicates,NativeRdb::ConflictResolution conflictResolution,int32_t * errCode)182 int32_t RdbStoreImpl::Update(ValuesBucket valuesBucket, RdbPredicatesImpl &predicates, 183 NativeRdb::ConflictResolution conflictResolution, int32_t *errCode) 184 { 185 if (errCode == nullptr || rdbStore_ == nullptr || predicates.GetPredicates() == nullptr) { 186 return -1; 187 } 188 int32_t affectedRows; 189 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBucket); 190 *errCode = rdbStore_->UpdateWithConflictResolution(affectedRows, predicates.GetPredicates()->GetTableName(), 191 nativeValuesBucket, predicates.GetPredicates()->GetWhereClause(), predicates.GetPredicates()->GetBindArgs(), 192 conflictResolution); 193 return affectedRows; 194 } 195 UpdateEx(ValuesBucketEx valuesBucket,RdbPredicatesImpl & predicates,NativeRdb::ConflictResolution conflictResolution,int32_t * errCode)196 int32_t RdbStoreImpl::UpdateEx(ValuesBucketEx valuesBucket, RdbPredicatesImpl &predicates, 197 NativeRdb::ConflictResolution conflictResolution, int32_t *errCode) 198 { 199 if (errCode == nullptr || rdbStore_ == nullptr || predicates.GetPredicates() == nullptr) { 200 return -1; 201 } 202 int32_t affectedRows; 203 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucketEx(valuesBucket); 204 *errCode = rdbStore_->UpdateWithConflictResolution(affectedRows, predicates.GetPredicates()->GetTableName(), 205 nativeValuesBucket, predicates.GetPredicates()->GetWhereClause(), predicates.GetPredicates()->GetBindArgs(), 206 conflictResolution); 207 return affectedRows; 208 } 209 Delete(RdbPredicatesImpl & predicates,int32_t * errCode)210 int RdbStoreImpl::Delete(RdbPredicatesImpl &predicates, int32_t *errCode) 211 { 212 if (errCode == nullptr || rdbStore_ == nullptr || predicates.GetPredicates() == nullptr) { 213 return -1; 214 } 215 int deletedRows = 0; 216 *errCode = rdbStore_->Delete(deletedRows, *(predicates.GetPredicates())); 217 return deletedRows; 218 } 219 SetDistributedTables(char ** tables,int64_t tablesSize)220 int32_t RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize) 221 { 222 if (tables == nullptr || rdbStore_ == nullptr) { 223 return -1; 224 } 225 std::vector<std::string> tablesVector; 226 for (int64_t i = 0; i < tablesSize; i++) { 227 if (tables[i] == nullptr) { 228 return -1; 229 } 230 tablesVector.push_back(std::string(tables[i])); 231 } 232 return rdbStore_->SetDistributedTables(tablesVector, DistributedRdb::DISTRIBUTED_DEVICE, 233 DistributedRdb::DistributedConfig{false}); 234 } 235 SetDistributedTables(char ** tables,int64_t tablesSize,int32_t type)236 int32_t RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize, int32_t type) 237 { 238 if (tables == nullptr || rdbStore_ == nullptr) { 239 return -1; 240 } 241 std::vector<std::string> tablesVector; 242 for (int64_t i = 0; i < tablesSize; i++) { 243 if (tables[i] == nullptr) { 244 return -1; 245 } 246 tablesVector.push_back(std::string(tables[i])); 247 } 248 return rdbStore_->SetDistributedTables(tablesVector, type, DistributedRdb::DistributedConfig{false}); 249 } 250 SetDistributedTables(char ** tables,int64_t tablesSize,int32_t type,DistributedRdb::DistributedConfig & distributedConfig)251 int32_t RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize, int32_t type, 252 DistributedRdb::DistributedConfig &distributedConfig) 253 { 254 if (tables == nullptr || rdbStore_ == nullptr) { 255 return -1; 256 } 257 std::vector<std::string> tablesVector; 258 for (int64_t i = 0; i < tablesSize; i++) { 259 if (tables[i] == nullptr) { 260 return -1; 261 } 262 tablesVector.push_back(std::string(tables[i])); 263 } 264 return rdbStore_->SetDistributedTables(tablesVector, type, distributedConfig); 265 } 266 RollBack()267 int32_t RdbStoreImpl::RollBack() 268 { 269 if (rdbStore_ == nullptr) { 270 return -1; 271 } 272 return rdbStore_->RollBack(); 273 } 274 Commit()275 int32_t RdbStoreImpl::Commit() 276 { 277 if (rdbStore_ == nullptr) { 278 return -1; 279 } 280 return rdbStore_->Commit(); 281 } 282 BeginTransaction()283 int32_t RdbStoreImpl::BeginTransaction() 284 { 285 if (rdbStore_ == nullptr) { 286 return -1; 287 } 288 return rdbStore_->BeginTransaction(); 289 } 290 Backup(const char * destName)291 int32_t RdbStoreImpl::Backup(const char* destName) 292 { 293 if (rdbStore_ == nullptr || destName == nullptr) { 294 return -1; 295 } 296 return rdbStore_->Backup(destName, newKey); 297 } 298 Restore(const char * srcName)299 int32_t RdbStoreImpl::Restore(const char* srcName) 300 { 301 if (rdbStore_ == nullptr || srcName == nullptr) { 302 return -1; 303 } 304 return rdbStore_->Restore(srcName, newKey); 305 } 306 ObtainDistributedTableName(const char * device,const char * table)307 char* RdbStoreImpl::ObtainDistributedTableName(const char* device, const char* table) 308 { 309 if (rdbStore_ == nullptr || device == nullptr || table == nullptr) { 310 return nullptr; 311 } 312 int errCode = RelationalStoreJsKit::E_INNER_ERROR; 313 std::string tableName = rdbStore_->ObtainDistributedTableName(device, table, errCode); 314 return MallocCString(tableName); 315 } 316 Emit(const char * event)317 int32_t RdbStoreImpl::Emit(const char* event) 318 { 319 if (rdbStore_ == nullptr || event == nullptr) { 320 return -1; 321 } 322 return rdbStore_->Notify(event); 323 } 324 Insert(const char * table,ValuesBucket valuesBucket,int32_t conflict,int32_t * errCode)325 int64_t RdbStoreImpl::Insert(const char* table, ValuesBucket valuesBucket, int32_t conflict, int32_t *errCode) 326 { 327 if (rdbStore_ == nullptr || table == nullptr || errCode == nullptr) { 328 return -1; 329 } 330 std::string tableName = table; 331 int64_t result; 332 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBucket); 333 *errCode = rdbStore_->InsertWithConflictResolution(result, tableName, 334 nativeValuesBucket, NativeRdb::ConflictResolution(conflict)); 335 return result; 336 } 337 InsertEx(const char * table,ValuesBucketEx valuesBucket,int32_t conflict,int32_t * errCode)338 int64_t RdbStoreImpl::InsertEx(const char* table, ValuesBucketEx valuesBucket, int32_t conflict, int32_t *errCode) 339 { 340 if (rdbStore_ == nullptr || table == nullptr || errCode == nullptr) { 341 return -1; 342 } 343 std::string tableName = table; 344 int64_t result; 345 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucketEx(valuesBucket); 346 *errCode = rdbStore_->InsertWithConflictResolution(result, tableName, 347 nativeValuesBucket, NativeRdb::ConflictResolution(conflict)); 348 return result; 349 } 350 ExecuteSql(const char * sql,int32_t * errCode)351 void RdbStoreImpl::ExecuteSql(const char* sql, int32_t *errCode) 352 { 353 if (rdbStore_ == nullptr || sql == nullptr || errCode == nullptr) { 354 return; 355 } 356 *errCode = rdbStore_->ExecuteSql(sql, std::vector<OHOS::NativeRdb::ValueObject>()); 357 } 358 359 CleanDirtyData(const char * tableName,uint64_t cursor)360 int32_t RdbStoreImpl::CleanDirtyData(const char* tableName, uint64_t cursor) 361 { 362 if (rdbStore_ == nullptr || tableName == nullptr) { 363 return -1; 364 } 365 int32_t rtnCode = rdbStore_->CleanDirtyData(tableName, cursor); 366 return rtnCode; 367 } 368 BatchInsert(int64_t & insertNum,const char * tableName,ValuesBucket * valuesBuckets,int64_t valuesSize)369 int32_t RdbStoreImpl::BatchInsert(int64_t &insertNum, const char* tableName, ValuesBucket* valuesBuckets, 370 int64_t valuesSize) 371 { 372 if (rdbStore_ == nullptr || tableName == nullptr || valuesBuckets == nullptr) { 373 return -1; 374 } 375 std::vector<NativeRdb::ValuesBucket> valuesVector; 376 std::string tableNameStr = tableName; 377 if (tableNameStr.empty()) { 378 return RelationalStoreJsKit::E_PARAM_ERROR; 379 } 380 for (int64_t i = 0; i < valuesSize; i++) { 381 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBuckets[i]); 382 valuesVector.push_back(nativeValuesBucket); 383 } 384 int32_t rtnCode = rdbStore_->BatchInsert(insertNum, tableNameStr, valuesVector); 385 return rtnCode; 386 } 387 BatchInsertEx(int64_t & insertNum,const char * tableName,ValuesBucketEx * valuesBuckets,int64_t valuesSize)388 int32_t RdbStoreImpl::BatchInsertEx(int64_t &insertNum, const char* tableName, ValuesBucketEx* valuesBuckets, 389 int64_t valuesSize) 390 { 391 if (rdbStore_ == nullptr || tableName == nullptr || valuesBuckets == nullptr) { 392 return -1; 393 } 394 std::vector<NativeRdb::ValuesBucket> valuesVector; 395 std::string tableNameStr = tableName; 396 if (tableNameStr.empty()) { 397 return RelationalStoreJsKit::E_PARAM_ERROR; 398 } 399 for (int64_t i = 0; i < valuesSize; i++) { 400 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucketEx(valuesBuckets[i]); 401 valuesVector.push_back(nativeValuesBucket); 402 } 403 int32_t rtnCode = rdbStore_->BatchInsert(insertNum, tableNameStr, valuesVector); 404 return rtnCode; 405 } 406 Sync(int32_t mode,RdbPredicatesImpl & predicates)407 CArrSyncResult RdbStoreImpl::Sync(int32_t mode, RdbPredicatesImpl &predicates) 408 { 409 DistributedRdb::SyncOption option; 410 option.mode = static_cast<DistributedRdb::SyncMode>(mode); 411 option.isBlock = true; 412 DistributedRdb::SyncResult resMap; 413 if (rdbStore_ == nullptr) { 414 return CArrSyncResult{nullptr, nullptr, -1}; 415 } 416 rdbStore_->Sync(option, *(predicates.GetPredicates()), 417 [&resMap](const DistributedRdb::SyncResult &result) { resMap = result; }); 418 if (resMap.size() == 0) { 419 return CArrSyncResult{nullptr, nullptr, -1}; 420 } 421 char** resultStr = static_cast<char**>(malloc(resMap.size() * sizeof(char*))); 422 int32_t* resultNum = static_cast<int32_t*>(malloc(resMap.size() * sizeof(int32_t))); 423 if (resultStr == nullptr || resultNum == nullptr) { 424 free(resultStr); 425 free(resultNum); 426 return CArrSyncResult{nullptr, nullptr, -1}; 427 } 428 size_t i = 0; 429 for (auto it = resMap.begin(); it != resMap.end(); ++it) { 430 resultStr[i] = MallocCString(it->first); 431 resultNum[i] = it->second; 432 i++; 433 } 434 return CArrSyncResult{resultStr, resultNum, int64_t(resMap.size())}; 435 } 436 QuerySql(const char * sql,ValueType * bindArgs,int64_t size)437 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::QuerySql(const char *sql, ValueType *bindArgs, int64_t size) 438 { 439 if (sql == nullptr || bindArgs == nullptr || rdbStore_ == nullptr) { 440 return nullptr; 441 } 442 std::string tmpSql = sql; 443 std::vector<NativeRdb::ValueObject> tmpBindArgs = std::vector<NativeRdb::ValueObject>(); 444 for (int64_t i = 0; i < size; i++) { 445 tmpBindArgs.push_back(ValueTypeToValueObject(bindArgs[i])); 446 } 447 auto result = rdbStore_->QueryByStep(tmpSql, tmpBindArgs); 448 return result; 449 } 450 QuerySqlEx(const char * sql,ValueTypeEx * bindArgs,int64_t size)451 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::QuerySqlEx(const char *sql, ValueTypeEx *bindArgs, int64_t size) 452 { 453 if (sql == nullptr || bindArgs == nullptr || rdbStore_ == nullptr) { 454 return nullptr; 455 } 456 std::string tmpSql = sql; 457 std::vector<NativeRdb::ValueObject> tmpBindArgs = std::vector<NativeRdb::ValueObject>(); 458 for (int64_t i = 0; i < size; i++) { 459 tmpBindArgs.push_back(ValueTypeExToValueObject(bindArgs[i])); 460 } 461 auto result = rdbStore_->QueryByStep(tmpSql, tmpBindArgs); 462 return result; 463 } 464 ExecuteSql(const char * sql,ValueType * bindArgs,int64_t bindArgsSize,int32_t * errCode)465 void RdbStoreImpl::ExecuteSql(const char* sql, ValueType* bindArgs, int64_t bindArgsSize, int32_t *errCode) 466 { 467 if (sql == nullptr || bindArgs == nullptr || rdbStore_ == nullptr || errCode == nullptr) { 468 return; 469 } 470 std::vector<NativeRdb::ValueObject> bindArgsObjects = std::vector<NativeRdb::ValueObject>(); 471 for (int64_t i = 0; i < bindArgsSize; i++) { 472 bindArgsObjects.push_back(ValueTypeToValueObject(bindArgs[i])); 473 } 474 *errCode = rdbStore_->ExecuteSql(sql, bindArgsObjects); 475 } 476 ExecuteSqlEx(const char * sql,ValueTypeEx * bindArgs,int64_t bindArgsSize,int32_t * errCode)477 void RdbStoreImpl::ExecuteSqlEx(const char* sql, ValueTypeEx* bindArgs, int64_t bindArgsSize, int32_t *errCode) 478 { 479 if (sql == nullptr || bindArgs == nullptr || rdbStore_ == nullptr || errCode == nullptr) { 480 return; 481 } 482 std::vector<NativeRdb::ValueObject> bindArgsObjects = std::vector<NativeRdb::ValueObject>(); 483 for (int64_t i = 0; i < bindArgsSize; i++) { 484 bindArgsObjects.push_back(ValueTypeExToValueObject(bindArgs[i])); 485 } 486 *errCode = rdbStore_->ExecuteSql(sql, bindArgsObjects); 487 } 488 RegisterObserver(const char * event,bool interProcess,int64_t callback,const std::function<void ()> & callbackRef)489 int32_t RdbStoreImpl::RegisterObserver(const char *event, bool interProcess, int64_t callback, 490 const std::function<void()>& callbackRef) 491 { 492 DistributedRdb::SubscribeOption option; 493 option.event = event; 494 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 495 DistributedRdb::SubscribeMode::LOCAL; 496 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 497 return RegisteredObserver(option, localObservers_, callback, callbackRef); 498 } 499 return RegisteredObserver(option, localSharedObservers_, callback, callbackRef); 500 } 501 isSameFunction(int64_t f1,int64_t f2)502 bool isSameFunction(int64_t f1, int64_t f2) 503 { 504 return f1 == f2; 505 } 506 HasRegisteredObserver(int64_t callback,std::list<std::shared_ptr<RdbStoreObserverImpl>> & observers)507 bool RdbStoreImpl::HasRegisteredObserver( 508 int64_t callback, 509 std::list<std::shared_ptr<RdbStoreObserverImpl>> &observers) 510 { 511 for (auto &it : observers) { 512 if (it == nullptr) { 513 return false; 514 } 515 if (isSameFunction(callback, it->GetCallBack())) { 516 return true; 517 } 518 } 519 return false; 520 } 521 RdbStoreObserverImpl(int64_t callback,const std::function<void ()> & callbackRef)522 RdbStoreObserverImpl::RdbStoreObserverImpl(int64_t callback, 523 const std::function<void()>& callbackRef) 524 { 525 m_callback = callback; 526 m_callbackRef = callbackRef; 527 } 528 GetCallBack()529 int64_t RdbStoreObserverImpl::GetCallBack() 530 { 531 return m_callback; 532 } 533 RegisteredObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers,int64_t callback,const std::function<void ()> & callbackRef)534 int32_t RdbStoreImpl::RegisteredObserver( 535 DistributedRdb::SubscribeOption option, 536 std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers, 537 int64_t callback, const std::function<void()>& callbackRef) 538 { 539 observers.try_emplace(option.event); 540 if (!HasRegisteredObserver(callback, observers[option.event])) { 541 auto localObserver = std::make_shared<RdbStoreObserverImpl>(callback, callbackRef); 542 if (rdbStore_ == nullptr) { 543 return -1; 544 } 545 int32_t errCode = rdbStore_->Subscribe(option, localObserver); 546 if (errCode != NativeRdb::E_OK) { 547 return errCode; 548 } 549 observers[option.event].push_back(localObserver); 550 LOGI("subscribe success event: %{public}s", option.event.c_str()); 551 } else { 552 LOGI("duplicate subscribe event: %{public}s", option.event.c_str()); 553 } 554 return RelationalStoreJsKit::OK; 555 } 556 RegisterObserverArrStr(int32_t subscribeType,int64_t callbackId)557 int32_t RdbStoreImpl::RegisterObserverArrStr(int32_t subscribeType, int64_t callbackId) 558 { 559 int32_t mode = subscribeType; 560 DistributedRdb::SubscribeOption option; 561 option.mode = static_cast<DistributedRdb::SubscribeMode>(mode); 562 option.event = "dataChange"; 563 auto observer = std::make_shared<RdbStoreObserverImpl>(callbackId, RdbStoreObserverImpl::ParamArrStr, mode); 564 int32_t errCode = NativeRdb::E_OK; 565 if (rdbStore_ == nullptr) { 566 return -1; 567 } 568 if (option.mode == DistributedRdb::SubscribeMode::LOCAL_DETAIL) { 569 errCode = rdbStore_->SubscribeObserver(option, observer); 570 } else { 571 errCode = rdbStore_->Subscribe(option, observer); 572 } 573 if (errCode == NativeRdb::E_OK) { 574 observers_[mode].push_back(observer); 575 LOGI("subscribe success"); 576 } 577 return errCode; 578 } 579 RegisterObserverChangeInfo(int32_t subscribeType,int64_t callbackId)580 int32_t RdbStoreImpl::RegisterObserverChangeInfo(int32_t subscribeType, int64_t callbackId) 581 { 582 int32_t mode = subscribeType; 583 DistributedRdb::SubscribeOption option; 584 option.mode = static_cast<DistributedRdb::SubscribeMode>(mode); 585 option.event = "dataChange"; 586 auto observer = std::make_shared<RdbStoreObserverImpl>(callbackId, RdbStoreObserverImpl::ParamChangeInfo, mode); 587 int32_t errCode = NativeRdb::E_OK; 588 if (rdbStore_ == nullptr) { 589 return -1; 590 } 591 if (option.mode == DistributedRdb::SubscribeMode::LOCAL_DETAIL) { 592 errCode = rdbStore_->SubscribeObserver(option, observer); 593 } else { 594 errCode = rdbStore_->Subscribe(option, observer); 595 } 596 if (errCode == NativeRdb::E_OK) { 597 observers_[mode].push_back(observer); 598 LOGI("subscribe success"); 599 } 600 return errCode; 601 } 602 RegisterObserverProgressDetails(int64_t callbackId)603 int32_t RdbStoreImpl::RegisterObserverProgressDetails(int64_t callbackId) 604 { 605 auto observer = std::make_shared<SyncObserverImpl>(callbackId); 606 if (rdbStore_ == nullptr) { 607 return -1; 608 } 609 int errCode = rdbStore_->RegisterAutoSyncCallback(observer); 610 if (errCode == NativeRdb::E_OK) { 611 syncObservers_.push_back(observer); 612 LOGI("progress subscribe success"); 613 } 614 return errCode; 615 } 616 UnRegisterObserver(const char * event,bool interProcess,int64_t callback)617 int32_t RdbStoreImpl::UnRegisterObserver(const char *event, bool interProcess, int64_t callback) 618 { 619 DistributedRdb::SubscribeOption option; 620 option.event = event; 621 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 622 DistributedRdb::SubscribeMode::LOCAL; 623 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 624 return UnRegisteredObserver(option, localObservers_, callback); 625 } 626 return UnRegisteredObserver(option, localSharedObservers_, callback); 627 } 628 UnRegisteredObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers,int64_t callback)629 int32_t RdbStoreImpl::UnRegisteredObserver(DistributedRdb::SubscribeOption option, 630 std::map<std::string, std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers, 631 int64_t callback) 632 { 633 auto obs = observers.find(option.event); 634 if (obs == observers.end()) { 635 LOGI("observer not found, event: %{public}s", option.event.c_str()); 636 return RelationalStoreJsKit::OK; 637 } 638 639 auto &list = obs->second; 640 for (auto it = list.begin(); it != list.end(); it++) { 641 if (isSameFunction(callback, (*it)->GetCallBack())) { 642 int errCode = rdbStore_->UnSubscribe(option, *it); 643 if (errCode != RelationalStoreJsKit::OK) { 644 return errCode; 645 } 646 list.erase(it); 647 break; 648 } 649 } 650 if (list.empty()) { 651 observers.erase(option.event); 652 } 653 LOGI("unsubscribe success, event: %{public}s", option.event.c_str()); 654 return RelationalStoreJsKit::OK; 655 } 656 UnRegisterAllObserver(const char * event,bool interProcess)657 int32_t RdbStoreImpl::UnRegisterAllObserver(const char *event, bool interProcess) 658 { 659 DistributedRdb::SubscribeOption option; 660 if (event == nullptr) { 661 return -1; 662 } 663 option.event = event; 664 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 665 DistributedRdb::SubscribeMode::LOCAL; 666 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 667 return UnRegisteredAllObserver(option, localObservers_); 668 } 669 return UnRegisteredAllObserver(option, localSharedObservers_); 670 } 671 UnRegisteredAllObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers)672 int32_t RdbStoreImpl::UnRegisteredAllObserver(DistributedRdb::SubscribeOption option, std::map<std::string, 673 std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers) 674 { 675 auto obs = observers.find(option.event); 676 if (obs == observers.end()) { 677 LOGI("observer not found, event: %{public}s", option.event.c_str()); 678 return RelationalStoreJsKit::OK; 679 } 680 681 int errCode = rdbStore_->UnSubscribe(option, nullptr); 682 if (errCode != RelationalStoreJsKit::OK) { 683 return errCode; 684 } 685 observers.erase(option.event); 686 LOGI("unsubscribe success, event: %{public}s", option.event.c_str()); 687 return RelationalStoreJsKit::OK; 688 } 689 UnRegisterObserverArrStrChangeInfo(int32_t subscribeType,int64_t callbackId)690 int32_t RdbStoreImpl::UnRegisterObserverArrStrChangeInfo(int32_t subscribeType, int64_t callbackId) 691 { 692 int32_t mode = subscribeType; 693 DistributedRdb::SubscribeOption option; 694 option.mode = static_cast<DistributedRdb::SubscribeMode>(mode); 695 option.event = "dataChange"; 696 for (auto it = observers_[mode].begin(); it != observers_[mode].end();) { 697 if (*it == nullptr) { 698 it = observers_[mode].erase(it); 699 continue; 700 } 701 if (((**it).GetCallBackId() != callbackId)) { 702 ++it; 703 continue; 704 } 705 int errCode = NativeRdb::E_OK; 706 if (rdbStore_ == nullptr) { 707 return -1; 708 } 709 if (option.mode == DistributedRdb::SubscribeMode::LOCAL_DETAIL) { 710 errCode = rdbStore_->UnsubscribeObserver(option, *it); 711 } else { 712 errCode = rdbStore_->UnSubscribe(option, *it); 713 } 714 if (errCode != NativeRdb::E_OK) { 715 return errCode; 716 } 717 it = observers_[mode].erase(it); 718 } 719 return NativeRdb::E_OK; 720 } 721 UnRegisterObserverArrStrChangeInfoAll(int32_t subscribeType)722 int32_t RdbStoreImpl::UnRegisterObserverArrStrChangeInfoAll(int32_t subscribeType) 723 { 724 int32_t mode = subscribeType; 725 DistributedRdb::SubscribeOption option; 726 option.mode = static_cast<DistributedRdb::SubscribeMode>(mode); 727 option.event = "dataChange"; 728 for (auto it = observers_[mode].begin(); it != observers_[mode].end();) { 729 if (*it == nullptr) { 730 it = observers_[mode].erase(it); 731 continue; 732 } 733 int errCode = NativeRdb::E_OK; 734 if (rdbStore_ == nullptr) { 735 return -1; 736 } 737 if (option.mode == DistributedRdb::SubscribeMode::LOCAL_DETAIL) { 738 errCode = rdbStore_->UnsubscribeObserver(option, *it); 739 } else { 740 errCode = rdbStore_->UnSubscribe(option, *it); 741 } 742 if (errCode != NativeRdb::E_OK) { 743 return errCode; 744 } 745 it = observers_[mode].erase(it); 746 } 747 return NativeRdb::E_OK; 748 } 749 UnRegisterObserverProgressDetails(int64_t callbackId)750 int32_t RdbStoreImpl::UnRegisterObserverProgressDetails(int64_t callbackId) 751 { 752 for (auto it = syncObservers_.begin(); it != syncObservers_.end();) { 753 if (*it == nullptr) { 754 it = syncObservers_.erase(it); 755 continue; 756 } 757 if (((**it).GetCallBackId() != callbackId)) { 758 ++it; 759 continue; 760 } 761 762 if (rdbStore_ == nullptr) { 763 return -1; 764 } 765 int32_t errCode = rdbStore_->UnregisterAutoSyncCallback(*it); 766 if (errCode != NativeRdb::E_OK) { 767 return errCode; 768 } 769 it = syncObservers_.erase(it); 770 } 771 return NativeRdb::E_OK; 772 } 773 UnRegisterObserverProgressDetailsAll()774 int32_t RdbStoreImpl::UnRegisterObserverProgressDetailsAll() 775 { 776 for (auto it = syncObservers_.begin(); it != syncObservers_.end();) { 777 if (*it == nullptr) { 778 it = syncObservers_.erase(it); 779 continue; 780 } 781 if (rdbStore_ == nullptr) { 782 return -1; 783 } 784 int32_t errCode = rdbStore_->UnregisterAutoSyncCallback(*it); 785 if (errCode != NativeRdb::E_OK) { 786 return errCode; 787 } 788 it = syncObservers_.erase(it); 789 } 790 return NativeRdb::E_OK; 791 } 792 CloudSync(int32_t mode,CArrStr tables,int64_t callbackId)793 int32_t RdbStoreImpl::CloudSync(int32_t mode, CArrStr tables, int64_t callbackId) 794 { 795 DistributedRdb::SyncOption option; 796 option.mode = static_cast<DistributedRdb::SyncMode>(mode); 797 option.isBlock = false; 798 std::vector<std::string> arr = CArrStrToVector(tables); 799 auto cFunc = reinterpret_cast<void(*)(CProgressDetails details)>(callbackId); 800 auto async = [ lambda = CJLambda::Create(cFunc)](const DistributedRdb::Details &details) -> 801 void { lambda(ToCProgressDetails(details)); }; 802 if (rdbStore_ == nullptr) { 803 return -1; 804 } 805 int32_t errCode = rdbStore_->Sync(option, arr, async); 806 return errCode; 807 } 808 GetVersion(int32_t & errCode)809 int32_t RdbStoreImpl::GetVersion(int32_t& errCode) 810 { 811 int32_t version = 0; 812 if (rdbStore_ == nullptr) { 813 return -1; 814 } 815 errCode = rdbStore_->GetVersion(version); 816 return version; 817 } 818 SetVersion(int32_t value,int32_t & errCode)819 void RdbStoreImpl::SetVersion(int32_t value, int32_t &errCode) 820 { 821 if (rdbStore_ == nullptr) { 822 return; 823 } 824 errCode = rdbStore_->SetVersion(value); 825 } 826 GetModifyTime(char * cTables,char * cColumnName,CArrPRIKeyType & cPrimaryKeys,int32_t & errCode)827 ModifyTime RdbStoreImpl::GetModifyTime(char *cTables, char *cColumnName, CArrPRIKeyType &cPrimaryKeys, 828 int32_t& errCode) 829 { 830 std::string tableName = cTables; 831 std::string columnName = cColumnName; 832 std::vector<NativeRdb::RdbStore::PRIKey> keys = CArrPRIKeyTypeToPRIKeyArray(cPrimaryKeys); 833 if (rdbStore_ == nullptr) { 834 return ModifyTime{0}; 835 } 836 std::map<NativeRdb::RdbStore::PRIKey, NativeRdb::RdbStore::Date> map = 837 rdbStore_->GetModifyTime(tableName, columnName, keys); 838 if (map.empty()) { 839 errCode = NativeRdb::E_ERROR; 840 return ModifyTime{0}; 841 } 842 return MapToModifyTime(map, errCode); 843 } 844 GetRebuilt()845 int32_t RdbStoreImpl::GetRebuilt() 846 { 847 auto rebuilt = NativeRdb::RebuiltType::NONE; 848 if (rdbStore_ == nullptr) { 849 return -1; 850 } 851 rdbStore_->GetRebuilt(rebuilt); 852 return static_cast<int32_t>(rebuilt); 853 } 854 GetRealPath(AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const AppDataMgrJsKit::JSUtils::ContextParam & param,std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext)855 int32_t GetRealPath(AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, 856 const AppDataMgrJsKit::JSUtils::ContextParam ¶m, 857 std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext) 858 { 859 if (rdbConfig.name.find(PATH_SPLIT) != std::string::npos) { 860 LOGE("Parameter error. The StoreConfig.name must be a file name without path."); 861 return RelationalStoreJsKit::E_PARAM_ERROR; 862 } 863 864 if (!rdbConfig.customDir.empty()) { 865 // determine if the first character of customDir is '/' 866 if (rdbConfig.customDir.find_first_of(PATH_SPLIT) == 0) { 867 LOGE("Parameter error. The customDir must be a relative directory."); 868 return RelationalStoreJsKit::E_PARAM_ERROR; 869 } 870 // customDir length is limited to 128 bytes 871 if (rdbConfig.customDir.length() > 128) { 872 LOGE("Parameter error. The customDir length must be less than or equal to 128 bytes."); 873 return RelationalStoreJsKit::E_PARAM_ERROR; 874 } 875 } 876 877 std::string baseDir = param.baseDir; 878 if (!rdbConfig.dataGroupId.empty()) { 879 if (!param.isStageMode) { 880 return RelationalStoreJsKit::E_NOT_STAGE_MODE; 881 } 882 std::string groupDir; 883 int errCode = abilitycontext->GetSystemDatabaseDir(rdbConfig.dataGroupId, groupDir); 884 if (errCode != NativeRdb::E_OK && groupDir.empty()) { 885 return RelationalStoreJsKit::E_DATA_GROUP_ID_INVALID; 886 } 887 baseDir = groupDir; 888 } 889 890 auto [realPath, errorCode] = 891 NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(baseDir, rdbConfig.name, rdbConfig.customDir); 892 // realPath length is limited to 1024 bytes 893 if (errorCode != NativeRdb::E_OK || realPath.length() > 1024) { 894 LOGE("Parameter error. The database path must be a valid path."); 895 return RelationalStoreJsKit::E_PARAM_ERROR; 896 } 897 rdbConfig.path = realPath; 898 return NativeRdb::E_OK; 899 } 900 initContextParam(AppDataMgrJsKit::JSUtils::ContextParam & param,std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext)901 void initContextParam(AppDataMgrJsKit::JSUtils::ContextParam ¶m, 902 std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext) 903 { 904 param.bundleName = abilitycontext->GetBundleName(); 905 param.moduleName = abilitycontext->GetModuleName(); 906 param.baseDir = abilitycontext->GetDatabaseDir(); 907 param.area = abilitycontext->GetArea(); 908 param.isSystemApp = abilitycontext->IsSystemAppCalled(); 909 param.isStageMode = abilitycontext->IsStageMode(); 910 } 911 initRdbConfig(AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,StoreConfig & config)912 void initRdbConfig(AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, StoreConfig &config) 913 { 914 rdbConfig.isEncrypt = config.encrypt; 915 rdbConfig.isSearchable = config.isSearchable; 916 rdbConfig.isAutoClean = config.autoCleanDirtyData; 917 rdbConfig.securityLevel = static_cast<NativeRdb::SecurityLevel>(config.securityLevel); 918 rdbConfig.dataGroupId = config.dataGroupId; 919 rdbConfig.name = config.name; 920 rdbConfig.customDir = config.customDir; 921 } 922 initRdbConfigEx(AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const StoreConfigEx & config)923 void initRdbConfigEx(AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, const StoreConfigEx &config) 924 { 925 rdbConfig.isEncrypt = config.encrypt; 926 rdbConfig.isSearchable = config.isSearchable; 927 rdbConfig.isAutoClean = config.autoCleanDirtyData; 928 rdbConfig.securityLevel = static_cast<NativeRdb::SecurityLevel>(config.securityLevel); 929 rdbConfig.dataGroupId = config.dataGroupId; 930 rdbConfig.name = config.name; 931 rdbConfig.customDir = config.customDir; 932 rdbConfig.rootDir = config.rootDir; 933 rdbConfig.vector = config.vector; 934 rdbConfig.allowRebuild = config.allowRebuild; 935 rdbConfig.isReadOnly = config.isReadOnly; 936 rdbConfig.pluginLibs = CArrStrToVector(config.pluginLibs); 937 rdbConfig.cryptoParam = ToCCryptoParam(config.cryptoParam); 938 rdbConfig.tokenizer = static_cast<OHOS::NativeRdb::Tokenizer>(config.tokenizer); 939 rdbConfig.persist = config.persist; 940 } 941 getRdbStoreConfig(const AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const AppDataMgrJsKit::JSUtils::ContextParam & param)942 NativeRdb::RdbStoreConfig getRdbStoreConfig(const AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, 943 const AppDataMgrJsKit::JSUtils::ContextParam ¶m) 944 { 945 NativeRdb::RdbStoreConfig rdbStoreConfig(rdbConfig.path); 946 rdbStoreConfig.SetEncryptStatus(rdbConfig.isEncrypt); 947 rdbStoreConfig.SetSearchable(rdbConfig.isSearchable); 948 rdbStoreConfig.SetIsVector(rdbConfig.vector); 949 rdbStoreConfig.SetAutoClean(rdbConfig.isAutoClean); 950 rdbStoreConfig.SetSecurityLevel(rdbConfig.securityLevel); 951 rdbStoreConfig.SetDataGroupId(rdbConfig.dataGroupId); 952 rdbStoreConfig.SetName(rdbConfig.name); 953 rdbStoreConfig.SetCustomDir(rdbConfig.customDir); 954 rdbStoreConfig.SetAllowRebuild(rdbConfig.allowRebuild); 955 956 if (!param.bundleName.empty()) { 957 rdbStoreConfig.SetBundleName(param.bundleName); 958 } 959 rdbStoreConfig.SetModuleName(param.moduleName); 960 rdbStoreConfig.SetArea(param.area); 961 return rdbStoreConfig; 962 } 963 getRdbStoreConfigEx(const AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const AppDataMgrJsKit::JSUtils::ContextParam & param)964 NativeRdb::RdbStoreConfig getRdbStoreConfigEx(const AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, 965 const AppDataMgrJsKit::JSUtils::ContextParam ¶m) 966 { 967 NativeRdb::RdbStoreConfig rdbStoreConfig(rdbConfig.path); 968 rdbStoreConfig.SetEncryptStatus(rdbConfig.isEncrypt); 969 rdbStoreConfig.SetSearchable(rdbConfig.isSearchable); 970 rdbStoreConfig.SetIsVector(rdbConfig.vector); 971 rdbStoreConfig.SetDBType(rdbConfig.vector ? NativeRdb::DB_VECTOR : NativeRdb::DB_SQLITE); 972 rdbStoreConfig.SetStorageMode(rdbConfig.persist ? NativeRdb::StorageMode::MODE_DISK : 973 NativeRdb::StorageMode::MODE_MEMORY); 974 rdbStoreConfig.SetAutoClean(rdbConfig.isAutoClean); 975 rdbStoreConfig.SetSecurityLevel(rdbConfig.securityLevel); 976 rdbStoreConfig.SetDataGroupId(rdbConfig.dataGroupId); 977 rdbStoreConfig.SetName(rdbConfig.name); 978 rdbStoreConfig.SetCustomDir(rdbConfig.customDir); 979 rdbStoreConfig.SetAllowRebuild(rdbConfig.allowRebuild); 980 rdbStoreConfig.SetReadOnly(rdbConfig.isReadOnly); 981 rdbStoreConfig.SetIntegrityCheck(NativeRdb::IntegrityCheck::NONE); 982 rdbStoreConfig.SetTokenizer(rdbConfig.tokenizer); 983 984 if (!param.bundleName.empty()) { 985 rdbStoreConfig.SetBundleName(param.bundleName); 986 } 987 rdbStoreConfig.SetModuleName(param.moduleName); 988 rdbStoreConfig.SetArea(param.area); 989 rdbStoreConfig.SetPluginLibs(rdbConfig.pluginLibs); 990 rdbStoreConfig.SetHaMode(rdbConfig.haMode); 991 992 rdbStoreConfig.SetCryptoParam(rdbConfig.cryptoParam); 993 return rdbStoreConfig; 994 } 995 GetRdbStore(OHOS::AbilityRuntime::Context * context,StoreConfig config,int32_t * errCode)996 int64_t GetRdbStore(OHOS::AbilityRuntime::Context* context, StoreConfig config, 997 int32_t *errCode) 998 { 999 if (errCode == nullptr) { 1000 return -1; 1001 } 1002 if (context == nullptr) { 1003 *errCode = -1; 1004 return -1; 1005 } 1006 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 1007 AppDataMgrJsKit::JSUtils::ContextParam param; 1008 initContextParam(param, abilitycontext); 1009 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 1010 initRdbConfig(rdbConfig, config); 1011 1012 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 1013 if (*errCode != NativeRdb::E_OK) { 1014 return -1; 1015 } 1016 1017 DefaultOpenCallback callback; 1018 auto rdbStore = 1019 NativeRdb::RdbHelper::GetRdbStore(getRdbStoreConfig(rdbConfig, param), -1, callback, *errCode); 1020 if (*errCode != 0) { 1021 return -1; 1022 } 1023 auto nativeRdbStore = FFIData::Create<RdbStoreImpl>(rdbStore); 1024 if (nativeRdbStore == nullptr) { 1025 *errCode = -1; 1026 return -1; 1027 } 1028 return nativeRdbStore->GetID(); 1029 } 1030 GetRdbStoreEx(OHOS::AbilityRuntime::Context * context,const StoreConfigEx * config,int32_t * errCode)1031 int64_t GetRdbStoreEx(OHOS::AbilityRuntime::Context* context, const StoreConfigEx *config, 1032 int32_t *errCode) 1033 { 1034 if (errCode == nullptr) { 1035 return -1; 1036 } 1037 if (context == nullptr) { 1038 *errCode = ERROR_VALUE; 1039 return ERROR_VALUE; 1040 } 1041 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 1042 AppDataMgrJsKit::JSUtils::ContextParam param; 1043 initContextParam(param, abilitycontext); 1044 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 1045 initRdbConfigEx(rdbConfig, *config); 1046 if (!rdbConfig.cryptoParam.IsValid()) { 1047 *errCode = RelationalStoreJsKit::E_PARAM_ERROR; 1048 return ERROR_VALUE; 1049 } 1050 1051 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 1052 if (*errCode != NativeRdb::E_OK) { 1053 return ERROR_VALUE; 1054 } 1055 1056 DefaultOpenCallback callback; 1057 auto rdbStore = 1058 NativeRdb::RdbHelper::GetRdbStore(getRdbStoreConfigEx(rdbConfig, param), -1, callback, *errCode); 1059 if (*errCode != 0) { 1060 return ERROR_VALUE; 1061 } 1062 auto nativeRdbStore = FFIData::Create<RdbStoreImpl>(rdbStore); 1063 if (nativeRdbStore == nullptr) { 1064 *errCode = ERROR_VALUE; 1065 return ERROR_VALUE; 1066 } 1067 return nativeRdbStore->GetID(); 1068 } 1069 DeleteRdbStore(OHOS::AbilityRuntime::Context * context,const char * name,int32_t * errCode)1070 void DeleteRdbStore(OHOS::AbilityRuntime::Context* context, const char* name, 1071 int32_t *errCode) 1072 { 1073 if (errCode == nullptr) { 1074 return; 1075 } 1076 if (context == nullptr) { 1077 *errCode = -1; 1078 return; 1079 } 1080 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 1081 AppDataMgrJsKit::JSUtils::ContextParam param; 1082 initContextParam(param, abilitycontext); 1083 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 1084 rdbConfig.name = name; 1085 1086 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 1087 if (*errCode != NativeRdb::E_OK) { 1088 return; 1089 } 1090 *errCode = NativeRdb::RdbHelper::DeleteRdbStore(rdbConfig.path, false); 1091 return; 1092 } 1093 DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context * context,StoreConfig config,int32_t * errCode)1094 void DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context* context, StoreConfig config, 1095 int32_t *errCode) 1096 { 1097 if (errCode == nullptr) { 1098 return; 1099 } 1100 if (context == nullptr) { 1101 *errCode = -1; 1102 return; 1103 } 1104 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 1105 AppDataMgrJsKit::JSUtils::ContextParam param; 1106 initContextParam(param, abilitycontext); 1107 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 1108 initRdbConfig(rdbConfig, config); 1109 1110 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 1111 if (*errCode != NativeRdb::E_OK) { 1112 return; 1113 } 1114 *errCode = NativeRdb::RdbHelper::DeleteRdbStore(rdbConfig.path, false); 1115 return; 1116 } 1117 DeleteRdbStoreConfigEx(OHOS::AbilityRuntime::Context * context,const StoreConfigEx * config,int32_t * errCode)1118 void DeleteRdbStoreConfigEx(OHOS::AbilityRuntime::Context* context, const StoreConfigEx *config, 1119 int32_t *errCode) 1120 { 1121 if (errCode == nullptr) { 1122 return; 1123 } 1124 if (context == nullptr || config == nullptr) { 1125 *errCode = ERROR_VALUE; 1126 return; 1127 } 1128 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 1129 AppDataMgrJsKit::JSUtils::ContextParam param; 1130 initContextParam(param, abilitycontext); 1131 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 1132 initRdbConfigEx(rdbConfig, *config); 1133 1134 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 1135 if (*errCode != NativeRdb::E_OK) { 1136 return; 1137 } 1138 *errCode = NativeRdb::RdbHelper::DeleteRdbStore(rdbConfig.path, false); 1139 return; 1140 } 1141 } 1142 }