1 /*
2 * Copyright (c) 2021 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 "abs_result_set.h"
17
18 #include <algorithm>
19 #include <string>
20
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "rdb_trace.h"
24
25 namespace OHOS {
26 namespace NativeRdb {
AbsResultSet()27 AbsResultSet::AbsResultSet() : rowPos_(INIT_POS), isClosed(false)
28 {
29 }
30
~AbsResultSet()31 AbsResultSet::~AbsResultSet() {}
32
GetRowCount(int & count)33 int AbsResultSet::GetRowCount(int &count)
34 {
35 return E_OK;
36 }
37
GetAllColumnNames(std::vector<std::string> & columnNames)38 int AbsResultSet::GetAllColumnNames(std::vector<std::string> &columnNames)
39 {
40 return E_OK;
41 }
42
GetBlob(int columnIndex,std::vector<uint8_t> & blob)43 int AbsResultSet::GetBlob(int columnIndex, std::vector<uint8_t> &blob)
44 {
45 return E_OK;
46 }
47
GetString(int columnIndex,std::string & value)48 int AbsResultSet::GetString(int columnIndex, std::string &value)
49 {
50 return E_OK;
51 }
52
GetInt(int columnIndex,int & value)53 int AbsResultSet::GetInt(int columnIndex, int &value)
54 {
55 return E_OK;
56 }
57
GetLong(int columnIndex,int64_t & value)58 int AbsResultSet::GetLong(int columnIndex, int64_t &value)
59 {
60 return E_OK;
61 }
62
GetDouble(int columnIndex,double & value)63 int AbsResultSet::GetDouble(int columnIndex, double &value)
64 {
65 return E_OK;
66 }
67
IsColumnNull(int columnIndex,bool & isNull)68 int AbsResultSet::IsColumnNull(int columnIndex, bool &isNull)
69 {
70 return E_OK;
71 }
72
GoToRow(int position)73 int AbsResultSet::GoToRow(int position)
74 {
75 return E_OK;
76 }
77
GetColumnType(int columnIndex,ColumnType & columnType)78 int AbsResultSet::GetColumnType(int columnIndex, ColumnType &columnType)
79 {
80 return E_OK;
81 }
82
GetRowIndex(int & position) const83 int AbsResultSet::GetRowIndex(int &position) const
84 {
85 position = rowPos_;
86 return E_OK;
87 }
88
GoTo(int offset)89 int AbsResultSet::GoTo(int offset)
90 {
91 int ret = GoToRow(rowPos_ + offset);
92 if (ret != E_OK) {
93 LOG_WARN("AbsResultSet::GoTo return ret is wrong!");
94 return ret;
95 }
96 return E_OK;
97 }
98
GoToFirstRow()99 int AbsResultSet::GoToFirstRow()
100 {
101 int ret = GoToRow(0);
102 if (ret != E_OK) {
103 LOG_WARN("AbsResultSet::GoToFirstRow return ret is wrong!");
104 return ret;
105 }
106 return E_OK;
107 }
108
GoToLastRow()109 int AbsResultSet::GoToLastRow()
110 {
111 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
112 int rowCnt = 0;
113 int ret = GetRowCount(rowCnt);
114 if (ret != E_OK) {
115 LOG_WARN("AbsResultSet::GoToLastRow return GetRowCount::ret is wrong!");
116 return ret;
117 }
118
119 ret = GoToRow(rowCnt - 1);
120 if (ret != E_OK) {
121 LOG_WARN("AbsResultSet::GoToLastRow return GoToRow::ret is wrong!");
122 return ret;
123 }
124 return E_OK;
125 }
126
GoToNextRow()127 int AbsResultSet::GoToNextRow()
128 {
129 int ret = GoToRow(rowPos_ + 1);
130 if (ret != E_OK) {
131 LOG_WARN("AbsResultSet::GoToNextRow return GoToRow::ret is wrong!");
132 return ret;
133 }
134 return E_OK;
135 }
136
GoToPreviousRow()137 int AbsResultSet::GoToPreviousRow()
138 {
139 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
140 int ret = GoToRow(rowPos_ - 1);
141 if (ret != E_OK) {
142 LOG_WARN("AbsResultSet::GoToPreviousRow return GoToRow::ret is wrong!");
143 return ret;
144 }
145 return E_OK;
146 }
147
IsAtFirstRow(bool & result) const148 int AbsResultSet::IsAtFirstRow(bool &result) const
149 {
150 result = (rowPos_ == 0);
151 return E_OK;
152 }
153
IsAtLastRow(bool & result)154 int AbsResultSet::IsAtLastRow(bool &result)
155 {
156 int rowCnt = 0;
157 int ret = GetRowCount(rowCnt);
158 if (ret != E_OK) {
159 LOG_ERROR("AbsResultSet::IsAtLastRow return GetRowCount::ret is wrong!");
160 return ret;
161 }
162 result = (rowPos_ == (rowCnt - 1));
163 return E_OK;
164 }
165
IsStarted(bool & result) const166 int AbsResultSet::IsStarted(bool &result) const
167 {
168 result = (rowPos_ != INIT_POS);
169 return E_OK;
170 }
171
IsEnded(bool & result)172 int AbsResultSet::IsEnded(bool &result)
173 {
174 int rowCnt = 0;
175 int ret = GetRowCount(rowCnt);
176 if (ret != E_OK) {
177 LOG_ERROR("AbsResultSet::IsEnded return GetRowCount::ret is wrong!");
178 return ret;
179 }
180 result = (rowCnt == 0) ? true : (rowPos_ == rowCnt);
181 return E_OK;
182 }
183
GetColumnCount(int & count)184 int AbsResultSet::GetColumnCount(int &count)
185 {
186 if (!columnNames_.empty()){
187 count = static_cast<int>(columnNames_.size());
188 return E_OK;
189 }
190
191 std::vector<std::string> columnNames;
192 int ret = GetAllColumnNames(columnNames);
193 if (ret != E_OK) {
194 LOG_ERROR("AbsResultSet::GetColumnCount return GetAllColumnNames::ret is wrong!");
195 return ret;
196 }
197 count = static_cast<int>(columnNames.size());
198 return E_OK;
199 }
200
GetColumnIndex(const std::string & columnName,int & columnIndex)201 int AbsResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex)
202 {
203 auto it = columnMap_.find(columnName);
204 if (it != columnMap_.end()) {
205 columnIndex = it->second;
206 return E_OK;
207 }
208
209 auto periodIndex = columnName.rfind('.');
210 std::string columnNameLower = columnName;
211 if (periodIndex != std::string::npos) {
212 columnNameLower = columnNameLower.substr(periodIndex + 1);
213 }
214 transform(columnNameLower.begin(), columnNameLower.end(), columnNameLower.begin(), ::tolower);
215 std::vector<std::string> columnNames;
216 int ret = GetAllColumnNames(columnNames);
217 if (ret != E_OK) {
218 LOG_ERROR("AbsResultSet::GetColumnIndex return GetAllColumnNames::ret is wrong!");
219 return ret;
220 }
221
222 columnIndex = 0;
223 for (auto name : columnNames) {
224 transform(name.begin(), name.end(), name.begin(), ::tolower);
225 if (name == columnNameLower) {
226 columnMap_.insert(std::make_pair(name, columnIndex));
227 return E_OK;
228 }
229 columnIndex++;
230 }
231 columnIndex = -1;
232 return E_ERROR;
233 }
234
GetColumnName(int columnIndex,std::string & columnName)235 int AbsResultSet::GetColumnName(int columnIndex, std::string &columnName)
236 {
237 int rowCnt = 0;
238 int ret = GetColumnCount(rowCnt);
239 if (ret != E_OK) {
240 LOG_ERROR("AbsResultSet::GetColumnName return GetColumnCount::ret is wrong!");
241 return ret;
242 }
243 if (columnIndex >= rowCnt || columnIndex < 0) {
244 return E_INVALID_COLUMN_INDEX;
245 }
246 std::vector<std::string> columnNames;
247 GetAllColumnNames(columnNames);
248 columnName = columnNames[columnIndex];
249 return E_OK;
250 }
251
IsClosed() const252 bool AbsResultSet::IsClosed() const
253 {
254 return isClosed;
255 }
256
Close()257 int AbsResultSet::Close()
258 {
259 isClosed = true;
260 return E_OK;
261 }
262 } // namespace NativeRdb
263 } // namespace OHOS