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 "sqlite_global_config.h"
17
18 #include <sys/stat.h>
19 #include <sys/types.h>
20
21 #include "logger.h"
22 #include "sqlite3sym.h"
23
24 namespace OHOS {
25 namespace NativeRdb {
26 using namespace OHOS::Rdb;
27
InitSqliteGlobalConfig()28 void SqliteGlobalConfig::InitSqliteGlobalConfig()
29 {
30 static SqliteGlobalConfig globalConfig;
31 }
32
SqliteGlobalConfig()33 SqliteGlobalConfig::SqliteGlobalConfig()
34 {
35 umask(GlobalExpr::APP_DEFAULT_UMASK);
36
37 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
38
39 sqlite3_config(SQLITE_CONFIG_LOG, &SqliteLogCallback,
40 GlobalExpr::CALLBACK_LOG_SWITCH ? reinterpret_cast<void *>(1) : NULL);
41
42 sqlite3_soft_heap_limit(GlobalExpr::SOFT_HEAP_LIMIT);
43
44 sqlite3_initialize();
45 }
46
~SqliteGlobalConfig()47 SqliteGlobalConfig::~SqliteGlobalConfig()
48 {
49 }
50
SqliteLogCallback(const void * data,int err,const char * msg)51 void SqliteGlobalConfig::SqliteLogCallback(const void *data, int err, const char *msg)
52 {
53 bool verboseLog = (data != nullptr);
54 auto errType = static_cast<unsigned int>(err);
55 errType &= 0xFF;
56 if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA || errType == SQLITE_NOTICE
57 || err == SQLITE_WARNING_AUTOINDEX) {
58 if (verboseLog) {
59 LOG_INFO("SQLite Error(%{public}d) %{public}s ", err, msg);
60 }
61 } else {
62 LOG_ERROR("SQLite Error(%{public}d) %{public}s", err, msg);
63 }
64 }
65
GetMemoryDbPath()66 std::string SqliteGlobalConfig::GetMemoryDbPath()
67 {
68 return GlobalExpr::MEMORY_DB_PATH;
69 }
70
GetPageSize()71 int SqliteGlobalConfig::GetPageSize()
72 {
73 return GlobalExpr::DB_PAGE_SIZE;
74 }
75
GetWalSyncMode()76 std::string SqliteGlobalConfig::GetWalSyncMode()
77 {
78 return GlobalExpr::WAL_SYNC_MODE;
79 }
80
GetJournalFileSize()81 int SqliteGlobalConfig::GetJournalFileSize()
82 {
83 return GlobalExpr::DB_JOURNAL_SIZE;
84 }
85
GetWalAutoCheckpoint()86 int SqliteGlobalConfig::GetWalAutoCheckpoint()
87 {
88 return GlobalExpr::WAL_AUTO_CHECKPOINT;
89 }
90
GetDefaultJournalMode()91 std::string SqliteGlobalConfig::GetDefaultJournalMode()
92 {
93 return GlobalExpr::DEFAULT_JOURNAL_MODE;
94 }
95 } // namespace NativeRdb
96 } // namespace OHOS
97