package lama.sqlite3.gen;

import java.sql.Connection;
import java.sql.SQLException;

import lama.Query;
import lama.QueryAdapter;
import lama.Randomly;
import lama.Main.StateToReproduce;

public class SQLite3TransactionGenerator {

	public static Query generateCommit(Connection con, StateToReproduce state) {
		return new QueryAdapter("COMMIT;") {
			@Override
			public void execute(Connection con) throws SQLException {
				try {
					super.execute(con);
				} catch (SQLException e) {
					// TODO ignore for now
				}
			}
		};
	}
	
	public static Query generateBeginTransaction(Connection con, StateToReproduce state) {
		StringBuilder sb = new StringBuilder();
		sb.append("BEGIN ");
		sb.append(Randomly.fromOptions("DEFERRED", "IMMEDIATE", "EXCLUSIVE"));
		sb.append(" TRANSACTION;");
		return new QueryAdapter(sb.toString()) {
			@Override
			public void execute(Connection con) throws SQLException {
				try {
					super.execute(con);
				} catch (SQLException e) {
					if (!e.getMessage().contentEquals("[SQLITE_ERROR] SQL error or missing database (cannot start a transaction within a transaction)")) {
						throw e;
					}
				}
			}
		};
	}
	
	public static Query generateRollbackTransaction(Connection con, StateToReproduce state) {
		return new QueryAdapter("ROLLBACK TRANSACTION;") {
			@Override
			public void execute(Connection con) throws SQLException {
				try {
					super.execute(con);
				} catch (SQLException e) {
					if (!e.getMessage().contentEquals("[SQLITE_ERROR] SQL error or missing database (cannot rollback - no transaction is active)")) {
						throw e;
					}
				}
			}

			@Override
			public boolean couldAffectSchema() {
				return true;
			}
			
		};
	}


}

@@ -513,32 +513,108 @@
 r now%0A%09%09%09%09%7D%0A%09%09%09%7D
+%0A%0A%09%09%09@Override%0A%09%09%09public boolean couldAffectSchema() %7B%0A%09%09%09%09return true;%0A%09%09%09%7D
 %0A%09%09%7D;%0A%09%7D%0A%09%0A%09publ
@@ -598,34 +598,33 @@
 e;%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A
-%09
 %0A
+
 %09public static Q
@@ -1077,32 +1077,40 @@
 ).contentEquals(
+%0A%09%09%09%09%09%09%09
 %22%5BSQLITE_ERROR%5D 
@@ -1235,18 +1235,17 @@
 %09%09%7D;%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -1559,16 +1559,24 @@
 tEquals(
+%0A%09%09%09%09%09%09%09
 %22%5BSQLITE
@@ -1776,20 +1776,16 @@
 %09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%7D;%0A
+
 %09%7D%0A%0A
-%0A
 %7D%0A

Add missing couldAffectSchema declaration