package sqlancer.tidb.gen;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import sqlancer.Query;
import sqlancer.QueryAdapter;
import sqlancer.Randomly;
import sqlancer.tidb.TiDBExpressionGenerator;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.TiDBSchema.TiDBTable;
import sqlancer.tidb.visitor.TiDBVisitor;

public class TiDBDeleteGenerator {

	public static Query getQuery(TiDBGlobalState globalState) throws SQLException {
		Set<String> errors = new HashSet<>();
		TiDBTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
		TiDBExpressionGenerator gen = new TiDBExpressionGenerator(globalState).setColumns(table.getColumns());
		StringBuilder sb = new StringBuilder("DELETE FROM ");
		sb.append(table.getName());
		if (Randomly.getBoolean()) {
			sb.append(" WHERE ");
			sb.append(TiDBVisitor.asString(gen.generateExpression()));
			errors.add("Truncated incorrect");
			errors.add("Data truncation");
		}
		return new QueryAdapter(sb.toString(), errors);
		
	}
	
}

@@ -98,16 +98,52 @@
 til.Set;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -215,16 +215,49 @@
 ndomly;%0A
+import sqlancer.tidb.TiDBErrors;%0A
 import s
@@ -822,234 +822,843 @@
 ETE 
-FROM %22);%0A%09%09sb.append(table.getName());%0A%09%09if (Randomly.getBoolean()) %7B%0A%09%09%09sb.append(%22 WHERE %22);%0A%09%09%09sb.append(TiDBVisitor.asString(gen.generateExpression()));%0A%09%09%09errors.add(%22Truncated incorrect%22);%0A%09%09%09errors.add(%22Data truncation%22
+%22);%0A%09%09if (Randomly.getBooleanWithSmallProbability()) %7B%0A%09%09%09sb.append(%22LOW_PRIORITY %22);%0A%09%09%7D%0A%09%09if (Randomly.getBooleanWithSmallProbability()) %7B%0A%09%09%09sb.append(%22QUICK %22);%0A%09%09%7D%0A%09%09if (Randomly.getBooleanWithSmallProbability()) %7B%0A%09%09%09sb.append(%22IGNORE %22);%0A%09%09%7D%0A%09%09sb.append(%22FROM %22);%0A%09%09sb.append(table.getName());%0A%09%09if (Randomly.getBoolean()) %7B%0A%09%09%09sb.append(%22 WHERE %22);%0A%09%09%09sb.append(TiDBVisitor.asString(gen.generateExpression()));%0A%09%09%09errors.add(%22Truncated incorrect%22);%0A%09%09%09errors.add(%22Data truncation%22);%0A%09%09%7D%0A%09%09if (Randomly.getBoolean()) %7B%0A%09%09%09sb.append(%22 ORDER BY %22);%0A%09%09%09TiDBErrors.addExpressionErrors(errors);%0A%09%09%09sb.append(gen.generateOrderBys().stream().map(o -%3E TiDBVisitor.asString(o)).collect(Collectors.joining(%22, %22)));%0A%09%09%7D%0A%09%09if (Randomly.getBoolean()) %7B%0A%09%09%09sb.append(%22 LIMIT %22);%0A%09%09%09sb.append(Randomly.getNotCachedInteger(0, Integer.MAX_VALUE)
 );%0A%09

Enhance DELETE