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