1 /* 2 * Copyright (c) 2021 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 #ifndef SQLITE_RELATIONAL_STORE_CONNECTION_H 16 #define SQLITE_RELATIONAL_STORE_CONNECTION_H 17 #ifdef RELATIONAL_STORE 18 19 #include <atomic> 20 #include <string> 21 #include "macro_utils.h" 22 #include "relational_store_connection.h" 23 #include "sqlite_single_ver_relational_storage_executor.h" 24 #include "sqlite_relational_store.h" 25 26 namespace DistributedDB { 27 class SQLiteRelationalStoreConnection : public RelationalStoreConnection { 28 public: 29 explicit SQLiteRelationalStoreConnection(SQLiteRelationalStore *store); 30 31 ~SQLiteRelationalStoreConnection() override = default; 32 33 DISABLE_COPY_ASSIGN_MOVE(SQLiteRelationalStoreConnection); 34 35 // Close and release the connection. 36 int Close() override; 37 int SyncToDevice(SyncInfo &info) override; 38 std::string GetIdentifier() override; 39 int CreateDistributedTable(const std::string &tableName, TableSyncType syncType) override; 40 int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) override; 41 int RemoveDeviceData() override; 42 int RemoveDeviceData(const std::string &device) override; 43 int RemoveDeviceData(const std::string &device, const std::string &tableName) override; 44 int RegisterObserverAction(const StoreObserver *observer, const RelationalObserverAction &action) override; 45 int UnRegisterObserverAction(const StoreObserver *observer) override; 46 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 47 std::shared_ptr<ResultSet> &result) override; 48 49 int GetStoreInfo(std::string &userId, std::string &appId, std::string &storeId) override; 50 51 int SetTrackerTable(const TrackerSchema &schema) override; 52 int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records) override; 53 int CleanTrackerData(const std::string &tableName, int64_t cursor) override; 54 55 int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty) override; 56 57 int Pragma(PragmaCmd cmd, PragmaData &pragmaData) override; 58 59 int UpsertData(RecordStatus status, const std::string &tableName, const std::vector<VBucket> &records) override; 60 61 int SetDistributedDbSchema(const DistributedSchema &schema, bool isForceUpgrade) override; 62 63 int GetDownloadingAssetsCount(int32_t &count) override; 64 65 int SetTableMode(DistributedTableMode tableMode) override; 66 #ifdef USE_DISTRIBUTEDDB_CLOUD 67 int DoClean(ClearMode mode) override; 68 69 int ClearCloudWatermark(const std::set<std::string> &tableNames) override; 70 71 int32_t GetCloudSyncTaskCount() override; 72 73 int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) override; 74 75 int PrepareAndSetCloudDbSchema(const DataBaseSchema &schema) override; 76 77 int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) override; 78 79 int SetCloudSyncConfig(const CloudSyncConfig &config) override; 80 81 int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess, uint64_t taskId) override; 82 83 SyncProcess GetCloudTaskStatus(uint64_t taskId) override; 84 #endif 85 86 int OperateDataStatus(uint32_t dataOperator) override; 87 88 int32_t GetDeviceSyncTaskCount() override; 89 protected: 90 91 int Pragma(int cmd, void *parameter) override; 92 private: 93 94 SQLiteSingleVerRelationalStorageExecutor *GetExecutor(bool isWrite, int &errCode) const; 95 void ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&executor) const; 96 int StartTransaction(); 97 // Commit the transaction 98 int Commit(); 99 100 // Roll back the transaction 101 int RollBack(); 102 103 SQLiteSingleVerRelationalStorageExecutor *writeHandle_ = nullptr; 104 mutable std::mutex transactionMutex_; // used for transaction 105 }; 106 } // namespace DistributedDB 107 #endif 108 #endif // SQLITE_RELATIONAL_STORE_CONNECTION_H