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