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 
16 #ifndef I_SCHEMA_H
17 #define I_SCHEMA_H
18 
19 #include <string>
20 
21 #include "db_types.h"
22 
23 namespace DistributedDB {
24 // SchemaType::NONE represent for KV database which do not have schema. Only invalid SchemaObject is NONE type.
25 // Enum value must not be changed except SchemaType::UNRECOGNIZED.
26 enum class SchemaType : uint8_t {
27     NONE = 0,
28     JSON = 1,
29     FLATBUFFER = 2,
30     RELATIVE = 3,
31     UNRECOGNIZED = 4
32 };
33 
ReadSchemaType(uint8_t inType)34 inline SchemaType ReadSchemaType(uint8_t inType)
35 {
36     if (inType >= static_cast<uint8_t>(SchemaType::UNRECOGNIZED)) {
37         return SchemaType::UNRECOGNIZED;
38     }
39     return static_cast<SchemaType>(inType);
40 }
41 
42 struct SchemaAttribute {
43     FieldType type = FieldType::LEAF_FIELD_NULL;
44     bool isIndexable = false;
45     bool hasNotNullConstraint = false;
46     bool hasDefaultValue = false;
47     FieldValue defaultValue; // Has default value in union part and default construction in string part
48     std::string customFieldType {}; // Custom field type like BIGINT, DECIMAL, CHARACTER ...
49 };
50 
51 class ISchema {
52 public:
53     ISchema() = default;
54     virtual ~ISchema() = default;
55     virtual int ParseFromSchemaString(const std::string &inSchemaString) = 0;
56     virtual bool IsSchemaValid() const = 0;
57     virtual SchemaType GetSchemaType() const = 0;
58     virtual std::string ToSchemaString() const = 0;
59 };
60 }
61 #endif // I_SCHEMA_H