1 /*
2  * Copyright (c) 2023 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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H
18 #include "serializable/serializable.h"
19 namespace OHOS::DistributedData {
20 struct API_EXPORT Field final : public Serializable {
21     std::string colName;
22     std::string alias;
23     int32_t type = 0;
24     bool primary = false;
25     bool nullable = true;
26     bool Marshal(json &node) const override;
27     bool Unmarshal(const json &node) override;
28 };
29 
30 struct API_EXPORT Table final : public Serializable {
31     std::string name;
32     std::string sharedTableName;
33     std::string alias;
34     std::vector<Field> fields;
35     bool Marshal(json &node) const override;
36     bool Unmarshal(const json &node) override;
37 };
38 
39 struct API_EXPORT Database final : public Serializable {
40     std::string name = "";
41     std::string alias;
42     std::vector<Table> tables;
43     std::vector<std::string> GetTableNames() const;
44     bool Marshal(json &node) const override;
45     bool Unmarshal(const json &node) override;
46 };
47 
48 class API_EXPORT SchemaMeta final : public Serializable {
49 public:
50     using Database = Database;
51     using Table = Table;
52     using Field = Field;
53     static constexpr const char *DELETE_FIELD = "#_deleted";
54     static constexpr const char *GID_FIELD = "#_gid";
55     static constexpr const char *CREATE_FIELD = "#_createTime";
56     static constexpr const char *MODIFY_FIELD = "#_modifyTime";
57     static constexpr const char *CURSOR_FIELD = "#_cursor";
58     static constexpr const char *ERROR_FIELD = "#_error";
59     static constexpr const char *VERSION_FIELD = "#_version";
60     static constexpr const char *REFERENCE_FIELD = "#_reference";
61     static constexpr const char *CLOUD_OWNER = "cloud_owner";
62     static constexpr const char *CLOUD_PRIVILEGE = "cloud_privilege";
63     static constexpr const char *SHARING_RESOURCE = "#_sharing_resource";
64     static constexpr const char *HASH_KEY = "#_hash_key";
65 
66     static constexpr uint32_t CURRENT_VERSION = 0x10000;
67     static inline uint32_t GetLowVersion(uint32_t metaVersion = CURRENT_VERSION)
68     {
69         return metaVersion & 0xFFFF;
70     }
71 
72     static inline uint32_t GetHighVersion(uint32_t metaVersion = CURRENT_VERSION)
73     {
74         return metaVersion & ~0xFFFF;
75     }
76     uint32_t metaVersion = CURRENT_VERSION;
77     int32_t version = 0;
78     std::string bundleName;
79     std::vector<Database> databases;
80 
81     bool Marshal(json &node) const override;
82     bool Unmarshal(const json &node) override;
83     bool IsValid() const;
84     Database GetDataBase(const std::string &storeId);
85 };
86 } // namespace OHOS::DistributedData
87 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H
88