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