<commit_before>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.sql.embedded;

import com.foundationdb.sql.server.ServerCallContextStack;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.*;

import java.math.BigDecimal;

public class EmbeddedJDBCIT extends EmbeddedJDBCITBase
{

    @Before
    public void loadDB() throws Exception {
        int cid = createTable(SCHEMA_NAME, "c",
                              "cid int primary key not null",
                              "name varchar(16) not null");
        int oid = createTable(SCHEMA_NAME, "o",
                              "oid int primary key not null",
                              "cid int not null",
                              "grouping foreign key(cid) references c(cid)",
                              "order_date date not null");
        writeRow(cid, 1, "Smith");
        writeRow(oid, 101, 1, 2012 * 512 + 1 * 32 + 31);
        writeRow(oid, 102, 1, 2012 * 512 + 2 * 32 + 1);
        writeRow(cid, 2, "Jones");
        writeRow(oid, 201, 2, 2012 * 512 + 4 * 32 + 1);
    }
    
    @Test
    public void testSimple() throws Exception {
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT name FROM c WHERE cid = 1")) {
            assertTrue("has first row", rs.next());
            assertEquals("result value", "Smith", rs.getString(1));
            assertFalse("has more rows", rs.next());
        }
    }

    @Test
    public void testPrepared() throws Exception {
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT name, order_date FROM c INNER JOIN o USING(cid) WHERE c.cid = ?")) {
            assertEquals("estimated count", 2, ((JDBCPreparedStatement)stmt).getEstimatedRowCount());
            stmt.setInt(1, 2);
            assertTrue("has result set", stmt.execute());
            try (ResultSet rs = stmt.getResultSet()) {
                assertTrue("has first row", rs.next());
                assertEquals("result value", "Jones", rs.getString(1));
                assertEquals("result value", "2012-04-01", rs.getDate(2).toString());
                assertFalse("has more rows", rs.next());
            }
            stmt.setInt(1, 1);
            try (ResultSet rs = stmt.executeQuery()) {
                assertTrue("has first row", rs.next());
                assertEquals("result value", "Smith", rs.getString(1));
                assertEquals("result value", "2012-01-31", rs.getString(2));
                assertTrue("has next row", rs.next());
                assertEquals("result value", "Smith", rs.getString(1));
                assertEquals("result value", "2012-02-01 00:00:00.0", rs.getTimestamp(2).toString());
                assertFalse("has more rows", rs.next());
            }
        }
    }

    @Test
    public void testPreparedBinary() throws Exception {
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT name FROM c WHERE AES_ENCRYPT(name, 'key') = ?")) {
            stmt.setBytes(1, new byte[] { 1, 2, 3 });
            assertTrue("has result set", stmt.execute());
            try (ResultSet rs = stmt.getResultSet()) {
                assertFalse("doesn't have first row", rs.next());
            }
        }
    }

    @Test
    public void testNested() throws Exception {
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT name, (SELECT oid,order_date FROM o WHERE o.cid = c.cid) FROM c WHERE cid = 1")) {
            assertTrue("has first row", rs.next());
            assertEquals("result value", "Smith", rs.getString(1));
            try (ResultSet nrs = (ResultSet)rs.getObject(2)) {
                assertTrue("nested first row", nrs.next());
                assertEquals("result value", 101, nrs.getInt(1));
                assertEquals("result value", "2012-01-31", nrs.getString(2).toString());
                assertTrue("nested second row", nrs.next());
                assertEquals("result value", 102, nrs.getInt(1));
                assertEquals("result value", "2012-02-01", nrs.getString(2).toString());<commit_msg>Add a test for the correct behavior.<commit_after>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.sql.embedded;

import com.foundationdb.server.error.ErrorCode;
import com.foundationdb.sql.server.ServerCallContextStack;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.*;

import java.math.BigDecimal;

public class EmbeddedJDBCIT extends EmbeddedJDBCITBase
{

    @Before
    public void loadDB() throws Exception {
        int cid = createTable(SCHEMA_NAME, "c",
                              "cid int primary key not null",
                              "name varchar(16) not null");
        int oid = createTable(SCHEMA_NAME, "o",
                              "oid int primary key not null",
                              "cid int not null",
                              "grouping foreign key(cid) references c(cid)",
                              "order_date date not null");
        writeRow(cid, 1, "Smith");
        writeRow(oid, 101, 1, 2012 * 512 + 1 * 32 + 31);
        writeRow(oid, 102, 1, 2012 * 512 + 2 * 32 + 1);
        writeRow(cid, 2, "Jones");
        writeRow(oid, 201, 2, 2012 * 512 + 4 * 32 + 1);
    }
    
    @Test
    public void testSimple() throws Exception {
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT name FROM c WHERE cid = 1")) {
            assertTrue("has first row", rs.next());
            assertEquals("result value", "Smith", rs.getString(1));
            assertFalse("has more rows", rs.next());
        }
    }

    @Test
    public void testPrepared() throws Exception {
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT name, order_date FROM c INNER JOIN o USING(cid) WHERE c.cid = ?")) {
            assertEquals("estimated count", 2, ((JDBCPreparedStatement)stmt).getEstimatedRowCount());
            stmt.setInt(1, 2);
            assertTrue("has result set", stmt.execute());
            try (ResultSet rs = stmt.getResultSet()) {
                assertTrue("has first row", rs.next());
                assertEquals("result value", "Jones", rs.getString(1));
                assertEquals("result value", "2012-04-01", rs.getDate(2).toString());
                assertFalse("has more rows", rs.next());
            }
            stmt.setInt(1, 1);
            try (ResultSet rs = stmt.executeQuery()) {
                assertTrue("has first row", rs.next());
                assertEquals("result value", "Smith", rs.getString(1));
                assertEquals("result value", "2012-01-31", rs.getString(2));
                assertTrue("has next row", rs.next());
                assertEquals("result value", "Smith", rs.getString(1));
                assertEquals("result value", "2012-02-01 00:00:00.0", rs.getTimestamp(2).toString());
                assertFalse("has more rows", rs.next());
            }
        }
    }

    @Test
    public void testPreparedBinary() throws Exception {
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement("SELECT name FROM c WHERE AES_ENCRYPT(name, 'key') = ?")) {
            stmt.setBytes(1, new byte[] { 1, 2, 3 });
            assertTrue("has result set", stmt.execute());
            try (ResultSet rs = stmt.getResultSet()) {
                assertFalse("doesn't have first row", rs.next());
            }
        }
    }

    @Test
    public void testPreparedStale() throws Exception {
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement("SELECT name FROM c")) {
            assertTrue("has result set", pstmt.execute());
            try (ResultSet rs = pstmt.getResultSet()) {
            }
            try (Statement stmt = conn.createStatement()) {
                stmt.execute("ALTER TABLE c DROP COLUMN name");
            }
            SQLException staleEx = null;
            try {
                pstmt.execute();
            }
            catch (SQLException ex) {
                staleEx = ex;
            }
            assertNotNull("exception thrown", staleEx);
            assertEquals("error code", ErrorCode.STALE_STATEMENT.getFormattedValue(), staleEx.getSQLState());
        }
    }

    @Test
    public void testNested() throws Exception {
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT name, (SELECT oid,order_date FROM o WHERE o.cid = c.cid) FROM c WHERE cid = 1")) {
            assertTrue("has first row", rs.next());
            assertEquals("result value", "Smith", rs.getString(1));
            try (ResultSet nrs = (ResultSet)rs.getObject(2)) {
                assertTrue("nested first row", nrs.next());
                assertEquals("result value", 101, nrs.getInt(1));
                assertEquals("result value", "2012-01-31", nrs.getString(2).toString());
                assertTrue("nested second row", nrs.next());
                assertEquals("result value", 102, nrs.getInt(1));
                assertEquals("result value", "2012-02-01", nrs.getString(2).toString());
{'fraction_non_alphanumeric': 0.1201923076923077, 'fraction_numerical': 0.024216524216524215, 'mean_word_length': 2.2446562680531486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632166', 'n_tokens_mistral': 3188, 'n_tokens_neox': 2934, 'n_words': 925}