package lama.tablegen.sqlite3;

import java.sql.Connection;
import java.sql.Statement;

import lama.Randomly;
import lama.Main.ReduceMeException;
import lama.Main.StateToReproduce;
import lama.schema.Schema.Table;
import lama.sqlite3.SQLite3Visitor;

public class SQLite3DeleteGenerator {

	public static void deleteContent(Table tableName, Connection con, StateToReproduce state) {
		try {
			try (Statement s = con.createStatement()) {
				String query = new SQLite3DeleteGenerator().getDeleteQuery(tableName);
				state.statements.add(query);
				s.execute(query);
			}
		} catch (Throwable e) {
			if (e.getMessage().startsWith("[SQLITE_ERROR] SQL error or missing database (integer overflow)")) {
				return;
			}
			state.logInconsistency(e);
			throw new ReduceMeException();
		}

	}

	private final StringBuilder sb = new StringBuilder();

	private String getDeleteQuery(Table tableName) {
		sb.append("DELETE FROM ");
		sb.append(tableName.getName());
		if (Randomly.getBoolean()) {
			sb.append(" WHERE ");
			sb.append(SQLite3Visitor
					.asString(SQLite3ExpressionGenerator.getRandomExpression(tableName.getColumns(), false)));
		}
		return sb.toString();
	}

}

@@ -707,16 +707,286 @@
 return;%0A
+%09%09%09%7D else if (e.getMessage().startsWith(%22%5BSQLITE_ERROR%5D SQL error or missing database (foreign key mismatch%22)) %7B%0A%09%09%09%09return;%0A%09%09%09%7D else if (e.getMessage().startsWith(%22%5BSQLITE_CONSTRAINT%5D  Abort due to constraint violation (FOREIGN KEY constraint failed)%22)) %7B%0A%09%09%09%09return;%0A
 %09%09%09%7D%0A%09%09%09

Check for additional errors when deleting rows