package lama.tablegen.sqlite3;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

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

public class Sqlite3RowGenerator {

	public static void insertRows(Table table, Connection con, StateToReproduce state) throws SQLException {
		for (int i = 0; i < Main.NR_INSERT_ROW_TRIES; i++) {
			String query = insertRow(table);
			try (Statement s = con.createStatement()) {
				state.statements.add(query);
				s.execute(query);
			} catch (Exception e) {
				e.printStackTrace();
				System.err.println(state);
			}
		}
	}

	private static String insertRow(Table table) {
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT OR IGNORE INTO " + table.getName());
		if (Randomly.getBooleanWithSmallProbability()) {
			sb.append(" DEFAULT VALUES");
		} else {
			sb.append("(");
			List<Column> columns = table.getColumns();
			for (int i = 0; i < columns.size(); i++) {
				if (i != 0) {
					sb.append(", ");
				}
				sb.append(columns.get(i).getName());
			}
			sb.append(") VALUES (");
			for (int i = 0; i < columns.size(); i++) {
				if (i != 0) {
					sb.append(", ");
				}
				Expression randomLiteral = SQLite3ExpressionGenerator.getRandomLiteralValue(false);
				SQLite3Visitor visitor = new SQLite3Visitor();
				visitor.visit(randomLiteral);
				sb.append(visitor.get());
			}
			sb.append(")");
		}
		return sb.toString();
	}

}

@@ -1059,29 +1059,295 @@
 s = 
-table.getColumns();%0A%09
+appendColumnNames(table, sb);%0A%09%09%09sb.append(%22)%22);%0A%09%09%09sb.append(%22 VALUES %22);%0A%09%09%09int nrValues = 1 + Randomly.smallNumber();%0A%09%09%09appendNrValues(sb, columns, nrValues);%0A%09%09%7D%0A%09%09return sb.toString();%0A%09%7D%0A%0A%09private static void appendNrValues(StringBuilder sb, List%3CColumn%3E columns, int nrValues) %7B%0A
 %09%09fo
@@ -1360,38 +1360,32 @@
  i = 0; i %3C 
-columns.size()
+nrValues
 ; i++) %7B%0A%09%09%09
@@ -1373,33 +1373,32 @@
 rValues; i++) %7B%0A
-%09
 %09%09%09if (i != 0) %7B
@@ -1394,33 +1394,32 @@
  (i != 0) %7B%0A%09%09%09%09
-%09
 sb.append(%22, %22);
@@ -1422,20 +1422,18 @@
  %22);%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09sb.ap
@@ -1441,69 +1441,145 @@
 end(
-columns.get(i).getName()
+%22(%22);%0A%09%09%09appendValue(sb, columns);%0A%09%09%09sb.append(%22)%22
 );%0A%09%09
+%7D%0A
 %09%7D%0A
-%09%09%09sb.append(%22) VALUES (%22);%0A%09
+%0A%09private static void appendValue(StringBuilder sb, List%3CColumn%3E columns) %7B%0A
 %09%09fo
@@ -1619,17 +1619,16 @@
  i++) %7B%0A
-%09
 %09%09%09if (i
@@ -1640,17 +1640,16 @@
 ) %7B%0A%09%09%09%09
-%09
 sb.appen
@@ -1660,20 +1660,18 @@
  %22);%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09Expre
@@ -1752,17 +1752,16 @@
 se);%0A%09%09%09
-%09
 SQLite3V
@@ -1799,17 +1799,16 @@
 itor();%0A
-%09
 %09%09%09visit
@@ -1831,25 +1831,24 @@
 iteral);%0A%09%09%09
-%09
 sb.append(vi
@@ -1859,27 +1859,218 @@
 r.get());%0A%09%09
+%7D%0A
 %09%7D%0A
+%0A%09private static List%3CColumn%3E appendColumnNames(Table table, StringBuilder sb) %7B%0A%09%09List%3CColumn%3E columns = table.getColumns();%0A%09%09for (int i = 0; i %3C columns.size(); i++) %7B%0A%09%09%09if (i != 0) %7B%0A%09
 %09%09%09sb.append
@@ -2075,39 +2075,79 @@
 nd(%22
-)
+, 
 %22);%0A%09%09
+%09
 %7D%0A%09%09
-return sb.toString()
+%09sb.append(columns.get(i).getName());%0A%09%09%7D%0A%09%09return columns
 ;%0A%09%7D

Allow inserting multiple values at once