package org.postgresql.test.jdbc3;

import junit.framework.TestCase;
import org.postgresql.core.NativeQuery;
import org.postgresql.core.Parser;

import java.util.List;

public class CompositeQueryParseTest extends TestCase {

    public void testSimpleQuery()
    {
        assertEquals("select 1", reparse("select 1", true, false, true));
    }

    public void testSimpleBind()
    {
        assertEquals("select $1", reparse("select ?", true, false, true));
    }

    public void testQuotedQuestionmark()
    {
        assertEquals("select '?'", reparse("select '?'", true, false, true));
    }

    public void testDoubleQuestionmark()
    {
        assertEquals("select '?', $1 ?=> $2", reparse("select '?', ? ??=> ?", true, false, true));
    }

    public void testCompositeBasic()
    {
        assertEquals("select 1;/*cut*/\n select 2", reparse("select 1; select 2", true, false, true));
    }

    public void testCompositeWithBinds()
    {
        assertEquals("select $1;/*cut*/\n select $1", reparse("select ?; select ?", true, false, true));
    }

    public void testTrailingSemicolon()
    {
        assertEquals("select 1", reparse("select 1;", true, false, true));
    }

    public void testTrailingSemicolonAndSpace()
    {
        assertEquals("select 1", reparse("select 1; ", true, false, true));
    }

    public void testMultipleTrailingSemicolons()
    {
        assertEquals("select 1", reparse("select 1;;;", true, false, true));
    }

    public void testMultipleEmptyQueries()
    {
        assertEquals("select 1;/*cut*/\n" +
                "select 2", reparse("select 1; ;\t;select 2", true, false, true));
    }

    public void testCompositeWithComments()
    {
        assertEquals("select 1;/*cut*/\n" +
                "/* noop */;/*cut*/\n" +
                "select 2", reparse("select 1;/* noop */;select 2", true, false, true));
    }

    private String reparse(String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements)
    {
        return toString(Parser.parseJdbcSql(query, standardConformingStrings, withParameters, splitStatements));
    }

    private String toString(List<NativeQuery> queries)
    {
        StringBuilder sb = new StringBuilder();
        for (NativeQuery query : queries)
        {
            if (sb.length() != 0)
            {
                sb.append(";/*cut*/\n");
            }
            sb.append(query.nativeSql);
        }
        return sb.toString();
    }
}

@@ -219,16 +219,358 @@
 Case %7B%0A%0A
+    public void testEmptyQuery()%0A    %7B%0A        assertEquals(%22%22, reparse(%22%22, true, false, true));%0A    %7D%0A%0A    public void testWhitespaceQuery()%0A    %7B%0A        assertEquals(%22%22, reparse(%22     %22, true, false, true));%0A    %7D%0A%0A    public void testOnlyEmptyQueries()%0A    %7B%0A        assertEquals(%22%22, reparse(%22;;;;  ;  %5Cn;%5Cn%22, true, false, true));%0A    %7D%0A%0A
     publ

add tests for parsing of empty queries separated by semicolons