1 /*
2 * Copyright (c) 2023 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 #include <iostream>
17 #include <sstream>
18 #include <string>
19 #include <utility>
20
21 #include "logger.h"
22 #include "oh_cursor.h"
23 #include "relational_cursor.h"
24 #include "relational_store_error_code.h"
25 #include "rdb_errno.h"
26 #include "securec.h"
27
28 namespace OHOS {
29 namespace RdbNdk {
GetColumnCount(OH_Cursor * cursor,int * count)30 int RelationalCursor::GetColumnCount(OH_Cursor *cursor, int *count)
31 {
32 auto self = GetSelf(cursor);
33 if (self == nullptr || count == nullptr) {
34 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
35 }
36 return self->resultSet_->GetColumnCount(*count);
37 }
38
GetColumnType(OH_Cursor * cursor,int32_t columnIndex,OH_ColumnType * columnType)39 int RelationalCursor::GetColumnType(OH_Cursor *cursor, int32_t columnIndex, OH_ColumnType *columnType)
40 {
41 auto self = GetSelf(cursor);
42 if (self == nullptr || columnType == nullptr || columnIndex < 0) {
43 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
44 }
45 OHOS::NativeRdb::ColumnType type;
46 int result = self->resultSet_->GetColumnType(columnIndex, type);
47 *columnType = static_cast<OH_ColumnType>(static_cast<int>(type));
48 return result;
49 }
50
GetColumnIndex(OH_Cursor * cursor,const char * name,int * columnIndex)51 int RelationalCursor::GetColumnIndex(OH_Cursor *cursor, const char *name, int *columnIndex)
52 {
53 auto self = GetSelf(cursor);
54 if (self == nullptr || name == nullptr || columnIndex == nullptr) {
55 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
56 }
57 return self->resultSet_->GetColumnIndex(name, *columnIndex);
58 }
59
GetColumnName(OH_Cursor * cursor,int32_t columnIndex,char * name,int length)60 int RelationalCursor::GetColumnName(OH_Cursor *cursor, int32_t columnIndex, char *name, int length)
61 {
62 auto self = GetSelf(cursor);
63 if (self == nullptr || name == nullptr || length <= 0) {
64 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
65 }
66 std::string str;
67 int errCode = self->resultSet_->GetColumnName(columnIndex, str);
68 if (errCode != OHOS::NativeRdb::E_OK) {
69 return errCode;
70 }
71 errno_t result = memcpy_s(name, length, str.c_str(), str.length());
72 if (result != EOK) {
73 LOG_ERROR("memcpy_s failed, result is %{public}d", result);
74 return OH_Rdb_ErrCode::RDB_ERR;
75 }
76 return OH_Rdb_ErrCode::RDB_OK;
77 }
78
GetRowCount(OH_Cursor * cursor,int * count)79 int RelationalCursor::GetRowCount(OH_Cursor *cursor, int *count)
80 {
81 auto self = GetSelf(cursor);
82 if (self == nullptr || count == nullptr) {
83 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
84 }
85 return self->resultSet_->GetRowCount(*count);
86 }
87
GoToNextRow(OH_Cursor * cursor)88 int RelationalCursor::GoToNextRow(OH_Cursor *cursor)
89 {
90 auto self = GetSelf(cursor);
91 if (self == nullptr) {
92 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
93 }
94 return self->resultSet_->GoToNextRow();
95 }
96
GetSize(OH_Cursor * cursor,int32_t columnIndex,size_t * size)97 int RelationalCursor::GetSize(OH_Cursor *cursor, int32_t columnIndex, size_t *size)
98 {
99 auto self = GetSelf(cursor);
100 if (self == nullptr || size == nullptr) {
101 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
102 }
103 return self->resultSet_->GetSize(columnIndex, *size);
104 }
105
GetText(OH_Cursor * cursor,int32_t columnIndex,char * value,int length)106 int RelationalCursor::GetText(OH_Cursor *cursor, int32_t columnIndex, char *value, int length)
107 {
108 auto self = GetSelf(cursor);
109 if (self == nullptr || value == nullptr || length <= 0) {
110 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
111 }
112 std::string str;
113 int errCode = self->resultSet_->GetString(columnIndex, str);
114 if (errCode != OHOS::NativeRdb::E_OK) {
115 return errCode;
116 }
117 errno_t result = memcpy_s(value, length, str.c_str(), str.length());
118 if (result != EOK) {
119 LOG_ERROR("memcpy_s failed, result is %{public}d", result);
120 return OH_Rdb_ErrCode::RDB_ERR;
121 }
122 return OH_Rdb_ErrCode::RDB_OK;
123 }
124
GetInt64(OH_Cursor * cursor,int32_t columnIndex,int64_t * value)125 int RelationalCursor::GetInt64(OH_Cursor *cursor, int32_t columnIndex, int64_t *value)
126 {
127 auto self = GetSelf(cursor);
128 if (self == nullptr || value == nullptr) {
129 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
130 }
131 return self->resultSet_->GetLong(columnIndex, *value);
132 }
133
GetReal(OH_Cursor * cursor,int32_t columnIndex,double * value)134 int RelationalCursor::GetReal(OH_Cursor *cursor, int32_t columnIndex, double *value)
135 {
136 auto self = GetSelf(cursor);
137 if (self == nullptr || value == nullptr) {
138 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
139 }
140 return self->resultSet_->GetDouble(columnIndex, *value);
141 }
142
GetBlob(OH_Cursor * cursor,int32_t columnIndex,unsigned char * value,int length)143 int RelationalCursor::GetBlob(OH_Cursor *cursor, int32_t columnIndex, unsigned char *value, int length)
144 {
145 auto self = GetSelf(cursor);
146 if (self == nullptr || value == nullptr || length <= 0) {
147 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
148 }
149 std::vector<uint8_t> vec;
150 int errCode = self->resultSet_->GetBlob(columnIndex, vec);
151 if (errCode != OHOS::NativeRdb::E_OK) {
152 return errCode;
153 }
154 errno_t result = memcpy_s(value, length, vec.data(), vec.size());
155 if (result != EOK) {
156 LOG_ERROR("memcpy_s failed, result is %{public}d", result);
157 return OH_Rdb_ErrCode::RDB_ERR;
158 }
159 return OH_Rdb_ErrCode::RDB_OK;
160 }
161
IsNull(OH_Cursor * cursor,int32_t columnIndex,bool * isNull)162 int RelationalCursor::IsNull(OH_Cursor *cursor, int32_t columnIndex, bool *isNull)
163 {
164 auto self = GetSelf(cursor);
165 if (self == nullptr || isNull == nullptr) {
166 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
167 }
168 return self->resultSet_->IsColumnNull(columnIndex, *isNull);
169 }
170
Destroy(OH_Cursor * cursor)171 int RelationalCursor::Destroy(OH_Cursor *cursor)
172 {
173 auto self = GetSelf(cursor);
174 if (self == nullptr) {
175 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
176 }
177 int errCode = self->resultSet_->Close();
178 if (errCode != OHOS::NativeRdb::E_OK) {
179 return errCode;
180 }
181 delete self;
182 return errCode;
183 }
184
RelationalCursor(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet)185 RelationalCursor::RelationalCursor(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet)
186 : resultSet_(std::move(resultSet))
187 {
188 id = RDB_CURSOR_CID;
189
190 getColumnCount = GetColumnCount;
191 getColumnType = GetColumnType;
192 getColumnIndex = GetColumnIndex;
193 getColumnName = GetColumnName;
194 getRowCount = GetRowCount;
195 goToNextRow = GoToNextRow;
196 getSize = GetSize;
197 getText = GetText;
198 getInt64 = GetInt64;
199 getReal = GetReal;
200 getBlob = GetBlob;
201 isNull = IsNull;
202 destroy = Destroy;
203 }
204
GetSelf(OH_Cursor * cursor)205 RelationalCursor *RelationalCursor::GetSelf(OH_Cursor *cursor)
206 {
207 if (cursor == nullptr || cursor->id != OHOS::RdbNdk::RDB_CURSOR_CID) {
208 LOG_ERROR("cursor invalid. is null %{public}d", (cursor == nullptr));
209 return nullptr;
210 }
211 return static_cast<OHOS::RdbNdk::RelationalCursor *>(cursor);
212 }
213 } // namespace RdbNdk
214 } // namespace OHOS