/
SelectQuery.java
600 lines (525 loc) · 21.1 KB
/
SelectQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.communication.semistructured;
import jakarta.data.Sort;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static java.util.Objects.requireNonNull;
/**
* Class that contains information to perform a select operation on {@link CommunicationEntity}.
*
* @see DatabaseManager#select(SelectQuery)
* @see CriteriaCondition
* @see Sort
*/
public interface SelectQuery {
/**
* Retrieves the maximum number of results the select object was set to retrieve.
* The implementation might ignore this option.
*
* @return the maximum number of results
*/
long limit();
/**
* Retrieves the position of the first result the select object was set to retrieve.
* The implementation might ignore this option.
*
* @return the position of the first result
*/
long skip();
/**
* Retrieves the entity name.
*
* @return the entity name
*/
String name();
/**
* Retrieves the conditions contained in this {@link SelectQuery}.
* If empty, {@link Optional#empty()} indicates that the implementation might either return an unsupported exception or return the same elements in the database.
*
* @return the conditions
*/
Optional<CriteriaCondition> condition();
/**
* Returns the columns to be returned in the query. If empty, all elements in the query will be returned.
* The implementation might ignore this option.
*
* @return the columns
*/
List<String> columns();
/**
* Retrieves the sorts contained in this {@link SelectQuery}.
* The implementation might ignore this option.
*
* @return the sorts
*/
List<Sort<?>> sorts();
/**
* Starts the first step of {@link SelectElements} creation using a fluent API.
* This step informs the fields to return to the query, similar to "select field, fieldB from database" in a database query.
*
* @param names - The document fields to query, optional.
* @return a new {@link SelectElements} instance
* @throws NullPointerException when any element is null
*/
static SelectElements select(String... names) {
Stream.of(names).forEach(d -> Objects.requireNonNull(d, "A document in the query is null"));
return new DefaultFluentSelectQueryBuilderElements(Arrays.asList(names));
}
/**
* Starts the first step of {@link SelectQuery} creation using a fluent API.
* This step informs the fields to return to the query, similar to "select field, fieldB from database" in a database query.
* If empty, all elements in the query will be returned, similar to "select * from database".
*
* @return a new {@link SelectElements} instance
*/
static SelectElements select() {
return new DefaultFluentSelectQueryBuilderElements(Collections.emptyList());
}
/**
* Starts the first step of {@link SelectQuery} creation using a builder pattern.
* This step informs the fields to return to the query, similar to "select field, fieldB from database" in a database query.
*
* @return a {@link QueryBuilder} instance
*/
static QueryBuilder builder() {
return new DefaultQueryBuilder();
}
/**
* Starts the first step of {@link SelectQuery} creation using a builder pattern.
* This step informs the fields to return to the query, similar to "select field, fieldB from database" in a database query.
* If empty, all elements in the query will be returned, similar to "select * from database".
*
* @param names The document fields to query, optional.
* @return a {@link QueryBuilder} instance
*/
static QueryBuilder builder(String... names) {
Stream.of(names).forEach(d -> Objects.requireNonNull(d, "A column in the query is null"));
QueryBuilder builder = new DefaultQueryBuilder();
Stream.of(names).forEach(builder::select);
return builder;
}
/**
* Represents a query builder interface for selecting columns from a NoSQL database.
*/
interface SelectFrom extends SelectQueryBuild {
/**
* Starts a new condition by defining the column name.
*
* @param name the column name
* @return a new {@link SelectNameCondition} representing the condition
* @throws NullPointerException when name is null
*/
SelectNameCondition where(String name);
/**
* Defines the position of the first result to retrieve.
*
* @param skip the position of the first result to retrieve
* @return a query with the specified first result position
* @throws IllegalArgumentException if skip is negative
*/
SelectSkip skip(long skip);
/**
* Defines the maximum number of results to retrieve.
*
* @param limit the maximum number of results to retrieve
* @return the {@link QueryBuilder} with the specified limit
* @throws IllegalArgumentException if limit is negative
*/
SelectLimit limit(long limit);
/**
* Specifies the order in which the results should be returned.
*
* @param name the name of the column to be ordered
* @return a query with the specified sort order
* @throws NullPointerException when name is null
*/
SelectOrder orderBy(String name);
}
/**
* Represents an interface for defining the maximum number of results to retrieve.
*/
interface SelectLimit extends SelectQueryBuild {
/**
* Defines the position of the first result to retrieve.
*
* @param skip the position of the first result to retrieve
* @return a query with the specified first result position
* @throws IllegalArgumentException if skip is negative
*/
SelectSkip skip(long skip);
}
/**
* Represents an interface for defining the order of columns in a query.
*/
interface SelectNameOrder extends SelectQueryBuild {
/**
* Specifies the order in which the results should be returned.
*
* @param name the name of the column to be ordered
* @return a query with the specified sort order
* @throws NullPointerException when name is null
*/
SelectOrder orderBy(String name);
/**
* Defines the position of the first result to retrieve.
*
* @param skip the position of the first result to retrieve
* @return a query with the specified first result position
* @throws IllegalArgumentException if skip is negative
*/
SelectSkip skip(long skip);
/**
* Defines the maximum number of results to retrieve.
*
* @param limit the maximum number of results to retrieve
* @return the {@link QueryBuilder} with the specified limit
* @throws IllegalArgumentException if limit is negative
*/
SelectLimit limit(long limit);
}
/**
* Defines the order direction for a query result.
*/
interface SelectOrder {
/**
* Defines the order as ascending.
*
* @return the {@link SelectNameOrder} instance
*/
SelectNameOrder asc();
/**
* Defines the order as descending.
*
* @return the {@link SelectNameOrder} instance
*/
SelectNameOrder desc();
}
/**
* Represents the final step in building a {@link SelectQuery}.
* It can either return a new {@link SelectQuery} instance or execute a query with a {@link DatabaseManager}.
*/
interface SelectQueryBuild {
/**
* Creates a new instance of {@link SelectQuery}.
*
* @return a new {@link SelectQuery} instance
*/
SelectQuery build();
/**
* Executes the query and returns the result as a stream of {@link CommunicationEntity}.
*
* @param manager the entity manager
* @return the result of the query as a stream of {@link CommunicationEntity}
* @throws NullPointerException when manager is null
*/
Stream<CommunicationEntity> getResult(DatabaseManager manager);
/**
* Executes the query and returns a single result wrapped in an optional {@link CommunicationEntity}.
*
* @param manager the entity manager
* @return the single result of the query wrapped in an optional {@link CommunicationEntity}
* @throws NullPointerException when manager is null
*/
Optional<CommunicationEntity> getSingleResult(DatabaseManager manager);
}
/**
* Represents the initial element in a column-based query.
*/
interface SelectElements {
/**
* Defines the entity to query.
*
* @param entity the entity to query
* @return a {@link SelectFrom} query
* @throws NullPointerException when entity is null
*/
SelectFrom from(String entity);
}
/**
* A provider class for {@link SelectElements}.
*/
interface SelectProvider extends Function<String[], SelectElements>, Supplier<SelectElements> {
}
/**
* A provider class for {@link QueryBuilder}.
*/
interface QueryBuilderProvider extends Function<String[], QueryBuilder>, Supplier<QueryBuilder> {
}
/**
* Represents the Column Order which defines the position of the first result to retrieve.
*/
interface SelectSkip extends SelectQueryBuild {
/**
* Defines the maximum number of results to retrieve.
* It will truncate to be no longer than the specified limit.
* The default value is zero, and it will replace the current property.
*
* @param limit the maximum number of results to retrieve
* @return the {@link QueryBuilder} instance
* @throws IllegalArgumentException if the limit is negative
*/
SelectLimit limit(long limit);
}
/**
* Represents the Column Where which defines the conditions in the query.
*/
interface SelectWhere extends SelectQueryBuild {
/**
* Starts a new condition in the select using {@link CriteriaCondition#and(CriteriaCondition)}.
*
* @param name a condition to be added
* @return the same {@link SelectNameCondition} with the condition appended
* @throws NullPointerException when the condition is null
*/
SelectNameCondition and(String name);
/**
* Appends a new condition in the select using {@link CriteriaCondition#or(CriteriaCondition)}.
*
* @param name a condition to be added
* @return the same {@link SelectNameCondition} with the condition appended
* @throws NullPointerException when the condition is null
*/
SelectNameCondition or(String name);
/**
* Defines the position of the first result to retrieve.
* It will depend on the NoSQL vendor implementation, but it will discard or skip the search result.
* The default value is zero, and it will replace the current property.
*
* @param skip the first result to retrieve
* @return a query with the first result defined
* @throws IllegalArgumentException if the limit is negative
*/
SelectSkip skip(long skip);
/**
* Defines the maximum number of results to retrieve.
* It will truncate to be no longer than the specified limit.
* The default value is zero, and it will replace the current property.
*
* @param limit the maximum number of results to retrieve
* @return the {@link QueryBuilder} instance
* @throws IllegalArgumentException if the limit is negative
*/
SelectLimit limit(long limit);
/**
* Adds the order of how the result will be returned.
*
* @param name the name to order
* @return a query with the sort defined
* @throws NullPointerException when the name is null
*/
SelectOrder orderBy(String name);
}
/**
* Represents the base for named conditions.
*/
interface SelectNameCondition {
/**
* Creates an equals condition.
*
* @param value the value for the condition
* @param <T> the type of the value
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
<T> SelectWhere eq(T value);
/**
* Creates a like condition.
*
* @param value the value for the condition
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
SelectWhere like(String value);
/**
* Creates a greater than condition.
*
* @param <T> the type of the value
* @param value the value for the condition
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
<T> SelectWhere gt(T value);
/**
* Creates a greater than or equals condition.
*
* @param <T> the type of the value
* @param value the value for the condition
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
<T> SelectWhere gte(T value);
/**
* Creates a lesser than condition.
*
* @param <T> the type of the value
* @param value the value for the condition
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
<T> SelectWhere lt(T value);
/**
* Creates a lesser than or equals condition.
*
* @param <T> the type of the value
* @param value the value for the condition
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the value is null
*/
<T> SelectWhere lte(T value);
/**
* Creates a between condition.
*
* @param <T> the type of the value
* @param valueA the start value of the range
* @param valueB the end value of the range
* @return the {@link SelectWhere} instance
* @throws NullPointerException if either valueA or valueB is null
*/
<T> SelectWhere between(T valueA, T valueB);
/**
* Creates an in condition.
*
* @param values the values for the condition
* @param <T> the type of the values
* @return the {@link SelectWhere} instance
* @throws NullPointerException if the values are null
*/
<T> SelectWhere in(Iterable<T> values);
/**
* Creates an equals condition.
*
* @return {@link SelectNotCondition}
*/
SelectNotCondition not();
}
/**
* Represents the negation of a condition.
*/
interface SelectNotCondition extends SelectNameCondition {
}
/**
* A builder interface for constructing queries using a fluent API.
* This builder supports the creation of {@link SelectQuery} instances using a builder pattern, providing more possibilities for complex queries.
* The ColumnQueryBuilder is not brighter than a fluent API; it has the same validation in the creation method.
* It is a mutable and non-thread-safe class.
*/
interface QueryBuilder {
/**
* Append a new column in the search result. The query will return the result by elements declared such as "select column from database".
* If it remains empty, it will return all the possible fields, similar to "select * from database".
*
* @param column a field to return to the search
* @return the {@link QueryBuilder}
* @throws NullPointerException when the column is null
*/
QueryBuilder select(String column);
/**
* Append new columns in the search result. The query will return the result by elements declared such as "select column from database".
* If it remains empty, it will return all the possible fields, similar to "select * from database".
*
* @param columns fields to return in the search
* @return the {@link QueryBuilder}
* @throws NullPointerException when there is a null element in columns
*/
QueryBuilder select(String... columns);
/**
* Append a new sort in the query. The first one has more precedence than the next one.
*
* @param sort the {@link Sort}
* @return the {@link QueryBuilder}
* @throws NullPointerException when the sort is null
*/
QueryBuilder sort(Sort<?> sort);
/**
* Append sorts in the query. The first one has more precedence than the next one.
*
* @param sorts an array of {@link Sort}
* @return the {@link QueryBuilder}
* @throws NullPointerException when there is a null element in sorts
*/
QueryBuilder sort(Sort<?>... sorts);
/**
* Define the column family in the query, this element is mandatory to build the {@link SelectQuery}.
*
* @param entity the entity to query
* @return the {@link QueryBuilder}
* @throws NullPointerException when the entity is null
*/
QueryBuilder from(String entity);
/**
* Either add or replace the condition in the query. It has a different behavior than the previous method because it won't append it.
* Therefore, it will create when it is the first time or replace when it was executed once.
*
* @param condition the {@link CriteriaCondition} in the query
* @return the {@link QueryBuilder}
* @throws NullPointerException when the condition is null
*/
QueryBuilder where(CriteriaCondition condition);
/**
* Defines the position of the first result to retrieve.
* It will depend on the NoSQL vendor implementation, but it will discard or skip the search result.
* The default value is zero, and it will replace the current property.
*
* @param skip the first result to retrieve
* @return a query with the first result defined
* @throws IllegalArgumentException if skip is negative
*/
QueryBuilder skip(long skip);
/**
* Defines the maximum number of results to retrieve.
* It will truncate to be no longer than limit.
* The default value is zero, and it will replace the current property.
*
* @param limit the limit
* @return the {@link QueryBuilder}
* @throws IllegalArgumentException if limit is negative
*/
QueryBuilder limit(long limit);
/**
* Validate and create a {@link SelectQuery} instance.
*
* @return {@link SelectQuery}
* @throws IllegalStateException when an element is not valid or not filled up, such as the {@link QueryBuilder#from(String)} method not being called
*/
SelectQuery build();
/**
* Execute {@link DatabaseManager#select(SelectQuery)}.
*
* @param manager the entity manager
* @return the result of {@link DatabaseManager#select(SelectQuery)}
* @throws NullPointerException when manager is null
* @throws IllegalStateException when an element is not valid or not filled up, such as the {@link QueryBuilder#from(String)} method not being called
*/
Stream<CommunicationEntity> getResult(DatabaseManager manager);
/**
* Execute {@link DatabaseManager#singleResult(SelectQuery)}.
*
* @param manager the entity manager
* @return the result of {@link DatabaseManager#singleResult(SelectQuery)}
* @throws NullPointerException when manager is null
* @throws IllegalStateException when an element is not valid or not filled up, such as the {@link QueryBuilder#from(String)} method not being called
*/
Optional<CommunicationEntity> getSingleResult(DatabaseManager manager);
}
}