userver: userver/storages/sqlite/options.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
options.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/sqlite/options.hpp
4/// @brief SQLite options
5
6#include <string>
7
8#include <userver/components/component_config.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace storages::sqlite::settings {
13
14/// @brief SQLite transaction options.
16 /// @brief Locking mode for the transaction.
17 ///
18 /// Determines how database locks are acquired during the transaction.
19 /// @see https://www.sqlite.org/lang_transaction.html
21 kDeferred, ///< Locks are acquired as needed (default behavior).
22 kImmediate, ///< Acquires a reserved lock immediately.
23 kExclusive ///< Acquires an exclusive lock immediately.
24 };
25
26 /// @brief Isolation level for the transaction.
27 ///
28 /// SQLite supports limited isolation levels. Note that full support for
29 /// READ UNCOMMITTED requires shared-cache mode.
30 /// @see https://www.sqlite.org/isolation.html
31 enum class IsolationLevel {
32 kSerializable, ///< Default isolation level; ensures full isolation.
33 kReadUncommitted ///< Allows reading uncommitted changes; requires shared-cache mode.
34 };
35
38
39 constexpr TransactionOptions() = default;
40 constexpr explicit TransactionOptions(IsolationLevel lvl) : isolation_level{lvl} {}
41 constexpr TransactionOptions(IsolationLevel lvl, LockingMode m) : isolation_level{lvl}, mode{m} {}
42 constexpr explicit TransactionOptions(LockingMode m) : mode{m} {}
43
44 /// @brief Creates a TransactionOptions instance with deferred locking mode.
46};
47
48constexpr inline bool operator==(const TransactionOptions& lhs, const TransactionOptions& rhs) {
49 return lhs.mode == rhs.mode;
50}
51
52std::string IsolationLevelToString(const TransactionOptions::IsolationLevel& lvl);
53
54/// @brief Default maximum size for the prepared statements cache.
55inline constexpr std::size_t kDefaultMaxPreparedCacheSize = 200;
56
57/// @brief Default setting for caching prepared statements.
58inline constexpr bool kDefaultPrepareStatement = true;
59
60/// @brief SQLite connection settings.
61///
62/// Configures behavior related to prepared statements and their caching.
64 /// @brief Options for handling prepared statements.
66 kCachePreparedStatements, ///< Enable caching of prepared statements.
67 kNoPreparedStatements, ///< Disable caching of prepared statements.
68 };
69
70 /// Cache prepared statements or not
73
74 /// Maximum number of prepared statements to cache
76
77 static ConnectionSettings Create(const components::ComponentConfig& config);
78};
79
80/// @brief Default initial size for the connection pool
81inline constexpr std::size_t kDefaultInitialPoolSize = 5;
82
83/// @brief Default maximum size for the connection pool
84inline constexpr std::size_t kDefaultMaxPoolSize = 10;
85
86/// @brief SQLite connection pool settings.
87///
88/// Configures the behavior of the connection pool, including its size.
89struct PoolSettings final {
90 /// @brief Number of connections created initially.
92
93 /// @brief Maximum number of connections in the pool.
95
96 static PoolSettings Create(const components::ComponentConfig& config);
97};
98
99inline constexpr bool kDefaultCreateFile = true;
100inline constexpr bool kDefaultIsReadOnly = false;
101inline constexpr bool kDefaultSharedCache = false;
102inline constexpr bool kDefaultReadUncommitted = false;
103inline constexpr bool kDefaultForeignKeys = true;
104inline constexpr std::string_view kDefaultJournalMode = "wal";
105inline constexpr std::string_view kDefaultSynchronous = "normal";
106inline constexpr std::string_view kDefaultTempStore = "memory";
107inline constexpr int kDefaultBusyTimeout = 5000;
108inline constexpr int kDefaultCacheSize = -2000;
109inline constexpr int kDefaultJournalSizeLimit = 67108864;
110inline constexpr int kDefaultMmapSize = 134217728;
111inline constexpr int kDefaultPageSize = 4096;
112
113/// @brief Comprehensive SQLite settings.
114///
115/// Aggregates various settings for configuring SQLite behavior.
117 /// @brief Read mode for the database.
118 enum class ReadMode {
119 kReadOnly, ///< Open the database in read-only mode
120 kReadWrite ///< Open the database in read-write mode
121 };
122
123 /// @brief Journal mode options.
124 enum class JournalMode {
125 kDelete, ///< Delete the journal file after each transaction.
126 kTruncate, ///< Truncate the journal file to zero length.
127 kPersist, ///< Keep the journal file but zero its header.
128 kMemory, ///< Store the journal in memory.
129 kWal, ///< Use write-ahead logging.
130 kOff ///< Disable the journal.
131 };
132
133 /// @brief Synchronous setting options.
135 kExtra, ///< Extra synchronization.
136 kFull, ///< Full synchronization.
137 kNormal, ///< Normal synchronization.
138 kOff ///< No synchronization.
139 };
140
141 /// @brief Temporary store options.
143 kMemory, ///< Store temporary tables in memory.
144 kFile ///< Store temporary tables in a file.
145 };
146
147 ReadMode read_mode = !kDefaultIsReadOnly ? ReadMode::kReadWrite : ReadMode::kReadOnly;
148 bool create_file = kDefaultCreateFile;
149 bool shared_cache = kDefaultSharedCache;
150 bool read_uncommitted = kDefaultReadUncommitted;
151 bool foreign_keys = kDefaultForeignKeys;
152 JournalMode journal_mode = JournalMode::kWal;
153 int busy_timeout = kDefaultBusyTimeout;
154 Synchronous synchronous = Synchronous::kNormal;
155 int cache_size = kDefaultCacheSize;
156 TempStore temp_store = TempStore::kMemory;
157 int journal_size_limit = kDefaultJournalSizeLimit;
158 int mmap_size = kDefaultMmapSize;
159 int page_size = kDefaultPageSize;
160 std::string db_path;
161 ConnectionSettings conn_settings;
162 PoolSettings pool_settings;
163};
164
165std::string JournalModeToString(const SQLiteSettings::JournalMode& mode);
166
167} // namespace storages::sqlite::settings
168
169USERVER_NAMESPACE_END