• 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     readOnly = config.IsReadOnly();
32     databaseFileType = config.GetDatabaseFileType();
33     securityLevel = config.GetSecurityLevel();
34     isCreateNecessary_ = config.IsCreateNecessary();
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,SecurityLevel securityLevel,bool isCreateNecessary)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, SecurityLevel securityLevel, bool isCreateNecessary)
40     : name(name),
41       path(name),
42       storageMode(storageMode),
43       journalMode(journalMode),
44       syncMode(syncMode),
45       readOnly(isReadOnly),
46       databaseFileType(databaseFileType),
47       securityLevel(securityLevel),
48       isCreateNecessary_(isCreateNecessary)
49 {
50 }
51 
52 RdbStoreConfig::~RdbStoreConfig() = default;
53 
54 /**
55  * Obtains the database name.
56  */
GetName() const57 std::string RdbStoreConfig::GetName() const
58 {
59     return name;
60 }
61 
62 /**
63  * Obtains the database path.
64  */
GetPath() const65 std::string RdbStoreConfig::GetPath() const
66 {
67     return path;
68 }
69 
70 /**
71  * Obtains the storage mode.
72  */
GetStorageMode() const73 StorageMode RdbStoreConfig::GetStorageMode() const
74 {
75     return storageMode;
76 }
77 
78 /**
79  * Obtains the journal mode in this {@code StoreConfig} object.
80  */
GetJournalMode() const81 std::string RdbStoreConfig::GetJournalMode() const
82 {
83     return journalMode;
84 }
85 
86 /**
87  * Obtains the synchronization mode in this {@code StoreConfig} object.
88  */
GetSyncMode() const89 std::string RdbStoreConfig::GetSyncMode() const
90 {
91     return syncMode;
92 }
93 
94 /**
95  * Checks whether the database is read-only.
96  */
IsReadOnly() const97 bool RdbStoreConfig::IsReadOnly() const
98 {
99     return readOnly;
100 }
101 
102 /**
103  * Checks whether the database is memory.
104  */
IsMemoryRdb() const105 bool RdbStoreConfig::IsMemoryRdb() const
106 {
107     return GetStorageMode() == StorageMode::MODE_MEMORY;
108 }
109 
110 /**
111  * Obtains the database file type in this {@code StoreConfig} object.
112  */
GetDatabaseFileType() const113 std::string RdbStoreConfig::GetDatabaseFileType() const
114 {
115     return databaseFileType;
116 }
117 
SetName(std::string name)118 void RdbStoreConfig::SetName(std::string name)
119 {
120     this->name = std::move(name);
121 }
122 
123 /**
124  * Sets the journal mode  for the object.
125  */
SetJournalMode(JournalMode journalMode)126 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
127 {
128     this->journalMode = GetJournalModeValue(journalMode);
129 }
130 
SetDatabaseFileType(DatabaseFileType type)131 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
132 {
133     this->databaseFileType = GetDatabaseFileTypeValue(type);
134 }
135 
136 /**
137  * Sets the path  for the object.
138  */
SetPath(std::string path)139 void RdbStoreConfig::SetPath(std::string path)
140 {
141     this->path = std::move(path);
142 }
143 
SetStorageMode(StorageMode storageMode)144 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
145 {
146     this->storageMode = storageMode;
147 }
148 
SetReadOnly(bool readOnly)149 void RdbStoreConfig::SetReadOnly(bool readOnly)
150 {
151     this->readOnly = readOnly;
152 }
153 
154 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
SetDistributedType(DistributedType type)155 int RdbStoreConfig::SetDistributedType(DistributedType type)
156 {
157     if (type < DistributedType::RDB_DEVICE_COLLABORATION || type >= DistributedType::RDB_DISTRIBUTED_TYPE_MAX) {
158         LOG_ERROR("type is invalid");
159         return E_ERROR;
160     }
161     distributedType_ = type;
162     return E_OK;
163 }
164 
GetDistributedType() const165 DistributedType RdbStoreConfig::GetDistributedType() const
166 {
167     return distributedType_;
168 }
169 #endif
170 
SetBundleName(const std::string & bundleName)171 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
172 {
173     if (bundleName.empty()) {
174         LOG_ERROR("bundleName is empty");
175         return E_ERROR;
176     }
177     bundleName_ = bundleName;
178     return E_OK;
179 }
180 
GetBundleName() const181 std::string RdbStoreConfig::GetBundleName() const
182 {
183     return bundleName_;
184 }
185 
SetModuleName(const std::string & moduleName)186 void RdbStoreConfig::SetModuleName(const std::string &moduleName)
187 {
188     moduleName_ = moduleName;
189 }
190 
GetModuleName() const191 std::string RdbStoreConfig::GetModuleName() const
192 {
193     return moduleName_;
194 }
195 
SetServiceName(const std::string & serviceName)196 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
197 {
198     SetBundleName(serviceName);
199 }
200 
SetArea(int32_t area)201 void RdbStoreConfig::SetArea(int32_t area)
202 {
203     area_ = area + 1;
204 }
205 
GetArea() const206 int32_t RdbStoreConfig::GetArea() const
207 {
208     return area_;
209 }
210 
GetJournalModeValue(JournalMode journalMode)211 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
212 {
213     std::string value = "";
214 
215     switch (journalMode) {
216         case JournalMode::MODE_DELETE:
217             return "DELETE";
218         case JournalMode::MODE_TRUNCATE:
219             return "TRUNCATE";
220         case JournalMode::MODE_PERSIST:
221             return  "PERSIST";
222         case JournalMode::MODE_MEMORY:
223             return "MEMORY";
224         case JournalMode::MODE_WAL:
225             return "WAL";
226         case JournalMode::MODE_OFF:
227             return "OFF";
228         default:
229             break;
230     }
231     return value;
232 }
233 
GetSyncModeValue(SyncMode syncMode)234 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
235 {
236     std::string value = "";
237     switch (syncMode) {
238         case SyncMode::MODE_OFF:
239             return "MODE_OFF";
240         case SyncMode::MODE_NORMAL:
241             return "MODE_NORMAL";
242         case SyncMode::MODE_FULL:
243             return "MODE_FULL";
244         case SyncMode::MODE_EXTRA:
245             return "MODE_EXTRA";
246         default:
247             break;
248     }
249 
250     return value;
251 }
252 
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)253 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
254 {
255     std::string value = "";
256     switch (databaseFileType) {
257         case DatabaseFileType::NORMAL:
258             return "db";
259         case DatabaseFileType::BACKUP:
260             return "backup";
261         case DatabaseFileType::CORRUPT:
262             return "corrupt";
263         default:
264             break;
265     }
266 
267     return value;
268 }
269 
SetSecurityLevel(SecurityLevel sl)270 void RdbStoreConfig::SetSecurityLevel(SecurityLevel sl)
271 {
272     securityLevel = sl;
273 }
274 
GetSecurityLevel() const275 SecurityLevel RdbStoreConfig::GetSecurityLevel() const
276 {
277     return securityLevel;
278 }
279 
SetEncryptStatus(const bool status)280 void RdbStoreConfig::SetEncryptStatus(const bool status)
281 {
282     this->isEncrypt_ = status;
283 }
284 
IsEncrypt() const285 bool RdbStoreConfig::IsEncrypt() const
286 {
287     return this->isEncrypt_;
288 }
GetUri() const289 std::string RdbStoreConfig::GetUri() const
290 {
291     return uri_;
292 }
293 
SetUri(const std::string & uri)294 void RdbStoreConfig::SetUri(const std::string &uri)
295 {
296     uri_ = uri;
297 }
298 
GetReadPermission() const299 std::string RdbStoreConfig::GetReadPermission() const
300 {
301     return readPermission_;
302 }
303 
SetReadPermission(const std::string & permission)304 void RdbStoreConfig::SetReadPermission(const std::string &permission)
305 {
306     readPermission_ = permission;
307 }
308 
GetWritePermission() const309 std::string RdbStoreConfig::GetWritePermission() const
310 {
311     return writePermission_;
312 }
313 
SetWritePermission(const std::string & permission)314 void RdbStoreConfig::SetWritePermission(const std::string &permission)
315 {
316     writePermission_ = permission;
317 }
IsCreateNecessary() const318 bool RdbStoreConfig::IsCreateNecessary() const
319 {
320     return isCreateNecessary_;
321 }
322 
SetCreateNecessary(bool isCreateNecessary)323 void RdbStoreConfig::SetCreateNecessary(bool isCreateNecessary)
324 {
325     isCreateNecessary_ = isCreateNecessary;
326 }
327 } // namespace OHOS::NativeRdb
328