• 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 #include "cloud/schema_meta.h"
17 namespace OHOS::DistributedData {
18 static constexpr const char *KEY_PREFIX = "DistributedSchema";
19 static constexpr const char *KEY_SEPARATOR = "###";
Marshal(Serializable::json & node) const20 bool SchemaMeta::Marshal(Serializable::json &node) const
21 {
22     SetValue(node[GET_NAME(metaVersion)], metaVersion);
23     SetValue(node[GET_NAME(version)], version);
24     SetValue(node[GET_NAME(bundleName)], bundleName);
25     SetValue(node[GET_NAME(databases)], databases);
26     return true;
27 }
28 
Unmarshal(const Serializable::json & node)29 bool SchemaMeta::Unmarshal(const Serializable::json &node)
30 {
31     if (!GetValue(node, GET_NAME(metaVersion), metaVersion)) {
32         metaVersion = 0;
33     }
34     GetValue(node, GET_NAME(version), version);
35     GetValue(node, GET_NAME(bundleName), bundleName);
36     GetValue(node, GET_NAME(databases), databases);
37     GetValue(node, GET_NAME(dbSchema), databases);
38     return true;
39 }
40 
GetTableNames() const41 std::vector<std::string> Database::GetTableNames() const
42 {
43     std::vector<std::string> tableNames;
44     tableNames.reserve(tables.size());
45     for (auto &table : tables) {
46         tableNames.push_back(table.name);
47         if (!table.sharedTableName.empty()) {
48             tableNames.push_back(table.sharedTableName);
49         }
50     }
51     return tableNames;
52 }
53 
GetKey() const54 std::string Database::GetKey() const
55 {
56     return GetKey({user, "default", bundleName, name});
57 }
58 
GetKey(const std::initializer_list<std::string> & fields)59 std::string Database::GetKey(const std::initializer_list<std::string> &fields)
60 {
61     std::string prefix = KEY_PREFIX;
62     for (const auto &field : fields) {
63         prefix.append(KEY_SEPARATOR).append(field);
64     }
65     return prefix;
66 }
67 
GetPrefix(const std::initializer_list<std::string> & fields)68 std::string Database::GetPrefix(const std::initializer_list<std::string> &fields)
69 {
70     return GetKey(fields).append(KEY_SEPARATOR);
71 }
72 
Marshal(Serializable::json & node) const73 bool Database::Marshal(Serializable::json &node) const
74 {
75     SetValue(node[GET_NAME(name)], name);
76     SetValue(node[GET_NAME(alias)], alias);
77     SetValue(node[GET_NAME(tables)], tables);
78     SetValue(node[GET_NAME(version)], version);
79     SetValue(node[GET_NAME(bundleName)], bundleName);
80     SetValue(node[GET_NAME(user)], user);
81     SetValue(node[GET_NAME(deviceId)], deviceId);
82     SetValue(node[GET_NAME(autoSyncType)], autoSyncType);
83     return true;
84 }
85 
Unmarshal(const Serializable::json & node)86 bool Database::Unmarshal(const Serializable::json &node)
87 {
88     GetValue(node, GET_NAME(name), name);
89     GetValue(node, GET_NAME(alias), alias);
90     GetValue(node, GET_NAME(tables), tables);
91     GetValue(node, GET_NAME(dbName), name);
92     GetValue(node, GET_NAME(autoSyncType), autoSyncType);
93     GetValue(node, GET_NAME(user), user);
94     GetValue(node, GET_NAME(deviceId), deviceId);
95     GetValue(node, GET_NAME(version), version);
96     GetValue(node, GET_NAME(bundleName), bundleName);
97     return true;
98 }
99 
Marshal(Serializable::json & node) const100 bool Table::Marshal(Serializable::json &node) const
101 {
102     SetValue(node[GET_NAME(name)], name);
103     SetValue(node[GET_NAME(sharedTableName)], sharedTableName);
104     SetValue(node[GET_NAME(alias)], alias);
105     SetValue(node[GET_NAME(fields)], fields);
106     SetValue(node[GET_NAME(deviceSyncFields)], deviceSyncFields);
107     SetValue(node[GET_NAME(cloudSyncFields)], cloudSyncFields);
108     return true;
109 }
110 
Unmarshal(const Serializable::json & node)111 bool Table::Unmarshal(const Serializable::json &node)
112 {
113     GetValue(node, GET_NAME(name), name);
114     GetValue(node, GET_NAME(sharedTableName), sharedTableName);
115     GetValue(node, GET_NAME(alias), alias);
116     GetValue(node, GET_NAME(fields), fields);
117     GetValue(node, GET_NAME(tableName), name);
118     GetValue(node, GET_NAME(deviceSyncFields), deviceSyncFields);
119     GetValue(node, GET_NAME(cloudSyncFields), cloudSyncFields);
120     return true;
121 }
122 
Marshal(Serializable::json & node) const123 bool Field::Marshal(Serializable::json &node) const
124 {
125     SetValue(node[GET_NAME(colName)], colName);
126     SetValue(node[GET_NAME(alias)], alias);
127     SetValue(node[GET_NAME(type)], type);
128     SetValue(node[GET_NAME(primary)], primary);
129     SetValue(node[GET_NAME(nullable)], nullable);
130     return true;
131 }
132 
Unmarshal(const Serializable::json & node)133 bool Field::Unmarshal(const Serializable::json &node)
134 {
135     GetValue(node, GET_NAME(colName), colName);
136     GetValue(node, GET_NAME(alias), alias);
137     GetValue(node, GET_NAME(type), type);
138     GetValue(node, GET_NAME(primary), primary);
139     GetValue(node, GET_NAME(primaryKey), primary);
140     GetValue(node, GET_NAME(nullable), nullable);
141     GetValue(node, GET_NAME(columnName), colName);
142     GetValue(node, GET_NAME(notNull), nullable);
143     return true;
144 }
145 
GetDataBase(const std::string & storeId)146 Database SchemaMeta::GetDataBase(const std::string &storeId)
147 {
148     for (const auto &database : databases) {
149         if (database.name == storeId) {
150             return database;
151         }
152     }
153     return {};
154 }
155 
IsValid() const156 bool SchemaMeta::IsValid() const
157 {
158     return !bundleName.empty() && !databases.empty();
159 }
160 
GetStores()161 std::vector<std::string> SchemaMeta::GetStores()
162 {
163     std::vector<std::string> stores;
164     for (const auto &it : databases) {
165         stores.push_back(it.name);
166     }
167     return stores;
168 }
169 } // namespace OHOS::DistributedData