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 "AniRdbPredicates"
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_rdb_predicates.h"
25 #include "ani_rdb_error.h"
26
27 namespace OHOS {
28 namespace RelationalStoreAniKit {
29
30 using namespace OHOS::NativeRdb;
31 using namespace OHOS::Rdb;
32
InitNativePredicates(ani_env * env,ani_object object,ani_string tableName)33 void InitNativePredicates(ani_env *env, ani_object object, ani_string tableName)
34 {
35 if (env == nullptr) {
36 LOG_ERROR("env is nullptr.");
37 return;
38 }
39 auto proxy = new PredicatesProxy();
40 if (proxy == nullptr) {
41 LOG_ERROR("proxy is nullptr.");
42 return;
43 }
44 auto tname = AniStringUtils::ToStd(env, tableName);
45 proxy->predicates = std::make_shared<RdbPredicates>(tname);
46
47 ani_status status = env->Object_SetFieldByName_Long(object, "nativePtr", reinterpret_cast<ani_long>(proxy));
48 if (ANI_OK != status) {
49 delete proxy;
50 LOG_ERROR("[ANI] Failed to set nativePtr to predicates object.");
51 }
52 }
PredicatesInit(ani_env * env)53 ani_status PredicatesInit(ani_env *env)
54 {
55 if (env == nullptr) {
56 LOG_ERROR("env is nullptr.");
57 return ANI_ERROR;
58 }
59
60 static const char *namespaceName = "L@ohos/data/relationalStore/relationalStore;";
61 ani_namespace ns;
62 if (ANI_OK != env->FindNamespace(namespaceName, &ns)) {
63 LOG_ERROR("Not found '%{public}s'", namespaceName);
64 return ANI_ERROR;
65 }
66
67 ani_class cls;
68 const char *className = "LRdbPredicates;";
69 if (ANI_OK != env->Namespace_FindClass(ns, className, &cls)) {
70 LOG_ERROR("Not found '%{public}s'", className);
71 return ANI_ERROR;
72 }
73
74 std::array methods = {
75 ani_native_function {"initNativePredicates", nullptr, reinterpret_cast<void *>(InitNativePredicates)},
76 };
77 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
78 LOG_ERROR("Cannot bind native methods to '%{public}s'", className);
79 return ANI_ERROR;
80 }
81 return ANI_OK;
82 }
83
84 } // namespace RelationalStoreAniKit
85 } // namespace OHOS
86
87