vdb support ndk

Signed-off-by: wy524668781@icloud.com <wy524668781@icloud.com>
This commit is contained in:
wy524668781@icloud.com 2024-09-12 10:18:11 +08:00
parent 566de22382
commit 1210bbcdf6
4 changed files with 318 additions and 70 deletions

View File

@ -17,6 +17,7 @@
#define RDB_GRD_API_MANAGER_H
#include "grd_type_export.h"
#include "rdb_visibility.h"
namespace OHOS {
namespace NativeRdb {
@ -86,6 +87,8 @@ struct GRD_APIInfo {
DBSetConfig DBSetConfigApi = nullptr;
};
API_EXPORT bool IsUsingArkData();
GRD_APIInfo GetApiInfoInstance();
} // namespace NativeRdb

View File

@ -63,6 +63,18 @@ void GRD_DBApiInitEnhance(GRD_APIInfo &GRD_DBApiInfo)
#endif
}
bool IsUsingArkData()
{
#ifndef _WIN32
if (g_library == nullptr) {
GetApiInfoInstance();
}
return g_library != nullptr;
#else
return false;
#endif
}
GRD_APIInfo GetApiInfoInstance()
{
GRD_APIInfo GRD_TempApiStruct;

View File

@ -80,7 +80,7 @@ int RdUtils::RdDbOpen(const char *dbPath, const char *configStr, uint32_t flags,
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBOpenApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBOpenApi(dbPath, configStr, flags, db));
}
@ -92,55 +92,51 @@ int RdUtils::RdDbClose(GRD_DB *db, uint32_t flags)
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBCloseApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBCloseApi(db, flags));
}
int RdUtils::RdDbRepair(const char *dbPath, const char *configStr)
{
LOG_DEBUG("[RdUtils::RdDbRepair]");
if (GRD_KVApiInfo.DBRepairApi == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBRepairApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBRepairApi(dbPath, configStr));
}
int RdUtils::RdSqlPrepare(GRD_DB *db, const char *str, uint32_t strLen, GRD_SqlStmt **stmt, const char **unusedStr)
{
LOG_DEBUG("[RdUtils::RdSqlPrepare]");
if (GRD_KVApiInfo.DBSqlPrepare == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlPrepare == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlPrepare(db, str, strLen, stmt, unusedStr));
}
int RdUtils::RdSqlReset(GRD_SqlStmt *stmt)
{
LOG_DEBUG("[RdUtils::RdSqlReset]");
if (GRD_KVApiInfo.DBSqlReset == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlReset == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlReset(stmt));
}
int RdUtils::RdSqlFinalize(GRD_SqlStmt *stmt)
{
LOG_DEBUG("[RdUtils::RdSqlFinalize]");
if (GRD_KVApiInfo.DBSqlFinalize == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlFinalize == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlFinalize(stmt));
}
@ -152,12 +148,11 @@ void RdSqlFreeBlob(void *blobElementSize)
int RdUtils::RdSqlBindBlob(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int32_t len, void (*freeFunc)(void *))
{
LOG_DEBUG("[RdUtils::RdSqlBindBlob]");
if (GRD_KVApiInfo.DBSqlBindBlob == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindBlob == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
if (len <= 0) {
LOG_ERROR("Invalid len %{public}d", len);
@ -186,12 +181,11 @@ void RdSqlFreeCharStr(void *charStr)
int RdUtils::RdSqlBindText(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int32_t len, void (*freeFunc)(void *))
{
LOG_DEBUG("[RdUtils::RdSqlBindText]");
if (GRD_KVApiInfo.DBSqlBindText == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindText == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
if (len <= 0) {
LOG_ERROR("Invalid len %{public}d", len);
@ -215,48 +209,44 @@ int RdUtils::RdSqlBindText(GRD_SqlStmt *stmt, uint32_t idx, const void *val, int
int RdUtils::RdSqlBindInt(GRD_SqlStmt *stmt, uint32_t idx, int32_t val)
{
LOG_DEBUG("[RdUtils::RdSqlBindInt]");
if (GRD_KVApiInfo.DBSqlBindInt == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindInt == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlBindInt(stmt, idx, val));
}
int RdUtils::RdSqlBindInt64(GRD_SqlStmt *stmt, uint32_t idx, int64_t val)
{
LOG_DEBUG("[RdUtils::RdSqlBindInt64]");
if (GRD_KVApiInfo.DBSqlBindInt64 == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindInt64 == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlBindInt64(stmt, idx, val));
}
int RdUtils::RdSqlBindDouble(GRD_SqlStmt *stmt, uint32_t idx, double val)
{
LOG_DEBUG("[RdUtils::RdSqlBindDouble]");
if (GRD_KVApiInfo.DBSqlBindDouble == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindDouble == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlBindDouble(stmt, idx, val));
}
int RdUtils::RdSqlBindNull(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlBindNull]");
if (GRD_KVApiInfo.DBSqlBindNull == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindNull == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlBindNull(stmt, idx));
}
@ -269,12 +259,11 @@ void RdSqlFreeFloatArr(void *floatElement)
int RdUtils::RdSqlBindFloatVector(GRD_SqlStmt *stmt, uint32_t idx, float *val,
uint32_t dim, void (*freeFunc)(void *))
{
LOG_DEBUG("[RdUtils::RdSqlBindFloatVector]");
if (GRD_KVApiInfo.DBSqlBindFloatVector == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlBindFloatVector == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
if (dim <= 0) {
LOG_ERROR("Invalid dim %{public}d", dim);
@ -298,31 +287,28 @@ int RdUtils::RdSqlBindFloatVector(GRD_SqlStmt *stmt, uint32_t idx, float *val,
int RdUtils::RdSqlStep(GRD_SqlStmt *stmt)
{
LOG_DEBUG("[RdUtils::RdSqlStep]");
if (GRD_KVApiInfo.DBSqlStep == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlStep == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlStep(stmt));
}
int RdUtils::RdSqlColCnt(GRD_SqlStmt *stmt)
{
LOG_DEBUG("[RdUtils::RdSqlColCnt]");
if (GRD_KVApiInfo.DBSqlColCnt == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlColCnt == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlColCnt(stmt));
}
ColumnType RdUtils::RdSqlColType(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColType]");
if (GRD_KVApiInfo.DBSqlColType == nullptr) {
GetApiInfoInstance();
}
@ -334,19 +320,17 @@ ColumnType RdUtils::RdSqlColType(GRD_SqlStmt *stmt, uint32_t idx)
int RdUtils::RdSqlColBytes(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColBytes]");
if (GRD_KVApiInfo.DBSqlColBytes == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSqlColBytes == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBSqlColBytes(stmt, idx));
}
char *RdUtils::RdSqlColName(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColName]");
if (GRD_KVApiInfo.DBSqlColName == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -358,7 +342,6 @@ char *RdUtils::RdSqlColName(GRD_SqlStmt *stmt, uint32_t idx)
GRD_DbValueT RdUtils::RdSqlColValue(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColValue]");
if (GRD_KVApiInfo.DBSqlColValue == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -370,7 +353,6 @@ GRD_DbValueT RdUtils::RdSqlColValue(GRD_SqlStmt *stmt, uint32_t idx)
uint8_t *RdUtils::RdSqlColBlob(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColBlob]");
if (GRD_KVApiInfo.DBSqlColBlob == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -382,7 +364,6 @@ uint8_t *RdUtils::RdSqlColBlob(GRD_SqlStmt *stmt, uint32_t idx)
char *RdUtils::RdSqlColText(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColText]");
if (GRD_KVApiInfo.DBSqlColText == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -394,7 +375,6 @@ char *RdUtils::RdSqlColText(GRD_SqlStmt *stmt, uint32_t idx)
int RdUtils::RdSqlColInt(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColInt]");
if (GRD_KVApiInfo.DBSqlColInt == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -406,7 +386,6 @@ int RdUtils::RdSqlColInt(GRD_SqlStmt *stmt, uint32_t idx)
uint64_t RdUtils::RdSqlColInt64(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColInt64]");
if (GRD_KVApiInfo.DBSqlColInt64 == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -418,7 +397,6 @@ uint64_t RdUtils::RdSqlColInt64(GRD_SqlStmt *stmt, uint32_t idx)
double RdUtils::RdSqlColDouble(GRD_SqlStmt *stmt, uint32_t idx)
{
LOG_DEBUG("[RdUtils::RdSqlColDouble]");
if (GRD_KVApiInfo.DBSqlColDouble == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -430,7 +408,6 @@ double RdUtils::RdSqlColDouble(GRD_SqlStmt *stmt, uint32_t idx)
const float *RdUtils::RdSqlColumnFloatVector(GRD_SqlStmt *stmt, uint32_t idx, uint32_t *dim)
{
LOG_DEBUG("[RdUtils::RdSqlColumnFloatVector]");
if (GRD_KVApiInfo.DBSqlColumnFloatVector == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
@ -442,24 +419,22 @@ const float *RdUtils::RdSqlColumnFloatVector(GRD_SqlStmt *stmt, uint32_t idx, ui
int RdUtils::RdDbBackup(GRD_DB *db, const char *backupDbFile, uint8_t *encryptedKey, uint32_t encryptedKeyLen)
{
LOG_DEBUG("[RdUtils::RdDbBackup]");
if (GRD_KVApiInfo.DBBackupApi == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBBackupApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBBackupApi(db, backupDbFile, encryptedKey, encryptedKeyLen));
}
int RdUtils::RdDbRestore(GRD_DB *db, const char *backupDbFile, uint8_t *encryptedKey, uint32_t encryptedKeyLen)
{
LOG_DEBUG("[RdUtils::RdDbRestore]");
if (GRD_KVApiInfo.DBRestoreApi == nullptr) {
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBRestoreApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
return TransferGrdErrno(GRD_KVApiInfo.DBRestoreApi(db, backupDbFile, encryptedKey, encryptedKeyLen));
}
@ -470,7 +445,7 @@ int RdUtils::RdDbGetVersion(GRD_DB *db, GRD_ConfigTypeE type, int &version)
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBGetConfigApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
GRD_DbValueT value = GRD_KVApiInfo.DBGetConfigApi(db, type);
version = value.value.longValue;
@ -483,7 +458,7 @@ int RdUtils::RdDbSetVersion(GRD_DB *db, GRD_ConfigTypeE type, int version)
GRD_KVApiInfo = GetApiInfoInstance();
}
if (GRD_KVApiInfo.DBSetConfigApi == nullptr) {
return TransferGrdErrno(GRD_INNER_ERR);
return E_NOT_SUPPORT;
}
GRD_DbValueT value;
value.type = GRD_DB_DATATYPE_INTEGER;

View File

@ -15,6 +15,8 @@
#define LOG_TAG "RelationalStore"
#include "relational_store.h"
#include "convertor_error_code.h"
#include "grd_api_manager.h"
#include "logger.h"
#include "modify_time_cursor.h"
#include "raw_data_parser.h"
@ -22,6 +24,7 @@
#include "rdb_helper.h"
#include "rdb_predicates.h"
#include "rdb_sql_utils.h"
#include "rdb_store_config.h"
#include "relational_cursor.h"
#include "relational_predicates.h"
#include "relational_predicates_objects.h"
@ -31,13 +34,157 @@
#include "relational_values_bucket.h"
#include "securec.h"
#include "sqlite_global_config.h"
#include "convertor_error_code.h"
using namespace OHOS::RdbNdk;
using namespace OHOS::DistributedRdb;
constexpr int RDB_STORE_CID = 1234560; // The class id used to uniquely identify the OH_Rdb_Store class.
constexpr int RDB_CONFIG_SIZE_V0 = 41;
constexpr int RDB_CONFIG_SIZE_V1 = 45;
constexpr int RDB_CONFIG_V2_MAGIC_CODE = 0xDBCF2ADE;
constexpr int RDB_SQLITE = 1;
constexpr int RDB_CARLEY = 2;
static int g_supportDbTypes[] = {RDB_SQLITE, RDB_CARLEY};
typedef struct OH_Rdb_ConfigV2 {
int magicNum = RDB_CONFIG_V2_MAGIC_CODE;
std::string dataBaseDir = "";
std::string storeName = "";
std::string bundleName = "";
std::string moduleName = "";
bool isEncrypt = false;
int securityLevel = 0;
int area = 0;
int dbType = RDB_SQLITE;
} OH_Rdb_ConfigV2;
OH_Rdb_ConfigV2 *OH_Rdb_CreateConfig()
{
return new (std::nothrow) OH_Rdb_ConfigV2();
}
int OH_Rdb_DestroyConfig(OH_Rdb_ConfigV2 *config)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when destroy.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
delete config;
config = nullptr;
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetDataBaseDir(OH_Rdb_ConfigV2 *config, const char *dataBaseDir)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when Set DataBaseDir.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->dataBaseDir = std::string(dataBaseDir);
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetStoreName(OH_Rdb_ConfigV2 *config, const char *storeName)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x When set storeName.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->storeName = std::string(storeName);
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetBundleName(OH_Rdb_ConfigV2 *config, const char *bundleName)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when set bundleName.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->bundleName = std::string(bundleName);
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetModuleName(OH_Rdb_ConfigV2 *config, const char *moduleName)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when set moduleName.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->moduleName = std::string(moduleName);
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetEncrypt(OH_Rdb_ConfigV2 *config, bool isEncrypt)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when set encrypt.", (config == nullptr),
(config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->isEncrypt = isEncrypt;
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetSecurityLevel(OH_Rdb_ConfigV2 *config, int securityLevel)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when set security level.",
(config == nullptr), (config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
if (securityLevel < S1 || securityLevel > S4) {
LOG_ERROR("securityLevel value is out of range %{public}d", securityLevel);
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->securityLevel = securityLevel;
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetArea(OH_Rdb_ConfigV2 *config, int area)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is null %{public}d or magic num not valid %{public}x when set area.", (config == nullptr),
(config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
if (area < RDB_SECURITY_AREA_EL1 || area > RDB_SECURITY_AREA_EL5) {
LOG_ERROR("area value is out of range %{public}d", area);
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
config->area = area;
return OH_Rdb_ErrCode::RDB_OK;
}
int OH_Rdb_SetDbType(OH_Rdb_ConfigV2 *config, int dbType)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE) ||
(dbType < RDB_SQLITE || dbType > RDB_CARLEY)) {
LOG_ERROR("config is null %{public}d or magicNum not valid %{public}d or dbType is out of range %{public}d",
(config == nullptr), (config == nullptr ? 0 : config->magicNum), dbType);
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
if (dbType == RDB_CARLEY && !(OHOS::NativeRdb::IsUsingArkData())) {
return OH_Rdb_ErrCode::RDB_E_NOT_SUPPORTED;
}
config->dbType = dbType;
return OH_Rdb_ErrCode::RDB_OK;
}
const int *OH_Rdb_GetSupportDBType(int *numType)
{
if (numType == nullptr) {
return nullptr;
}
// if use arkData, then numType will be 2 {RDB_SQLITE and RDB_CARLEY}, otherwise only 1 {RDB_SQLITE}
*numType = OHOS::NativeRdb::IsUsingArkData() ? 2 : 1;
return g_supportDbTypes;
}
OH_VObject *OH_Rdb_CreateValueObject()
{
return new (std::nothrow) RelationalPredicatesObjects();
@ -63,10 +210,9 @@ OHOS::RdbNdk::RelationalStore::RelationalStore(std::shared_ptr<OHOS::NativeRdb::
int RelationalStore::SubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback)
{
std::lock_guard<decltype(mutex_)> lock(mutex_) ;
bool result = std::any_of(callbacks_.begin(), callbacks_.end(), [callback](const auto &observer) {
return *observer == callback;
});
std::lock_guard<decltype(mutex_)> lock(mutex_);
bool result = std::any_of(
callbacks_.begin(), callbacks_.end(), [callback](const auto &observer) { return *observer == callback; });
if (result) {
LOG_INFO("duplicate subscribe.");
return OH_Rdb_ErrCode::RDB_OK;
@ -83,7 +229,7 @@ int RelationalStore::SubscribeAutoSyncProgress(const Rdb_ProgressObserver *callb
int RelationalStore::UnsubscribeAutoSyncProgress(const Rdb_ProgressObserver *callback)
{
std::lock_guard<decltype(mutex_)> lock(mutex_) ;
std::lock_guard<decltype(mutex_)> lock(mutex_);
for (auto it = callbacks_.begin(); it != callbacks_.end();) {
if (callback != nullptr && !(**it == callback)) {
++it;
@ -164,6 +310,36 @@ RelationalStore *GetRelationalStore(OH_Rdb_Store *store)
return static_cast<RelationalStore *>(store);
}
static OHOS::NativeRdb::RdbStoreConfig GetRdbStoreConfig(const OH_Rdb_ConfigV2 *config, int *errCode)
{
if (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE ||
(OHOS::NativeRdb::SecurityLevel(config->securityLevel) < OHOS::NativeRdb::SecurityLevel::S1 ||
OHOS::NativeRdb::SecurityLevel(config->securityLevel) >= OHOS::NativeRdb::SecurityLevel::LAST) ||
(config->area < RDB_SECURITY_AREA_EL1 || config->area > RDB_SECURITY_AREA_EL5) ||
(config->dbType < RDB_SQLITE || config->dbType > RDB_CARLEY)) {
*errCode = OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
LOG_ERROR("Config magic number is not valid %{public}x or securityLevel %{public}d area %{public}d"
"dbType %{public}d ret %{public}d", config->magicNum, config->securityLevel, config->area, config->dbType,
*errCode);
return OHOS::NativeRdb::RdbStoreConfig("");
}
std::string realPath =
OHOS::NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(config->dataBaseDir, config->storeName, *errCode);
if (*errCode != 0) {
*errCode = ConvertorErrorCode::NativeToNdk(*errCode);
LOG_ERROR("Get database path failed from new config, ret %{public}d ", *errCode);
return OHOS::NativeRdb::RdbStoreConfig("");
}
OHOS::NativeRdb::RdbStoreConfig rdbStoreConfig(realPath);
rdbStoreConfig.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel(config->securityLevel));
rdbStoreConfig.SetEncryptStatus(config->isEncrypt);
rdbStoreConfig.SetArea(config->area - 1);
rdbStoreConfig.SetIsVector(config->dbType == RDB_CARLEY);
rdbStoreConfig.SetBundleName(config->bundleName);
rdbStoreConfig.SetName(config->storeName);
return rdbStoreConfig;
}
OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode)
{
if (config == nullptr || config->selfSize > RDB_CONFIG_SIZE_V1 || errCode == nullptr) {
@ -184,7 +360,7 @@ OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode)
rdbStoreConfig.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel(config->securityLevel));
rdbStoreConfig.SetEncryptStatus(config->isEncrypt);
if (config->selfSize > RDB_CONFIG_SIZE_V0) {
rdbStoreConfig.SetArea(config->area);
rdbStoreConfig.SetArea(config->area - 1);
}
if (config->bundleName != nullptr) {
rdbStoreConfig.SetBundleName(config->bundleName);
@ -202,6 +378,29 @@ OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode)
return new (std::nothrow) RelationalStore(store);
}
OH_Rdb_Store *OH_Rdb_CreateOrOpen(const OH_Rdb_ConfigV2 *config, int *errCode)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE) || errCode == nullptr) {
LOG_ERROR("Parameters set error:config is NULL ? %{public}d or magicNum is not valid %{public}d or"
" errCode is NULL ? %{public}d ", (config == nullptr), (config == nullptr ? 0 : config->magicNum),
(errCode == nullptr));
return nullptr;
}
OHOS::NativeRdb::RdbStoreConfig rdbStoreConfig = GetRdbStoreConfig(config, errCode);
if (*errCode != OH_Rdb_ErrCode::RDB_OK) {
return nullptr;
}
MainOpenCallback callback;
std::shared_ptr<OHOS::NativeRdb::RdbStore> store =
OHOS::NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, -1, callback, *errCode);
*errCode = ConvertorErrorCode::NativeToNdk(*errCode);
if (store == nullptr) {
LOG_ERROR("Get RDB Store fail %{public}s", rdbStoreConfig.GetPath().c_str());
return nullptr;
}
return new (std::nothrow) RelationalStore(store);
}
int OH_Rdb_CloseStore(OH_Rdb_Store *store)
{
auto rdbStore = GetRelationalStore(store);
@ -227,6 +426,22 @@ int OH_Rdb_DeleteStore(const OH_Rdb_Config *config)
return ConvertorErrorCode::NativeToNdk(OHOS::NativeRdb::RdbHelper::DeleteRdbStore(realPath));
}
int OH_Rdb_DeleteStoreV2(const OH_Rdb_ConfigV2 *config)
{
if (config == nullptr || (config->magicNum != RDB_CONFIG_V2_MAGIC_CODE)) {
LOG_ERROR("config is NULL ? %{public}d, config is invalid ? %{public}d", (config == nullptr),
(config == nullptr ? 0 : config->magicNum));
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
int errCode = OHOS::NativeRdb::E_OK;
std::string realPath =
OHOS::NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(config->dataBaseDir, config->storeName, errCode);
if (errCode != OHOS::NativeRdb::E_OK) {
return ConvertorErrorCode::NativeToNdk(errCode);
}
return ConvertorErrorCode::NativeToNdk(OHOS::NativeRdb::RdbHelper::DeleteRdbStore(realPath));
}
int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_VBucket *valuesBucket)
{
auto rdbStore = GetRelationalStore(store);
@ -294,7 +509,9 @@ OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql)
return nullptr;
}
std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet =
rdbStore->GetStore()->QuerySql(sql, std::vector<std::string>{});
rdbStore->GetStore()->GetDbType() == OHOS::NativeRdb::DB_VECTOR
? rdbStore->GetStore()->QueryByStep(sql, std::vector<std::string>{})
: rdbStore->GetStore()->QuerySql(sql, std::vector<std::string>{});
if (resultSet == nullptr) {
return nullptr;
}
@ -311,6 +528,16 @@ int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql)
rdbStore->GetStore()->ExecuteSql(sql, std::vector<OHOS::NativeRdb::ValueObject>{}));
}
int OH_Rdb_ExecuteByTrxId(OH_Rdb_Store *store, int64_t trxId, const char *sql)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr || sql == nullptr) {
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
return ConvertorErrorCode::NativeToNdk(
(rdbStore->GetStore()->Execute(sql, std::vector<OHOS::NativeRdb::ValueObject>{}, trxId)).first);
}
int OH_Rdb_BeginTransaction(OH_Rdb_Store *store)
{
auto rdbStore = GetRelationalStore(store);
@ -338,6 +565,35 @@ int OH_Rdb_Commit(OH_Rdb_Store *store)
return ConvertorErrorCode::NativeToNdk(rdbStore->GetStore()->Commit());
}
int OH_Rdb_BeginTransWithTrxId(OH_Rdb_Store *store, int64_t *trxId)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr || trxId == nullptr) {
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
std::pair<int, int64_t> res = rdbStore->GetStore()->BeginTrans();
*trxId = res.second;
return ConvertorErrorCode::NativeToNdk(res.first);
}
int OH_Rdb_RollBackByTrxId(OH_Rdb_Store *store, int64_t trxId)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr) {
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
return ConvertorErrorCode::NativeToNdk(rdbStore->GetStore()->RollBack(trxId));
}
int OH_Rdb_CommitByTrxId(OH_Rdb_Store *store, int64_t trxId)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr) {
return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
}
return ConvertorErrorCode::NativeToNdk(rdbStore->GetStore()->Commit(trxId));
}
int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath)
{
auto rdbStore = GetRelationalStore(store);
@ -393,7 +649,7 @@ static std::pair<int32_t, Rdb_DistributedConfig> Convert(const Rdb_DistributedCo
}
int OH_Rdb_SetDistributedTables(OH_Rdb_Store *store, const char *tables[], uint32_t count, Rdb_DistributedType type,
const Rdb_DistributedConfig *config)
const Rdb_DistributedConfig *config)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr || type != Rdb_DistributedType::RDB_DISTRIBUTED_CLOUD || (count > 0 && tables == nullptr)) {
@ -412,8 +668,8 @@ int OH_Rdb_SetDistributedTables(OH_Rdb_Store *store, const char *tables[], uint3
}
tableNames.emplace_back(tables[i]);
}
return ConvertorErrorCode::NativeToNdk(rdbStore->GetStore()->SetDistributedTables(tableNames,
DistributedTableType::DISTRIBUTED_CLOUD, { cfg.isAutoSync }));
return ConvertorErrorCode::NativeToNdk(rdbStore->GetStore()->SetDistributedTables(
tableNames, DistributedTableType::DISTRIBUTED_CLOUD, { cfg.isAutoSync }));
}
OH_Cursor *OH_Rdb_FindModifyTime(OH_Rdb_Store *store, const char *tableName, const char *columnName, OH_VObject *values)
@ -610,8 +866,8 @@ Rdb_TableDetails *OH_Rdb_GetTableDetails(Rdb_ProgressDetails *progress, int32_t
return details->GetTableDetails(version);
}
int OH_Rdb_CloudSync(OH_Rdb_Store *store, Rdb_SyncMode mode, const char *tables[], uint32_t count,
const Rdb_ProgressObserver *observer)
int OH_Rdb_CloudSync(
OH_Rdb_Store *store, Rdb_SyncMode mode, const char *tables[], uint32_t count, const Rdb_ProgressObserver *observer)
{
auto rdbStore = GetRelationalStore(store);
if (rdbStore == nullptr || mode < RDB_SYNC_MODE_TIME_FIRST || mode > RDB_SYNC_MODE_CLOUD_FIRST ||
@ -713,7 +969,7 @@ OH_Cursor *OH_Rdb_QueryLockedRow(
return new OHOS::RdbNdk::RelationalCursor(std::move(resultSet));
}
NDKDetailProgressObserver::NDKDetailProgressObserver(const Rdb_ProgressObserver *callback):callback_(callback)
NDKDetailProgressObserver::NDKDetailProgressObserver(const Rdb_ProgressObserver *callback) : callback_(callback)
{
}
@ -732,7 +988,9 @@ bool NDKDetailProgressObserver::operator==(const Rdb_ProgressObserver *callback)
return callback == callback_;
}
NDKStoreObserver::NDKStoreObserver(const Rdb_DataObserver *observer, int mode) : mode_(mode), observer_(observer) {}
NDKStoreObserver::NDKStoreObserver(const Rdb_DataObserver *observer, int mode) : mode_(mode), observer_(observer)
{
}
void NDKStoreObserver::OnChange(const std::vector<std::string> &devices)
{
@ -771,8 +1029,8 @@ int32_t NDKStoreObserver::GetKeyDataType(std::vector<RdbStoreObserver::PrimaryKe
return OH_ColumnType::TYPE_NULL;
}
void NDKStoreObserver::OnChange(const Origin &origin, const RdbStoreObserver::PrimaryFields &fields,
RdbStoreObserver::ChangeInfo &&changeInfo)
void NDKStoreObserver::OnChange(
const Origin &origin, const RdbStoreObserver::PrimaryFields &fields, RdbStoreObserver::ChangeInfo &&changeInfo)
{
uint32_t count = changeInfo.size();
if (count == 0) {
@ -783,7 +1041,7 @@ void NDKStoreObserver::OnChange(const Origin &origin, const RdbStoreObserver::Pr
if (mode_ == Rdb_SubscribeType::RDB_SUBSCRIBE_TYPE_CLOUD_DETAILS ||
mode_ == Rdb_SubscribeType::RDB_SUBSCRIBE_TYPE_LOCAL_DETAILS) {
size_t size = count * (sizeof(Rdb_ChangeInfo *) + sizeof(Rdb_ChangeInfo)) +
GetKeyInfoSize(std::forward<RdbStoreObserver::ChangeInfo &&>(changeInfo));
GetKeyInfoSize(std::forward<RdbStoreObserver::ChangeInfo &&>(changeInfo));
std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
Rdb_ChangeInfo **infos = (Rdb_ChangeInfo **)(buffer.get());
if (infos == nullptr) {
@ -808,15 +1066,15 @@ void NDKStoreObserver::OnChange(const Origin &origin, const RdbStoreObserver::Pr
infos[index]->deleted.type = GetKeyDataType(it->second[RdbStoreObserver::CHG_TYPE_DELETE]);
ConvertKeyInfoData(data, it->second[RdbStoreObserver::CHG_TYPE_INSERT]);
infos[index]->inserted.data = data;
ConvertKeyInfoData(data+infos[index]->inserted.count, it->second[RdbStoreObserver::CHG_TYPE_UPDATE]);
infos[index]->updated.data = data+infos[index]->inserted.count;
ConvertKeyInfoData(data+infos[index]->inserted.count+infos[index]->updated.count,
ConvertKeyInfoData(data + infos[index]->inserted.count, it->second[RdbStoreObserver::CHG_TYPE_UPDATE]);
infos[index]->updated.data = data + infos[index]->inserted.count;
ConvertKeyInfoData(data + infos[index]->inserted.count + infos[index]->updated.count,
it->second[RdbStoreObserver::CHG_TYPE_DELETE]);
infos[index]->deleted.data = data+infos[index]->inserted.count+infos[index]->updated.count;
infos[index]->deleted.data = data + infos[index]->inserted.count + infos[index]->updated.count;
index++;
}
(*observer_->callback.detailsObserver)(observer_->context, const_cast<const Rdb_ChangeInfo**>(infos), count);
(*observer_->callback.detailsObserver)(observer_->context, const_cast<const Rdb_ChangeInfo **>(infos), count);
}
}
@ -825,8 +1083,8 @@ void NDKStoreObserver::OnChange()
RdbStoreObserver::OnChange();
}
void NDKStoreObserver::ConvertKeyInfoData(Rdb_KeyInfo::Rdb_KeyData *keyInfoData,
std::vector<RdbStoreObserver::PrimaryKey> &primaryKey)
void NDKStoreObserver::ConvertKeyInfoData(
Rdb_KeyInfo::Rdb_KeyData *keyInfoData, std::vector<RdbStoreObserver::PrimaryKey> &primaryKey)
{
if (keyInfoData == nullptr || primaryKey.empty()) {
LOG_WARN("no data, keyInfoData is nullptr:%{public}d", keyInfoData == nullptr);