Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
SqlBuilder.java
Go to the documentation of this file.
1package dev.rafex.ether.database.core.sql;
2
3/*-
4 * #%L
5 * ether-database-core
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.ArrayList;
30import java.util.List;
31import java.util.Objects;
32
33public final class SqlBuilder {
34
35 private final StringBuilder sql;
36 private final List<SqlParameter> parameters;
37
38 public SqlBuilder() {
39 this("");
40 }
41
42 public SqlBuilder(final String initialSql) {
43 this.sql = new StringBuilder(Objects.requireNonNull(initialSql, "initialSql"));
44 this.parameters = new ArrayList<>();
45 }
46
47 public SqlBuilder append(final String fragment) {
48 sql.append(fragment);
49 return this;
50 }
51
52 public SqlBuilder param(final Object value) {
53 sql.append('?');
54 parameters.add(SqlParameter.of(value));
55 return this;
56 }
57
58 public SqlBuilder param(final SqlParameter parameter) {
59 sql.append('?');
60 parameters.add(Objects.requireNonNull(parameter, "parameter"));
61 return this;
62 }
63
64 public SqlBuilder appendPlaceholders(final int count) {
65 if (count < 0) {
66 throw new IllegalArgumentException("count must be >= 0");
67 }
68 for (int i = 0; i < count; i++) {
69 if (i > 0) {
70 sql.append(", ");
71 }
72 sql.append('?');
73 }
74 return this;
75 }
76
77 public SqlBuilder paramList(final Iterable<?> values) {
78 boolean first = true;
79 for (final Object value : values) {
80 if (!first) {
81 sql.append(", ");
82 }
83 first = false;
84 param(value);
85 }
86 return this;
87 }
88
89 public SqlQuery build() {
90 return new SqlQuery(sql.toString(), parameters);
91 }
92}
SqlBuilder paramList(final Iterable<?> values)
SqlBuilder appendPlaceholders(final int count)
SqlBuilder param(final SqlParameter parameter)
SqlBuilder param(final Object value)
SqlBuilder append(final String fragment)
record SqlQuery(String sql, List< SqlParameter > parameters)
Definition SqlQuery.java:33
record SqlParameter(Object value, Integer sqlType, String arrayElementType)