• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         TableSyncType syncType);
39 
40     int CleanDistributedDeviceTable(std::vector<std::string> &missingTables);
41 
42     const RelationalDBProperties &GetProperties() const;
43     void SetProperties(const RelationalDBProperties &properties);
44 
45 protected:
46     StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override;
47     int Upgrade(sqlite3 *db) override;
48     int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override;
49 
50 private:
51     // For executor.
52     int ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&handle);
53 
54     // For db.
55     int RegisterFunction(sqlite3 *db) const;
56 
57     int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged, TableSyncType syncType);
58     int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity,
59         RelationalSchemaObject &schema, TableSyncType tableSyncType);
60 
61     int CreateRelationalMetaTable(sqlite3 *db);
62 
63     RelationalSchemaObject schema_;
64     mutable std::mutex schemaMutex_;
65 
66     RelationalDBProperties properties_;
67 };
68 } // namespace DistributedDB
69 #endif
70 #endif // SQLITE_RELATIONAL_ENGINE_H