1 /* 2 * Copyright (c) 2025 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 OHOS_MEDIA_ORM_RESULT_SET_READER_H 17 #define OHOS_MEDIA_ORM_RESULT_SET_READER_H 18 19 #include <string> 20 #include <map> 21 22 #include "cloud_media_sync_const.h" 23 #include "i_object_writer.h" 24 #include "result_set_utils.h" 25 26 namespace OHOS::Media::ORM { 27 template <typename OBJECT_WRITER, typename OBJECT_TYPE> 28 class ResultSetReader { 29 private: 30 std::shared_ptr<NativeRdb::ResultSet> resultSet_; 31 std::vector<std::string> columnNames_; 32 33 public: ResultSetReader(const std::shared_ptr<NativeRdb::ResultSet> resultSet)34 ResultSetReader(const std::shared_ptr<NativeRdb::ResultSet> resultSet) : resultSet_(resultSet) 35 {} 36 ~ResultSetReader() = default; 37 38 private: ReadRecord()39 OBJECT_TYPE ReadRecord() 40 { 41 OBJECT_TYPE objectPo; 42 bool errConn = this->resultSet_ == nullptr; 43 CHECK_AND_RETURN_RET(!errConn, objectPo); 44 CHECK_AND_RETURN_RET_LOG(!this->columnNames_.empty(), objectPo, "columnNames_ is empty"); 45 std::shared_ptr<IObjectWriter> objectWriter = std::make_shared<OBJECT_WRITER>(objectPo); 46 // read data from resultSet into PO 47 std::map<std::string, MediaColumnType::DataType> columns = objectWriter->GetColumns(); 48 for (const std::string &columnName : this->columnNames_) { 49 auto it = columns.find(columnName); 50 CHECK_AND_CONTINUE_ERR_LOG(it != columns.end(), "column [%{public}s] not found", columnName.c_str()); 51 const std::string key = it->first; 52 MediaColumnType::DataType type = it->second; 53 std::variant<int32_t, int64_t, double, std::string> val; 54 switch (type) { 55 case MediaColumnType::DataType::INT: { 56 val = GetInt32Val(key, this->resultSet_); 57 objectWriter->SetMemberVariable(key, val); 58 break; 59 } 60 case MediaColumnType::DataType::LONG: { 61 val = GetInt64Val(key, this->resultSet_); 62 objectWriter->SetMemberVariable(key, val); 63 break; 64 } 65 case MediaColumnType::DataType::DOUBLE: { 66 val = GetDoubleVal(key, this->resultSet_); 67 objectWriter->SetMemberVariable(key, val); 68 break; 69 } 70 case MediaColumnType::DataType::STRING: { 71 val = GetStringVal(key, this->resultSet_); 72 objectWriter->SetMemberVariable(key, val); 73 break; 74 } 75 } 76 } 77 // provide the copy of PO 78 return objectPo; 79 } LoadColumnNames(std::shared_ptr<NativeRdb::ResultSet> & resultSet)80 int32_t LoadColumnNames(std::shared_ptr<NativeRdb::ResultSet> &resultSet) 81 { 82 bool conn = this->resultSet_ != nullptr; 83 CHECK_AND_RETURN_RET_LOG(conn, E_HAS_DB_ERROR, "LoadColumnNames resultSet is null"); 84 auto ret = this->resultSet_->GetAllColumnNames(this->columnNames_); 85 MEDIA_DEBUG_LOG("LoadColumnNames, ret: %{public}d, count: %{public}zu", ret, this->columnNames_.size()); 86 return ret; 87 } 88 89 public: ReadRecords(std::vector<OBJECT_TYPE> & records)90 int32_t ReadRecords(std::vector<OBJECT_TYPE> &records) 91 { 92 bool conn = this->resultSet_ != nullptr; 93 CHECK_AND_RETURN_RET_LOG(conn, E_HAS_DB_ERROR, "ReadRecords resultSet is null"); 94 int32_t ret = this->LoadColumnNames(this->resultSet_); 95 CHECK_AND_RETURN_RET_LOG(ret == E_OK, ret, "ReadRecords LoadColumnNames failed, ret: %{public}d", ret); 96 while (this->resultSet_->GoToNextRow() == NativeRdb::E_OK) { 97 records.emplace_back(this->ReadRecord()); 98 } 99 this->resultSet_->Close(); 100 return E_OK; 101 } ReadRecords()102 std::vector<OBJECT_TYPE> ReadRecords() 103 { 104 std::vector<OBJECT_TYPE> records; 105 this->ReadRecords(records); 106 return records; 107 } 108 }; 109 } // namespace OHOS::Media::ORM 110 #endif // OHOS_MEDIA_ORM_RESULT_SET_READER_H 111