• 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 "AniResultSet"
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 
27 namespace OHOS {
28 namespace RelationalStoreAniKit {
29 
30 using namespace OHOS::NativeRdb;
31 using namespace OHOS::Rdb;
32 
GetColumnIndex(ani_env * env,ani_object object,ani_string col_name)33 ani_double GetColumnIndex(ani_env *env, ani_object object, ani_string col_name)
34 {
35     if (env == nullptr) {
36         LOG_ERROR("env is nullptr.");
37         return 0;
38     }
39     auto proxy = AniObjectUtils::Unwrap<ResultSetProxy>(env, object);
40     if (proxy == nullptr || proxy->resultset == nullptr) {
41         LOG_ERROR("ResultSet should be initialized properly.");
42         ThrowBusinessError(env, E_INNER_ERROR, "ResultSet uninitialized.");
43         return 0;
44     }
45     auto name = AniStringUtils::ToStd(env, col_name);
46     int index;
47     auto status = proxy->resultset->GetColumnIndex(name, index);
48     ThrowBusinessError(env, status);
49     return index;
50 }
51 
GetLong(ani_env * env,ani_object object,ani_double index)52 ani_double GetLong(ani_env *env, ani_object object, ani_double index)
53 {
54     if (env == nullptr) {
55         LOG_ERROR("env is nullptr.");
56         return 0;
57     }
58     auto proxy = AniObjectUtils::Unwrap<ResultSetProxy>(env, object);
59     if (proxy == nullptr || proxy->resultset == nullptr) {
60         LOG_ERROR("ResultSet should be initialized properly.");
61         ThrowBusinessError(env, E_INNER_ERROR, "ResultSet uninitialized.");
62         return 0;
63     }
64     int64_t ret;
65     auto status = proxy->resultset->GetLong(index, ret);
66     ThrowBusinessError(env, status);
67     return ret;
68 }
69 
GetString(ani_env * env,ani_object object,ani_double index)70 ani_string GetString(ani_env *env, ani_object object, ani_double index)
71 {
72     if (env == nullptr) {
73         LOG_ERROR("env is nullptr.");
74         return nullptr;
75     }
76     auto proxy = AniObjectUtils::Unwrap<ResultSetProxy>(env, object);
77     if (proxy == nullptr || proxy->resultset == nullptr) {
78         LOG_ERROR("ResultSet should be initialized properly.");
79         ThrowBusinessError(env, E_INNER_ERROR, "ResultSet uninitialized.");
80         return nullptr;
81     }
82     std::string ret;
83     auto status = proxy->resultset->GetString(index, ret);
84     ThrowBusinessError(env, status);
85     ani_string retStr;
86     env->String_NewUTF8(ret.c_str(), ret.size(), &retStr);
87     return retStr;
88 }
89 
GoToFirstRow(ani_env * env,ani_object object)90 ani_boolean GoToFirstRow(ani_env *env, ani_object object)
91 {
92     if (env == nullptr) {
93         LOG_ERROR("env is nullptr.");
94         return false;
95     }
96     auto proxy = AniObjectUtils::Unwrap<ResultSetProxy>(env, object);
97     if (proxy == nullptr || proxy->resultset == nullptr) {
98         LOG_ERROR("ResultSet should be initialized properly.");
99         ThrowBusinessError(env, E_INNER_ERROR, "ResultSet uninitialized.");
100         return false;
101     }
102     auto status = proxy->resultset->GoToFirstRow();
103     return (status == ANI_OK);
104 }
105 
ResultSetInit(ani_env * env)106 ani_status ResultSetInit(ani_env *env)
107 {
108     if (env == nullptr) {
109         LOG_ERROR("env is nullptr.");
110         return ANI_ERROR;
111     }
112     static const char *namespaceName = "L@ohos/data/relationalStore/relationalStore;";
113     ani_namespace ns;
114     if (ANI_OK != env->FindNamespace(namespaceName, &ns)) {
115         LOG_ERROR("Not found '%{public}s", namespaceName);
116         return ANI_ERROR;
117     }
118 
119     ani_class cls;
120     const char *className = "LResultSetInner;";
121     if (ANI_OK != env->Namespace_FindClass(ns, className, &cls)) {
122         LOG_ERROR("Not found '%{public}s", className);
123         return ANI_ERROR;
124     }
125 
126     std::array methods = {
127         ani_native_function {"getColumnIndex", nullptr, reinterpret_cast<void *>(GetColumnIndex)},
128         ani_native_function {"getLong", nullptr, reinterpret_cast<void *>(GetLong)},
129         ani_native_function {"getString", nullptr, reinterpret_cast<void *>(GetString)},
130         ani_native_function {"goToFirstRow", nullptr, reinterpret_cast<void *>(GoToFirstRow)},
131     };
132     if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
133         LOG_ERROR("Cannot bind native methods to '%{public}s", className);
134         return ANI_ERROR;
135     }
136     return ANI_OK;
137 }
138 
139 } // namespace RelationalStoreAniKit
140 } // namespace OHOS
141 
142