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 RELATIONAL_SCHEMA_OBJECT_H 16 #define RELATIONAL_SCHEMA_OBJECT_H 17 #ifdef RELATIONAL_STORE 18 #include <map> 19 #include "data_value.h" 20 #include "json_object.h" 21 #include "parcel.h" 22 #include "ischema.h" 23 24 namespace DistributedDB { 25 using CompositeFields = std::vector<FieldName>; 26 class FieldInfo { 27 public: 28 const std::string &GetFieldName() const; 29 void SetFieldName(const std::string &fileName); 30 const std::string &GetDataType() const; 31 void SetDataType(const std::string &dataType); 32 bool IsNotNull() const; 33 void SetNotNull(bool isNotNull); 34 // Use string type to save the default value define in the create table sql. 35 // No need to use the real value because sqlite will complete them. 36 bool HasDefaultValue() const; 37 const std::string &GetDefaultValue() const; 38 void SetDefaultValue(const std::string &value); 39 // convert to StorageType according "Determination Of Column Affinity" 40 StorageType GetStorageType() const; 41 void SetStorageType(StorageType storageType); 42 43 int GetColumnId() const; 44 void SetColumnId(int cid); 45 46 // return field define string like ("fieldName": "MY INT(21), NOT NULL, DEFAULT 123") 47 std::string ToAttributeString() const; 48 49 int CompareWithField(const FieldInfo &inField) const; 50 private: 51 std::string fieldName_; 52 std::string dataType_; // Type may be null 53 StorageType storageType_ = StorageType::STORAGE_TYPE_NONE; 54 bool isNotNull_ = false; 55 bool hasDefaultValue_ = false; 56 std::string defaultValue_; 57 int64_t cid_ = -1; 58 }; 59 60 class TableInfo { 61 public: 62 const std::string &GetTableName() const; 63 bool GetAutoIncrement() const; 64 const std::string &GetCreateTableSql() const; 65 const std::map<FieldName, FieldInfo> &GetFields() const; // <colName, colAttr> 66 const std::map<std::string, CompositeFields> &GetIndexDefine() const; 67 const FieldName &GetPrimaryKey() const; 68 69 void SetTableName(const std::string &tableName); 70 void SetAutoIncrement(bool autoInc); 71 void SetCreateTableSql(std::string sql); // set 'autoInc_' flag when set sql 72 void AddField(const FieldInfo &field); 73 void AddIndexDefine(const std::string &indexName, const CompositeFields &indexDefine); 74 void SetPrimaryKey(const FieldName &fieldName); // not support composite index now 75 std::string ToTableInfoString() const; 76 77 int CompareWithTable(const TableInfo &inTableInfo) const; 78 std::map<FieldPath, SchemaAttribute> GetSchemaDefine() const; 79 std::string GetFieldName(uint32_t cid) const; // cid begin with 0 80 const std::vector<FieldInfo> &GetFieldInfos() const; // Sort by cid 81 bool IsValid() const; 82 83 private: 84 void AddFieldDefineString(std::string &attrStr) const; 85 void AddIndexDefineString(std::string &attrStr) const; 86 87 int CompareWithTableFields(const std::map<std::string, FieldInfo> &inTableFields) const; 88 int CompareWithTableIndex(const std::map<std::string, CompositeFields> &inTableIndex) const; 89 90 std::string tableName_; 91 bool autoInc_ = false; // only 'INTEGER PRIMARY KEY' could be defined as 'AUTOINCREMENT' 92 std::string sql_; 93 std::map<std::string, FieldInfo> fields_; 94 FieldName primaryKey_; 95 std::map<std::string, CompositeFields> indexDefines_; 96 mutable std::vector<FieldInfo> fieldInfos_; 97 }; 98 99 class RelationalSchemaObject : public ISchema { 100 public: 101 RelationalSchemaObject() = default; 102 ~RelationalSchemaObject() override = default; 103 104 bool IsSchemaValid() const override; 105 106 SchemaType GetSchemaType() const override; 107 108 std::string ToSchemaString() const override; 109 110 // Should be called on an invalid SchemaObject, create new SchemaObject if need to reparse 111 int ParseFromSchemaString(const std::string &inSchemaString) override; 112 113 void AddRelationalTable(const TableInfo& tb); 114 115 void RemoveRelationalTable(const std::string &tableName); 116 117 const std::map<std::string, TableInfo> &GetTables() const; 118 119 std::vector<std::string> GetTableNames() const; 120 121 TableInfo GetTable(const std::string& tableName) const; 122 123 private: 124 int CompareAgainstSchemaObject(const std::string &inSchemaString, std::map<std::string, int> &cmpRst) const; 125 126 int CompareAgainstSchemaObject(const RelationalSchemaObject &inSchemaObject, 127 std::map<std::string, int> &cmpRst) const; 128 129 int ParseRelationalSchema(const JsonObject &inJsonObject); 130 int ParseCheckSchemaType(const JsonObject &inJsonObject); 131 int ParseCheckSchemaVersion(const JsonObject &inJsonObject); 132 int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject); 133 int ParseCheckTableInfo(const JsonObject &inJsonObject); 134 int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable); 135 int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable); 136 int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table); 137 int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable); 138 int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable); 139 int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable); 140 141 void GenerateSchemaString(); 142 143 bool isValid_ = false; // set to true after parse success from string or add at least one relational table 144 SchemaType schemaType_ = SchemaType::RELATIVE; // Default RELATIVE 145 std::string schemaString_; // The minified and valid schemaString 146 std::string schemaVersion_; 147 std::map<std::string, TableInfo> tables_; 148 }; 149 } // namespace DistributedDB 150 #endif // RELATIONAL_STORE 151 #endif // RELATIONAL_SCHEMA_OBJECT_H