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