package com.github.ledoyen.automocker.extension.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Connections {

	public static List<String> tables(Connection c) {
		List<String> tables = new ArrayList<>();
		try (PreparedStatement ps = c.prepareStatement("SHOW TABLES"); ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				tables.add(rs.getString(1));
			}
		} catch (SQLException e) {
			throw new IllegalStateException("Unable to list tables", e);
		}
		return tables;
	}

	public static void truncate(Connection c, String tableName) {
		try (PreparedStatement p = c.prepareStatement("TRUNCATE TABLE " + tableName)) {
			p.execute();
		} catch (SQLException e) {
			throw new IllegalStateException("Could not truncate Table [" + tableName + "]", e);
		}
	}
}

@@ -665,22 +665,21 @@
 ic void 
-trunca
+execu
 te(Conne
@@ -694,25 +694,25 @@
  String 
+s
 ta
-bleName
+tement
 ) %7B%0D%0A%09%09t
@@ -760,57 +760,825 @@
 ent(
-%22TRUNCATE TABLE %22 + tableName)) %7B%0D%0A%09%09%09p.execute()
+statement)) %7B%0D%0A%09%09%09p.execute();%0D%0A%09%09%7D catch (SQLException e) %7B%0D%0A%09%09%09throw new IllegalStateException(%22Could not execute Statement %5B%22 + statement + %22%5D%22, e);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09public static void truncate(Connection c, String tableName) %7B%0D%0A%09%09execute(c, %22TRUNCATE TABLE %22 + tableName);%0D%0A%09%7D%0D%0A%0D%0A%09public static Object selectFirstLines(Connection c, String tableName, int maxLines) %7B%0D%0A%09%09try (PreparedStatement p = c.prepareStatement(%22SELECT TOP %22 + maxLines + %22 * FROM %22 + tableName);%0D%0A%09%09%09%09ResultSet rs = p.executeQuery()) %7B%0D%0A%0D%0A%09%09%09int columns = rs.getMetaData()%0D%0A%09%09%09%09%09.getColumnCount();%0D%0A%09%09%09List%3CList%3CObject%3E%3E result = new ArrayList%3C%3E();%0D%0A%09%09%09while (rs.next()) %7B%0D%0A%09%09%09%09List%3CObject%3E line = new ArrayList%3C%3E();%0D%0A%09%09%09%09for (int i = 1; i %3C= columns; i++) %7B%0D%0A%09%09%09%09%09line.add(rs.getObject(i));%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09result.add(line);%0D%0A%09%09%09%7D%0D%0A%09%09%09return result
 ;%0D%0A%09
@@ -1652,24 +1652,22 @@
 uld not 
-truncate
+select
  Table %5B
@@ -1701,11 +1701,13 @@
 %09%7D%0D%0A%09%7D%0D%0A
+%0D%0A
 %7D%0D%0A

add SQL execute utility