• 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     }
41     return tableNames;
42 }
43 
Marshal(Serializable::json & node) const44 bool Database::Marshal(Serializable::json &node) const
45 {
46     SetValue(node[GET_NAME(name)], name);
47     SetValue(node[GET_NAME(alias)], alias);
48     SetValue(node[GET_NAME(tables)], tables);
49     return true;
50 }
51 
Unmarshal(const Serializable::json & node)52 bool Database::Unmarshal(const Serializable::json &node)
53 {
54     GetValue(node, GET_NAME(name), name);
55     GetValue(node, GET_NAME(alias), alias);
56     GetValue(node, GET_NAME(tables), tables);
57     return true;
58 }
59 
Marshal(Serializable::json & node) const60 bool Table::Marshal(Serializable::json &node) const
61 {
62     SetValue(node[GET_NAME(name)], name);
63     SetValue(node[GET_NAME(alias)], alias);
64     SetValue(node[GET_NAME(fields)], fields);
65     return true;
66 }
67 
Unmarshal(const Serializable::json & node)68 bool Table::Unmarshal(const Serializable::json &node)
69 {
70     GetValue(node, GET_NAME(name), name);
71     GetValue(node, GET_NAME(alias), alias);
72     GetValue(node, GET_NAME(fields), fields);
73     return true;
74 }
75 
Marshal(Serializable::json & node) const76 bool Field::Marshal(Serializable::json &node) const
77 {
78     SetValue(node[GET_NAME(colName)], colName);
79     SetValue(node[GET_NAME(alias)], alias);
80     SetValue(node[GET_NAME(type)], type);
81     SetValue(node[GET_NAME(primary)], primary);
82     SetValue(node[GET_NAME(nullable)], nullable);
83     return true;
84 }
85 
Unmarshal(const Serializable::json & node)86 bool Field::Unmarshal(const Serializable::json &node)
87 {
88     GetValue(node, GET_NAME(colName), colName);
89     GetValue(node, GET_NAME(alias), alias);
90     GetValue(node, GET_NAME(type), type);
91     GetValue(node, GET_NAME(primary), primary);
92     GetValue(node, GET_NAME(nullable), nullable);
93     return true;
94 }
95 
GetDataBase(const std::string & storeId)96 Database SchemaMeta::GetDataBase(const std::string &storeId)
97 {
98     for (const auto &database : databases) {
99         if (database.name == storeId) {
100             return database;
101         }
102     }
103     return {};
104 }
105 
IsValid() const106 bool SchemaMeta::IsValid() const
107 {
108     return !bundleName.empty() && !databases.empty();
109 }
110 } // namespace OHOS::DistributedData