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 int CLEAR_MEMORY_SIZE = 1024 * 1024; /* default file size : 1M */ 33 static constexpr ssize_t DB_WAL_SIZE_LIMIT_MIN = 20 * 1024 * 1024; /* default wal file maximum size : 20M */ 34 static constexpr ssize_t DB_WAL_WARNING_SIZE = 256 * 1024 * 1024; /* default wal file maximum size : 256M */ 35 static constexpr ssize_t DB_WAL_DEFAULT_SIZE = 0x20000000; /* default wal file size 512M */ 36 static constexpr ssize_t DB_WAL_SIZE_LIMIT_MAX = 0x7FFFFFFF; /* default wal file maximum size : 2G - 1 */ 37 static constexpr int WAL_AUTO_CHECKPOINT = 100; /* 100 pages */ 38 static constexpr int APP_DEFAULT_UMASK = 0002; 39 static constexpr int SQLITE_MAX_COLUMN = 2000; 40 static constexpr char ATTACH_BACKUP_SQL[] = "ATTACH ? AS backup KEY ?"; 41 static constexpr char ATTACH_WITH_KEY_SQL[] = "ATTACH DATABASE ? AS ? KEY ?"; 42 static constexpr char ATTACH_SQL[] = "ATTACH DATABASE ? AS ?"; 43 static constexpr char DETACH_SQL[] = "DETACH DATABASE ?"; 44 static constexpr char EXPORT_SQL[] = "SELECT export_database('backup')"; 45 static constexpr char DETACH_BACKUP_SQL[] = "detach backup"; 46 static constexpr char PRAGMA_JOUR_MODE_EXP[] = "PRAGMA journal_mode"; 47 static constexpr char PRAGMA_BACKUP_JOUR_MODE_WAL[] = "PRAGMA backup.journal_mode=WAL"; 48 static constexpr char PRAGMA_VERSION[] = "PRAGMA user_version"; 49 static constexpr char JOURNAL_MODE_WAL[] = "WAL"; 50 static constexpr char DEFAULE_SYNC_MODE[] = "FULL"; 51 static constexpr char MEMORY_DB_PATH[] = ":memory:"; 52 static constexpr char SHARED_MEMORY_DB_PATH_PREFIX[] = "file:"; 53 static constexpr char SHARED_MEMORY_DB_PATH_SUFFIX[] = "?mode=memory&cache=shared"; 54 static constexpr char CODEC_HMAC_ALGO[] = "PRAGMA codec_hmac_algo=sha256"; 55 static constexpr char CODEC_HMAC_ALGO_PREFIX[] = "PRAGMA codec_hmac_algo='"; 56 static constexpr char CODEC_KDF_ALGO_PREFIX[] = "PRAGMA codec_kdf_algo='"; 57 static constexpr char CODEC_PAGE_SIZE_PREFIX[] = "PRAGMA codec_page_size="; 58 static constexpr char CODEC_REKEY_HMAC_ALGO[] = "PRAGMA codec_rekey_hmac_algo=sha256"; 59 static constexpr char CIPHER_DEFAULT_ALGO[] = "PRAGMA codec_cipher='aes-256-gcm'"; 60 static constexpr char CIPHER_ALGO_PREFIX[] = "PRAGMA codec_cipher='"; 61 static constexpr char ALGO_SUFFIX[] = "'"; 62 static constexpr char CIPHER_KDF_ITER[] = "PRAGMA codec_kdf_iter="; 63 static constexpr char CIPHER_DEFAULT_ATTACH_HMAC_ALGO[] = "PRAGMA cipher_default_attach_hmac_algo=sha256"; 64 static constexpr char CIPHER_DEFAULT_ATTACH_CIPHER_PREFIX[] = "PRAGMA cipher_default_attach_cipher='"; 65 static constexpr char CIPHER_DEFAULT_ATTACH_HMAC_ALGO_PREFIX[] = "PRAGMA cipher_default_attach_hmac_algo='"; 66 static constexpr char CIPHER_DEFAULT_ATTACH_KDF_ALGO_PREFIX[] = "PRAGMA cipher_default_attach_kdf_algo='"; 67 static constexpr char CIPHER_DEFAULT_ATTACH_PAGE_SIZE_PREFIX[] = "PRAGMA cipher_default_attach_page_size="; 68 static constexpr char CIPHER_DEFAULT_ATTACH_KDF_ITER_PREFIX[] = "PRAGMA cipher_default_attach_kdf_iter="; 69 }; 70 71 class SqliteGlobalConfig { 72 public: 73 SqliteGlobalConfig(); 74 ~SqliteGlobalConfig(); 75 static void InitSqliteGlobalConfig(); 76 static void Log(const void *data, int err, const char *msg); 77 static std::string GetMemoryDbPath(); 78 static std::string GetSharedMemoryDbPath(const std::string &name); 79 static int GetPageSize(); 80 static std::string GetSyncMode(); 81 static int GetJournalFileSize(); 82 static int GetWalAutoCheckpoint(); 83 static std::string GetDefaultJournalMode(); 84 static int GetDbPath(const RdbStoreConfig &config, std::string &dbPath); 85 static void Corruption(void *arg, const void *msg); 86 static std::string GetLastCorruptionMsg(); 87 88 private: 89 static void SqliteErrReport(int err, const char *msg); 90 }; 91 92 } // namespace NativeRdb 93 } // namespace OHOS 94 95 #endif 96