• 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 #ifndef DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
17 #define DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
18 
19 #include <atomic>
20 #include <shared_mutex>
21 
22 #include "result_set.h"
23 #include "rdb_errno.h"
24 #include "store/cursor.h"
25 #include "value_proxy.h"
26 #include "store/general_value.h"
27 
28 #define RDB_UTILS_PUSH_WARNING _Pragma("GCC diagnostic push")
29 #define RDB_UTILS_POP_WARNING _Pragma("GCC diagnostic pop")
30 #define RDB_UTILS_DISABLE_WARNING_INTERNAL2(warningName) #warningName
31 #define RDB_UTILS_DISABLE_WARNING(warningName) \
32     _Pragma(                                     \
33         RDB_UTILS_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
34 
35 namespace OHOS::DistributedRdb {
36 
37 class RdbResultSetImpl final : public NativeRdb::ResultSet {
38 public:
39     using ValueProxy = DistributedData::ValueProxy;
40     using ColumnType = NativeRdb::ColumnType;
41     explicit RdbResultSetImpl(std::shared_ptr<DistributedData::Cursor> resultSet);
~RdbResultSetImpl()42     ~RdbResultSetImpl() override {};
43     int GetAllColumnNames(std::vector<std::string> &columnNames) override;
44     int GetColumnCount(int &count) override;
45     int GetColumnType(int columnIndex, ColumnType &columnType) override;
46     int GetColumnIndex(const std::string &columnName, int &columnIndex) override;
47     int GetColumnName(int columnIndex, std::string &columnName) override;
48     int GetRowCount(int &count) override;
49     int GetRowIndex(int &position) const override;
50     int GoTo(int offset) override;
51     int GoToRow(int position) override;
52     int GoToFirstRow() override;
53     int GoToLastRow() override;
54     int GoToNextRow() override;
55     int GoToPreviousRow() override;
56     int IsEnded(bool &result) override;
57     int IsStarted(bool &result) const override;
58     int IsAtFirstRow(bool &result) const override;
59     int IsAtLastRow(bool &result) override;
60     int GetBlob(int columnIndex, std::vector<uint8_t> &value) override;
61     int GetString(int columnIndex, std::string &value) override;
62     int GetInt(int columnIndex, int &value) override;
63     int GetLong(int columnIndex, int64_t &value) override;
64     int GetDouble(int columnIndex, double &value) override;
65     int IsColumnNull(int columnIndex, bool &isNull) override;
66     bool IsClosed() const override;
67     int Close() override;
68     int GetAsset(int32_t col, NativeRdb::ValueObject::Asset& value) override;
69     int GetAssets(int32_t col, NativeRdb::ValueObject::Assets& value) override;
70     int GetFloat32Array(int32_t index, NativeRdb::ValueObject::FloatVector& vecs) override;
71     int Get(int32_t col, NativeRdb::ValueObject& value) override;
72     int GetRow(NativeRdb::RowEntity& rowEntity) override;
73     int GetSize(int columnIndex, size_t& size) override;
74 
75 private:
76     template<typename T>
77     std::enable_if_t < ValueProxy::CVT_INDEX<T, ValueProxy::Proxy><ValueProxy::MAX, int>
78     Get(int columnIndex, T &value) const
79     {
80         auto [ret, val] = GetValue(columnIndex);
81         value = val.operator T();
82         return ret;
83     };
84 
GetValue(int columnIndex)85     std::pair<int32_t, ValueProxy::Value> GetValue(int columnIndex) const
86     {
87         DistributedData::Value var;
88         auto status = resultSet_->Get(columnIndex, var);
89         if (status != DistributedData::GeneralError::E_OK) {
90             return { NativeRdb::E_ERROR, ValueProxy::Value() };
91         }
92         return {NativeRdb::E_OK, ValueProxy::Convert(std::move(var))};
93     };
94 
95     mutable std::shared_mutex mutex_ {};
96 RDB_UTILS_PUSH_WARNING
97 RDB_UTILS_DISABLE_WARNING("-Wc99-designator")
98     static constexpr ColumnType COLUMNTYPES[DistributedData::TYPE_MAX] = {
99         [DistributedData::TYPE_INDEX<std::monostate>] = ColumnType::TYPE_NULL,
100         [DistributedData::TYPE_INDEX<int64_t>] = ColumnType::TYPE_INTEGER,
101         [DistributedData::TYPE_INDEX<double>] = ColumnType::TYPE_FLOAT,
102         [DistributedData::TYPE_INDEX<std::string>] = ColumnType::TYPE_STRING,
103         [DistributedData::TYPE_INDEX<bool>] = ColumnType::TYPE_INTEGER,
104         [DistributedData::TYPE_INDEX<DistributedData::Bytes>] = ColumnType::TYPE_BLOB,
105         [DistributedData::TYPE_INDEX<DistributedData::Asset>] = ColumnType::TYPE_BLOB,
106         [DistributedData::TYPE_INDEX<DistributedData::Assets>] = ColumnType::TYPE_BLOB,
107     };
108 RDB_UTILS_POP_WARNING
109     std::shared_ptr<DistributedData::Cursor> resultSet_;
110     std::atomic<int32_t> current_ = -1;
111     int32_t count_ = 0;
112     std::vector<std::string> colNames_;
113     ColumnType ConvertColumnType(int32_t columnType) const;
114 };
115 } // namespace OHOS::DistributedRdb
116 #endif // DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
117