• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / FasterSQL / 13591799490

28 Feb 2025 03:59PM UTC coverage: 65.237% (-1.3%) from 66.563%
13591799490

push

github

web-flow
Merge pull request #14 from torand/having-support

feat: supporting the HAVING clause + IS NULL operator now supports an…

214 of 408 branches covered (52.45%)

Branch coverage included in aggregate %.

273 of 389 new or added lines in 69 files covered. (70.18%)

3 existing lines in 3 files now uncovered.

1079 of 1574 relevant lines covered (68.55%)

3.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.98
/src/main/java/io/github/torand/fastersql/statement/SelectStatement.java
1
/*
2
 * Copyright (c) 2024 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.fastersql.statement;
17

18
import io.github.torand.fastersql.Column;
19
import io.github.torand.fastersql.Context;
20
import io.github.torand.fastersql.Join;
21
import io.github.torand.fastersql.Table;
22
import io.github.torand.fastersql.alias.Alias;
23
import io.github.torand.fastersql.alias.ColumnAlias;
24
import io.github.torand.fastersql.expression.Expression;
25
import io.github.torand.fastersql.function.aggregate.AggregateFunction;
26
import io.github.torand.fastersql.order.Order;
27
import io.github.torand.fastersql.predicate.OptionalPredicate;
28
import io.github.torand.fastersql.predicate.Predicate;
29
import io.github.torand.fastersql.projection.Projection;
30
import io.github.torand.fastersql.subquery.Subquery;
31

32
import java.util.LinkedList;
33
import java.util.List;
34
import java.util.Optional;
35
import java.util.Set;
36
import java.util.function.Supplier;
37
import java.util.stream.Stream;
38

39
import static io.github.torand.fastersql.Command.SELECT;
40
import static io.github.torand.fastersql.dialect.Capability.LIMIT_OFFSET;
41
import static io.github.torand.fastersql.statement.Helpers.unwrapSuppliers;
42
import static io.github.torand.fastersql.util.collection.CollectionHelper.asList;
43
import static io.github.torand.fastersql.util.collection.CollectionHelper.concat;
44
import static io.github.torand.fastersql.util.collection.CollectionHelper.isEmpty;
45
import static io.github.torand.fastersql.util.collection.CollectionHelper.nonEmpty;
46
import static io.github.torand.fastersql.util.collection.CollectionHelper.streamSafely;
47
import static io.github.torand.fastersql.util.contract.Requires.require;
48
import static io.github.torand.fastersql.util.contract.Requires.requireNonEmpty;
49
import static io.github.torand.fastersql.util.functional.Functions.castTo;
50
import static io.github.torand.fastersql.util.functional.Optionals.mapIfNonNull;
51
import static io.github.torand.fastersql.util.functional.Predicates.instanceOf;
52
import static io.github.torand.fastersql.util.lang.StringHelper.nonBlank;
53
import static java.util.Objects.isNull;
54
import static java.util.Objects.nonNull;
55
import static java.util.Objects.requireNonNull;
56
import static java.util.function.Predicate.not;
57
import static java.util.stream.Collectors.joining;
58
import static java.util.stream.Collectors.toSet;
59

60
public class SelectStatement extends PreparableStatement {
61
    private final List<Projection> projections;
62
    private final List<Table<?>> tables;
63
    private final List<Join> joins;
64
    private final Subquery subqueryFrom;
65
    private final List<Predicate> wherePredicates;
66
    private final List<Column> groups;
67
    private final List<Predicate> havingPredicates;
68
    private final List<Order> orders;
69
    private final boolean distinct;
70
    private final Long limit;
71
    private final Long offset;
72
    private final boolean forUpdate;
73

74
    SelectStatement(List<Projection> projections, List<Table<?>> tables, List<Join> joins, Subquery subqueryFrom, List<Predicate> wherePredicates, List<Column> groups, List<Predicate> havingPredicates, List<Order> orders, boolean distinct, Long limit, Long offset, boolean forUpdate) {
2✔
75
        this.projections = asList(projections);
4✔
76
        this.tables = asList(tables);
4✔
77
        this.joins = asList(joins);
4✔
78
        this.subqueryFrom = subqueryFrom;
3✔
79
        this.wherePredicates = asList(wherePredicates);
4✔
80
        this.groups = asList(groups);
4✔
81
        this.havingPredicates = asList(havingPredicates);
4✔
82
        this.orders = asList(orders);
4✔
83
        this.distinct = distinct;
3✔
84
        this.limit = limit;
3✔
85
        this.offset = offset;
3✔
86
        this.forUpdate = forUpdate;
3✔
87
    }
1✔
88

89
    public SelectStatement join(Join... joins) {
90
        requireNonEmpty(joins, "No joins specified");
6✔
91
        require(() -> isNull(subqueryFrom), "Can't combine a subquery FROM clause with joins");
10✔
92

93
        List<Join> concatenated = concat(this.joins, joins);
5✔
94
        return new SelectStatement(projections, tables, concatenated, subqueryFrom, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
27✔
95
    }
96

97
    public SelectStatement leftOuterJoin(Join join) {
98
        requireNonNull(join, "No join specified");
×
99
        return join(join.leftOuter());
×
100
    }
101

102
    public SelectStatement rightOuterJoin(Join join) {
103
        requireNonNull(join, "No join specified");
×
104
        return join(join.rightOuter());
×
105
    }
106

107
    @SafeVarargs
108
    public final SelectStatement joinIf(boolean condition, Supplier<Join>... joinSuppliers) {
109
        requireNonEmpty(joinSuppliers, "No join suppliers specified");
×
110
        require(() -> isNull(subqueryFrom), "Can't combine a subquery FROM clause with joins");
×
111
        if (condition) {
×
112
            List<Join> concatenated = concat(this.joins, unwrapSuppliers(joinSuppliers));
×
NEW
113
            return new SelectStatement(projections, tables, concatenated, subqueryFrom, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
×
114
        } else {
115
            return this;
×
116
        }
117
    }
118

119
    /**
120
     * Adds one or more predicates to the WHERE clause.
121
     * @param predicates the WHERE predicates to add
122
     * @return updated statement, for method chaining
123
     */
