/*
 * Copyright 2014 Continuuity, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.continuuity.explore.jdbc;

import com.continuuity.explore.client.ExploreClient;
import com.continuuity.explore.service.ColumnDesc;
import com.continuuity.explore.service.Result;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 */
public class ExplorePreparedStatementTest {

  @Test
  public void executeTest() throws Exception {
    ExploreClient exploreClient = new MockExploreClient(
      ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<ColumnDesc>) Lists.newArrayList(
        new ColumnDesc("column1", "STRING", 1, ""),
        new ColumnDesc("column2", "int", 2, ""))
      ),
      ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<Result>) Lists.<Result>newArrayList())
    );


    ExplorePreparedStatement statement = new ExplorePreparedStatement(null, exploreClient,
                                                                      "SELECT * FROM table WHERE id=?, name=?");
    statement.setInt(1, 100);
    try {
      statement.execute();
      Assert.fail();
    } catch (SQLException e) {
      // Parameter 2 has not been set
    }

    statement.setString(2, "foo");
    Assert.assertEquals("SELECT * FROM table WHERE id=100, name='foo'", statement.updateSql());

    Assert.assertTrue(statement.execute());
    ResultSet rs = statement.getResultSet();
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?'");
    Assert.assertEquals("SELECT * FROM table WHERE name='?'", statement.updateSql());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?', id=?");
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name='?', id=100", statement.updateSql());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"?\", id=?");
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"?\", id=100", statement.updateSql());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?'\", id=?");
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"'?'\", id=100", statement.updateSql());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?\", id=?");
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"'?\", id=100", statement.updateSql());

    statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=?");
    statement.setInt(1, 100);
    Assert.assertEquals("SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=100", statement.updateSql());
  }
}

@@ -706,31 +706,21 @@
 inuuity.
-explore.service
+proto
 .ColumnD
@@ -751,24 +751,19 @@
 ity.
-explore.service.
+proto.Query
 Resu
@@ -1439,23 +1439,50 @@
 ='foo'%22,
+%0A                     
  (List%3C
+Query
 Result%3E)
@@ -1489,16 +1489,21 @@
  Lists.%3C
+Query
 Result%3En

Fix compile error in ExplorePreparedStatementTest due to bad auto-merge