package com.impossibl.postgres.jdbc;

import static com.impossibl.postgres.jdbc.SQLTextUtils.getProtocolSQLText;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.sql.SQLException;

import org.junit.Test;


public class SQLTextTests {

	String[][] sqlTransformTests = new String[][] {
		new String[] {
				
				//Input
				"select \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	from\n" +
				"		test\n" +
				"	where\n" +
				"		'a string with a ?' =  ?",
				
				//Output
				"select \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	from\n" +
				"		test\n" +
				"	where\n" +
				"		'a string with a ?' =  $1"
		},			
		new String[] {
	
				//Input
				"insert into \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	(a, \"b\", \"c\", \"d\")\n" +
				"	values /* a nested\n" +
				" /* comment with  */ a ? */" +
				"	(?,'a string with a ?', \"another ?\", ?, ?)",
			
				//Output
				"insert into \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	(a, \"b\", \"c\", \"d\")\n" +
				"	values /* a nested\n" +
				" /* comment with  */ a ? */" +
				"	($1,'a string with a ?', \"another ?\", $2, $3)",
		},
		new String[] {
				
				//Input
				"insert into \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	(a, \"b\", \"c\", \"d\")\n" +
				"	values /* a nested\n" +
				" /* comment with  */ a ? */" +
				"	(?,'a string with a ?', \"another  \"\" ?\", {fn concat('{fn '' some()}', {fn char(?)})}, ?)",
			
				//Output
				"insert into \"somthing\" -- This is a SQL comment ?WTF?\n" +
				"	(a, \"b\", \"c\", \"d\")\n" +
				"	values /* a nested\n" +
				" /* comment with  */ a ? */" +
				"	($1,'a string with a ?', \"another \"\" ?\", concat('{fn '' some()}', insert($2)), $3), $4)",
		},
		new String[] {
				"select {fn abs(-10)} as absval, {fn user()}, {fn concat(x,y)} as val from {oj tblA left outer join tblB on x=y}",
				"select abs(-10) as absval, concat(x,y) as val from tblA left outer join tblB ON (x=y)",
		}
	};

	/**
	 * Tests transforming JDBC SQL input into PostgreSQL's wire
	 * protocol format. 
	 * @throws SQLException 
	 * 
	 * @see SQLTextUtils.getProtocolSQLText 
	 */
	@Test
	public void testPostgreSQLText() throws SQLException {

		for(String[] test : sqlTransformTests) {
			
			String expected = test[1];
			String output = getProtocolSQLText(test[0], true, null);
			
			assertThat(output, is(equalTo(expected)));
		}
	}
	
	@Test
	public void testParse() throws SQLException {
		
		
		for(String[] test : sqlTransformTests) {
			
			SQLText text = new SQLText(test[0]);
			
			SQLTextEscapes.processEscapes(text, null);
			
			System.out.println(text);
		}
	}

}

@@ -721,10 +721,11 @@
   $1
+;
 %22%0A
-
 %09%09%7D,
@@ -1218,16 +1218,17 @@
  $2, $3)
+;
 %22,%0A%09%09%7D,%0A
@@ -1469,17 +1469,16 @@
 another 
- 
 %5C%22%5C%22 ?%5C%22
@@ -1765,22 +1765,16 @@
 %5C%22 ?%5C%22, 
-concat
 ('%7Bfn ''
@@ -1786,16 +1786,15 @@
 ()%7D'
-, insert
+ %7C%7C chr
 ($2)
@@ -1803,13 +1803,9 @@
  $3)
-, $4)
+;
 %22,%0A%09
@@ -1976,25 +1976,28 @@
 absval, 
-concat(x,
+user, (x %7C%7C 
 y) as va
@@ -2009,34 +2009,34 @@
 m tblA left 
-outer join
+OUTER JOIN
  tblB ON (x=
@@ -2038,11 +2038,14 @@
 N (x
-=
+ = 
 y)
+;
 %22,%0A%09
@@ -2423,16 +2423,16 @@
 l);%0A%09%09%09%0A
+
 %09%09%09asser
@@ -2479,247 +2479,8 @@
 %7D%0A%09%7D
-%0A%09%0A%09@Test%0A%09public void testParse() throws SQLException %7B%0A%09%09%0A%09%09%0A%09%09for(String%5B%5D test : sqlTransformTests) %7B%0A%09%09%09%0A%09%09%09SQLText text = new SQLText(test%5B0%5D);%0A%09%09%09%0A%09%09%09SQLTextEscapes.processEscapes(text, null);%0A%09%09%09%0A%09%09%09System.out.println(text);%0A%09%09%7D%0A%09%7D
 %0A%0A%7D%0A

Return SQLTextTests to being a, primitive, unit test for SQL text transforms