124
    public SelectStatement where(Predicate... predicates) {
125
        requireNonEmpty(predicates, "No WHERE predicates specified");
6✔
126

127
        List<Predicate> concatenated = concat(this.wherePredicates, predicates);
5✔
128
        return new SelectStatement(projections, tables, joins, subqueryFrom, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
27✔
129
    }
130

131
    /**
132
     * Same as other method of same name, but only adds to the WHERE clause predicates that are present.
133
     * @param maybePredicates the WHERE predicate that may be present or not
134
     * @return updated statement, for method chaining
135
     */
136
    @SafeVarargs
137
    public final SelectStatement where(OptionalPredicate... maybePredicates) {
138
        requireNonEmpty(maybePredicates, "No optional WHERE predicates specified");
6✔
139

140
        List<Predicate> concatenated = concat(this.wherePredicates, OptionalPredicate.unwrap(maybePredicates));
6✔
141
        return new SelectStatement(projections, tables, joins, subqueryFrom, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
27✔
142
    }
143

144
    /**
145
     * Adds one or more predicates to the WHERE clause, if a predicate is true.
146
     * @param condition the condition that must be true for predicates to be added
147
     * @param predicateSuppliers the suppliers providing WHERE predicates to add
148
     * @return updatet statement, for method chaining
149
     */
150
    @SafeVarargs
151
    public final SelectStatement whereIf(boolean condition, Supplier<Predicate>... predicateSuppliers) {
NEW
152
        requireNonEmpty(predicateSuppliers, "No WHERE predicate suppliers specified");
×
153
        if (condition) {
×
NEW
154
            List<Predicate> concatenated = concat(this.wherePredicates, unwrapSuppliers(predicateSuppliers));
×
NEW
155
            return new SelectStatement(projections, tables, joins, subqueryFrom, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
×
156
        } else {
157
            return this;
×
158
        }
159
    }
160

161
    public SelectStatement groupBy(Column... groups) {
162
        requireNonEmpty(groups, "No groups specified");
6✔
163

164
        List<Column> concatenated = concat(this.groups, groups);
5✔
165
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, concatenated, havingPredicates, orders, distinct, limit, offset, forUpdate);
27✔
166
    }
167

168
    /**
169
     * Adds one or more predicates to the HAVING clause.
170
     * @param predicates the HAVING predicates to add
171
     * @return updated statement, for method chaining
172
     */
173
    public SelectStatement having(Predicate... predicates) {
174
        requireNonEmpty(predicates, "No HAVING predicates specified");
6✔
175

176
        List<Predicate> concatenated = concat(this.havingPredicates, predicates);
5✔
177
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
27✔
178
    }
179

180
    /**
181
     * Same as other method of same name, but only adds to the HAVING clause predicates that are present.
182
     * @param maybePredicates the HAVING predicate that may be present or not
183
     * @return updated statement, for method chaining
184
     */
185
    @SafeVarargs
186
    public final SelectStatement having(OptionalPredicate... maybePredicates) {
NEW
187
        requireNonEmpty(maybePredicates, "No optional HAVING predicates specified");
×
188

NEW
189
        List<Predicate> concatenated = concat(this.havingPredicates, OptionalPredicate.unwrap(maybePredicates));
×
NEW
190
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
×
191
    }
192

193
    /**
194
     * Adds one or more predicates to the HAVING clause, if a predicate is true.
195
     * @param condition the condition that must be true for predicates to be added
196
     * @param predicateSuppliers the suppliers providing HAVING predicates to add
197
     * @return updatet statement, for method chaining
198
     */
199
    @SafeVarargs
200
    public final SelectStatement havingIf(boolean condition, Supplier<Predicate>... predicateSuppliers) {
NEW
201
        requireNonEmpty(predicateSuppliers, "No HAVING predicate suppliers specified");
×
NEW
202
        if (condition) {
×
NEW
203
            List<Predicate> concatenated = concat(this.havingPredicates, unwrapSuppliers(predicateSuppliers));
×
NEW
204
            return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
×
205
        } else {
NEW
206
            return this;
×
207
        }
208
    }
209

210
    public SelectStatement orderBy(Order... orders) {
211
        requireNonEmpty(orders, "No orders specified");
6✔
212

213
        List<Order> concatenated = concat(this.orders, orders);
5✔
214
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, havingPredicates, concatenated, distinct, limit, offset, forUpdate);
27✔
215
    }
216

217
    public SelectStatement limit(long limit) {
218
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
28✔
219
    }
220

221
    public SelectStatement offset(long offset) {
222
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
28✔
223
    }
224

225
    public SelectStatement forUpdate() {
226
        return new SelectStatement(projections, tables, joins, subqueryFrom, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, true);
27✔
227
    }
228

229
    @Override
230
    public String sql(Context context) {
231
        final Context localContext = context.withCommand(SELECT);
4✔
232
        validate();
2✔
233

234
        StringBuilder sb = new StringBuilder();
4✔
235
        sb.append("select ");
4✔
236
        if (distinct) {
3✔
237
            sb.append("distinct ");
4✔
238
        }
239

240
        sb.append(streamSafely(projections)
8✔
241
            .map(p -> p.sql(localContext) + (p.alias().isEmpty() ? "" : " " + p.alias().map(a -> a.sql(localContext)).get()))
25✔
242
            .collect(joining(", ")));
3✔
243

244
        sb.append(" from ");
4✔
245

246
        if (nonNull(subqueryFrom)) {
4✔
247
            sb.append(subqueryFrom.sql(context));
7✔
248
            if (nonBlank(subqueryFrom.alias())) {
5!
249
                sb.append(" ");
4✔
250
                sb.append(subqueryFrom.alias());
7✔
251
            }
252
        } else {
253
            // Tables that are joined with should not be specified in the FROM clause
254
            Set<Table<?>> joinedTables = streamSafely(joins).map(Join::joined).collect(toSet());
9✔
255

256
            sb.append(streamSafely(tables)
8✔
257
                .filter(not(joinedTables::contains))
7✔
258
                .map(t -> t.sql(localContext))
6✔
259
                .collect(joining(", ")));
3✔
260

261
            if (nonEmpty(joins)) {
4✔
262
                sb.append(" ");
4✔
263
                sb.append(streamSafely(joins)
8✔
264
                    .map(j -> j.sql(localContext))
6✔
265
                    .collect(joining(" ")));
3✔
266
            }
267
        }
268

269
        if (nonEmpty(wherePredicates)) {
4✔
270
            sb.append(" where ");
4✔
271
            sb.append(streamSafely(wherePredicates)
8✔
272
                .map(p -> p.sql(localContext))
6✔
273
                .collect(joining(" and ")));
3✔
274
        }
275

276
        if (nonEmpty(groups)) {
4✔
277
            sb.append(" group by ");
4✔
278
            sb.append(streamSafely(groups)
8✔
279
                .map(g -> g.sql(localContext))
6✔
280
                .collect(joining(", ")));
3✔
281
        }
282

283
        if (nonEmpty(havingPredicates)) {
4✔
284
            sb.append(" having ");
4✔
285
            sb.append(streamSafely(havingPredicates)
8✔
286
                .map(p -> p.sql(localContext))
6✔
287
                .collect(joining(" and ")));
3✔
288
        }
289

290
        if (nonEmpty(orders)) {
4✔
291
            sb.append(" order by ");
4✔
292
            sb.append(streamSafely(orders)
8✔
293
                .map(o -> o.sql(localContext))
6✔
294
                .collect(joining(", ")));
3✔
295
        }
296

297
        if (nonNull(offset) || nonNull(limit)) {
8!
298
            if (context.getDialect().supports(LIMIT_OFFSET)) {
5✔
299
                String offsetClause = nonNull(offset) ? " " + context.getDialect().formatRowOffsetClause().orElseThrow(() -> new RuntimeException("Dialect " + context.getDialect().getProductName() + " has no row offset clause")) : "";
14!
300
                String limitClause = nonNull(limit) ? " " + context.getDialect().formatRowLimitClause().orElseThrow(() -> new RuntimeException("Dialect " + context.getDialect().getProductName() + " has no row limit clause")) : "";
14!
301

302
                if (context.getDialect().offsetBeforeLimit()) {
4✔
303
                    sb.append(offsetClause).append(limitClause);
7✔
304
                } else {
305
                    sb.append(limitClause).append(offsetClause);
6✔
306
                }
307
            } else {
1✔
308
                sb = addLimitOffsetFallback(context, sb, rowFrom(), rowTo());
9✔
309
            }
310
        }
311

312
        if (forUpdate) {
3✔
313
            sb.append(" for update");
4✔
314
        }
315

316
        return sb.toString();
3✔
317
    }
318

319
    private Long rowFrom() {
320
        return mapIfNonNull(offset, o -> o + 1);
12✔
321
    }
322

323
    private Long rowTo() {
324
        return mapIfNonNull(limit, l -> (nonNull(offset) ? offset : 0) + l);
20!
325
    }
326

327
    private StringBuilder addLimitOffsetFallback(Context context, StringBuilder innerSql, Long rowFrom, Long rowTo) {
328
        String rowNum = context.getDialect().formatRowNumLiteral()
5✔
329
            .orElseThrow(() -> new RuntimeException("Dialect " + context.getDialect().getProductName() + " has no row number literal"));
3✔
330

331
        if (nonNull(rowFrom) && nonNull(rowTo)) {
6!
332
            String limitSql = "select ORIGINAL.*, {ROWNUM} ROW_NO from ( " + innerSql.toString() + " ) ORIGINAL where {ROWNUM} <= ?";
4✔
333
            String offsetSql = "select * from ( " + limitSql + " ) where ROW_NO >= ?";
3✔
334
            return new StringBuilder(offsetSql.replace("{ROWNUM}", rowNum));
8✔
335
        } else if (nonNull(rowFrom)) {
×
336
            String offsetSql = "select * from ( " + innerSql.toString() + " ) where {ROWNUM} >= ?";
×
337
            return new StringBuilder(offsetSql.replace("{ROWNUM}", rowNum));
×
338
        } else if (nonNull(rowTo)) {
×
339
            String limitSql = "select * from ( " + innerSql.toString() + " ) where {ROWNUM} <= ?";
×
340
            return new StringBuilder(limitSql.replace("{ROWNUM}", rowNum));
×
341
        }
342

343
        return innerSql;
×
344
    }
345

346
    @Override
347
    public List<Object> params(Context context) {
348
        List<Object> params = new LinkedList<>();
4✔
349

350
        streamSafely(projections).flatMap(p -> p.params(context)).forEach(params::add);
16✔
351

352
        if (nonNull(subqueryFrom)) {
4✔
353
            subqueryFrom.params(context).forEach(params::add);
10✔
354
        }
355

356
        streamSafely(wherePredicates).flatMap(p -> p.params(context)).forEach(params::add);
16✔
357

358
        streamSafely(havingPredicates).flatMap(p -> p.params(context)).forEach(params::add);
16✔
359

360
        if (context.getDialect().supports(LIMIT_OFFSET)) {
5✔
361
            if (context.getDialect().offsetBeforeLimit()) {
4✔
362
                if (nonNull(offset)) {
4✔
363
                    params.add(offset);
5✔
364
                }
365
                if (nonNull(limit)) {
4✔
366
                    params.add(limit);
6✔
367
                }
368
            } else {
369
                if (nonNull(limit)) {
4✔
370
                    params.add(limit);
5✔
371
                }
372
                if (nonNull(offset)) {
4✔
373
                    params.add(offset);
6✔
374
                }
375
            }
376
        } else {
377
            if (nonNull(limit)) {
4!
378
                params.add(rowTo());
5✔
379
            }
380
            if (nonNull(offset)) {
4!
381
                params.add(rowFrom());
5✔
382
            }
383
        }
384

385
        return params;
2✔
386
    }
387

388
    private void validate() {
389
        if (isEmpty(tables) && isNull(subqueryFrom)) {
8!
390
            throw new IllegalStateException("No FROM clause specified");
×
391
        }
392

393
        Stream<Column> projectedColumns = streamSafely(projections)
4✔
394
            .filter(instanceOf(Expression.class))
3✔
395
            .map(castTo(Expression.class))
3✔
396
            .flatMap(Expression::columnRefs);
2✔
397
        validateColumnTableRelations(projectedColumns);
3✔
398

399
        if (nonNull(joins)) {
4!
400
            validateColumnTableRelations(streamSafely(joins).flatMap(Join::columnRefs));
7✔
401
        }
402

403
        if (nonNull(orders)) {
4!
404
            Set<String> orderableAliases = streamSafely(projections)
4✔
405
                .map(Projection::alias)
2✔
406
                .flatMap(Optional::stream)
2✔
407
                .map(Alias::name)
1✔
408
                .collect(toSet());
4✔
409

410
            streamSafely(orders)
4✔
411
                .flatMap(o -> o.aliasRefs())
5✔
412
                .map(ColumnAlias::name)
3✔
413
                .filter(a -> !orderableAliases.contains(a))
7!
414
                .findFirst()
2✔
415
                .ifPresent(a -> {
1✔
NEW
416
                    throw new IllegalStateException("ORDER BY column alias " + a + " is not specified in the SELECT clause");
×
417
                });
418
        }
419

420
        validateColumnTableRelations(streamSafely(wherePredicates).flatMap(Predicate::columnRefs));
7✔
421
        validateColumnTableRelations(streamSafely(groups));
5✔
422
        validateColumnTableRelations(streamSafely(havingPredicates).flatMap(Predicate::columnRefs));
7✔
423
        validateColumnTableRelations(streamSafely(orders).flatMap(Order::columnRefs));
7✔
424

425
        if (forUpdate) {
3✔
426
            if (distinct || nonEmpty(groups) || streamSafely(projections).anyMatch(instanceOf(AggregateFunction.class))) {
14!
427
                throw new IllegalStateException("SELECT ... FOR UPDATE can't be used with DISTINCT, GROUP BY or aggregates");
×
428
            }
429
        }
430
    }
1✔
431

432
    private void validateColumnTableRelations(Stream<Column> columns) {
433
        Set<String> tableNames = Stream.concat(streamSafely(tables), streamSafely(joins).map(Join::joined))
10✔
434
            .map(Table::name)
1✔
435
            .collect(toSet());
4✔
436

437
        columns
3✔
438
            .filter(c -> !tableNames.contains(c.table().name()))
9!
439
            .findFirst()
2✔
440
            .ifPresent(c -> {
1✔
NEW
441
                throw new IllegalStateException("Column " + c.name() + " belongs to table " + c.table().name() + ", but is not specified in a FROM or JOIN clause");
×
442
            });
443
    }
1✔
444
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc