|
| 1 | +package sqlancer.mariadb.gen; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +import sqlancer.Randomly; |
| 6 | +import sqlancer.common.query.ExpectedErrors; |
| 7 | +import sqlancer.common.query.SQLQueryAdapter; |
| 8 | +import sqlancer.common.schema.AbstractTables; |
| 9 | +import sqlancer.mariadb.MariaDBSchema; |
| 10 | +import sqlancer.mariadb.MariaDBSchema.MariaDBColumn; |
| 11 | +import sqlancer.mariadb.MariaDBSchema.MariaDBTable; |
| 12 | +import sqlancer.mariadb.ast.MariaDBVisitor; |
| 13 | + |
| 14 | +public final class MariaDBDeleteGenerator { |
| 15 | + |
| 16 | + private MariaDBDeleteGenerator() { |
| 17 | + } |
| 18 | + |
| 19 | + public static SQLQueryAdapter delete(MariaDBSchema schema, Randomly r) { |
| 20 | + MariaDBTable table = schema.getRandomTable(); |
| 21 | + |
| 22 | + MariaDBExpressionGenerator expressionGenerator = new MariaDBExpressionGenerator(r); |
| 23 | + |
| 24 | + AbstractTables<MariaDBTable, MariaDBColumn> tablesAndColumns = new AbstractTables<>( |
| 25 | + Collections.singletonList(table)); |
| 26 | + expressionGenerator.setTablesAndColumns(tablesAndColumns); |
| 27 | + |
| 28 | + ExpectedErrors errors = new ExpectedErrors(); |
| 29 | + |
| 30 | + errors.add("foreign key constraint fails"); |
| 31 | + errors.add("cannot delete or update a parent row"); |
| 32 | + errors.add("Data truncated"); |
| 33 | + errors.add("Division by 0"); |
| 34 | + errors.add("Incorrect value"); |
| 35 | + |
| 36 | + StringBuilder sb = new StringBuilder("DELETE"); |
| 37 | + |
| 38 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 39 | + sb.append(" LOW_PRIORITY"); |
| 40 | + } |
| 41 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 42 | + sb.append(" QUICK"); |
| 43 | + } |
| 44 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 45 | + sb.append(" IGNORE"); |
| 46 | + } |
| 47 | + |
| 48 | + sb.append(" FROM "); |
| 49 | + sb.append(table.getName()); |
| 50 | + |
| 51 | + if (Randomly.getBoolean()) { |
| 52 | + sb.append(" WHERE "); |
| 53 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 54 | + sb.append(MariaDBVisitor.asString(MariaDBExpressionGenerator.getRandomConstant(r))); |
| 55 | + } else { |
| 56 | + sb.append(MariaDBVisitor.asString(expressionGenerator.getRandomExpression())); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // ORDER BY + LIMIT |
| 61 | + if (Randomly.getBooleanWithRatherLowProbability() && !table.getColumns().isEmpty()) { |
| 62 | + sb.append(" ORDER BY "); |
| 63 | + sb.append(Randomly.fromList(table.getColumns()).getName()); |
| 64 | + if (Randomly.getBoolean()) { |
| 65 | + sb.append(Randomly.getBoolean() ? " ASC" : " DESC"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 70 | + sb.append(" LIMIT "); |
| 71 | + sb.append(Randomly.getNotCachedInteger(1, 10)); |
| 72 | + } |
| 73 | + |
| 74 | + // RETURNING clause (MariaDB >= 10.5) |
| 75 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 76 | + sb.append(" RETURNING "); |
| 77 | + if (Randomly.getBooleanWithRatherLowProbability()) { |
| 78 | + sb.append(MariaDBVisitor.asString(MariaDBExpressionGenerator.getRandomConstant(r))); |
| 79 | + } else { |
| 80 | + sb.append(MariaDBVisitor.asString(expressionGenerator.getRandomExpression())); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + String query = sb.toString(); |
| 85 | + if (query.contains("RLIKE") || query.contains("REGEXP")) { |
| 86 | + errors.add("Regex error"); |
| 87 | + errors.add("quantifier does not follow a repeatable item"); |
| 88 | + errors.add("Got error"); |
| 89 | + } |
| 90 | + |
| 91 | + return new SQLQueryAdapter(query, errors); |
| 92 | + } |
| 93 | +} |
0 commit comments