• 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 #include "rdb_store_config.h"
17 
18 #include "logger.h"
19 #include "rdb_errno.h"
20 
21 namespace OHOS::NativeRdb {
22 using namespace OHOS::Rdb;
23 
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,bool autoCheck,int journalSize,int pageSize,const std::string & encryptAlgo)24 RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode, bool isReadOnly,
25     const std::vector<uint8_t> &encryptKey, const std::string &journalMode, const std::string &syncMode,
26     const std::string &databaseFileType, SecurityLevel securityLevel, bool isCreateNecessary, bool autoCheck,
27     int journalSize, int pageSize, const std::string &encryptAlgo)
28     : name(name),
29       path(name),
30       storageMode(storageMode),
31       journalMode(journalMode),
32       syncMode(syncMode),
33       readOnly(isReadOnly),
34       databaseFileType(databaseFileType),
35       encryptKey_(encryptKey),
36       securityLevel(securityLevel),
37       isCreateNecessary_(isCreateNecessary),
38       autoCheck(autoCheck),
39       journalSize(journalSize),
40       pageSize(pageSize),
41       encryptAlgo(encryptAlgo)
42 {
43 }
44 
~RdbStoreConfig()45 RdbStoreConfig::~RdbStoreConfig()
46 {
47     ClearEncryptKey();
48 }
49 
50 /**
51  * Obtains the database name.
52  */
GetName() const53 std::string RdbStoreConfig::GetName() const
54 {
55     return name;
56 }
57 
58 /**
59  * Obtains the database path.
60  */
GetPath() const61 std::string RdbStoreConfig::GetPath() const
62 {
63     return path;
64 }
65 
66 /**
67  * Obtains the storage mode.
68  */
GetStorageMode() const69 StorageMode RdbStoreConfig::GetStorageMode() const
70 {
71     return storageMode;
72 }
73 
74 /**
75  * Obtains the journal mode in this {@code StoreConfig} object.
76  */
GetJournalMode() const77 std::string RdbStoreConfig::GetJournalMode() const
78 {
79     return journalMode;
80 }
81 
82 /**
83  * Obtains the synchronization mode in this {@code StoreConfig} object.
84  */
GetSyncMode() const85 std::string RdbStoreConfig::GetSyncMode() const
86 {
87     return syncMode;
88 }
89 
90 /**
91  * Checks whether the database is read-only.
92  */
IsReadOnly() const93 bool RdbStoreConfig::IsReadOnly() const
94 {
95     return readOnly;
96 }
97 
98 /**
99  * Checks whether the database is memory.
100  */
IsMemoryRdb() const101 bool RdbStoreConfig::IsMemoryRdb() const
102 {
103     return GetStorageMode() == StorageMode::MODE_MEMORY;
104 }
105 
106 /**
107  * Obtains the database file type in this {@code StoreConfig} object.
108  */
GetDatabaseFileType() const109 std::string RdbStoreConfig::GetDatabaseFileType() const
110 {
111     return databaseFileType;
112 }
113 
SetName(std::string name)114 void RdbStoreConfig::SetName(std::string name)
115 {
116     this->name = std::move(name);
117 }
118 
119 /**
120  * Sets the journal mode  for the object.
121  */
SetJournalMode(JournalMode journalMode)122 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
123 {
124     this->journalMode = GetJournalModeValue(journalMode);
125 }
126 
SetDatabaseFileType(DatabaseFileType type)127 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
128 {
129     this->databaseFileType = GetDatabaseFileTypeValue(type);
130 }
131 
132 /**
133  * Sets the path  for the object.
134  */
SetPath(std::string path)135 void RdbStoreConfig::SetPath(std::string path)
136 {
137     this->path = std::move(path);
138 }
139 
SetStorageMode(StorageMode storageMode)140 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
141 {
142     this->storageMode = storageMode;
143 }
144 
IsAutoCheck() const145 bool RdbStoreConfig::IsAutoCheck() const
146 {
147     return autoCheck;
148 }
SetAutoCheck(bool autoCheck)149 void RdbStoreConfig::SetAutoCheck(bool autoCheck)
150 {
151     this->autoCheck = autoCheck;
152 }
GetJournalSize() const153 int RdbStoreConfig::GetJournalSize() const
154 {
155     return journalSize;
156 }
SetJournalSize(int journalSize)157 void RdbStoreConfig::SetJournalSize(int journalSize)
158 {
159     this->journalSize = journalSize;
160 }
GetPageSize() const161 int RdbStoreConfig::GetPageSize() const
162 {
163     return pageSize;
164 }
SetPageSize(int pageSize)165 void RdbStoreConfig::SetPageSize(int pageSize)
166 {
167     this->pageSize = pageSize;
168 }
GetEncryptAlgo() const169 const std::string RdbStoreConfig::GetEncryptAlgo() const
170 {
171     return encryptAlgo;
172 }
SetEncryptAlgo(const std::string & encryptAlgo)173 void RdbStoreConfig::SetEncryptAlgo(const std::string &encryptAlgo)
174 {
175     this->encryptAlgo = encryptAlgo;
176 }
177 
SetReadOnly(bool readOnly)178 void RdbStoreConfig::SetReadOnly(bool readOnly)
179 {
180     this->readOnly = readOnly;
181 }
182 
183 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
SetDistributedType(DistributedType type)184 int RdbStoreConfig::SetDistributedType(DistributedType type)
185 {
186     if (type < DistributedType::RDB_DEVICE_COLLABORATION || type >= DistributedType::RDB_DISTRIBUTED_TYPE_MAX) {
187         LOG_ERROR("type is invalid");
188         return E_ERROR;
189     }
190     distributedType_ = type;
191     return E_OK;
192 }
193 
GetDistributedType() const194 DistributedType RdbStoreConfig::GetDistributedType() const
195 {
196     return distributedType_;
197 }
198 #endif
199 
SetBundleName(const std::string & bundleName)200 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
201 {
202     if (bundleName.empty()) {
203         LOG_ERROR("bundleName is empty");
204         return E_ERROR;
205     }
206     bundleName_ = bundleName;
207     return E_OK;
208 }
209 
GetBundleName() const210 std::string RdbStoreConfig::GetBundleName() const
211 {
212     return bundleName_;
213 }
214 
SetModuleName(const std::string & moduleName)215 void RdbStoreConfig::SetModuleName(const std::string &moduleName)
216 {
217     moduleName_ = moduleName;
218 }
219 
GetModuleName() const220 std::string RdbStoreConfig::GetModuleName() const
221 {
222     return moduleName_;
223 }
224 
SetServiceName(const std::string & serviceName)225 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
226 {
227     SetBundleName(serviceName);
228 }
229 
SetArea(int32_t area)230 void RdbStoreConfig::SetArea(int32_t area)
231 {
232     area_ = area + 1;
233 }
234 
GetArea() const235 int32_t RdbStoreConfig::GetArea() const
236 {
237     return area_;
238 }
239 
GetJournalModeValue(JournalMode journalMode)240 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
241 {
242     std::string value = "";
243 
244     switch (journalMode) {
245         case JournalMode::MODE_DELETE:
246             return "DELETE";
247         case JournalMode::MODE_TRUNCATE:
248             return "TRUNCATE";
249         case JournalMode::MODE_PERSIST:
250             return  "PERSIST";
251         case JournalMode::MODE_MEMORY:
252             return "MEMORY";
253         case JournalMode::MODE_WAL:
254             return "WAL";
255         case JournalMode::MODE_OFF:
256             return "OFF";
257         default:
258             break;
259     }
260     return value;
261 }
262 
GetSyncModeValue(SyncMode syncMode)263 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
264 {
265     std::string value = "";
266     switch (syncMode) {
267         case SyncMode::MODE_OFF:
268             return "MODE_OFF";
269         case SyncMode::MODE_NORMAL:
270             return "MODE_NORMAL";
271         case SyncMode::MODE_FULL:
272             return "MODE_FULL";
273         case SyncMode::MODE_EXTRA:
274             return "MODE_EXTRA";
275         default:
276             break;
277     }
278 
279     return value;
280 }
281 
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)282 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
283 {
284     std::string value = "";
285     switch (databaseFileType) {
286         case DatabaseFileType::NORMAL:
287             return "db";
288         case DatabaseFileType::BACKUP:
289             return "backup";
290         case DatabaseFileType::CORRUPT:
291             return "corrupt";
292         default:
293             break;
294     }
295 
296     return value;
297 }
298 
SetSecurityLevel(SecurityLevel sl)299 void RdbStoreConfig::SetSecurityLevel(SecurityLevel sl)
300 {
301     securityLevel = sl;
302 }
303 
GetSecurityLevel() const304 SecurityLevel RdbStoreConfig::GetSecurityLevel() const
305 {
306     return securityLevel;
307 }
308 
SetEncryptStatus(const bool status)309 void RdbStoreConfig::SetEncryptStatus(const bool status)
310 {
311     this->isEncrypt_ = status;
312 }
313 
IsEncrypt() const314 bool RdbStoreConfig::IsEncrypt() const
315 {
316     return this->isEncrypt_;
317 }
GetUri() const318 std::string RdbStoreConfig::GetUri() const
319 {
320     return uri_;
321 }
322 
SetUri(const std::string & uri)323 void RdbStoreConfig::SetUri(const std::string &uri)
324 {
325     uri_ = uri;
326 }
327 
GetReadPermission() const328 std::string RdbStoreConfig::GetReadPermission() const
329 {
330     return readPermission_;
331 }
332 
SetReadPermission(const std::string & permission)333 void RdbStoreConfig::SetReadPermission(const std::string &permission)
334 {
335     readPermission_ = permission;
336 }
337 
GetWritePermission() const338 std::string RdbStoreConfig::GetWritePermission() const
339 {
340     return writePermission_;
341 }
342 
SetWritePermission(const std::string & permission)343 void RdbStoreConfig::SetWritePermission(const std::string &permission)
344 {
345     writePermission_ = permission;
346 }
IsCreateNecessary() const347 bool RdbStoreConfig::IsCreateNecessary() const
348 {
349     return isCreateNecessary_;
350 }
351 
SetCreateNecessary(bool isCreateNecessary)352 void RdbStoreConfig::SetCreateNecessary(bool isCreateNecessary)
353 {
354     isCreateNecessary_ = isCreateNecessary;
355 }
356 
GetReadConSize() const357 int RdbStoreConfig::GetReadConSize() const
358 {
359     return readConSize_;
360 }
361 
SetReadConSize(int readConSize)362 void RdbStoreConfig::SetReadConSize(int readConSize)
363 {
364     readConSize_= readConSize;
365 }
366 
SetEncryptKey(const std::vector<uint8_t> & encryptKey)367 void RdbStoreConfig::SetEncryptKey(const std::vector<uint8_t> &encryptKey)
368 {
369     encryptKey_ = encryptKey;
370 }
371 
GetEncryptKey() const372 std::vector<uint8_t> RdbStoreConfig::GetEncryptKey() const
373 {
374     return encryptKey_;
375 }
376 
ClearEncryptKey()377 void RdbStoreConfig::ClearEncryptKey()
378 {
379     encryptKey_.assign(encryptKey_.size(), 0);
380 }
381 
SetScalarFunction(const std::string & functionName,int argc,ScalarFunction function)382 void RdbStoreConfig::SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function)
383 {
384     customScalarFunctions.try_emplace(functionName, ScalarFunctionInfo(function, argc));
385 }
386 
GetScalarFunctions() const387 std::map<std::string, ScalarFunctionInfo> RdbStoreConfig::GetScalarFunctions() const
388 {
389     return customScalarFunctions;
390 }
391 
SetDataGroupId(const std::string & DataGroupId)392 void RdbStoreConfig::SetDataGroupId(const std::string &DataGroupId)
393 {
394     dataGroupId_ = DataGroupId;
395 }
396 
GetDataGroupId() const397 std::string RdbStoreConfig::GetDataGroupId() const
398 {
399     return dataGroupId_;
400 }
401 } // namespace OHOS::NativeRdb
402