Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
SQLiteConfig.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.util.Objects;
30
31/**
32 * Configuration for SQLite database connections.
33 * <p>
34 * Provides settings for journal mode, synchronous behavior, and other
35 * SQLite-specific optimizations.
36 */
37public final class SQLiteConfig {
38
39 private final JournalMode journalMode;
40 private final SynchronousMode synchronousMode;
41 private final boolean foreignKeys;
42 private final int busyTimeout;
43 private final boolean caseSensitiveLike;
44 private final boolean recursiveTriggers;
45 private final boolean autoVacuum;
46
47 private SQLiteConfig(Builder builder) {
48 this.journalMode = builder.journalMode;
49 this.synchronousMode = builder.synchronousMode;
50 this.foreignKeys = builder.foreignKeys;
51 this.busyTimeout = builder.busyTimeout;
52 this.caseSensitiveLike = builder.caseSensitiveLike;
53 this.recursiveTriggers = builder.recursiveTriggers;
54 this.autoVacuum = builder.autoVacuum;
55 }
56
57 /**
58 * Returns the journal mode.
59 *
60 * @return journal mode, never {@code null}
61 */
63 return journalMode;
64 }
65
66 /**
67 * Returns the synchronous mode.
68 *
69 * @return synchronous mode, never {@code null}
70 */
72 return synchronousMode;
73 }
74
75 /**
76 * Returns whether foreign key constraints are enabled.
77 *
78 * @return {@code true} if foreign keys are enabled
79 */
80 public boolean foreignKeys() {
81 return foreignKeys;
82 }
83
84 /**
85 * Returns the busy timeout in milliseconds.
86 *
87 * @return busy timeout in milliseconds
88 */
89 public int busyTimeout() {
90 return busyTimeout;
91 }
92
93 /**
94 * Returns whether LIKE operator is case-sensitive.
95 *
96 * @return {@code true} if LIKE is case-sensitive
97 */
98 public boolean caseSensitiveLike() {
99 return caseSensitiveLike;
100 }
101
102 /**
103 * Returns whether recursive triggers are enabled.
104 *
105 * @return {@code true} if recursive triggers are enabled
106 */
107 public boolean recursiveTriggers() {
108 return recursiveTriggers;
109 }
110
111 /**
112 * Returns whether auto-vacuum is enabled.
113 *
114 * @return {@code true} if auto-vacuum is enabled
115 */
116 public boolean autoVacuum() {
117 return autoVacuum;
118 }
119
120 @Override
121 public boolean equals(Object o) {
122 if (this == o) return true;
123 if (o == null || getClass() != o.getClass()) return false;
124 SQLiteConfig that = (SQLiteConfig) o;
125 return foreignKeys == that.foreignKeys &&
126 busyTimeout == that.busyTimeout &&
127 caseSensitiveLike == that.caseSensitiveLike &&
128 recursiveTriggers == that.recursiveTriggers &&
129 autoVacuum == that.autoVacuum &&
130 journalMode == that.journalMode &&
131 synchronousMode == that.synchronousMode;
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hash(journalMode, synchronousMode, foreignKeys, busyTimeout,
137 caseSensitiveLike, recursiveTriggers, autoVacuum);
138 }
139
140 @Override
141 public String toString() {
142 return "SQLiteConfig{" +
143 "journalMode=" + journalMode +
144 ", synchronousMode=" + synchronousMode +
145 ", foreignKeys=" + foreignKeys +
146 ", busyTimeout=" + busyTimeout +
147 ", caseSensitiveLike=" + caseSensitiveLike +
148 ", recursiveTriggers=" + recursiveTriggers +
149 ", autoVacuum=" + autoVacuum +
150 '}';
151 }
152
153 /**
154 * Creates a new builder with default values:
155 * <ul>
156 * <li>Journal mode: {@link JournalMode#WAL}</li>
157 * <li>Synchronous mode: {@link SynchronousMode#NORMAL}</li>
158 * <li>Foreign keys: enabled ({@code true})</li>
159 * <li>Busy timeout: 5000ms</li>
160 * <li>Case-sensitive LIKE: disabled ({@code false})</li>
161 * <li>Recursive triggers: disabled ({@code false})</li>
162 * <li>Auto-vacuum: disabled ({@code false})</li>
163 * </ul>
164 *
165 * @return a new builder
166 */
167 public static Builder builder() {
168 return new Builder();
169 }
170
171 /**
172 * Returns the default configuration.
173 *
174 * @return default configuration
175 */
176 public static SQLiteConfig defaults() {
177 return builder().build();
178 }
179
180 /**
181 * Builder for {@link SQLiteConfig}.
182 */
183 public static final class Builder {
186 private boolean foreignKeys = true;
187 private int busyTimeout = 5000;
188 private boolean caseSensitiveLike = false;
189 private boolean recursiveTriggers = false;
190 private boolean autoVacuum = false;
191
192 private Builder() {}
193
194 /**
195 * Sets the journal mode.
196 *
197 * @param journalMode journal mode
198 * @return this builder
199 */
200 public Builder journalMode(JournalMode journalMode) {
201 this.journalMode = Objects.requireNonNull(journalMode, "journalMode must not be null");
202 return this;
203 }
204
205 /**
206 * Sets the synchronous mode.
207 *
208 * @param synchronousMode synchronous mode
209 * @return this builder
210 */
211 public Builder synchronousMode(SynchronousMode synchronousMode) {
212 this.synchronousMode = Objects.requireNonNull(synchronousMode, "synchronousMode must not be null");
213 return this;
214 }
215
216 /**
217 * Enables or disables foreign key constraints.
218 *
219 * @param foreignKeys {@code true} to enable foreign keys
220 * @return this builder
221 */
222 public Builder foreignKeys(boolean foreignKeys) {
223 this.foreignKeys = foreignKeys;
224 return this;
225 }
226
227 /**
228 * Sets the busy timeout in milliseconds.
229 * <p>
230 * When a database is locked, SQLite will retry for up to this duration
231 * before returning {@code SQLITE_BUSY}.
232 *
233 * @param busyTimeout timeout in milliseconds, must be non-negative
234 * @return this builder
235 */
236 public Builder busyTimeout(int busyTimeout) {
237 if (busyTimeout < 0) {
238 throw new IllegalArgumentException("busyTimeout must be non-negative");
239 }
240 this.busyTimeout = busyTimeout;
241 return this;
242 }
243
244 /**
245 * Enables or disables case-sensitive LIKE operator.
246 *
247 * @param caseSensitiveLike {@code true} for case-sensitive LIKE
248 * @return this builder
249 */
250 public Builder caseSensitiveLike(boolean caseSensitiveLike) {
251 this.caseSensitiveLike = caseSensitiveLike;
252 return this;
253 }
254
255 /**
256 * Enables or disables recursive triggers.
257 *
258 * @param recursiveTriggers {@code true} to enable recursive triggers
259 * @return this builder
260 */
261 public Builder recursiveTriggers(boolean recursiveTriggers) {
262 this.recursiveTriggers = recursiveTriggers;
263 return this;
264 }
265
266 /**
267 * Enables or disables auto-vacuum.
268 *
269 * @param autoVacuum {@code true} to enable auto-vacuum
270 * @return this builder
271 */
272 public Builder autoVacuum(boolean autoVacuum) {
273 this.autoVacuum = autoVacuum;
274 return this;
275 }
276
277 /**
278 * Builds the configuration.
279 *
280 * @return new SQLiteConfig instance
281 */
282 public SQLiteConfig build() {
283 return new SQLiteConfig(this);
284 }
285 }
286}
boolean recursiveTriggers()
Returns whether recursive triggers are enabled.
boolean foreignKeys()
Returns whether foreign key constraints are enabled.
static SQLiteConfig defaults()
Returns the default configuration.
boolean autoVacuum()
Returns whether auto-vacuum is enabled.
boolean caseSensitiveLike()
Returns whether LIKE operator is case-sensitive.
static Builder builder()
Creates a new builder with default values:
JournalMode journalMode()
Returns the journal mode.
int busyTimeout()
Returns the busy timeout in milliseconds.
SynchronousMode synchronousMode()
Returns the synchronous mode.
NORMAL
SQLite syncs at the most critical moments, but less often than in FULL mode.