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 GetSize(int columnIndex, size_t& size) override; 64 65 private: 66 template<typename T> 67 std::enable_if_t < ValueProxy::CVT_INDEX<T, ValueProxy::Proxy><ValueProxy::MAX, int> 68 Get(int columnIndex, T &value) const 69 { 70 auto [ret, val] = GetValue(columnIndex); 71 value = val.operator T(); 72 return ret; 73 }; 74 GetValue(int columnIndex)75 std::pair<int32_t, ValueProxy::Value> GetValue(int columnIndex) const 76 { 77 DistributedData::Value var; 78 auto status = resultSet_->Get(columnIndex, var); 79 if (status != DistributedData::GeneralError::E_OK) { 80 return { NativeRdb::E_ERROR, ValueProxy::Value() }; 81 } 82 return {NativeRdb::E_OK, ValueProxy::Convert(std::move(var))}; 83 }; 84 85 mutable std::shared_mutex mutex_ {}; 86 static constexpr ColumnType COLUMNTYPES[DistributedData::TYPE_MAX] = { 87 [DistributedData::TYPE_INDEX<std::monostate>] = ColumnType::TYPE_NULL, 88 [DistributedData::TYPE_INDEX<int64_t>] = ColumnType::TYPE_INTEGER, 89 [DistributedData::TYPE_INDEX<double>] = ColumnType::TYPE_FLOAT, 90 [DistributedData::TYPE_INDEX<std::string>] = ColumnType::TYPE_STRING, 91 [DistributedData::TYPE_INDEX<bool>] = ColumnType::TYPE_INTEGER, 92 [DistributedData::TYPE_INDEX<DistributedData::Bytes>] = ColumnType::TYPE_BLOB, 93 [DistributedData::TYPE_INDEX<DistributedData::Asset>] = ColumnType::TYPE_BLOB, 94 [DistributedData::TYPE_INDEX<DistributedData::Assets>] = ColumnType::TYPE_BLOB, 95 }; 96 std::shared_ptr<DistributedData::Cursor> resultSet_; 97 std::atomic<int32_t> current_ = -1; 98 int32_t count_ = 0; 99 std::vector<std::string> colNames_; 100 ColumnType ConvertColumnType(int32_t columnType) const; 101 }; 102 } // namespace OHOS::DistributedRdb 103 #endif // DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H 104