package com.github.randyp.jdbj;

import org.junit.ClassRule;
import org.junit.Test;

import java.sql.Connection;

import static org.junit.Assert.assertEquals;

public class ExecuteQueryRunnableTest {

    @ClassRule
    public static final H2Rule db = new H2Rule();

    @Test
    public void selectRunExecute() throws Exception {
        final String[] firstColumnName = {null};
        final ExecuteQueryRunnable query = JDBJ.string("SELECT * FROM INFORMATION_SCHEMA.TABLES")
                .query()
                .runnable(rs -> firstColumnName[0] = rs.getMetaData().getColumnName(1));

        try (Connection connection = db.getConnection()) {
            query.execute(connection);
        }

        assertEquals("TABLE_CATALOG", firstColumnName[0]);
    }

    @Test
    public void selectRunBindValueExecute() throws Exception {
        final int[] count = {0};
        final ExecuteQueryRunnable query = JDBJ.string("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE id = :id")
                .query()
                .runnable(rs -> {
                    while (rs.next()) {
                        count[0]++;
                    }
                })
                .bindLong(":id", -28L);

        try (Connection connection = db.getConnection()) {
            query.execute(connection);
        }

        assertEquals(1, count[0]);
    }

    @Test
    public void selectRunBindStringListExecute() throws Exception {
        final int[] count = {0};
        final ExecuteQueryRunnable query = JDBJ.string("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA in :schemas")
                .query()
                .runnable(rs -> {
                    while (rs.next()) {
                        count[0]++;
                    }
                })
                .bindStrings(":schemas", "INFORMATION_SCHEMA");

        try (Connection connection = db.getConnection()) {
            query.execute(connection);
        }

        assertEquals(29, count[0]);
    }

}

@@ -1605,32 +1605,95 @@
        .query()%0A
+                .bindStrings(%22:schemas%22, %22INFORMATION_SCHEMA%22)%0A
                 
@@ -1830,71 +1830,8 @@
   %7D)
-%0A                .bindStrings(%22:schemas%22, %22INFORMATION_SCHEMA%22)
 ;%0A%0A 

move binding around to test recent work