userver: userver/storages/sqlite/options.hpp Source File
Loading...
Searching...
No Matches
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)
41 : isolation_level{lvl}
42 {}
43 constexpr TransactionOptions(IsolationLevel lvl, LockingMode m)
44 : isolation_level{lvl},
45 mode{m}
46 {}
47 constexpr explicit TransactionOptions(LockingMode m)
48 : mode{m}
49 {}
50
51 /// @brief Creates a TransactionOptions instance with deferred locking mode.
53};
54
55constexpr inline bool operator==(const TransactionOptions& lhs, const TransactionOptions& rhs) {
56 return lhs.mode == rhs.mode;
57}
58
59std::string IsolationLevelToString(const TransactionOptions::IsolationLevel& lvl);
60
61/// @brief Default maximum size for the prepared statements cache.
62inline constexpr std::size_t kDefaultMaxPreparedCacheSize = 200;
63
64/// @brief Default setting for caching prepared statements.
65inline constexpr bool kDefaultPrepareStatement = true;
66
67/// @brief SQLite connection settings.
68///
69/// Configures behavior related to prepared statements and their caching.
71 /// @brief Options for handling prepared statements.
73 kCachePreparedStatements, ///< Enable caching of prepared statements.
74 kNoPreparedStatements, ///< Disable caching of prepared statements.
75 };
76
77 /// Cache prepared statements or not
80
81 /// Maximum number of prepared statements to cache
83
84 static ConnectionSettings Create(const components::ComponentConfig& config);
85};
86
87/// @brief Default initial size for the connection pool
88inline constexpr std::size_t kDefaultInitialPoolSize = 5;
89
90/// @brief Default maximum size for the connection pool
91inline constexpr std::size_t kDefaultMaxPoolSize = 10;
92
93/// @brief SQLite connection pool settings.
94///
95/// Configures the behavior of the connection pool, including its size.
96struct PoolSettings final {
97 /// @brief Number of connections created initially.
99
100 /// @brief Maximum number of connections in the pool.
102
103 static PoolSettings Create(const components::ComponentConfig& config);
104};
105
106inline constexpr bool kDefaultCreateFile = true;
107inline constexpr bool kDefaultIsReadOnly = false;
108inline constexpr bool kDefaultSharedCache = false;
109inline constexpr bool kDefaultReadUncommitted = false;
110inline constexpr bool kDefaultForeignKeys = true;
111inline constexpr std::string_view kDefaultJournalMode = "wal";
112inline constexpr std::string_view kDefaultSynchronous = "normal";
113inline constexpr std::string_view kDefaultTempStore = "memory";
114inline constexpr int kDefaultBusyTimeout = 5000;
115inline constexpr int kDefaultCacheSize = -2000;
116inline constexpr int kDefaultJournalSizeLimit = 67108864;
117inline constexpr int kDefaultMmapSize = 134217728;
118inline constexpr int kDefaultPageSize = 4096;
119
120/// @brief Comprehensive SQLite settings.
121///
122/// Aggregates various settings for configuring SQLite behavior.
124 /// @brief Read mode for the database.
125 enum class ReadMode {
126 kReadOnly, ///< Open the database in read-only mode
127 kReadWrite ///< Open the database in read-write mode
128 };
129
130 /// @brief Journal mode options.
131 enum class JournalMode {
132 kDelete, ///< Delete the journal file after each transaction.
133 kTruncate, ///< Truncate the journal file to zero length.
134 kPersist, ///< Keep the journal file but zero its header.
135 kMemory, ///< Store the journal in memory.
136 kWal, ///< Use write-ahead logging.
137 kOff ///< Disable the journal.
138 };
139
140 /// @brief Synchronous setting options.
142 kExtra, ///< Extra synchronization.
143 kFull, ///< Full synchronization.
144 kNormal, ///< Normal synchronization.
145 kOff ///< No synchronization.
146 };
147
148 /// @brief Temporary store options.
150 kMemory, ///< Store temporary tables in memory.
151 kFile ///< Store temporary tables in a file.
152 };
153
154 ReadMode read_mode = !kDefaultIsReadOnly ? ReadMode::kReadWrite : ReadMode::kReadOnly;
155 bool create_file = kDefaultCreateFile;
156 bool shared_cache = kDefaultSharedCache;
157 bool read_uncommitted = kDefaultReadUncommitted;
158 bool foreign_keys = kDefaultForeignKeys;
159 JournalMode journal_mode = JournalMode::kWal;
160 int busy_timeout = kDefaultBusyTimeout;
161 Synchronous synchronous = Synchronous::kNormal;
162 int cache_size = kDefaultCacheSize;
163 TempStore temp_store = TempStore::kMemory;
164 int journal_size_limit = kDefaultJournalSizeLimit;
165 int mmap_size = kDefaultMmapSize;
166 int page_size = kDefaultPageSize;
167 std::string db_path;
168 ConnectionSettings conn_settings;
169 PoolSettings pool_settings;
170};
171
172std::string JournalModeToString(const SQLiteSettings::JournalMode& mode);
173
174} // namespace storages::sqlite::settings
175
176USERVER_NAMESPACE_END