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 bool IsPrimaryKey(const FieldName &fieldName) const; 87 bool IsUniqueField(const std::string &colName) const; 88 const std::vector<CompositeFields> &GetUniqueDefine() const; 89 90 void SetTableName(const std::string &tableName); 91 void SetOriginTableName(const std::string &originTableName); 92 void SetSharedTableMark(bool sharedTableMark); 93 void SetAutoIncrement(bool autoInc); 94 void SetTableSyncType(TableSyncType tableSyncType); 95 void SetCreateTableSql(const std::string &sql); // set 'autoInc_' flag when set sql 96 void AddField(const FieldInfo &field); 97 void AddIndexDefine(const std::string &indexName, const CompositeFields &indexDefine); 98 void SetPrimaryKey(const std::map<int, FieldName> &key); 99 void SetPrimaryKey(const FieldName &fieldName, int keyIndex); 100 std::string ToTableInfoString(const std::string &schemaVersion) const; 101 void SetTrackerTable(const TrackerTable &table); 102 int CheckTrackerTable(); 103 const TrackerTable &GetTrackerTable() const; 104 void AddTableReferenceProperty(const TableReferenceProperty &tableRefProperty); 105 void SetSourceTableReference(const std::vector<TableReferenceProperty> &tableReference); 106 const std::vector<TableReferenceProperty> &GetTableReference() const; 107 108 void SetUniqueDefine(const std::vector<CompositeFields> &uniqueDefine); 109 110 int CompareWithTable(const TableInfo &inTableInfo, 111 const std::string &schemaVersion = SchemaConstant::SCHEMA_SUPPORT_VERSION_V2) const; 112 int CompareWithLiteSchemaTable(const TableInfo &liteTableInfo) const; 113 114 std::map<FieldPath, SchemaAttribute> GetSchemaDefine() const; 115 std::string GetFieldName(uint32_t cid) const; // cid begin with 0 116 const std::vector<FieldInfo> &GetFieldInfos() const; // Sort by cid 117 bool IsValid() const; 118 119 // return a key to Identify a row data, 120 // If there is a primary key, return the primary key, 121 // If there is no primary key, return the first unique key, 122 // If there is no unique key, return the rowid. 123 CompositeFields GetIdentifyKey() const; 124 125 void SetTableId(int id); 126 int GetTableId() const; 127 128 bool Empty() const; 129 130 bool IsNoPkTable() const; 131 bool IsMultiPkTable() const; 132 133 bool IsFieldExist(const std::string &fieldName) const; 134 135 void SetDistributedTable(const DistributedTable &distributedTable); 136 137 std::vector<std::string> GetSyncField() const; 138 139 std::vector<std::string> GetSyncDistributedPk() const; 140 141 const std::vector<CompositeFields> GetUniqueAndPkDefine() const; 142 private: 143 void AddFieldDefineString(std::string &attrStr) const; 144 void AddIndexDefineString(std::string &attrStr) const; 145 void AddUniqueDefineString(std::string &attrStr) const; 146 147 int CompareWithPrimaryKey(const std::map<int, FieldName> &local, const std::map<int, FieldName> &remove) const; 148 int CompareWithTableFields(const FieldInfoMap &inTableFields, bool isLite = false) const; 149 int CompareWithTableIndex(const IndexInfoMap &inTableIndex) const; 150 int CompareWithTableUnique(const std::vector<CompositeFields> &inTableUnique) const; 151 int CompareCompositeFields(const CompositeFields &local, const CompositeFields &remote) const; 152 153 int CompareWithLiteTableFields(const FieldInfoMap &liteTableFields) const; 154 155 std::string tableName_; 156 std::string originTableName_ = ""; 157 bool sharedTableMark_ = false; 158 bool autoInc_ = false; // only 'INTEGER PRIMARY KEY' could be defined as 'AUTOINCREMENT' 159 TableSyncType tableSyncType_ = DEVICE_COOPERATION; 160 std::string sql_; 161 FieldInfoMap fields_; 162 std::map<int, FieldName> primaryKey_; 163 IndexInfoMap indexDefines_; 164 mutable std::vector<FieldInfo> fieldInfos_; 165 166 std::vector<CompositeFields> uniqueDefines_; 167 int id_ = -1; 168 TrackerTable trackerTable_; 169 DistributedTable distributedTable_; 170 // a 171 // b c 172 // d e f ,table_info[a] = {b,c} [b] = {d, e} [c] = {f} 173 std::vector<TableReferenceProperty> sourceTableReferenced_; 174 }; 175 } // namespace DistributedDB 176 #endif // TABLE_INFO_H