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 "AniRdbStoreHelper"
17 #include <ani.h>
18 #include <iostream>
19 #include <string>
20
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "ani_utils.h"
24 #include "ani_result_set.h"
25 #include "ani_rdb_error.h"
26 #include "ani_rdb_store_helper.h"
27 #include "rdb_sql_utils.h"
28
29 namespace OHOS {
30 namespace RelationalStoreAniKit {
31
32 using namespace OHOS::NativeRdb;
33 using namespace OHOS::Rdb;
34
35 class DefaultOpenCallback : public RdbOpenCallback {
36 public:
OnCreate(RdbStore & rdbStore)37 int OnCreate(RdbStore &rdbStore) override
38 {
39 return E_OK;
40 }
OnUpgrade(RdbStore & rdbStore,int oldVersion,int newVersion)41 int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override
42 {
43 return E_OK;
44 }
45 };
46
GetRdbStoreSync(ani_env * env,ani_object context,ani_object config)47 static ani_object GetRdbStoreSync([[maybe_unused]] ani_env *env, ani_object context, ani_object config)
48 {
49 DefaultOpenCallback callback;
50 if (env == nullptr) {
51 LOG_ERROR("env is nullptr.");
52 return nullptr;
53 }
54 int errCode = OK;
55 auto proxy = new RdbStoreProxy();
56 if (proxy == nullptr) {
57 LOG_ERROR("new RdbStoreProxy failed.");
58 return nullptr;
59 }
60 int errorCode = RdbSqlUtils::CreateDirectory("/data/storage/el2/database/rdb");
61 if (errorCode != E_OK) {
62 delete proxy;
63 ThrowBusinessError(env, E_INNER_ERROR, "CreateDirectory failed.");
64 return nullptr;
65 }
66 auto rdbConfig = RdbStoreConfig("/data/storage/el2/database/rdb/test.db");
67 proxy->nativeRdb = RdbHelper::GetRdbStore(rdbConfig, -1, callback, errCode);
68 if (proxy->nativeRdb == nullptr) {
69 delete proxy;
70 ThrowBusinessError(env, E_INNER_ERROR, "RdbHelper returned nullptr.");
71 return nullptr;
72 }
73 static const char *namespaceName = "L@ohos/data/relationalStore/relationalStore;";
74 static const char *className = "LRdbStoreInner;";
75 static const char *initFinalizer = "initFinalizer";
76 ani_object obj = AniObjectUtils::Create(env, namespaceName, className);
77 if (nullptr == obj) {
78 delete proxy;
79 LOG_ERROR("[ANI] Failed to create class '%{public}s' obj", className);
80 ThrowBusinessError(env, E_INNER_ERROR, "ANI create class.");
81 return nullptr;
82 }
83 ani_status status = AniObjectUtils::Wrap(env, obj, proxy);
84 if (ANI_OK != status) {
85 delete proxy;
86 LOG_ERROR("[ANI] Failed to wrap for class '%{public}s'", className);
87 ThrowBusinessError(env, E_INNER_ERROR, "ANI SetField.");
88 return nullptr;
89 }
90 status = AniObjectUtils::CallObjMethod(env, namespaceName, className, initFinalizer, obj);
91 if (ANI_OK != status) {
92 LOG_ERROR("[ANI] Failed to initFinalizer for class '%{public}s'.", className);
93 ThrowBusinessError(env, E_INNER_ERROR, "init rdbStore finalizer failed.");
94 return nullptr;
95 }
96 return obj;
97 }
98
RdbStoreHelperInit(ani_env * env)99 ani_status RdbStoreHelperInit(ani_env *env)
100 {
101 if (env == nullptr) {
102 LOG_ERROR("env is nullptr.");
103 return ANI_ERROR;
104 }
105
106 static const char *namespaceName = "L@ohos/data/relationalStore/relationalStore;";
107 ani_namespace ns;
108 if (ANI_OK != env->FindNamespace(namespaceName, &ns)) {
109 LOG_ERROR("Not found '%{public}s'", namespaceName);
110 return ANI_ERROR;
111 }
112
113 std::array functions = {
114 ani_native_function {"getRdbStoreSync", nullptr, reinterpret_cast<void *>(GetRdbStoreSync)},
115 };
116 if (ANI_OK != env->Namespace_BindNativeFunctions(ns, functions.data(), functions.size())) {
117 LOG_ERROR("Cannot bind native functions to '%{public}s'", namespaceName);
118 return ANI_ERROR;
119 }
120 return ANI_OK;
121 }
122
123 } // namespace RelationalStoreAniKit
124 } // namespace OHOS
125
126