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 #ifndef TRACKER_TABLE_H
16 #define TRACKER_TABLE_H
17 #include <set>
18 #include "store_types.h"
19 #include "db_types.h"
20 #include "db_errno.h"
21 #include "json_object.h"
22 #include "sqlite_import.h"
23 
24 #ifdef RELATIONAL_STORE
25 namespace DistributedDB {
26 using AfterBuildAction = std::function<int()>;
27 namespace TriggerMode {
28 enum class TriggerModeEnum;
29 }
30 class TrackerTable {
31 public:
32     TrackerTable() = default;
~TrackerTable()33     virtual ~TrackerTable() {};
34 
35     std::string GetTableName() const;
36     const std::set<std::string> &GetTrackerColNames() const;
37     void Init(const TrackerSchema &schema);
38     const std::string GetAssignValSql(bool isDelete = false) const;
39     const std::string GetExtendAssignValSql(bool isDelete = false) const;
40     const std::string GetDiffTrackerValSql() const;
41     const std::string GetDiffIncCursorSql(const std::string &tableName) const;
42     const std::string GetExtendName() const;
43     std::string ToString() const;
44     const std::vector<std::string> GetDropTempTriggerSql() const;
45     const std::string GetTempInsertTriggerSql(bool incFlag = false) const;
46     const std::string GetDropTempTriggerSql(TriggerMode::TriggerModeEnum mode) const;
47     const std::string GetCreateTempTriggerSql(TriggerMode::TriggerModeEnum mode) const;
48     const std::string GetTempTriggerName(TriggerMode::TriggerModeEnum mode) const;
49     const std::string GetTempUpdateTriggerSql(bool incFlag = false) const;
50     const std::string GetTempDeleteTriggerSql(bool incFlag = false) const;
51     void SetTableName(const std::string &tableName);
52     void SetExtendName(const std::string &colName);
53     void SetTrackerNames(const std::set<std::string> &trackerNames);
54     bool IsEmpty() const;
55     bool IsTableNameEmpty() const;
56     bool IsChanging(const TrackerSchema &schema);
57     int ReBuildTempTrigger(sqlite3 *db, TriggerMode::TriggerModeEnum mode, const AfterBuildAction &action);
58 
59 private:
60     std::string tableName_;
61     std::string extendColName_;
62     std::set<std::string> trackerColNames_;
63 };
64 
65 } // namespace DistributedDB
66 #endif // RELATIONAL_STORE
67 #endif // TRACKER_TABLE_H