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_helper.h" 20 #include "abs_rdb_predicates.h" 21 #include "logger.h" 22 #include "rdb_errno.h" 23 #include "rdb_open_callback.h" 24 #include "rdb_sql_utils.h" 25 #include "rdb_store_config.h" 26 #include "unistd.h" 27 #include "js_ability.h" 28 #include "native_log.h" 29 #include "value_object.h" 30 #include "rdb_common.h" 31 #include "native_log.h" 32 #include "relational_store_impl_rdbstore.h" 33 34 #ifndef PATH_SPLIT 35 #define PATH_SPLIT '/' 36 #endif 37 38 using ContextParam = OHOS::AppDataMgrJsKit::JSUtils::ContextParam; 39 using RdbConfig = OHOS::AppDataMgrJsKit::JSUtils::RdbConfig; 40 41 using namespace OHOS::FFI; 42 43 namespace OHOS { 44 namespace Relational { 45 class DefaultOpenCallback : public NativeRdb::RdbOpenCallback { 46 public: OnCreate(NativeRdb::RdbStore & rdbStore)47 int OnCreate(NativeRdb::RdbStore &rdbStore) override 48 { 49 return RelationalStoreJsKit::OK; 50 } 51 OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)52 int OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override 53 { 54 return RelationalStoreJsKit::OK; 55 } 56 }; 57 RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore)58 RdbStoreImpl::RdbStoreImpl(std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore) 59 { 60 rdbStore_ = rdbStore; 61 } 62 GetClassType()63 OHOS::FFI::RuntimeType* RdbStoreImpl::GetClassType() 64 { 65 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("RdbStoreImpl"); 66 return &runtimeType; 67 } 68 ConvertFromValueBucket(ValuesBucket valuesBucket)69 NativeRdb::ValuesBucket ConvertFromValueBucket(ValuesBucket valuesBucket) 70 { 71 int64_t mapSize = valuesBucket.size; 72 NativeRdb::ValuesBucket nativeValuesBucket = NativeRdb::ValuesBucket(); 73 74 for (int64_t i = 0; i < mapSize; i++) { 75 NativeRdb::ValueObject valueObject = ValueTypeToValueObject(valuesBucket.value[i]); 76 std::string keyStr = valuesBucket.key[i]; 77 nativeValuesBucket.Put(keyStr, valueObject); 78 } 79 return nativeValuesBucket; 80 } 81 Query(RdbPredicatesImpl & predicates,char ** column,int64_t columnSize)82 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::Query(RdbPredicatesImpl &predicates, char** column, 83 int64_t columnSize) 84 { 85 std::vector<std::string> columnsVector = std::vector<std::string>(); 86 for (int64_t i = 0; i < columnSize; i++) { 87 columnsVector.push_back(std::string(column[i])); 88 } 89 auto resultSet = rdbStore_->Query(*(predicates.GetPredicates()), columnsVector); 90 return resultSet; 91 } 92 RemoteQuery(char * device,RdbPredicatesImpl & predicates,char ** column,int64_t columnSize)93 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::RemoteQuery(char* device, RdbPredicatesImpl &predicates, 94 char** column, int64_t columnSize) 95 { 96 std::vector<std::string> columnsVector; 97 for (int64_t i = 0; i < columnSize; i++) { 98 columnsVector.push_back(std::string(column[i])); 99 } 100 int32_t errCode; 101 auto resultSet = rdbStore_->RemoteQuery(std::string(device), *(predicates.GetPredicates()), columnsVector, 102 errCode); 103 return resultSet; 104 } 105 Update(ValuesBucket valuesBucket,RdbPredicatesImpl & predicates,NativeRdb::ConflictResolution conflictResolution,int32_t * errCode)106 int32_t RdbStoreImpl::Update(ValuesBucket valuesBucket, RdbPredicatesImpl &predicates, 107 NativeRdb::ConflictResolution conflictResolution, int32_t *errCode) 108 { 109 int32_t affectedRows; 110 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBucket); 111 *errCode = rdbStore_->UpdateWithConflictResolution(affectedRows, predicates.GetPredicates()->GetTableName(), 112 nativeValuesBucket, predicates.GetPredicates()->GetWhereClause(), predicates.GetPredicates()->GetBindArgs(), 113 conflictResolution); 114 return affectedRows; 115 } 116 Delete(RdbPredicatesImpl & predicates,int32_t * errCode)117 int RdbStoreImpl::Delete(RdbPredicatesImpl &predicates, int32_t *errCode) 118 { 119 int deletedRows = 0; 120 *errCode = rdbStore_->Delete(deletedRows, *(predicates.GetPredicates())); 121 return deletedRows; 122 } 123 SetDistributedTables(char ** tables,int64_t tablesSize)124 void RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize) 125 { 126 std::vector<std::string> tablesVector; 127 for (int64_t i = 0; i < tablesSize; i++) { 128 tablesVector.push_back(std::string(tables[i])); 129 } 130 rdbStore_->SetDistributedTables(tablesVector); 131 } 132 SetDistributedTables(char ** tables,int64_t tablesSize,int32_t type)133 void RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize, int32_t type) 134 { 135 std::vector<std::string> tablesVector; 136 for (int64_t i = 0; i < tablesSize; i++) { 137 tablesVector.push_back(std::string(tables[i])); 138 } 139 rdbStore_->SetDistributedTables(tablesVector, type); 140 } 141 SetDistributedTables(char ** tables,int64_t tablesSize,int32_t type,DistributedRdb::DistributedConfig & distributedConfig)142 void RdbStoreImpl::SetDistributedTables(char** tables, int64_t tablesSize, int32_t type, 143 DistributedRdb::DistributedConfig &distributedConfig) 144 { 145 std::vector<std::string> tablesVector; 146 for (int64_t i = 0; i < tablesSize; i++) { 147 tablesVector.push_back(std::string(tables[i])); 148 } 149 rdbStore_->SetDistributedTables(tablesVector, type, distributedConfig); 150 } 151 RollBack()152 int32_t RdbStoreImpl::RollBack() 153 { 154 return rdbStore_->RollBack(); 155 } 156 Commit()157 int32_t RdbStoreImpl::Commit() 158 { 159 return rdbStore_->Commit(); 160 } 161 BeginTransaction()162 int32_t RdbStoreImpl::BeginTransaction() 163 { 164 return rdbStore_->BeginTransaction(); 165 } 166 Backup(const char * destName)167 int32_t RdbStoreImpl::Backup(const char* destName) 168 { 169 return rdbStore_->Backup(destName, newKey); 170 } 171 Restore(const char * srcName)172 int32_t RdbStoreImpl::Restore(const char* srcName) 173 { 174 return rdbStore_->Restore(srcName, newKey); 175 } 176 ObtainDistributedTableName(const char * device,const char * table)177 char* RdbStoreImpl::ObtainDistributedTableName(const char* device, const char* table) 178 { 179 int errCode = RelationalStoreJsKit::E_INNER_ERROR; 180 std::string tableName = rdbStore_->ObtainDistributedTableName(device, table, errCode); 181 return MallocCString(tableName); 182 } 183 Emit(const char * event)184 int32_t RdbStoreImpl::Emit(const char* event) 185 { 186 return rdbStore_->Notify(event); 187 } 188 Insert(const char * table,ValuesBucket valuesBucket,int32_t conflict,int32_t * errCode)189 int64_t RdbStoreImpl::Insert(const char* table, ValuesBucket valuesBucket, int32_t conflict, int32_t *errCode) 190 { 191 std::string tableName = table; 192 int64_t result; 193 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBucket); 194 *errCode = rdbStore_->InsertWithConflictResolution(result, tableName, 195 nativeValuesBucket, NativeRdb::ConflictResolution(conflict)); 196 return result; 197 } 198 ExecuteSql(const char * sql,int32_t * errCode)199 void RdbStoreImpl::ExecuteSql(const char* sql, int32_t *errCode) 200 { 201 *errCode = rdbStore_->ExecuteSql(sql, bindArgs); 202 } 203 204 CleanDirtyData(const char * tableName,uint64_t cursor)205 int32_t RdbStoreImpl::CleanDirtyData(const char* tableName, uint64_t cursor) 206 { 207 int32_t rtnCode = rdbStore_->CleanDirtyData(tableName, cursor); 208 return rtnCode; 209 } 210 BatchInsert(int64_t & insertNum,const char * tableName,ValuesBucket * valuesBuckets,int64_t valuesSize)211 int32_t RdbStoreImpl::BatchInsert(int64_t &insertNum, const char* tableName, ValuesBucket* valuesBuckets, 212 int64_t valuesSize) 213 { 214 std::vector<NativeRdb::ValuesBucket> valuesVector; 215 std::string tableNameStr = tableName; 216 if (tableNameStr.empty()) { 217 return RelationalStoreJsKit::E_PARAM_ERROR; 218 } 219 for (int64_t i = 0; i < valuesSize; i++) { 220 NativeRdb::ValuesBucket nativeValuesBucket = ConvertFromValueBucket(valuesBuckets[i]); 221 valuesVector.push_back(nativeValuesBucket); 222 } 223 int32_t rtnCode = rdbStore_->BatchInsert(insertNum, tableNameStr, valuesVector); 224 return rtnCode; 225 } 226 Sync(int32_t mode,RdbPredicatesImpl & predicates)227 CArrSyncResult RdbStoreImpl::Sync(int32_t mode, RdbPredicatesImpl &predicates) 228 { 229 DistributedRdb::SyncOption option; 230 option.mode = static_cast<DistributedRdb::SyncMode>(mode); 231 option.isBlock = true; 232 DistributedRdb::SyncResult resMap; 233 rdbStore_->Sync(option, *(predicates.GetPredicates()), 234 [&resMap](const DistributedRdb::SyncResult &result) { resMap = result; }); 235 char** resultStr = static_cast<char**>(malloc(resMap.size() * sizeof(char*))); 236 int32_t* resultNum = static_cast<int32_t*>(malloc(resMap.size() * sizeof(int32_t))); 237 if (resultStr == nullptr || resultNum == nullptr) { 238 free(resultStr); 239 free(resultNum); 240 return CArrSyncResult{nullptr, nullptr, -1}; 241 } 242 size_t i = 0; 243 for (auto it = resMap.begin(); it != resMap.end(); ++it) { 244 resultStr[i] = MallocCString(it->first); 245 resultNum[i] = it->second; 246 i++; 247 } 248 return CArrSyncResult{resultStr, resultNum, int64_t(resMap.size())}; 249 } 250 QuerySql(const char * sql,ValueType * bindArgs,int64_t size)251 std::shared_ptr<NativeRdb::ResultSet> RdbStoreImpl::QuerySql(const char *sql, ValueType *bindArgs, int64_t size) 252 { 253 std::string tmpSql = sql; 254 std::vector<NativeRdb::ValueObject> tmpBindArgs = std::vector<NativeRdb::ValueObject>(); 255 for (int64_t i = 0; i < size; i++) { 256 tmpBindArgs.push_back(ValueTypeToValueObject(bindArgs[i])); 257 } 258 auto result = rdbStore_->QueryByStep(tmpSql, tmpBindArgs); 259 return result; 260 } 261 ExecuteSql(const char * sql,ValueType * bindArgs,int64_t bindArgsSize,int32_t * errCode)262 void RdbStoreImpl::ExecuteSql(const char* sql, ValueType* bindArgs, int64_t bindArgsSize, int32_t *errCode) 263 { 264 std::vector<NativeRdb::ValueObject> bindArgsObjects = std::vector<NativeRdb::ValueObject>(); 265 for (int64_t i = 0; i < bindArgsSize; i++) { 266 bindArgsObjects.push_back(ValueTypeToValueObject(bindArgs[i])); 267 } 268 *errCode = rdbStore_->ExecuteSql(sql, bindArgsObjects); 269 } 270 RegisterObserver(const char * event,bool interProcess,std::function<void ()> * callback,const std::function<void ()> & callbackRef)271 int32_t RdbStoreImpl::RegisterObserver(const char *event, bool interProcess, std::function<void()> *callback, 272 const std::function<void()>& callbackRef) 273 { 274 DistributedRdb::SubscribeOption option; 275 option.event = event; 276 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 277 DistributedRdb::SubscribeMode::LOCAL; 278 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 279 return RegisteredObserver(option, localObservers_, callback, callbackRef); 280 } 281 return RegisteredObserver(option, localSharedObservers_, callback, callbackRef); 282 } 283 isSameFunction(const std::function<void ()> * f1,const std::function<void ()> * f2)284 bool isSameFunction(const std::function<void()> *f1, const std::function<void()> *f2) 285 { 286 return f1 == f2; 287 } 288 HasRegisteredObserver(std::function<void ()> * callback,std::list<std::shared_ptr<RdbStoreObserverImpl>> & observers)289 bool RdbStoreImpl::HasRegisteredObserver( 290 std::function<void()> *callback, 291 std::list<std::shared_ptr<RdbStoreObserverImpl>> &observers) 292 { 293 for (auto &it : observers) { 294 if (isSameFunction(callback, it->GetCallBack())) { 295 return true; 296 } 297 } 298 return false; 299 } 300 RdbStoreObserverImpl(std::function<void ()> * callback,const std::function<void ()> & callbackRef)301 RdbStoreObserverImpl::RdbStoreObserverImpl(std::function<void()> *callback, 302 const std::function<void()>& callbackRef) 303 { 304 m_callback = callback; 305 m_callbackRef = callbackRef; 306 } 307 GetCallBack()308 std::function<void()> *RdbStoreObserverImpl::GetCallBack() 309 { 310 return m_callback; 311 } 312 RegisteredObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers,std::function<void ()> * callback,const std::function<void ()> & callbackRef)313 int32_t RdbStoreImpl::RegisteredObserver( 314 DistributedRdb::SubscribeOption option, 315 std::map<std::string, 316 std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers, 317 std::function<void()> *callback, const std::function<void()>& callbackRef) 318 { 319 observers.try_emplace(option.event); 320 if (!HasRegisteredObserver(callback, observers[option.event])) { 321 auto localObserver = std::make_shared<RdbStoreObserverImpl>(callback, callbackRef); 322 int32_t errCode = rdbStore_->Subscribe(option, localObserver.get()); 323 if (errCode != NativeRdb::E_OK) { 324 return errCode; 325 } 326 observers[option.event].push_back(localObserver); 327 LOGI("subscribe success event: %{public}s", option.event.c_str()); 328 } else { 329 LOGI("duplicate subscribe event: %{public}s", option.event.c_str()); 330 } 331 return RelationalStoreJsKit::OK; 332 } 333 OnChange()334 void RdbStoreObserverImpl::RdbStoreObserverImpl::OnChange() 335 { 336 m_callbackRef(); 337 } 338 UnRegisterObserver(const char * event,bool interProcess,std::function<void ()> * callback)339 int32_t RdbStoreImpl::UnRegisterObserver(const char *event, bool interProcess, std::function<void()> *callback) 340 { 341 DistributedRdb::SubscribeOption option; 342 option.event = event; 343 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 344 DistributedRdb::SubscribeMode::LOCAL; 345 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 346 return UnRegisteredObserver(option, localObservers_, callback); 347 } 348 return UnRegisteredObserver(option, localSharedObservers_, callback); 349 } 350 UnRegisteredObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers,std::function<void ()> * callback)351 int32_t RdbStoreImpl::UnRegisteredObserver(DistributedRdb::SubscribeOption option, std::map<std::string, 352 std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers, std::function<void()> *callback) 353 { 354 auto obs = observers.find(option.event); 355 if (obs == observers.end()) { 356 LOGI("observer not found, event: %{public}s", option.event.c_str()); 357 return RelationalStoreJsKit::OK; 358 } 359 360 auto &list = obs->second; 361 for (auto it = list.begin(); it != list.end(); it++) { 362 if (isSameFunction(callback, (*it)->GetCallBack())) { 363 int errCode = rdbStore_->UnSubscribe(option, it->get()); 364 if (errCode != RelationalStoreJsKit::OK) { 365 return errCode; 366 } 367 list.erase(it); 368 break; 369 } 370 } 371 if (list.empty()) { 372 observers.erase(option.event); 373 } 374 LOGI("unsubscribe success, event: %{public}s", option.event.c_str()); 375 return RelationalStoreJsKit::OK; 376 } 377 UnRegisterAllObserver(const char * event,bool interProcess)378 int32_t RdbStoreImpl::UnRegisterAllObserver(const char *event, bool interProcess) 379 { 380 DistributedRdb::SubscribeOption option; 381 option.event = event; 382 interProcess ? option.mode = DistributedRdb::SubscribeMode::LOCAL_SHARED : option.mode = 383 DistributedRdb::SubscribeMode::LOCAL; 384 if (option.mode == DistributedRdb::SubscribeMode::LOCAL) { 385 return UnRegisteredAllObserver(option, localObservers_); 386 } 387 return UnRegisteredAllObserver(option, localSharedObservers_); 388 } 389 UnRegisteredAllObserver(DistributedRdb::SubscribeOption option,std::map<std::string,std::list<std::shared_ptr<RdbStoreObserverImpl>>> & observers)390 int32_t RdbStoreImpl::UnRegisteredAllObserver(DistributedRdb::SubscribeOption option, std::map<std::string, 391 std::list<std::shared_ptr<RdbStoreObserverImpl>>> &observers) 392 { 393 auto obs = observers.find(option.event); 394 if (obs == observers.end()) { 395 LOGI("observer not found, event: %{public}s", option.event.c_str()); 396 return RelationalStoreJsKit::OK; 397 } 398 399 int errCode = rdbStore_->UnSubscribe(option, nullptr); 400 if (errCode != RelationalStoreJsKit::OK) { 401 return errCode; 402 } 403 observers.erase(option.event); 404 LOGI("unsubscribe success, event: %{public}s", option.event.c_str()); 405 return RelationalStoreJsKit::OK; 406 } 407 GetRealPath(AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const AppDataMgrJsKit::JSUtils::ContextParam & param,std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext)408 int32_t GetRealPath(AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, 409 const AppDataMgrJsKit::JSUtils::ContextParam ¶m, 410 std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext) 411 { 412 if (rdbConfig.name.find(PATH_SPLIT) != std::string::npos) { 413 LOGE("Parameter error. The StoreConfig.name must be a file name without path."); 414 return RelationalStoreJsKit::E_PARAM_ERROR; 415 } 416 417 if (!rdbConfig.customDir.empty()) { 418 // determine if the first character of customDir is '/' 419 if (rdbConfig.customDir.find_first_of(PATH_SPLIT) == 0) { 420 LOGE("Parameter error. The customDir must be a relative directory."); 421 return RelationalStoreJsKit::E_PARAM_ERROR; 422 } 423 // customDir length is limited to 128 bytes 424 if (rdbConfig.customDir.length() > 128) { 425 LOGE("Parameter error. The customDir length must be less than or equal to 128 bytes."); 426 return RelationalStoreJsKit::E_PARAM_ERROR; 427 } 428 } 429 430 std::string baseDir = param.baseDir; 431 if (!rdbConfig.dataGroupId.empty()) { 432 if (!param.isStageMode) { 433 return RelationalStoreJsKit::E_NOT_STAGE_MODE; 434 } 435 std::string groupDir; 436 int errCode = abilitycontext->GetSystemDatabaseDir(rdbConfig.dataGroupId, groupDir); 437 if (errCode != NativeRdb::E_OK && groupDir.empty()) { 438 return RelationalStoreJsKit::E_DATA_GROUP_ID_INVALID; 439 } 440 baseDir = groupDir; 441 } 442 443 auto [realPath, errorCode] = 444 NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(baseDir, rdbConfig.name, rdbConfig.customDir); 445 // realPath length is limited to 1024 bytes 446 if (errorCode != NativeRdb::E_OK || realPath.length() > 1024) { 447 LOGE("Parameter error. The database path must be a valid path."); 448 return RelationalStoreJsKit::E_PARAM_ERROR; 449 } 450 rdbConfig.path = realPath; 451 return NativeRdb::E_OK; 452 } 453 initContextParam(AppDataMgrJsKit::JSUtils::ContextParam & param,std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext)454 void initContextParam(AppDataMgrJsKit::JSUtils::ContextParam ¶m, 455 std::shared_ptr<OHOS::AppDataMgrJsKit::Context> abilitycontext) 456 { 457 param.bundleName = abilitycontext->GetBundleName(); 458 param.moduleName = abilitycontext->GetModuleName(); 459 param.baseDir = abilitycontext->GetDatabaseDir(); 460 param.area = abilitycontext->GetArea(); 461 param.isSystemApp = abilitycontext->IsSystemAppCalled(); 462 param.isStageMode = abilitycontext->IsStageMode(); 463 } 464 initRdbConfig(AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,StoreConfig & config)465 void initRdbConfig(AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, StoreConfig &config) 466 { 467 rdbConfig.isEncrypt = config.encrypt; 468 rdbConfig.isSearchable = config.isSearchable; 469 rdbConfig.isAutoClean = config.autoCleanDirtyData; 470 rdbConfig.securityLevel = static_cast<NativeRdb::SecurityLevel>(config.securityLevel); 471 rdbConfig.dataGroupId = config.dataGroupId; 472 rdbConfig.name = config.name; 473 rdbConfig.customDir = config.customDir; 474 } 475 getRdbStoreConfig(const AppDataMgrJsKit::JSUtils::RdbConfig & rdbConfig,const AppDataMgrJsKit::JSUtils::ContextParam & param)476 NativeRdb::RdbStoreConfig getRdbStoreConfig(const AppDataMgrJsKit::JSUtils::RdbConfig &rdbConfig, 477 const AppDataMgrJsKit::JSUtils::ContextParam ¶m) 478 { 479 NativeRdb::RdbStoreConfig rdbStoreConfig(rdbConfig.path); 480 rdbStoreConfig.SetEncryptStatus(rdbConfig.isEncrypt); 481 rdbStoreConfig.SetSearchable(rdbConfig.isSearchable); 482 rdbStoreConfig.SetIsVector(rdbConfig.vector); 483 rdbStoreConfig.SetAutoClean(rdbConfig.isAutoClean); 484 rdbStoreConfig.SetSecurityLevel(rdbConfig.securityLevel); 485 rdbStoreConfig.SetDataGroupId(rdbConfig.dataGroupId); 486 rdbStoreConfig.SetName(rdbConfig.name); 487 rdbStoreConfig.SetCustomDir(rdbConfig.customDir); 488 rdbStoreConfig.SetAllowRebuild(rdbConfig.allowRebuild); 489 490 if (!param.bundleName.empty()) { 491 rdbStoreConfig.SetBundleName(param.bundleName); 492 } 493 rdbStoreConfig.SetModuleName(param.moduleName); 494 rdbStoreConfig.SetArea(param.area); 495 return rdbStoreConfig; 496 } 497 GetRdbStore(OHOS::AbilityRuntime::Context * context,StoreConfig config,int32_t * errCode)498 int64_t GetRdbStore(OHOS::AbilityRuntime::Context* context, StoreConfig config, 499 int32_t *errCode) 500 { 501 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 502 AppDataMgrJsKit::JSUtils::ContextParam param; 503 initContextParam(param, abilitycontext); 504 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 505 initRdbConfig(rdbConfig, config); 506 507 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 508 if (*errCode != NativeRdb::E_OK) { 509 return -1; 510 } 511 512 DefaultOpenCallback callback; 513 auto rdbStore = 514 NativeRdb::RdbHelper::GetRdbStore(getRdbStoreConfig(rdbConfig, param), -1, callback, *errCode); 515 if (*errCode != 0) { 516 return -1; 517 } 518 auto nativeRdbStore = FFIData::Create<RdbStoreImpl>(rdbStore); 519 if (nativeRdbStore == nullptr) { 520 *errCode = -1; 521 return -1; 522 } 523 return nativeRdbStore->GetID(); 524 } 525 DeleteRdbStore(OHOS::AbilityRuntime::Context * context,const char * name,int32_t * errCode)526 void DeleteRdbStore(OHOS::AbilityRuntime::Context* context, const char* name, 527 int32_t *errCode) 528 { 529 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 530 AppDataMgrJsKit::JSUtils::ContextParam param; 531 initContextParam(param, abilitycontext); 532 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 533 rdbConfig.name = name; 534 535 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 536 if (*errCode != NativeRdb::E_OK) { 537 return; 538 } 539 *errCode = NativeRdb::RdbHelper::DeleteRdbStore(rdbConfig.path); 540 return; 541 } 542 DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context * context,StoreConfig config,int32_t * errCode)543 void DeleteRdbStoreConfig(OHOS::AbilityRuntime::Context* context, StoreConfig config, 544 int32_t *errCode) 545 { 546 auto abilitycontext = std::make_shared<AppDataMgrJsKit::Context>(context->shared_from_this()); 547 AppDataMgrJsKit::JSUtils::ContextParam param; 548 initContextParam(param, abilitycontext); 549 AppDataMgrJsKit::JSUtils::RdbConfig rdbConfig; 550 initRdbConfig(rdbConfig, config); 551 552 *errCode = GetRealPath(rdbConfig, param, abilitycontext); 553 if (*errCode != NativeRdb::E_OK) { 554 return; 555 } 556 *errCode = NativeRdb::RdbHelper::DeleteRdbStore(rdbConfig.path); 557 return; 558 } 559 } 560 }