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 #include "schema_constant.h" 24 #include "table_info.h" 25 26 namespace DistributedDB { 27 class RelationalSchemaObject : public ISchema { 28 public: 29 RelationalSchemaObject() = default; 30 ~RelationalSchemaObject() override = default; 31 32 bool IsSchemaValid() const override; 33 34 SchemaType GetSchemaType() const override; 35 36 std::string ToSchemaString() const override; 37 38 // Should be called on an invalid SchemaObject, create new SchemaObject if need to reparse 39 int ParseFromSchemaString(const std::string &inSchemaString) override; 40 41 void AddRelationalTable(const TableInfo& tb); 42 43 void RemoveRelationalTable(const std::string &tableName); 44 45 const std::map<std::string, TableInfo> &GetTables() const; 46 47 std::vector<std::string> GetTableNames() const; 48 49 TableInfo GetTable(const std::string& tableName) const; 50 51 std::string GetSchemaVersion() const; 52 53 DistributedTableMode GetTableMode() const; 54 void SetTableMode(DistributedTableMode mode); 55 56 private: 57 int CompareAgainstSchemaObject(const std::string &inSchemaString, std::map<std::string, int> &cmpRst) const; 58 59 int CompareAgainstSchemaObject(const RelationalSchemaObject &inSchemaObject, 60 std::map<std::string, int> &cmpRst) const; 61 62 int ParseRelationalSchema(const JsonObject &inJsonObject); 63 int ParseCheckSchemaType(const JsonObject &inJsonObject); 64 int ParseCheckTableMode(const JsonObject &inJsonObject); 65 int ParseCheckSchemaVersion(const JsonObject &inJsonObject); 66 int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject); 67 int ParseCheckTableInfo(const JsonObject &inJsonObject); 68 int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable); 69 int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable); 70 int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table); 71 int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable); 72 int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable); 73 int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable); 74 int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable); 75 76 void GenerateSchemaString(); 77 78 bool isValid_ = false; // set to true after parse success from string or add at least one relational table 79 SchemaType schemaType_ = SchemaType::RELATIVE; // Default RELATIVE 80 std::string schemaString_; // The minified and valid schemaString 81 std::string schemaVersion_ = SchemaConstant::SCHEMA_SUPPORT_VERSION_V2; // Default version 2.0 82 std::map<std::string, TableInfo> tables_; 83 84 DistributedTableMode tableMode_ = DistributedTableMode::SPLIT_BY_DEVICE; 85 }; 86 } // namespace DistributedDB 87 #endif // RELATIONAL_STORE 88 #endif // RELATIONAL_SCHEMA_OBJECT_H