package com.github.fjdbc.sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SqlUtils {
	/**
	 * Partition a list into sublists of length L. The last list may have a size smaller than L.<br>
	 * The sublists are backed by the original list.
	 */
	public static <T> List<List<T>> partition(Collection<T> collection, final int partitionSize) {
		assert partitionSize > 0;
		assert collection != null;
		return partitionList(new ArrayList<T>(collection), partitionSize);
	}

	/**
	 * Partition a list into sublists of length L. The last list may have a size smaller than L.<br>
	 * The sublists are backed by the original list.
	 */
	private static <T> List<List<T>> partitionList(List<T> list, final int partitionSize) {
		assert partitionSize > 0;
		assert list != null;
		final List<List<T>> res = new ArrayList<List<T>>();
		for (int i = 0; i < list.size(); i += partitionSize) {
			res.add(list.subList(i, Math.min(list.size(), i + partitionSize)));
		}
		return res;
	}

	public static String escapeComment(String comment) {
		final String res = comment
				.replace("/*", "\\slash \\star")
				.replace("*/", "\\star \\slash")
				.replace("--", "\\minus \\minus");
		return res;
	}

	public static String escapeString(String text) {
		return text.replace("'", "''");
	}

	public static String escapeLikeString(String text, char escapeChar) {
		String res = escapeString(text);
		final String escapeString = String.valueOf(escapeChar);
		res = res.replace(escapeString, escapeString + escapeChar);
		res = res.replace("%", escapeString + "%");
		res = res.replace("_", escapeString + "_");
		return res;
	}

	public static String toLiteralString(String s) {
		return "'" + escapeString(s) + "'";
	}
}

@@ -491,17 +491,16 @@
 rayList%3C
-T
 %3E(collec
@@ -519,24 +519,24 @@
 tionSize);%0D%0A
+
 %09%7D%0D%0A%0D%0A%09/**%0D%0A
@@ -876,23 +876,16 @@
 rayList%3C
-List%3CT%3E
 %3E();%0D%0A%09%09
@@ -1030,32 +1030,70 @@
 urn res;%0D%0A%09%7D%0D%0A%0D%0A
+%09/**%0D%0A%09 * Escape a SQL comment%0D%0A%09 */%0D%0A
 %09public static S
@@ -1131,16 +1131,37 @@
 ent) %7B%0D%0A
+%09%09// @formatter:off%0D%0A
 %09%09final 
@@ -1294,24 +1294,44 @@
 %5C%5Cminus%22);%0D%0A
+%09%09// @formatter:on%0D%0A
 %09%09return res
@@ -1331,32 +1331,77 @@
 urn res;%0D%0A%09%7D%0D%0A%0D%0A
+%09/**%0D%0A%09 * Escape a SQL string literal%0D%0A%09 */%0D%0A
 %09public static S
@@ -1813,32 +1813,89 @@
 urn res;%0D%0A%09%7D%0D%0A%0D%0A
+%09/**%0D%0A%09 * Convert a Java string to a SQL literal.%0D%0A%09 */%0D%0A
 %09public static S

add javadoc