|
| 1 | +package sqlancer.yugabyte.ysql.gen; |
| 2 | + |
| 3 | +import sqlancer.IgnoreMeException; |
| 4 | +import sqlancer.Randomly; |
| 5 | +import sqlancer.common.query.ExpectedErrors; |
| 6 | +import sqlancer.common.query.SQLQueryAdapter; |
| 7 | +import sqlancer.yugabyte.ysql.YSQLErrors; |
| 8 | +import sqlancer.yugabyte.ysql.YSQLGlobalState; |
| 9 | +import sqlancer.yugabyte.ysql.YSQLSchema.YSQLColumn; |
| 10 | +import sqlancer.yugabyte.ysql.YSQLSchema.YSQLDataType; |
| 11 | +import sqlancer.yugabyte.ysql.YSQLSchema.YSQLTable; |
| 12 | +import sqlancer.yugabyte.ysql.YSQLVisitor; |
| 13 | + |
| 14 | +public final class YSQLCursorGenerator { |
| 15 | + |
| 16 | + private static final int MAX_CURSORS = 5; |
| 17 | + |
| 18 | + private YSQLCursorGenerator() { |
| 19 | + } |
| 20 | + |
| 21 | + public static SQLQueryAdapter generate(YSQLGlobalState globalState) { |
| 22 | + ExpectedErrors errors = new ExpectedErrors(); |
| 23 | + YSQLErrors.addTransactionErrors(errors); |
| 24 | + YSQLErrors.addCommonExpressionErrors(errors); |
| 25 | + errors.add("does not exist"); |
| 26 | + errors.add("already in use"); |
| 27 | + errors.add("is not open"); |
| 28 | + errors.add("no such cursor"); |
| 29 | + errors.add("cursor can only scan forward"); |
| 30 | + errors.add("DECLARE CURSOR can only be used in transaction blocks"); |
| 31 | + errors.add("is not a simply updatable relation"); |
| 32 | + errors.add("no result set"); |
| 33 | + errors.add("portal"); |
| 34 | + |
| 35 | + switch (Randomly.fromOptions(0, 1, 2, 3)) { |
| 36 | + case 0: |
| 37 | + return generateDeclare(globalState, errors); |
| 38 | + case 1: |
| 39 | + return generateFetch(errors); |
| 40 | + case 2: |
| 41 | + return generateMove(errors); |
| 42 | + case 3: |
| 43 | + return generateClose(errors); |
| 44 | + default: |
| 45 | + throw new AssertionError(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static SQLQueryAdapter generateDeclare(YSQLGlobalState globalState, ExpectedErrors errors) { |
| 50 | + if (globalState.getSchema().getDatabaseTables().isEmpty()) { |
| 51 | + throw new IgnoreMeException(); |
| 52 | + } |
| 53 | + YSQLTable table = globalState.getSchema().getRandomTable(); |
| 54 | + StringBuilder sb = new StringBuilder(); |
| 55 | + String cursorName = "cur" + globalState.getRandomly().getInteger(0, MAX_CURSORS); |
| 56 | + sb.append("DECLARE ").append(cursorName); |
| 57 | + if (Randomly.getBoolean()) { |
| 58 | + sb.append(" BINARY"); |
| 59 | + } |
| 60 | + if (Randomly.getBoolean()) { |
| 61 | + sb.append(Randomly.fromOptions(" SCROLL", " NO SCROLL")); |
| 62 | + } |
| 63 | + sb.append(" CURSOR"); |
| 64 | + if (Randomly.getBoolean()) { |
| 65 | + sb.append(Randomly.fromOptions(" WITH HOLD", " WITHOUT HOLD")); |
| 66 | + } |
| 67 | + sb.append(" FOR SELECT "); |
| 68 | + YSQLColumn col = table.getRandomColumn(); |
| 69 | + sb.append(col.getName()); |
| 70 | + sb.append(" FROM ").append(table.getName()); |
| 71 | + if (Randomly.getBoolean()) { |
| 72 | + sb.append(" WHERE "); |
| 73 | + sb.append(YSQLVisitor.asString( |
| 74 | + YSQLExpressionGenerator.generateExpression(globalState, table.getColumns(), YSQLDataType.BOOLEAN))); |
| 75 | + } |
| 76 | + if (Randomly.getBoolean()) { |
| 77 | + sb.append(" ORDER BY ").append(col.getName()); |
| 78 | + } |
| 79 | + return new SQLQueryAdapter(sb.toString(), errors); |
| 80 | + } |
| 81 | + |
| 82 | + private static SQLQueryAdapter generateFetch(ExpectedErrors errors) { |
| 83 | + StringBuilder sb = new StringBuilder(); |
| 84 | + sb.append("FETCH "); |
| 85 | + appendDirection(sb); |
| 86 | + sb.append(" FROM cur").append(Randomly.smallNumber() % MAX_CURSORS); |
| 87 | + return new SQLQueryAdapter(sb.toString(), errors); |
| 88 | + } |
| 89 | + |
| 90 | + private static SQLQueryAdapter generateMove(ExpectedErrors errors) { |
| 91 | + StringBuilder sb = new StringBuilder(); |
| 92 | + sb.append("MOVE "); |
| 93 | + appendDirection(sb); |
| 94 | + sb.append(" FROM cur").append(Randomly.smallNumber() % MAX_CURSORS); |
| 95 | + return new SQLQueryAdapter(sb.toString(), errors); |
| 96 | + } |
| 97 | + |
| 98 | + private static SQLQueryAdapter generateClose(ExpectedErrors errors) { |
| 99 | + StringBuilder sb = new StringBuilder(); |
| 100 | + sb.append("CLOSE "); |
| 101 | + if (Randomly.getBoolean()) { |
| 102 | + sb.append("ALL"); |
| 103 | + } else { |
| 104 | + sb.append("cur").append(Randomly.smallNumber() % MAX_CURSORS); |
| 105 | + } |
| 106 | + return new SQLQueryAdapter(sb.toString(), errors); |
| 107 | + } |
| 108 | + |
| 109 | + private static void appendDirection(StringBuilder sb) { |
| 110 | + switch (Randomly.fromOptions(0, 1, 2, 3, 4, 5, 6, 7)) { |
| 111 | + case 0: |
| 112 | + sb.append("NEXT"); |
| 113 | + break; |
| 114 | + case 1: |
| 115 | + sb.append("PRIOR"); |
| 116 | + break; |
| 117 | + case 2: |
| 118 | + sb.append("FIRST"); |
| 119 | + break; |
| 120 | + case 3: |
| 121 | + sb.append("LAST"); |
| 122 | + break; |
| 123 | + case 4: |
| 124 | + sb.append("FORWARD"); |
| 125 | + break; |
| 126 | + case 5: |
| 127 | + sb.append("BACKWARD"); |
| 128 | + break; |
| 129 | + case 6: |
| 130 | + sb.append("FORWARD ").append(Randomly.smallNumber()); |
| 131 | + break; |
| 132 | + case 7: |
| 133 | + sb.append("ABSOLUTE ").append(Randomly.smallNumber()); |
| 134 | + break; |
| 135 | + default: |
| 136 | + throw new AssertionError(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments