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 {
Marshal(Serializable::json & node) const18 bool SchemaMeta::Marshal(Serializable::json &node) const
19 {
20 SetValue(node[GET_NAME(metaVersion)], metaVersion);
21 SetValue(node[GET_NAME(version)], version);
22 SetValue(node[GET_NAME(bundleName)], bundleName);
23 SetValue(node[GET_NAME(databases)], databases);
24 return true;
25 }
26
Unmarshal(const Serializable::json & node)27 bool SchemaMeta::Unmarshal(const Serializable::json &node)
28 {
29 if (!GetValue(node, GET_NAME(metaVersion), metaVersion)) {
30 metaVersion = 0;
31 }
32 GetValue(node, GET_NAME(version), version);
33 GetValue(node, GET_NAME(bundleName), bundleName);
34 GetValue(node, GET_NAME(databases), databases);
35 return true;
36 }
37
GetTableNames() const38 std::vector<std::string> Database::GetTableNames() const
39 {
40 std::vector<std::string> tableNames;
41 tableNames.reserve(tables.size());
42 for (auto &table : tables) {
43 tableNames.push_back(table.name);
44 if (!table.sharedTableName.empty()) {
45 tableNames.push_back(table.sharedTableName);
46 }
47 }
48 return tableNames;
49 }
50
Marshal(Serializable::json & node) const51 bool Database::Marshal(Serializable::json &node) const
52 {
53 SetValue(node[GET_NAME(name)], name);
54 SetValue(node[GET_NAME(alias)], alias);
55 SetValue(node[GET_NAME(tables)], tables);
56 return true;
57 }
58
Unmarshal(const Serializable::json & node)59 bool Database::Unmarshal(const Serializable::json &node)
60 {
61 GetValue(node, GET_NAME(name), name);
62 GetValue(node, GET_NAME(alias), alias);
63 GetValue(node, GET_NAME(tables), tables);
64 return true;
65 }
66
Marshal(Serializable::json & node) const67 bool Table::Marshal(Serializable::json &node) const
68 {
69 SetValue(node[GET_NAME(name)], name);
70 SetValue(node[GET_NAME(sharedTableName)], sharedTableName);
71 SetValue(node[GET_NAME(alias)], alias);
72 SetValue(node[GET_NAME(fields)], fields);
73 return true;
74 }
75
Unmarshal(const Serializable::json & node)76 bool Table::Unmarshal(const Serializable::json &node)
77 {
78 GetValue(node, GET_NAME(name), name);
79 GetValue(node, GET_NAME(sharedTableName), sharedTableName);
80 GetValue(node, GET_NAME(alias), alias);
81 GetValue(node, GET_NAME(fields), fields);
82 return true;
83 }
84
Marshal(Serializable::json & node) const85 bool Field::Marshal(Serializable::json &node) const
86 {
87 SetValue(node[GET_NAME(colName)], colName);
88 SetValue(node[GET_NAME(alias)], alias);
89 SetValue(node[GET_NAME(type)], type);
90 SetValue(node[GET_NAME(primary)], primary);
91 SetValue(node[GET_NAME(nullable)], nullable);
92 return true;
93 }
94
Unmarshal(const Serializable::json & node)95 bool Field::Unmarshal(const Serializable::json &node)
96 {
97 GetValue(node, GET_NAME(colName), colName);
98 GetValue(node, GET_NAME(alias), alias);
99 GetValue(node, GET_NAME(type), type);
100 GetValue(node, GET_NAME(primary), primary);
101 GetValue(node, GET_NAME(nullable), nullable);
102 return true;
103 }
104
GetDataBase(const std::string & storeId)105 Database SchemaMeta::GetDataBase(const std::string &storeId)
106 {
107 for (const auto &database : databases) {
108 if (database.name == storeId) {
109 return database;
110 }
111 }
112 return {};
113 }
114
IsValid() const115 bool SchemaMeta::IsValid() const
116 {
117 return !bundleName.empty() && !databases.empty();
118 }
119 } // namespace OHOS::DistributedData