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 TriggerAutoSync() override; 38 int SyncToDevice(SyncInfo &info) override; 39 std::string GetIdentifier() override; 40 int CreateDistributedTable(const std::string &tableName) override; 41 int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) override; 42 43 int RemoveDeviceData() override; 44 int RemoveDeviceData(const std::string &device) override; 45 int RemoveDeviceData(const std::string &device, const std::string &tableName) override; 46 void RegisterObserverAction(const RelationalObserverAction &action) override; 47 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 48 std::shared_ptr<ResultSet> &result) override; 49 50 protected: 51 52 int Pragma(int cmd, void *parameter) override; 53 private: 54 55 SQLiteSingleVerRelationalStorageExecutor *GetExecutor(bool isWrite, int &errCode) const; 56 void ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&executor) const; 57 int StartTransaction(); 58 // Commit the transaction 59 int Commit(); 60 61 // Roll back the transaction 62 int RollBack(); 63 64 SQLiteSingleVerRelationalStorageExecutor *writeHandle_ = nullptr; 65 mutable std::mutex transactionMutex_; // used for transaction 66 }; 67 } // namespace DistributedDB 68 #endif 69 #endif // SQLITE_RELATIONAL_STORE_CONNECTION_H