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 TABLE_INFO_H
16 #define TABLE_INFO_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "cloud/cloud_store_types.h"
23 #include "data_value.h"
24 #include "db_types.h"
25 #include "ischema.h"
26 #include "schema_constant.h"
27 #include "tracker_table.h"
28 
29 namespace DistributedDB {
30 using CompositeFields = std::vector<FieldName>;
31 class FieldInfo {
32 public:
33     const std::string &GetFieldName() const;
34     void SetFieldName(const std::string &fileName);
35     const std::string &GetDataType() const;
36     void SetDataType(const std::string &dataType);
37     bool IsNotNull() const;
38     void SetNotNull(bool isNotNull);
39     // Use string type to save the default value define in the create table sql.
40     // No need to use the real value because sqlite will complete them.
41     bool HasDefaultValue() const;
42     const std::string &GetDefaultValue() const;
43     void SetDefaultValue(const std::string &value);
44     // convert to StorageType according "Determination Of Column Affinity"
45     StorageType GetStorageType() const;
46     void SetStorageType(StorageType storageType);
47 
48     int GetColumnId() const;
49     void SetColumnId(int cid);
50 
51     // return field define string like ("fieldName": "MY INT(21), NOT NULL, DEFAULT 123")
52     std::string ToAttributeString() const;
53 
54     int CompareWithField(const FieldInfo &inField, bool isLite = false) const;
55 
56     bool IsAssetType() const;
57     bool IsAssetsType() const;
58 
59     CollateType GetCollateType() const;
60     void SetCollateType(CollateType collateType);
61 
62 private:
63     std::string fieldName_;
64     std::string dataType_; // Type may be null
65     StorageType storageType_ = StorageType::STORAGE_TYPE_NONE;
66     bool isNotNull_ = false;
67     bool hasDefaultValue_ = false;
68     std::string defaultValue_;
69     int64_t cid_ = -1;
70     CollateType collateType_ = CollateType::COLLATE_NONE;
71 };
72 
73 using FieldInfoMap = std::map<std::string, FieldInfo, CaseInsensitiveComparator>;
74 using IndexInfoMap = std::map<std::string, CompositeFields, CaseInsensitiveComparator>;
75 class TableInfo {
76 public:
77     const std::string &GetTableName() const;
78     const std::string &GetOriginTableName() const;
79     bool GetSharedTableMark() const;
80     bool GetAutoIncrement() const;
81     TableSyncType GetTableSyncType() const;
82     const std::string &GetCreateTableSql() const;
83     const FieldInfoMap &GetFields() const; // <colName, colAttr>
84     const IndexInfoMap &GetIndexDefine() const;
85     const std::map<int, FieldName> &GetPrimaryKey() const;
86     const std::vector<CompositeFields> &GetUniqueDefine() const;
87 
88     void SetTableName(const std::string &tableName);
89     void SetOriginTableName(const std::string &originTableName);
90     void SetSharedTableMark(bool sharedTableMark);
91     void SetAutoIncrement(bool autoInc);
92     void SetTableSyncType(TableSyncType tableSyncType);
93     void SetCreateTableSql(const std::string &sql); // set 'autoInc_' flag when set sql
94     void AddField(const FieldInfo &field);
95     void AddIndexDefine(const std::string &indexName, const CompositeFields &indexDefine);
96     void SetPrimaryKey(const std::map<int, FieldName> &key);
97     void SetPrimaryKey(const FieldName &fieldName, int keyIndex);
98     std::string ToTableInfoString(const std::string &schemaVersion) const;
99     void SetTrackerTable(const TrackerTable &table);
100     int CheckTrackerTable();
101     const TrackerTable &GetTrackerTable() const;
102     void AddTableReferenceProperty(const TableReferenceProperty &tableRefProperty);
103     void SetSourceTableReference(const std::vector<TableReferenceProperty> &tableReference);
104     const std::vector<TableReferenceProperty> &GetTableReference() const;
105 
106     void SetUniqueDefine(const std::vector<CompositeFields> &uniqueDefine);
107 
108     int CompareWithTable(const TableInfo &inTableInfo,
109         const std::string &schemaVersion = SchemaConstant::SCHEMA_SUPPORT_VERSION_V2) const;
110     int CompareWithLiteSchemaTable(const TableInfo &liteTableInfo) const;
111 
112     std::map<FieldPath, SchemaAttribute> GetSchemaDefine() const;
113     std::string GetFieldName(uint32_t cid) const;  // cid begin with 0
114     const std::vector<FieldInfo> &GetFieldInfos() const;  // Sort by cid
115     bool IsValid() const;
116 
117     // return a key to Identify a row data,
118     // If there is a primary key, return the primary key,
119     // If there is no primary key, return the first unique key,
120     // If there is no unique key, return the rowid.
121     CompositeFields GetIdentifyKey() const;
122 
123     void SetTableId(int id);
124     int GetTableId() const;
125 
126     bool Empty() const;
127 
128     bool IsNoPkTable() const;
129 
130 private:
131     void AddFieldDefineString(std::string &attrStr) const;
132     void AddIndexDefineString(std::string &attrStr) const;
133     void AddUniqueDefineString(std::string &attrStr) const;
134 
135     int CompareWithPrimaryKey(const std::map<int, FieldName> &local, const std::map<int, FieldName> &remove) const;
136     int CompareWithTableFields(const FieldInfoMap &inTableFields, bool isLite = false) const;
137     int CompareWithTableIndex(const IndexInfoMap &inTableIndex) const;
138     int CompareWithTableUnique(const std::vector<CompositeFields> &inTableUnique) const;
139     int CompareCompositeFields(const CompositeFields &local, const CompositeFields &remote) const;
140 
141     int CompareWithLiteTableFields(const FieldInfoMap &liteTableFields) const;
142 
143     std::string tableName_;
144     std::string originTableName_ = "";
145     bool sharedTableMark_ = false;
146     bool autoInc_ = false; // only 'INTEGER PRIMARY KEY' could be defined as 'AUTOINCREMENT'
147     TableSyncType tableSyncType_ = DEVICE_COOPERATION;
148     std::string sql_;
149     FieldInfoMap fields_;
150     std::map<int, FieldName> primaryKey_;
151     IndexInfoMap indexDefines_;
152     mutable std::vector<FieldInfo> fieldInfos_;
153 
154     std::vector<CompositeFields> uniqueDefines_;
155     int id_ = -1;
156     TrackerTable trackerTable_;
157     //     a
158     //  b     c
159     // d  e  f    ,table_info[a] = {b,c}  [b] = {d, e}  [c] = {f}
160     std::vector<TableReferenceProperty> sourceTableReferenced_;
161 };
162 } // namespace DistributedDB
163 #endif // TABLE_INFO_H