* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "napi/native_api.h"
#include "database/rdb/relational_store.h"
static napi_value DefaultConfigRdbStore(napi_env env, napi_callback_info info)
{
OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
OH_Rdb_SetDatabaseDir(config, "/data/storage/el2/database");
OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL2);
OH_Rdb_SetBundleName(config, "com.example.nativedemo");
OH_Rdb_SetStoreName(config, "RdbTest.db");
OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S3);
OH_Rdb_SetEncrypted(config, true);
int errCode = 0;
OH_Rdb_Store *store = OH_Rdb_CreateOrOpen(config, &errCode);
OH_Rdb_CloseStore(store);
store = nullptr;
OH_Rdb_DestroyConfig(config);
config = nullptr;
napi_value sum;
napi_create_int32(env, 0, &sum);
return sum;
}
static napi_value CustomizedConfigRdbStore(napi_env env,
napi_callback_info info)
{
OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
OH_Rdb_SetDatabaseDir(config, "/data/storage/el2/database");
OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL2);
OH_Rdb_SetStoreName(config, "RdbTestConfigEncryptParam.db");
OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S3);
OH_Rdb_SetBundleName(config, "com.example.nativedemo");
OH_Rdb_SetEncrypted(config, true);
OH_Rdb_CryptoParam *cryptoParam = OH_Rdb_CreateCryptoParam();
uint8_t key[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36};
const int32_t length = 6;
OH_Crypto_SetEncryptionKey(cryptoParam, key, length);
for (size_t i = 0; i < sizeof(key); i++) {
key[i] = 0;
}
const int64_t iteration = 64000;
OH_Crypto_SetIteration(cryptoParam, iteration);
OH_Crypto_SetEncryptionAlgo(cryptoParam, Rdb_EncryptionAlgo::RDB_AES_256_CBC);
OH_Crypto_SetHmacAlgo(cryptoParam, RDB_HMAC_SHA512);
OH_Crypto_SetKdfAlgo(cryptoParam, RDB_KDF_SHA512);
const int64_t pageSize = 4096;
OH_Crypto_SetCryptoPageSize(cryptoParam, pageSize);
OH_Rdb_SetCryptoParam(config, cryptoParam);
int errCode = 0;
OH_Rdb_Store *store = OH_Rdb_CreateOrOpen(config, &errCode);
OH_Rdb_DestroyCryptoParam(cryptoParam);
cryptoParam = nullptr;
OH_Rdb_CloseStore(store);
store = nullptr;
OH_Rdb_DestroyConfig(config);
config = nullptr;
napi_value sum;
napi_create_int32(env, 0, &sum);
return sum;
}
static napi_value BackupRdbStore(napi_env env, napi_callback_info info)
{
OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
OH_Rdb_SetDatabaseDir(config, "/data/storage/el2/database");
OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL2);
OH_Rdb_SetStoreName(config, "RdbTest.db");
OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S3);
OH_Rdb_SetBundleName(config, "com.example.nativedemo");
int errCode = 0;
OH_Rdb_Store *store = OH_Rdb_CreateOrOpen(config, &errCode);
int result = OH_Rdb_Backup(store, "/data/storage/el2/database/RdbTest_bak.db");
OH_Rdb_CloseStore(store);
store = nullptr;
OH_Rdb_DestroyConfig(config);
config = nullptr;
napi_value sum;
napi_create_int32(env, 0, &sum);
return sum;
}
static napi_value RestoreRdbStore(napi_env env, napi_callback_info info)
{
OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
OH_Rdb_SetDatabaseDir(config, "/data/storage/el2/database");
OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL2);
OH_Rdb_SetStoreName(config, "RdbRestoreTest.db");
OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S3);
OH_Rdb_SetBundleName(config, "com.example.nativedemo");
int errCode = 0;
OH_Rdb_Store *store = OH_Rdb_CreateOrOpen(config, &errCode);
int result2 =
OH_Rdb_Restore(store, "/data/storage/el2/database/RdbTest_bak.db");
OH_Rdb_CloseStore(store);
store = nullptr;
OH_Rdb_DestroyConfig(config);
config = nullptr;
napi_value sum;
napi_create_int32(env, 0, &sum);
return sum;
}
static napi_value SetSecurityLevelForRdbStore(napi_env env,
napi_callback_info info)
{
OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
OH_Rdb_SetDatabaseDir(config, "/data/storage/el2/database");
OH_Rdb_SetStoreName(config, "RdbTest.db");
OH_Rdb_SetBundleName(config, "com.example.nativedemo");
OH_Rdb_SetModuleName(config, "entry");
OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S3);
OH_Rdb_SetEncrypted(config, false);
OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL2);
int errCode = 0;
OH_Rdb_Store *store_ = OH_Rdb_CreateOrOpen(config, &errCode);
OH_Rdb_CloseStore(store_);
store_ = nullptr;
OH_Rdb_DestroyConfig(config);
config = nullptr;
napi_value ret;
napi_create_int32(env, 0, &ret);
return ret;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{"defaultConfigRdbStore", nullptr, DefaultConfigRdbStore, nullptr, nullptr, nullptr, napi_default, nullptr},
{"customizedConfigRdbStore", nullptr, CustomizedConfigRdbStore, nullptr, nullptr, nullptr, napi_default,
nullptr},
{"backupRdbStore", nullptr, BackupRdbStore, nullptr, nullptr, nullptr, napi_default, nullptr},
{"restoreRdbStore", nullptr, RestoreRdbStore, nullptr, nullptr, nullptr, napi_default, nullptr},
{"setSecurityLevelForRdbStore", nullptr, SetSecurityLevelForRdbStore, nullptr, nullptr, nullptr, napi_default,
nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void *)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
}