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 DRIVE_KIT_DATABASE_H 17 #define DRIVE_KIT_DATABASE_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 #include <vector> 23 24 #include "dk_asset_read_session.h" 25 #include "dk_assets_downloader.h" 26 #include "dk_cloud_callback.h" 27 #include "dk_context.h" 28 #include "dk_error.h" 29 #include "dk_record.h" 30 #include "dk_result.h" 31 32 namespace DriveKit { 33 enum class DKSavePolicy { 34 DK_SAVE_IF_UNCHANGED = 0, 35 DK_CURRENT_WRITER_WINS, 36 }; 37 38 enum class DKDatabaseScope { 39 DK_PUBLIC_DATABASE = 0, 40 DK_PRIVATE_DATABASE, 41 DK_SHARED_DATABASE, 42 }; 43 44 using DKSchemaRawData = std::string; 45 struct DKSchemaField { 46 DKFieldKey name; 47 DKRecordFieldType type; 48 bool primary; 49 bool nullable; 50 bool sortable; 51 bool searchable; 52 bool queryable; 53 DKRecordFieldType listType; 54 DKRecordType refRecordType; 55 }; 56 struct DKSchemaNode { 57 DKRecordType recordType; 58 std::string tableName; 59 std::map<DKFieldKey, DKSchemaField> fields; 60 std::vector<DKFieldKey> dupCheckFields; 61 }; 62 struct DKOrderTable { 63 DKRecordType recordType; 64 std::string tableName; 65 }; 66 struct DKSchema { 67 int version; 68 std::map<DKRecordType, DKSchemaNode> recordTypes; 69 DKSchemaRawData schemaData; 70 std::vector<DKOrderTable> orderTables; 71 }; 72 73 struct DKPredicate { 74 std::string predicateFormat; 75 }; 76 struct DKSortDescriptor { 77 bool ascending; 78 DKFieldKey key; 79 }; 80 using DKSortDescriptors = std::vector<DKSortDescriptor>; 81 struct DKQuery { 82 DKRecordType recordType; 83 DKPredicate predicate; 84 DKSortDescriptors sortDescriptors; 85 }; 86 87 class DKRecordOperResult : public DKResult { 88 public: DKRecordOperResult()89 DKRecordOperResult() {} DKRecordOperResult(DKLocalErrorCode code)90 DKRecordOperResult(DKLocalErrorCode code) 91 { 92 error_.SetLocalError(code); 93 } 94 public: SetDKRecord(const DKRecord & record)95 void SetDKRecord(const DKRecord &record) 96 { 97 record_ = record; 98 } SetDKRecord(DKRecord && record)99 void SetDKRecord(DKRecord &&record) 100 { 101 record_ = std::move(record); 102 } GetDKRecord()103 DKRecord GetDKRecord() const 104 { 105 return record_; 106 } StealDKRecord(DKRecord & record)107 void StealDKRecord(DKRecord &record) 108 { 109 record = std::move(record_); 110 return; 111 } 112 113 private: 114 DKRecord record_; 115 }; 116 117 struct DKLock { 118 int lockInterval; 119 std::string lockSessionId; 120 }; 121 using DKFieldKeyArray = std::vector<DKFieldKey>; 122 using DKQueryCursor = std::string; 123 class DKContainer; 124 class DKDatabase : public std::enable_shared_from_this<DKDatabase> { 125 friend class DKContainer; 126 public: 127 DKDatabase(std::shared_ptr<DKContainer> container, DKDatabaseScope scope); ~DKDatabase()128 ~DKDatabase() {} 129 130 using SaveRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 131 std::shared_ptr<DKDatabase>, 132 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>>, 133 const DKError &)>; 134 DKLocalErrorCode SaveRecords(std::shared_ptr<DKContext> context, 135 std::vector<DKRecord> &&records, 136 DKSavePolicy policy, 137 SaveRecordsCallback callback); 138 139 using SaveRecordCallback = std::function<void(std::shared_ptr<DKContext>, 140 std::shared_ptr<DKDatabase>, 141 DKRecordId, 142 DKRecordOperResult, 143 const DKError &)>; 144 DKLocalErrorCode SaveRecord(std::shared_ptr<DKContext> context, 145 DKRecord &&record, 146 DKSavePolicy policy, 147 SaveRecordCallback callback); 148 149 using FetchRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 150 std::shared_ptr<DKDatabase>, 151 std::shared_ptr<std::vector<DKRecord>>, 152 DKQueryCursor nextCursor, 153 const DKError &)>; 154 DKLocalErrorCode FetchRecords(std::shared_ptr<DKContext> context, 155 DKRecordType recordType, 156 DKFieldKeyArray &desiredKeys, 157 int resultLimit, 158 DKQueryCursor cursor, 159 FetchRecordsCallback callback); 160 161 using FetchRecordCallback = std::function<void(std::shared_ptr<DKContext>, 162 std::shared_ptr<DKDatabase>, 163 DKRecordId, 164 DKRecord &, 165 const DKError &)>; 166 DKLocalErrorCode FetchRecordWithId(std::shared_ptr<DKContext> context, 167 DKRecordType recordType, 168 DKRecordId recordId, 169 DKFieldKeyArray &desiredKeys, 170 FetchRecordCallback callback); 171 172 using DeleteRecordsCallback = std::function<void(std::shared_ptr<DKContext>, 173 std::shared_ptr<DKDatabase>, 174 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>>, 175 const DKError &)>; 176 DKLocalErrorCode DeleteRecords(std::shared_ptr<DKContext> context, 177 std::vector<DKRecord> &&records, 178 DKSavePolicy policy, 179 DeleteRecordsCallback callback); 180 181 using ModifyRecordsCallback = 182 std::function<void(std::shared_ptr<DKContext>, 183 std::shared_ptr<DKDatabase>, 184 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>> saveResult, 185 std::shared_ptr<std::map<DKRecordId, DKRecordOperResult>> delResult, 186 const DKError &)>; 187 DKLocalErrorCode ModifyRecords(std::shared_ptr<DKContext> context, 188 std::vector<DKRecord> &&recordsToSave, 189 std::vector<DKRecord> &&recordsToDelete, 190 DKSavePolicy policy, 191 bool atomically, 192 ModifyRecordsCallback callback); 193 194 DKLocalErrorCode FetchRecordsWithQuery(std::shared_ptr<DKContext> context, 195 DKFieldKeyArray &desiredKeys, 196 DKQuery query, 197 int resultLimit, 198 DKQueryCursor cursor, 199 FetchRecordsCallback callback); 200 201 using FetchDatabaseCallback = std::function<void(std::shared_ptr<DKContext>, 202 std::shared_ptr<DKDatabase>, 203 std::shared_ptr<std::vector<DKRecord>>, 204 DKQueryCursor nextCursor, 205 bool hasMore, 206 const DKError &)>; 207 DKLocalErrorCode FetchDatabaseChanges(std::shared_ptr<DKContext> context, 208 DKRecordType recordType, 209 DKFieldKeyArray &desiredKeys, 210 int resultLimit, 211 DKQueryCursor cursor, 212 FetchDatabaseCallback callback); 213 214 DKError GetStartCursor(DKRecordType recordType, DKQueryCursor &cursor); 215 DKError GenerateIds(int count, std::vector<DKRecordId> &ids); 216 DKError GetLock(DKLock &lock); 217 void DeleteLock(DKLock lock); 218 219 std::shared_ptr<DKAssetsDownloader> GetAssetsDownloader(); 220 std::shared_ptr<DKAssetReadSession> 221 NewAssetReadSession(DKRecordType recordType, DKRecordId recordId, DKFieldKey assetKey, DKAssetPath assetPath); 222 223 protected: 224 void Init(); 225 private: 226 DKContainerName containerName_; 227 }; 228 } // namespace DriveKit 229 #endif