• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "RdbResultSetBridge"
17 
18 #include "rdb_result_set_bridge.h"
19 
20 #include "rdb_errno.h"
21 #include "rdb_logger.h"
22 #include "result_set.h"
23 #include "securec.h"
24 
25 namespace OHOS {
26 namespace RdbDataShareAdapter {
27 using namespace OHOS::NativeRdb;
RdbResultSetBridge(std::shared_ptr<ResultSet> resultSet)28 RdbResultSetBridge::RdbResultSetBridge(std::shared_ptr<ResultSet> resultSet) : rdbResultSet_(resultSet)
29 {
30 }
31 
~RdbResultSetBridge()32 RdbResultSetBridge::~RdbResultSetBridge()
33 {
34     rdbResultSet_->Close();
35 }
36 
GetRowCount(int & count)37 int RdbResultSetBridge::GetRowCount(int &count)
38 {
39     return rdbResultSet_->GetRowCount(count);
40 }
41 
GetAllColumnNames(std::vector<std::string> & columnOrKeyNames)42 int RdbResultSetBridge::GetAllColumnNames(std::vector<std::string> &columnOrKeyNames)
43 {
44     return rdbResultSet_->GetAllColumnNames(columnOrKeyNames);
45 }
46 
OnGo(int32_t start,int32_t target,Writer & writer)47 int RdbResultSetBridge::OnGo(int32_t start, int32_t target, Writer &writer)
48 {
49     int rowCount;
50     rdbResultSet_->GetRowCount(rowCount);
51     if (start < 0 || target < 0 || target >= rowCount) {
52         LOG_ERROR("Invalid targetRowIndex: %{public}d.", rowCount);
53         return -1;
54     }
55 
56     int columnCount;
57     rdbResultSet_->GetColumnCount(columnCount);
58     if (columnCount <= 0) {
59         LOG_ERROR("Invalid columnCount: %{public}d.", columnCount);
60         return -1;
61     }
62     LOG_DEBUG("rowCount: %{public}d, columnCount: %{public}d.", rowCount, columnCount);
63 
64     bool bResultSet = false;
65     rdbResultSet_->IsStarted(bResultSet);
66     if (!bResultSet) {
67         rdbResultSet_->GoToFirstRow();
68     }
69 
70     int errCode = rdbResultSet_->GoToRow(start);
71     if (errCode) {
72         LOG_ERROR("Go to row %{public}d failed.", start);
73         return -1;
74     }
75 
76     return WriteBlock(start, target, columnCount, writer);
77 }
78 
WriteBlock(int32_t start,int32_t target,int columnCount,Writer & writer)79 int32_t RdbResultSetBridge::WriteBlock(int32_t start, int32_t target, int columnCount, Writer &writer)
80 {
81     bool isFull = false;
82     int errCode = 0;
83     int row = start;
84 
85     while (!isFull && !errCode && row <= target) {
86         int status = writer.AllocRow();
87         if (status != 0) {
88             isFull = true;
89             LOG_ERROR("SharedBlock is full.");
90             return row - 1;
91         }
92 
93         WriteColumn(columnCount, writer, row);
94         row++;
95         errCode = rdbResultSet_->GoToNextRow();
96     }
97     return target;
98 }
99 
WriteColumn(int columnCount,Writer & writer,int row)100 void RdbResultSetBridge::WriteColumn(int columnCount, Writer &writer, int row)
101 {
102     for (int i = 0; i < columnCount; i++) {
103         ColumnType type;
104         rdbResultSet_->GetColumnType(i, type);
105         switch (type) {
106             case ColumnType::TYPE_INTEGER:
107                 int64_t value;
108                 rdbResultSet_->GetLong(i, value);
109                 if (writer.Write(i, value)) {
110                     LOG_DEBUG("WriteLong failed of row: %{public}d, column: %{public}d", row, i);
111                 }
112                 break;
113             case ColumnType::TYPE_FLOAT:
114                 double dValue;
115                 rdbResultSet_->GetDouble(i, dValue);
116                 if (writer.Write(i, dValue)) {
117                     LOG_DEBUG("WriteDouble failed of row: %{public}d, column: %{public}d", row, i);
118                 }
119                 break;
120             case ColumnType::TYPE_NULL:
121                 if (writer.Write(i)) {
122                     LOG_DEBUG("WriteNull failed of row: row: %{public}d, column: %{public}d", row, i);
123                 }
124                 break;
125             case ColumnType::TYPE_BLOB:
126                 if (WriteBlobData(i, writer)) {
127                     LOG_DEBUG("WriteBlob failed of row: %{public}d, column: %{public}d", row, i);
128                 }
129                 break;
130             default:
131                 std::string stringValue;
132                 rdbResultSet_->GetString(i, stringValue);
133                 if (writer.Write(i, stringValue.c_str(), stringValue.size() + 1)) {
134                     LOG_DEBUG("WriteString failed of row: %{public}d, column: %{public}d", row, i);
135                 }
136         }
137     }
138 }
139 
WriteBlobData(int column,Writer & writer)140 bool RdbResultSetBridge::WriteBlobData(int column, Writer &writer)
141 {
142     std::vector<uint8_t> blobValue;
143     rdbResultSet_->GetBlob(column, blobValue);
144     if (blobValue.empty()) {
145         return false;
146     }
147 
148     return writer.Write(column, &blobValue[0], blobValue.size() * sizeof(uint8_t));
149 }
150 } // namespace RdbDataShareAdapter
151 } // namespace OHOS
152