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