1package dev.rafex.ether.database.sqlite.config;
29import java.sql.Connection;
30import java.sql.SQLException;
31import java.sql.Statement;
32import java.util.Objects;
40public final class SQLitePragmas {
42 private SQLitePragmas() {
58 Objects.requireNonNull(connection,
"connection must not be null");
59 Objects.requireNonNull(
config,
"config must not be null");
61 try (Statement stmt = connection.createStatement()) {
63 stmt.execute(
"PRAGMA journal_mode = " +
config.journalMode().name());
70 stmt.execute(
"PRAGMA synchronous = " +
config.synchronousMode().toSql());
72 stmt.execute(
"PRAGMA synchronous = " +
config.synchronousMode().pragmaValue());
76 stmt.execute(
"PRAGMA foreign_keys = " + (
config.foreignKeys() ?
"ON" :
"OFF"));
79 stmt.execute(
"PRAGMA busy_timeout = " +
config.busyTimeout());
82 stmt.execute(
"PRAGMA case_sensitive_like = " + (
config.caseSensitiveLike() ?
"1" :
"0"));
85 stmt.execute(
"PRAGMA recursive_triggers = " + (
config.recursiveTriggers() ?
"ON" :
"OFF"));
88 stmt.execute(
"PRAGMA auto_vacuum = " + (
config.autoVacuum() ?
"1" :
"0"));
91 stmt.execute(
"PRAGMA encoding = 'UTF-8'");
92 stmt.execute(
"PRAGMA temp_store = MEMORY");
93 stmt.execute(
"PRAGMA mmap_size = 268435456");
106 public static void applyDefaults(Connection connection)
throws SQLException {
119 public static void enableWal(Connection connection)
throws SQLException {
120 Objects.requireNonNull(connection,
"connection must not be null");
121 try (Statement stmt = connection.createStatement()) {
122 stmt.execute(
"PRAGMA journal_mode = WAL");
135 public static void disableWal(Connection connection)
throws SQLException {
136 Objects.requireNonNull(connection,
"connection must not be null");
137 try (Statement stmt = connection.createStatement()) {
138 stmt.execute(
"PRAGMA journal_mode = DELETE");
153 Objects.requireNonNull(connection,
"connection must not be null");
154 Objects.requireNonNull(mode,
"mode must not be null");
156 try (Statement stmt = connection.createStatement()) {
161 stmt.execute(
"PRAGMA synchronous = " + mode.toSql());
163 stmt.execute(
"PRAGMA synchronous = " + mode.pragmaValue());
Configuration for SQLite database connections.
static SQLiteConfig defaults()
Returns the default configuration.
static void disableWal(Connection connection)
Disables Write-Ahead Logging (WAL) mode on a connection.
static void setSynchronousMode(Connection connection, SynchronousMode mode)
Sets the synchronous mode on a connection.
static void applyDefaults(Connection connection)
Applies default configuration to a SQLite connection.
static void apply(Connection connection, SQLiteConfig config)
Applies configuration to a SQLite connection.
static void enableWal(Connection connection)
Enables Write-Ahead Logging (WAL) mode on a connection.
SQLite synchronous modes.
EXTRA
Extra synchronous mode similar to FULL but with additional directory sync operations for maximum dura...
FULL
SQLite syncs the database file after every write transaction.
NORMAL
SQLite syncs at the most critical moments, but less often than in FULL mode.
OFF
SQLite continues without syncing as soon as it has handed data off to the operating system.
SQLite configuration and PRAGMA utilities.