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