Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
SQLitePragmas.java
Go to the documentation of this file.
1package dev.rafex.ether.database.sqlite.config;
2
3/*-
4 * #%L
5 * ether-database-sqlite
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.sql.Connection;
30import java.sql.SQLException;
31import java.sql.Statement;
32import java.util.Objects;
33
34/**
35 * Utility to apply SQLite PRAGMA settings to a connection.
36 * <p>
37 * This class helps configure SQLite connections with optimal settings
38 * for performance, durability, and feature compatibility.
39 */
40public final class SQLitePragmas {
41
42 private SQLitePragmas() {
43 // Utility class
44 }
45
46 /**
47 * Applies configuration to a SQLite connection.
48 * <p>
49 * Executes the necessary {@code PRAGMA} statements to configure the
50 * connection according to the provided configuration.
51 *
52 * @param connection SQLite connection
53 * @param config configuration to apply
54 * @throws SQLException if any PRAGMA statement fails
55 * @throws NullPointerException if {@code connection} or {@code config} is {@code null}
56 */
57 public static void apply(Connection connection, SQLiteConfig config) throws SQLException {
58 Objects.requireNonNull(connection, "connection must not be null");
59 Objects.requireNonNull(config, "config must not be null");
60
61 try (Statement stmt = connection.createStatement()) {
62 // Journal mode
63 stmt.execute("PRAGMA journal_mode = " + config.journalMode().name());
64
65 // Synchronous mode (can be integer or string)
66 if (config.synchronousMode() == SynchronousMode.OFF ||
67 config.synchronousMode() == SynchronousMode.NORMAL ||
68 config.synchronousMode() == SynchronousMode.FULL ||
69 config.synchronousMode() == SynchronousMode.EXTRA) {
70 stmt.execute("PRAGMA synchronous = " + config.synchronousMode().toSql());
71 } else {
72 stmt.execute("PRAGMA synchronous = " + config.synchronousMode().pragmaValue());
73 }
74
75 // Foreign keys
76 stmt.execute("PRAGMA foreign_keys = " + (config.foreignKeys() ? "ON" : "OFF"));
77
78 // Busy timeout
79 stmt.execute("PRAGMA busy_timeout = " + config.busyTimeout());
80
81 // Case-sensitive LIKE
82 stmt.execute("PRAGMA case_sensitive_like = " + (config.caseSensitiveLike() ? "1" : "0"));
83
84 // Recursive triggers
85 stmt.execute("PRAGMA recursive_triggers = " + (config.recursiveTriggers() ? "ON" : "OFF"));
86
87 // Auto-vacuum
88 stmt.execute("PRAGMA auto_vacuum = " + (config.autoVacuum() ? "1" : "0"));
89
90 // Additional recommended pragmas
91 stmt.execute("PRAGMA encoding = 'UTF-8'");
92 stmt.execute("PRAGMA temp_store = MEMORY");
93 stmt.execute("PRAGMA mmap_size = 268435456"); // 256MB
94 }
95 }
96
97 /**
98 * Applies default configuration to a SQLite connection.
99 * <p>
100 * Equivalent to {@code apply(connection, SQLiteConfig.defaults())}.
101 *
102 * @param connection SQLite connection
103 * @throws SQLException if any PRAGMA statement fails
104 * @throws NullPointerException if {@code connection} is {@code null}
105 */
106 public static void applyDefaults(Connection connection) throws SQLException {
107 apply(connection, SQLiteConfig.defaults());
108 }
109
110 /**
111 * Enables Write-Ahead Logging (WAL) mode on a connection.
112 * <p>
113 * This is a convenience method for enabling WAL mode specifically.
114 *
115 * @param connection SQLite connection
116 * @throws SQLException if the PRAGMA statement fails
117 * @throws NullPointerException if {@code connection} is {@code null}
118 */
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");
123 }
124 }
125
126 /**
127 * Disables Write-Ahead Logging (WAL) mode on a connection.
128 * <p>
129 * This is a convenience method for disabling WAL mode specifically.
130 *
131 * @param connection SQLite connection
132 * @throws SQLException if the PRAGMA statement fails
133 * @throws NullPointerException if {@code connection} is {@code null}
134 */
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");
139 }
140 }
141
142 /**
143 * Sets the synchronous mode on a connection.
144 * <p>
145 * This is a convenience method for setting synchronous mode specifically.
146 *
147 * @param connection SQLite connection
148 * @param mode synchronous mode
149 * @throws SQLException if the PRAGMA statement fails
150 * @throws NullPointerException if {@code connection} or {@code mode} is {@code null}
151 */
152 public static void setSynchronousMode(Connection connection, SynchronousMode mode) throws SQLException {
153 Objects.requireNonNull(connection, "connection must not be null");
154 Objects.requireNonNull(mode, "mode must not be null");
155
156 try (Statement stmt = connection.createStatement()) {
157 if (mode == SynchronousMode.OFF ||
158 mode == SynchronousMode.NORMAL ||
159 mode == SynchronousMode.FULL ||
160 mode == SynchronousMode.EXTRA) {
161 stmt.execute("PRAGMA synchronous = " + mode.toSql());
162 } else {
163 stmt.execute("PRAGMA synchronous = " + mode.pragmaValue());
164 }
165 }
166 }
167}
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.
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.