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 #ifndef NATIVE_RDB_SQLITE_GLOBAL_CONFIG_H
17 #define NATIVE_RDB_SQLITE_GLOBAL_CONFIG_H
18 
19 #include <string>
20 
21 #include "rdb_store_config.h"
22 
23 namespace OHOS {
24 namespace NativeRdb {
25 
26 class GlobalExpr {
27 public:
28     static constexpr bool CALLBACK_LOG_SWITCH = true;      /* Sqlite callback log switch */
29     static constexpr int SOFT_HEAP_LIMIT = 8 * 1024 * 1024; /* 8MB */
30     static constexpr int DB_PAGE_SIZE = 4096;    /* default page size : 4k */
31     static constexpr int DB_JOURNAL_SIZE = 1024 * 1024; /* default file size : 1M */
32     static constexpr ssize_t DB_WAL_SIZE_LIMIT_MIN = 20 * 1024 * 1024; /* default wal file maximum size : 20M */
33     static constexpr ssize_t DB_WAL_WARNING_SIZE = 256 * 1024 * 1024; /* default wal file maximum size : 256M */
34     static constexpr ssize_t DB_WAL_DEFAULT_SIZE = 0x20000000; /* default wal file size 512M */
35     static constexpr ssize_t DB_WAL_SIZE_LIMIT_MAX = 0x7FFFFFFF; /* default wal file maximum size : 2G - 1 */
36     static constexpr int WAL_AUTO_CHECKPOINT = 100;  /* 100 pages */
37     static constexpr int APP_DEFAULT_UMASK = 0002;
38     static constexpr int SQLITE_MAX_COLUMN = 2000;
39     static constexpr char ATTACH_BACKUP_SQL[] = "ATTACH ? AS backup KEY ?";
40     static constexpr char ATTACH_WITH_KEY_SQL[] = "ATTACH DATABASE ? AS ? KEY ?";
41     static constexpr char ATTACH_SQL[] = "ATTACH DATABASE ? AS ?";
42     static constexpr char DETACH_SQL[] = "DETACH DATABASE ?";
43     static constexpr char EXPORT_SQL[] = "SELECT export_database('backup')";
44     static constexpr char DETACH_BACKUP_SQL[] = "detach backup";
45     static constexpr char PRAGMA_JOUR_MODE_EXP[] = "PRAGMA journal_mode";
46     static constexpr char PRAGMA_BACKUP_JOUR_MODE_WAL[] = "PRAGMA backup.journal_mode=WAL";
47     static constexpr char PRAGMA_VERSION[] = "PRAGMA user_version";
48     static constexpr char JOURNAL_MODE_WAL[] = "WAL";
49     static constexpr char DEFAULE_SYNC_MODE[] = "FULL";
50     static constexpr char MEMORY_DB_PATH[] = ":memory:";
51     static constexpr char CODEC_HMAC_ALGO[] = "PRAGMA codec_hmac_algo=sha256";
52     static constexpr char CODEC_HMAC_ALGO_PREFIX[] = "PRAGMA codec_hmac_algo='";
53     static constexpr char CODEC_KDF_ALGO_PREFIX[] = "PRAGMA codec_kdf_algo='";
54     static constexpr char CODEC_PAGE_SIZE_PREFIX[] = "PRAGMA codec_page_size=";
55     static constexpr char CODEC_REKEY_HMAC_ALGO[] = "PRAGMA codec_rekey_hmac_algo=sha256";
56     static constexpr char CIPHER_DEFAULT_ALGO[] = "PRAGMA codec_cipher='aes-256-gcm'";
57     static constexpr char CIPHER_ALGO_PREFIX[] = "PRAGMA codec_cipher='";
58     static constexpr char ALGO_SUFFIX[] = "'";
59     static constexpr char CIPHER_KDF_ITER[] = "PRAGMA codec_kdf_iter=";
60     static constexpr char CIPHER_DEFAULT_ATTACH_HMAC_ALGO[] = "PRAGMA cipher_default_attach_hmac_algo=sha256";
61     static constexpr char CIPHER_DEFAULT_ATTACH_CIPHER_PREFIX[] = "PRAGMA cipher_default_attach_cipher='";
62     static constexpr char CIPHER_DEFAULT_ATTACH_HMAC_ALGO_PREFIX[] = "PRAGMA cipher_default_attach_hmac_algo='";
63     static constexpr char CIPHER_DEFAULT_ATTACH_KDF_ALGO_PREFIX[] = "PRAGMA cipher_default_attach_kdf_algo='";
64     static constexpr char CIPHER_DEFAULT_ATTACH_PAGE_SIZE_PREFIX[] = "PRAGMA cipher_default_attach_page_size=";
65     static constexpr char CIPHER_DEFAULT_ATTACH_KDF_ITER_PREFIX[] = "PRAGMA cipher_default_attach_kdf_iter=";
66 };
67 
68 class SqliteGlobalConfig {
69 public:
70     SqliteGlobalConfig();
71     ~SqliteGlobalConfig();
72     static void InitSqliteGlobalConfig();
73     static void Log(const void *data, int err, const char *msg);
74     static std::string GetMemoryDbPath();
75     static int GetPageSize();
76     static std::string GetSyncMode();
77     static int GetJournalFileSize();
78     static int GetWalAutoCheckpoint();
79     static std::string GetDefaultJournalMode();
80     static int GetDbPath(const RdbStoreConfig &config, std::string &dbPath);
81 };
82 
83 } // namespace NativeRdb
84 } // namespace OHOS
85 
86 #endif
87