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 #define LOG_TAG "Config"
16 #include "sqlite_global_config.h"
17
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <chrono>
21 #include <cerrno>
22 #include <cinttypes>
23 #include <cstring>
24 #include "logger.h"
25 #include "sqlite3sym.h"
26 #include "sqlite_utils.h"
27 #include "rdb_errno.h"
28
29 namespace OHOS {
30 namespace NativeRdb {
31 using namespace OHOS::Rdb;
32 using namespace std::chrono;
InitSqliteGlobalConfig()33 void SqliteGlobalConfig::InitSqliteGlobalConfig()
34 {
35 static SqliteGlobalConfig globalConfig;
36 }
37
SqliteGlobalConfig()38 SqliteGlobalConfig::SqliteGlobalConfig()
39 {
40 umask(GlobalExpr::APP_DEFAULT_UMASK);
41
42 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
43
44 sqlite3_config(SQLITE_CONFIG_LOG, &Log,
45 GlobalExpr::CALLBACK_LOG_SWITCH ? reinterpret_cast<void *>(1) : NULL);
46
47 sqlite3_soft_heap_limit(GlobalExpr::SOFT_HEAP_LIMIT);
48
49 sqlite3_initialize();
50 }
51
~SqliteGlobalConfig()52 SqliteGlobalConfig::~SqliteGlobalConfig()
53 {
54 }
55
Log(const void * data,int err,const char * msg)56 void SqliteGlobalConfig::Log(const void *data, int err, const char *msg)
57 {
58 bool verboseLog = (data != nullptr);
59 auto errType = static_cast<unsigned int>(err);
60 errType &= 0xFF;
61 if (errType == SQLITE_ERROR && strstr(msg, "\"?\": syntax error in \"PRAGMA user_ve") != nullptr) {
62 return;
63 }
64 if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA || errType == SQLITE_NOTICE
65 || err == SQLITE_WARNING_AUTOINDEX) {
66 if (verboseLog) {
67 LOG_INFO("Error(%{public}d) %{public}s ", err, SqliteUtils::Anonymous(msg).c_str());
68 }
69 } else if (errType == SQLITE_WARNING) {
70 LOG_WARN("WARNING(%{public}d) %{public}s ", err, SqliteUtils::Anonymous(msg).c_str());
71 } else {
72 LOG_ERROR("Error(%{public}d) errno is:%{public}d %{public}s.", err, errno,
73 SqliteUtils::Anonymous(msg).c_str());
74 }
75 }
76
GetMemoryDbPath()77 std::string SqliteGlobalConfig::GetMemoryDbPath()
78 {
79 return GlobalExpr::MEMORY_DB_PATH;
80 }
81
GetPageSize()82 int SqliteGlobalConfig::GetPageSize()
83 {
84 return GlobalExpr::DB_PAGE_SIZE;
85 }
86
GetSyncMode()87 std::string SqliteGlobalConfig::GetSyncMode()
88 {
89 return GlobalExpr::DEFAULE_SYNC_MODE;
90 }
91
GetJournalFileSize()92 int SqliteGlobalConfig::GetJournalFileSize()
93 {
94 return GlobalExpr::DB_JOURNAL_SIZE;
95 }
96
GetWalAutoCheckpoint()97 int SqliteGlobalConfig::GetWalAutoCheckpoint()
98 {
99 return GlobalExpr::WAL_AUTO_CHECKPOINT;
100 }
101
GetDefaultJournalMode()102 std::string SqliteGlobalConfig::GetDefaultJournalMode()
103 {
104 return GlobalExpr::JOURNAL_MODE_WAL;
105 }
106
GetDbPath(const RdbStoreConfig & config,std::string & dbPath)107 int SqliteGlobalConfig::GetDbPath(const RdbStoreConfig &config, std::string &dbPath)
108 {
109 std::string path;
110 if (config.GetRoleType() == VISITOR) {
111 path = config.GetVisitorDir();
112 } else {
113 path = config.GetPath();
114 }
115
116 if (config.GetStorageMode() == StorageMode::MODE_MEMORY) {
117 if (config.GetRoleType() == VISITOR) {
118 LOG_ERROR("not support MODE_MEMORY, storeName:%{public}s, role:%{public}d",
119 SqliteUtils::Anonymous(config.GetName()).c_str(), config.GetRoleType());
120 return E_NOT_SUPPORT;
121 }
122 dbPath = SqliteGlobalConfig::GetMemoryDbPath();
123 } else if (path.empty() || (path.front() != '/' && path.at(1) != ':')) {
124 LOG_ERROR("SqliteConnection GetDbPath input empty database path.");
125 return E_INVALID_FILE_PATH;
126 } else {
127 dbPath = path;
128 }
129 return E_OK;
130 }
131 } // namespace NativeRdb
132 } // namespace OHOS
133