|
1 | 1 | package sqlancer.h2; |
2 | 2 |
|
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
3 | 6 | import sqlancer.Randomly; |
4 | 7 | import sqlancer.common.ast.BinaryOperatorNode.Operator; |
| 8 | +import sqlancer.common.gen.TLPWhereGenerator; |
5 | 9 | import sqlancer.common.gen.UntypedExpressionGenerator; |
| 10 | +import sqlancer.common.schema.AbstractTables; |
6 | 11 | import sqlancer.h2.H2Provider.H2GlobalState; |
7 | 12 | import sqlancer.h2.H2Schema.H2Column; |
8 | 13 | import sqlancer.h2.H2Schema.H2CompositeDataType; |
9 | 14 | import sqlancer.h2.H2Schema.H2DataType; |
| 15 | +import sqlancer.h2.H2Schema.H2Table; |
10 | 16 | import sqlancer.h2.ast.H2BetweenOperation; |
11 | 17 | import sqlancer.h2.ast.H2BinaryOperation; |
12 | 18 | import sqlancer.h2.ast.H2CaseOperation; |
|
15 | 21 | import sqlancer.h2.ast.H2Constant; |
16 | 22 | import sqlancer.h2.ast.H2Expression; |
17 | 23 | import sqlancer.h2.ast.H2InOperation; |
| 24 | +import sqlancer.h2.ast.H2Join; |
| 25 | +import sqlancer.h2.ast.H2Select; |
| 26 | +import sqlancer.h2.ast.H2TableReference; |
18 | 27 | import sqlancer.h2.ast.H2UnaryPostfixOperation; |
19 | 28 | import sqlancer.h2.ast.H2UnaryPrefixOperation; |
20 | 29 |
|
21 | | -public class H2ExpressionGenerator extends UntypedExpressionGenerator<H2Expression, H2Column> { |
| 30 | +public class H2ExpressionGenerator extends UntypedExpressionGenerator<H2Expression, H2Column> |
| 31 | + implements TLPWhereGenerator<H2Select, H2Join, H2Expression, H2Table, H2Column> { |
22 | 32 |
|
23 | 33 | private final H2GlobalState globalState; |
| 34 | + private List<H2Table> tables; |
24 | 35 |
|
25 | 36 | public H2ExpressionGenerator(H2GlobalState globalState) { |
26 | 37 | this.globalState = globalState; |
@@ -336,4 +347,45 @@ public H2Expression isNull(H2Expression expr) { |
336 | 347 | return new H2UnaryPostfixOperation(expr, H2UnaryPostfixOperator.IS_NULL); |
337 | 348 | } |
338 | 349 |
|
| 350 | + @Override |
| 351 | + public TLPWhereGenerator<H2Select, H2Join, H2Expression, H2Table, H2Column> setTablesAndColumns( |
| 352 | + AbstractTables<H2Table, H2Column> tables) { |
| 353 | + this.columns = tables.getColumns(); |
| 354 | + this.tables = tables.getTables(); |
| 355 | + |
| 356 | + return this; |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public H2Expression generateBooleanExpression() { |
| 361 | + return generateExpression(); |
| 362 | + } |
| 363 | + |
| 364 | + @Override |
| 365 | + public H2Select generateSelect() { |
| 366 | + return new H2Select(); |
| 367 | + } |
| 368 | + |
| 369 | + @Override |
| 370 | + public List<H2Join> getRandomJoinClauses() { |
| 371 | + List<H2TableReference> tableList = tables.stream().map(t -> new H2TableReference(t)) |
| 372 | + .collect(Collectors.toList()); |
| 373 | + List<H2Join> joins = H2Join.getJoins(tableList, globalState); |
| 374 | + tables = tableList.stream().map(t -> t.getTable()).collect(Collectors.toList()); |
| 375 | + return joins; |
| 376 | + } |
| 377 | + |
| 378 | + @Override |
| 379 | + public List<H2Expression> getTableRefs() { |
| 380 | + return tables.stream().map(t -> new H2TableReference(t)).collect(Collectors.toList()); |
| 381 | + } |
| 382 | + |
| 383 | + @Override |
| 384 | + public List<H2Expression> generateFetchColumns(boolean shouldCreateDummy) { |
| 385 | + if (shouldCreateDummy && Randomly.getBoolean()) { |
| 386 | + return List.of(new H2ColumnReference(new H2Column("*", null))); |
| 387 | + } |
| 388 | + return Randomly.nonEmptySubset(this.columns).stream().map(c -> new H2ColumnReference(c)) |
| 389 | + .collect(Collectors.toList()); |
| 390 | + } |
339 | 391 | } |
0 commit comments