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     return GoToRow(0);
100 }
101 
GoToLastRow()102 int DataShareAbsResultSet::GoToLastRow()
103 {
104     int rowCnt = 0;
105     int ret = GetRowCount(rowCnt);
106     if (ret != E_OK) {
107         LOG_WARN("return GoToLastRow.GetRowCount ret is wrong!");
108         return ret;
109     }
110 
111     ret = GoToRow(rowCnt - 1);
112     if (ret != E_OK) {
113         LOG_WARN("return GoToLastRow.GoToRow ret is wrong!");
114     }
115     return ret;
116 }
117 
GoToNextRow()118 int DataShareAbsResultSet::GoToNextRow()
119 {
120     return GoToRow(rowPos_ + 1);
121 }
122 
GoToPreviousRow()123 int DataShareAbsResultSet::GoToPreviousRow()
124 {
125     return GoToRow(rowPos_ - 1);
126 }
127 
IsAtFirstRow(bool & result) const128 int DataShareAbsResultSet::IsAtFirstRow(bool &result) const
129 {
130     result = (rowPos_ == 0);
131     return E_OK;
132 }
133 
IsAtLastRow(bool & result)134 int DataShareAbsResultSet::IsAtLastRow(bool &result)
135 {
136     int rowCnt = 0;
137     int ret = GetRowCount(rowCnt);
138     if (ret != E_OK) {
139         LOG_ERROR("return GetRowCount ret is wrong!");
140         return ret;
141     }
142     result = (rowPos_ == (rowCnt - 1));
143     return E_OK;
144 }
145 
IsStarted(bool & result) const146 int DataShareAbsResultSet::IsStarted(bool &result) const
147 {
148     result = (rowPos_ != INIT_POS);
149     return E_OK;
150 }
151 
IsEnded(bool & result)152 int DataShareAbsResultSet::IsEnded(bool &result)
153 {
154     int rowCnt = 0;
155     int ret =  GetRowCount(rowCnt);
156     if (ret != E_OK) {
157         LOG_ERROR("return GetRowCount ret is wrong!");
158         return ret;
159     }
160     result = (rowCnt == 0) ? true : (rowPos_ == rowCnt);
161     return E_OK;
162 }
163 
GetColumnCount(int & count)164 int DataShareAbsResultSet::GetColumnCount(int &count)
165 {
166     if (count_ == -1) {
167         std::vector<std::string> columnNames;
168         int ret = GetAllColumnNames(columnNames);
169         if (ret != E_OK) {
170             LOG_ERROR("return GetAllColumnNames ret is wrong!");
171             return ret;
172         }
173         count_ = static_cast<int>(columnNames.size());
174     }
175     count = count_;
176     return E_OK;
177 }
178 
GetColumnIndex(const std::string & columnName,int & columnIndex)179 int DataShareAbsResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex)
180 {
181     if (indexCache_.find(columnName) != indexCache_.end()) {
182         columnIndex = indexCache_[columnName];
183         return E_OK;
184     }
185     auto periodIndex = columnName.rfind('.');
186     std::string columnNameLower = columnName;
187     if (periodIndex != std::string::npos) {
188         columnNameLower = columnNameLower.substr(periodIndex + 1);
189     }
190     transform(columnNameLower.begin(), columnNameLower.end(), columnNameLower.begin(), ::tolower);
191     std::vector<std::string> columnNames;
192     int ret = GetAllColumnNames(columnNames);
193     if (ret != E_OK) {
194         LOG_ERROR("return GetAllColumnNames ret is wrong!");
195         return ret;
196     }
197 
198     columnIndex = 0;
199     for (const auto& name : columnNames) {
200         std::string lowerName = name;
201         transform(name.begin(), name.end(), lowerName.begin(), ::tolower);
202         if (lowerName == columnNameLower) {
203             indexCache_[columnName] = columnIndex;
204             return E_OK;
205         }
206         columnIndex++;
207     }
208     columnIndex = -1;
209     return E_ERROR;
210 }
211 
GetColumnName(int columnIndex,std::string & columnName)212 int DataShareAbsResultSet::GetColumnName(int columnIndex, std::string &columnName)
213 {
214     int rowCnt = 0;
215     int ret = GetColumnCount(rowCnt);
216     if (ret != E_OK) {
217         LOG_ERROR("return GetColumnCount ret is wrong!");
218         return ret;
219     }
220     if (columnIndex >= rowCnt || columnIndex < 0) {
221         return E_INVALID_COLUMN_INDEX;
222     }
223     std::vector<std::string> columnNames;
224     GetAllColumnNames(columnNames);
225     columnName = columnNames[columnIndex];
226     return E_OK;
227 }
228 
IsClosed() const229 bool DataShareAbsResultSet::IsClosed() const
230 {
231     return isClosed_;
232 }
233 
Close()234 int DataShareAbsResultSet::Close()
235 {
236     isClosed_ = true;
237     return E_OK;
238 }
239 } // namespace DataShare
240 } // namespace OHOS