1 /* 2 * Copyright (c) 2023 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 CALENDAR_NATIVE_UTILS_H 17 #define CALENDAR_NATIVE_UTILS_H 18 19 #include <string> 20 #include <string_view> 21 #include <vector> 22 #include <map> 23 24 #include "calendar_define.h" 25 #include "datashare_result_set.h" 26 #include "datashare_values_bucket.h" 27 #include "native_calendar.h" 28 29 namespace OHOS::CalendarApi::Native { 30 using DataShareResultSetPtr = std::shared_ptr<DataShare::DataShareResultSet>; 31 void DumpCalendarAccount(const CalendarAccount &account); 32 void DumpEvent(const Event &event); 33 DataShare::DataShareValuesBucket BuildValueEvent(const Event &event, int calendarId); 34 DataShare::DataShareValuesBucket BuildAttendeeValue(const Attendee &attendee, int eventId); 35 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, int& out); 36 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, std::string& out); 37 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, int64_t& out); 38 39 std::vector<std::shared_ptr<Calendar>> ResultSetToCalendars(DataShareResultSetPtr &resultSet); 40 int ResultSetToEvents(std::vector<Event> &events, 41 DataShareResultSetPtr &resultSet, const std::set<std::string>& columns); 42 int ResultSetToAttendees(std::vector<Attendee> &attendees, DataShareResultSetPtr &resultSet); 43 int ResultSetToReminders(std::vector<int> &reminders, DataShareResultSetPtr &resultSet); 44 void setField(const std::vector<string>& eventKey, 45 std::vector<string>& queryField, std::set<string>& resultSetField); 46 47 bool ColorParse(const std::string& colorStr, variant<string, int64_t>& colorValue); 48 49 template<typename T> GetValue(DataShareResultSetPtr & resultSet,string_view fieldName,T & out)50 int GetValue(DataShareResultSetPtr &resultSet, string_view fieldName, T& out) 51 { 52 int index = 0; 53 auto fieldNameStr = string(fieldName); 54 auto ret = resultSet->GetColumnIndex(fieldNameStr, index); 55 if (ret != DataShare::E_OK) { 56 return ret; 57 } 58 return GetIndexValue(resultSet, index, out); 59 } 60 61 template<typename T> GetValueOptional(DataShareResultSetPtr & resultSet,string_view fieldName,std::optional<T> & out)62 int GetValueOptional(DataShareResultSetPtr &resultSet, string_view fieldName, std::optional<T>& out) 63 { 64 out = std::nullopt; 65 int index = 0; 66 auto fieldNameStr = string(fieldName); 67 auto ret = resultSet->GetColumnIndex(string(fieldName), index); 68 if (ret != DataShare::E_OK) { 69 return ret; 70 } 71 T value; 72 ret = GetIndexValue(resultSet, index, value); 73 if (ret != DataShare::E_OK) { 74 return ret; 75 } 76 out = value; 77 return ret; 78 } 79 } 80 // namespace OHOS::Calendar::NativeUtils 81 82 #endif /* CALENDAR_NATIVE_UTILS_H */