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_ENGINE_H 16 #define SQLITE_RELATIONAL_ENGINE_H 17 #ifdef RELATIONAL_STORE 18 19 #include "macro_utils.h" 20 #include "relationaldb_properties.h" 21 #include "sqlite_storage_engine.h" 22 #include "sqlite_single_ver_relational_storage_executor.h" 23 24 namespace DistributedDB { 25 class SQLiteSingleRelationalStorageEngine : public SQLiteStorageEngine { 26 public: 27 explicit SQLiteSingleRelationalStorageEngine(RelationalDBProperties properties); 28 ~SQLiteSingleRelationalStorageEngine() override; 29 30 // Delete the copy and assign constructors 31 DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleRelationalStorageEngine); 32 33 void SetSchema(const RelationalSchemaObject &schema); 34 35 RelationalSchemaObject GetSchema() const; 36 37 int CreateDistributedTable(const std::string &tableName, const std::string &identity, bool &schemaChanged); 38 39 int CleanDistributedDeviceTable(std::vector<std::string> &missingTables); 40 41 const RelationalDBProperties &GetProperties() const; 42 void SetProperties(const RelationalDBProperties &properties); 43 44 protected: 45 StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *db, bool isWrite, bool isMemDb) override; 46 int Upgrade(sqlite3 *db) override; 47 int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override; 48 49 private: 50 // For executor. 51 int ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&handle); 52 53 // For db. 54 int RegisterFunction(sqlite3 *db) const; 55 56 int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged); 57 int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity, 58 RelationalSchemaObject &schema); 59 60 RelationalSchemaObject schema_; 61 mutable std::mutex schemaMutex_; 62 63 RelationalDBProperties properties_; 64 }; 65 } // namespace DistributedDB 66 #endif 67 #endif // SQLITE_RELATIONAL_ENGINE_H