package com.orientechnologies.orient.jdbc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import org.hamcrest.Matchers;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class OrientJdbcPreparedStatementTest extends OrientJdbcBaseTest {

	@Test
	public void shouldCreateStatement() throws Exception {
		PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Item WHERE stringKey = ? OR intKey = ?");
		assertNotNull(stmt);
		stmt.close();
		assertTrue(stmt.isClosed());

	}

	@Test
	public void shouldReturnEmptyResultSetOnEmptyQuery() throws SQLException {
		PreparedStatement stmt = conn.prepareStatement("");
		assertThat(stmt.execute(""), is(false));

		assertThat(stmt.getResultSet(), is(nullValue()));
		assertThat(stmt.getMoreResults(), is(false));
	}

	@Test
	public void shouldExectuteSelectOne() throws SQLException {
		PreparedStatement stmt = conn.prepareStatement("select 1");
		assertThat(stmt.execute(), is(true));
		assertNotNull(stmt.getResultSet());
		ResultSet resultSet = stmt.getResultSet();
		resultSet.first();
		int one = resultSet.getInt("1");
		assertThat(one, is(1));
		assertThat(stmt.getMoreResults(), is(false));

	}

	@Test
	public void shouldExecutePreparedStatement() throws Exception {
		PreparedStatement stmt = conn.prepareStatement(
				"SELECT  " +
						"FROM Item " +
						"WHERE stringKey = ? OR intKey = ?");
		assertNotNull(stmt);

		stmt.setString(1, "1");
		stmt.setInt(2, 1);

		ResultSet rs = stmt.executeQuery();
		assertTrue(rs.next());

		assertThat(rs.getInt("@version"), equalTo(0));

		assertThat(rs.getString("@class"), equalTo("Item"));

		assertThat(rs.getString("@rid"), equalTo("#7:0"));

		assertThat(rs.getString("stringKey"), equalTo("1"));
		assertThat(rs.getInt("intKey"), equalTo(1));

		assertThat(rs.getDate("date").toString(), equalTo(new java.sql.Date(System.currentTimeMillis()).toString()));
		assertThat(rs.getDate("time").toString(), equalTo(new java.sql.Date(System.currentTimeMillis()).toString()));

		stmt.close();
		assertTrue(stmt.isClosed());

	}

}

package com.orientechnologies.orient.jdbc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import org.hamcrest.Matchers;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class OrientJdbcPreparedStatementTest extends OrientJdbcBaseTest {

	@Test
	public void shouldCreateStatement() throws Exception {
		PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Item WHERE stringKey = ? OR intKey = ?");
		assertNotNull(stmt);
		stmt.close();
		assertTrue(stmt.isClosed());

	}

	@Test
	public void shouldReturnEmptyResultSetOnEmptyQuery() throws SQLException {
		PreparedStatement stmt = conn.prepareStatement("");
		assertThat(stmt.execute(""), is(false));

		assertThat(stmt.getResultSet(), is(nullValue()));
		assertThat(stmt.getMoreResults(), is(false));
	}

	@Test
	public void shouldExectuteSelectOne() throws SQLException {
		PreparedStatement stmt = conn.prepareStatement("select 1");
		assertThat(stmt.execute(), is(true));
		assertNotNull(stmt.getResultSet());
		ResultSet resultSet = stmt.getResultSet();
		resultSet.first();
		int one = resultSet.getInt("1");
		assertThat(one, is(1));
		assertThat(stmt.getMoreResults(), is(false));

	}

	@Test
	public void shouldExecutePreparedStatement() throws Exception {
		PreparedStatement stmt = conn.prepareStatement(
				"SELECT  " +
						"FROM Item " +
						"WHERE stringKey = ? OR intKey = ?");
		assertNotNull(stmt);

		stmt.setString(1, "1");
		stmt.setInt(2, 1);

		ResultSet rs = stmt.executeQuery();
		assertTrue(rs.next());

		assertThat(rs.getInt("@version"), equalTo(0));

		assertThat(rs.getString("@class"), equalTo("Item"));

		assertThat(rs.getString("@rid"), equalTo("#8:0"));

		assertThat(rs.getString("stringKey"), equalTo("1"));
		assertThat(rs.getInt("intKey"), equalTo(1));

		assertThat(rs.getDate("date").toString(), equalTo(new java.sql.Date(System.currentTimeMillis()).toString()));
		assertThat(rs.getDate("time").toString(), equalTo(new java.sql.Date(System.currentTimeMillis()).toString()));

		stmt.close();
		assertTrue(stmt.isClosed());

	}

}

test fixup