• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #define LOG_TAG "RdbStoreConfig"
17 
18 #include "rdb_store_config.h"
19 
20 #include "logger.h"
21 #include "rdb_errno.h"
22 
23 namespace OHOS::NativeRdb {
RdbStoreConfig(const RdbStoreConfig & config)24 RdbStoreConfig::RdbStoreConfig(const RdbStoreConfig &config)
25 {
26     name = config.GetName();
27     path = config.GetPath();
28     storageMode = config.GetStorageMode();
29     journalMode = config.GetJournalMode();
30     syncMode = config.GetSyncMode();
31     encryptKey = config.GetEncryptKey();
32     readOnly = config.IsReadOnly();
33     databaseFileType = config.GetDatabaseFileType();
34     databaseFileSecurityLevel = config.GetDatabaseFileSecurityLevel();
35 }
36 
RdbStoreConfig(const std::string & name,StorageMode storageMode,bool isReadOnly,const std::vector<uint8_t> & encryptKey,const std::string & journalMode,const std::string & syncMode,const std::string & databaseFileType,const std::string & databaseFileSecurityLevel)37 RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode, bool isReadOnly,
38     const std::vector<uint8_t> &encryptKey, const std::string &journalMode, const std::string &syncMode,
39     const std::string &databaseFileType, const std::string &databaseFileSecurityLevel)
40     : name(name),
41       path(name),
42       storageMode(storageMode),
43       journalMode(journalMode),
44       syncMode(syncMode),
45       encryptKey(encryptKey),
46       readOnly(isReadOnly),
47       databaseFileType(databaseFileType),
48       databaseFileSecurityLevel(databaseFileSecurityLevel)
49 {}
50 
~RdbStoreConfig()51 RdbStoreConfig::~RdbStoreConfig()
52 {
53     ClearEncryptKey();
54 }
55 
56 /**
57  * Obtains the database name.
58  */
GetName() const59 std::string RdbStoreConfig::GetName() const
60 {
61     return name;
62 }
63 
64 /**
65  * Obtains the database path.
66  */
GetPath() const67 std::string RdbStoreConfig::GetPath() const
68 {
69     return path;
70 }
71 
72 /**
73  * Obtains the storage mode.
74  */
GetStorageMode() const75 StorageMode RdbStoreConfig::GetStorageMode() const
76 {
77     return storageMode;
78 }
79 
80 /**
81  * Obtains the journal mode in this {@code StoreConfig} object.
82  */
GetJournalMode() const83 std::string RdbStoreConfig::GetJournalMode() const
84 {
85     return journalMode;
86 }
87 
88 /**
89  * Obtains the synchronization mode in this {@code StoreConfig} object.
90  */
GetSyncMode() const91 std::string RdbStoreConfig::GetSyncMode() const
92 {
93     return syncMode;
94 }
95 
96 /**
97  * Obtains the encrypt key in this {@code StoreConfig} object.
98  */
GetEncryptKey() const99 std::vector<uint8_t> RdbStoreConfig::GetEncryptKey() const
100 {
101     return encryptKey;
102 }
103 
104 /**
105  * Sets the encrypt key for the object.
106  */
SetEncryptKey(const std::vector<uint8_t> & encryptKey)107 void RdbStoreConfig::SetEncryptKey(const std::vector<uint8_t> &encryptKey)
108 {
109     this->encryptKey = encryptKey;
110 }
111 
112 /**
113  * Checks whether the database is read-only.
114  */
IsReadOnly() const115 bool RdbStoreConfig::IsReadOnly() const
116 {
117     return readOnly;
118 }
119 
120 /**
121  * Checks whether the database is memory.
122  */
IsMemoryRdb() const123 bool RdbStoreConfig::IsMemoryRdb() const
124 {
125     return GetStorageMode() == StorageMode::MODE_MEMORY;
126 }
127 
128 /**
129  * Obtains the database file type in this {@code StoreConfig} object.
130  */
GetDatabaseFileType() const131 std::string RdbStoreConfig::GetDatabaseFileType() const
132 {
133     return databaseFileType;
134 }
135 
136 /**
137  * Obtains the security level of the database file.
138  */
GetDatabaseFileSecurityLevel() const139 std::string RdbStoreConfig::GetDatabaseFileSecurityLevel() const
140 {
141     return databaseFileSecurityLevel;
142 }
143 
SetName(const std::string & name)144 void RdbStoreConfig::SetName(const std::string &name)
145 {
146     this->name = name;
147 }
148 
149 /**
150  * Sets the journal mode  for the object.
151  */
SetJournalMode(JournalMode journalMode)152 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
153 {
154     this->journalMode = GetJournalModeValue(journalMode);
155 }
156 
SetDatabaseFileType(DatabaseFileType type)157 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
158 {
159     this->databaseFileType = GetDatabaseFileTypeValue(type);
160 }
161 
162 /**
163  * Sets the path  for the object.
164  */
SetPath(std::string path)165 void RdbStoreConfig::SetPath(std::string path)
166 {
167     this->path = path;
168 }
169 
SetStorageMode(StorageMode storageMode)170 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
171 {
172     this->storageMode = storageMode;
173 }
174 
SetReadOnly(bool readOnly)175 void RdbStoreConfig::SetReadOnly(bool readOnly)
176 {
177     this->readOnly = readOnly;
178 }
179 
ClearEncryptKey()180 void RdbStoreConfig::ClearEncryptKey()
181 {
182     std::fill(encryptKey.begin(), encryptKey.end(), 0);
183     encryptKey.clear();
184 }
185 
SetDistributedType(DistributedType type)186 int RdbStoreConfig::SetDistributedType(DistributedType type)
187 {
188     if (type < DistributedType::RDB_DEVICE_COLLABORATION || type >= DistributedType::RDB_DISTRIBUTED_TYPE_MAX) {
189         LOG_ERROR("type is invalid");
190         return E_ERROR;
191     }
192     distributedType_ = type;
193     return E_OK;
194 }
195 
GetDistributedType() const196 DistributedType RdbStoreConfig::GetDistributedType() const
197 {
198     return distributedType_;
199 }
200 
SetBundleName(const std::string & bundleName)201 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
202 {
203     if (bundleName.empty()) {
204         LOG_ERROR("bundleName is empty");
205         return E_ERROR;
206     }
207     bundleName_ = bundleName;
208     return E_OK;
209 }
210 
GetBundleName() const211 std::string RdbStoreConfig::GetBundleName() const
212 {
213     return bundleName_;
214 }
215 
SetRelativePath(const std::string & relativePath)216 void RdbStoreConfig::SetRelativePath(const std::string &relativePath)
217 {
218     relativePath_ = relativePath;
219 }
220 
GetRelativePath() const221 std::string RdbStoreConfig::GetRelativePath() const
222 {
223     return relativePath_;
224 }
225 
SetAppModuleName(const std::string & moduleName)226 void RdbStoreConfig::SetAppModuleName(const std::string &moduleName)
227 {
228     moduleName_ = moduleName;
229 }
230 
GetAppModuleName() const231 std::string RdbStoreConfig::GetAppModuleName() const
232 {
233     return moduleName_;
234 }
235 
SetServiceName(const std::string & serviceName)236 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
237 {
238     SetBundleName(serviceName);
239 }
240 
SetEncryptLevel(const std::string & secLevel)241 void RdbStoreConfig::SetEncryptLevel(const std::string &secLevel)
242 {
243     encryptLevel_ = secLevel;
244 }
245 
GetEncryptLevel() const246 std::string RdbStoreConfig::GetEncryptLevel() const
247 {
248     return encryptLevel_;
249 }
250 
GetJournalModeValue(JournalMode journalMode)251 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
252 {
253     std::string value = "";
254 
255     switch (journalMode) {
256         case JournalMode::MODE_DELETE:
257             return "DELETE";
258         case JournalMode::MODE_TRUNCATE:
259             return "TRUNCATE";
260         case JournalMode::MODE_PERSIST:
261             return  "PERSIST";
262         case JournalMode::MODE_MEMORY:
263             return "MEMORY";
264         case JournalMode::MODE_WAL:
265             return "WAL";
266         case JournalMode::MODE_OFF:
267             return "OFF";
268         default:
269             break;
270     }
271     return value;
272 }
273 
GetSyncModeValue(SyncMode syncMode)274 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
275 {
276     std::string value = "";
277     switch (syncMode) {
278         case SyncMode::MODE_OFF:
279             return "MODE_OFF";
280         case SyncMode::MODE_NORMAL:
281             return "MODE_NORMAL";
282         case SyncMode::MODE_FULL:
283             return "MODE_FULL";
284         case SyncMode::MODE_EXTRA:
285             return "MODE_EXTRA";
286         default:
287             break;
288     }
289 
290     return value;
291 }
292 
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)293 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
294 {
295     std::string value = "";
296     switch (databaseFileType) {
297         case DatabaseFileType::NORMAL:
298             return "db";
299         case DatabaseFileType::BACKUP:
300             return "backup";
301         case DatabaseFileType::CORRUPT:
302             return "corrupt";
303         default:
304             break;
305     }
306 
307     return value;
308 }
309 
GetDatabaseFileSecurityLevelValue(DatabaseFileSecurityLevel databaseFileSecurityLevel)310 std::string RdbStoreConfig::GetDatabaseFileSecurityLevelValue(DatabaseFileSecurityLevel databaseFileSecurityLevel)
311 {
312     std::string value = "";
313 
314     switch (databaseFileSecurityLevel) {
315         case DatabaseFileSecurityLevel::S4:
316             return "S4";
317         case DatabaseFileSecurityLevel::S3:
318             return "S3";
319         case DatabaseFileSecurityLevel::S2:
320             return "S2";
321         case DatabaseFileSecurityLevel::S1:
322             return "S1";
323         case DatabaseFileSecurityLevel::S0:
324             return "S0";
325         case DatabaseFileSecurityLevel::NO_LEVEL:
326             return "NO_LEVEL";
327         default:
328             break;
329     }
330 
331     return value;
332 }
333 }
334