• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::vector<std::string> deviceSyncFields = {};
36     std::vector<std::string> cloudSyncFields = {};
37     bool Marshal(json &node) const override;
38     bool Unmarshal(const json &node) override;
39 };
40 
41 struct API_EXPORT Database final : public Serializable {
42     std::string name = "";
43     std::string alias;
44     std::vector<Table> tables;
45     std::vector<std::string> GetTableNames() const;
46     uint32_t autoSyncType = 0;
47     std::string user = "";
48     std::string deviceId = "";
49     uint32_t version = 0;
50     std::string bundleName = "";
51     API_EXPORT std::string GetKey() const;
52     API_EXPORT static std::string GetKey(const std::initializer_list<std::string> &fields);
53     API_EXPORT static std::string GetPrefix(const std::initializer_list<std::string> &fields);
54     bool Marshal(json &node) const override;
55     bool Unmarshal(const json &node) override;
56 };
57 
58 class API_EXPORT SchemaMeta final : public Serializable {
59 public:
60     using Database = Database;
61     using Table = Table;
62     using Field = Field;
63     static constexpr const char *DELETE_FIELD = "#_deleted";
64     static constexpr const char *GID_FIELD = "#_gid";
65     static constexpr const char *CREATE_FIELD = "#_createTime";
66     static constexpr const char *MODIFY_FIELD = "#_modifyTime";
67     static constexpr const char *CURSOR_FIELD = "#_cursor";
68     static constexpr const char *ERROR_FIELD = "#_error";
69     static constexpr const char *VERSION_FIELD = "#_version";
70     static constexpr const char *REFERENCE_FIELD = "#_reference";
71     static constexpr const char *CLOUD_OWNER = "cloud_owner";
72     static constexpr const char *CLOUD_PRIVILEGE = "cloud_privilege";
73     static constexpr const char *SHARING_RESOURCE = "#_sharing_resource";
74     static constexpr const char *HASH_KEY = "#_hash_key";
75 
76     static constexpr uint32_t CURRENT_VERSION = 0x10000;
77     static inline uint32_t GetLowVersion(uint32_t metaVersion = CURRENT_VERSION)
78     {
79         return metaVersion & 0xFFFF;
80     }
81 
82     static inline uint32_t GetHighVersion(uint32_t metaVersion = CURRENT_VERSION)
83     {
84         return metaVersion & ~0xFFFF;
85     }
86     uint32_t metaVersion = CURRENT_VERSION;
87     int32_t version = 0;
88     std::string bundleName;
89     std::vector<Database> databases;
90 
91     bool Marshal(json &node) const override;
92     bool Unmarshal(const json &node) override;
93     bool IsValid() const;
94     Database GetDataBase(const std::string &storeId);
95     std::vector<std::string> GetStores();
96 };
97 
98 // Table mode of device data sync time
99 enum AutoSyncType {
100     IS_NOT_AUTO_SYNC = 0,
101     SYNC_ON_CHANGE = 1, // datachange sync
102     SYNC_ON_READY, // onready sync
103     SYNC_ON_CHANGE_READY //datachange and onready sync
104 };
105 
106 } // namespace OHOS::DistributedData
107 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_SCHEMA_META_H
108