• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "RdbNdkUtils"
17 #include "rdb_ndk_utils.h"
18 
19 #include "logger.h"
20 #include "rdb_errno.h"
21 #include "rdb_sql_utils.h"
22 #include "relational_store_inner_types.h"
23 
24 namespace OHOS::RdbNdk {
25 // caller must ensure token is valid
ConvertTokenizer2Native(Rdb_Tokenizer token)26 static OHOS::NativeRdb::Tokenizer ConvertTokenizer2Native(Rdb_Tokenizer token)
27 {
28     if (token == Rdb_Tokenizer::RDB_NONE_TOKENIZER) {
29         return OHOS::NativeRdb::Tokenizer::NONE_TOKENIZER;
30     }
31     if (token == Rdb_Tokenizer::RDB_ICU_TOKENIZER) {
32         return OHOS::NativeRdb::Tokenizer::ICU_TOKENIZER;
33     }
34     if (token == Rdb_Tokenizer::RDB_CUSTOM_TOKENIZER) {
35         return OHOS::NativeRdb::Tokenizer::CUSTOM_TOKENIZER;
36     }
37     return OHOS::NativeRdb::Tokenizer::TOKENIZER_END;
38 }
39 
GetRdbStoreConfig(const OH_Rdb_ConfigV2 * config)40 std::pair<int32_t, OHOS::NativeRdb::RdbStoreConfig> RdbNdkUtils::GetRdbStoreConfig(const OH_Rdb_ConfigV2 *config)
41 {
42     if (config == nullptr || config->magicNum != RDB_CONFIG_V2_MAGIC_CODE || (config->persist &&
43         ((OHOS::NativeRdb::SecurityLevel(config->securityLevel) < OHOS::NativeRdb::SecurityLevel::S1 ||
44         OHOS::NativeRdb::SecurityLevel(config->securityLevel) >= OHOS::NativeRdb::SecurityLevel::LAST))) ||
45         (config->area < RDB_SECURITY_AREA_EL1 || config->area > RDB_SECURITY_AREA_EL5) ||
46         (config->dbType < RDB_SQLITE || config->dbType > RDB_CAYLEY) ||
47         (config->token < RDB_NONE_TOKENIZER || config->token > RDB_CUSTOM_TOKENIZER)) {
48         if (config != nullptr) {
49             LOG_ERROR("Config magic number is not valid %{public}x or securityLevel %{public}d area %{public}d"
50                       " dbType %{public}d token %{public}d persist %{public}d"
51                       " readOnly %{public}d",
52                 config->magicNum, config->securityLevel, config->area, config->dbType, config->token,
53                 config->persist, config->readOnly);
54         } else {
55             LOG_ERROR("Config is null");
56         }
57         return { OHOS::NativeRdb::E_INVALID_ARGS, OHOS::NativeRdb::RdbStoreConfig("") };
58     }
59 
60     std::string realPath;
61     int32_t code = OHOS::NativeRdb::E_OK;
62     if (config->persist) {
63         std::tie(realPath, code) = OHOS::NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(
64             config->dataBaseDir, config->storeName, config->customDir);
65     } else {
66         realPath = config->dataBaseDir;
67     }
68 
69     if (code != OHOS::NativeRdb::E_OK) {
70         LOG_ERROR("Get database path failed from new config, ret %{public}d ", code);
71         return { OHOS::NativeRdb::E_INVALID_ARGS, OHOS::NativeRdb::RdbStoreConfig("") };
72     }
73     OHOS::NativeRdb::RdbStoreConfig rdbStoreConfig(realPath);
74     rdbStoreConfig.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel(config->securityLevel));
75     rdbStoreConfig.SetEncryptStatus(config->isEncrypt);
76     rdbStoreConfig.SetArea(config->area - 1);
77     rdbStoreConfig.SetIsVector(config->dbType == RDB_CAYLEY);
78     rdbStoreConfig.SetBundleName(config->bundleName);
79     rdbStoreConfig.SetName(config->storeName);
80     rdbStoreConfig.SetTokenizer(ConvertTokenizer2Native(static_cast<Rdb_Tokenizer>(config->token)));
81     rdbStoreConfig.SetStorageMode(
82         config->persist ? OHOS::NativeRdb::StorageMode::MODE_DISK : OHOS::NativeRdb::StorageMode::MODE_MEMORY);
83     rdbStoreConfig.SetCustomDir(config->customDir);
84     rdbStoreConfig.SetReadOnly(config->readOnly);
85     rdbStoreConfig.SetPluginLibs(config->pluginLibs);
86     rdbStoreConfig.SetCryptoParam(config->cryptoParam);
87     rdbStoreConfig.SetEnableSemanticIndex(config->enableSemanticIndex);
88     return { OHOS::NativeRdb::E_OK, rdbStoreConfig };
89 }
90 } // namespace OHOS::RdbNdk