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