use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method testDecimalParameters.
@Test
public void testDecimalParameters() throws Exception {
final String tableName = "decimalParameters";
BigDecimal decimal = new BigDecimal("123451234512345");
try (Connection conn = getLocalConnection();
Statement stmt = conn.createStatement()) {
assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 DECIMAL(15,0))";
assertFalse(stmt.execute(sql));
// Insert a single decimal
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
ParameterMetaData metadata = pstmt.getParameterMetaData();
assertNotNull(metadata);
assertEquals(5, metadata.getPrecision(1));
assertEquals(0, metadata.getScale(1));
assertEquals(15, metadata.getPrecision(2));
assertEquals(0, metadata.getScale(2));
pstmt.setString(1, "asdfg");
pstmt.setBigDecimal(2, decimal);
assertEquals(1, pstmt.executeUpdate());
}
ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
assertNotNull(results);
assertTrue(results.next());
BigDecimal actualDecimal = results.getBigDecimal(2);
assertEquals(decimal, actualDecimal);
ResultSetMetaData resultMetadata = results.getMetaData();
assertNotNull(resultMetadata);
assertEquals(15, resultMetadata.getPrecision(2));
assertEquals(0, resultMetadata.getScale(2));
}
}
use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method checkStatementExecute.
private void checkStatementExecute(Connection connection, boolean prepare, int maxRowCount) throws SQLException {
final String sql = "select * from (\n" + " values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
final Statement statement;
final ResultSet resultSet;
final ParameterMetaData parameterMetaData;
if (prepare) {
final PreparedStatement ps = connection.prepareStatement(sql);
statement = ps;
ps.setMaxRows(maxRowCount);
parameterMetaData = ps.getParameterMetaData();
assertTrue(ps.execute());
resultSet = ps.getResultSet();
} else {
statement = connection.createStatement();
statement.setMaxRows(maxRowCount);
parameterMetaData = null;
assertTrue(statement.execute(sql));
resultSet = statement.getResultSet();
}
if (parameterMetaData != null) {
assertThat(parameterMetaData.getParameterCount(), equalTo(0));
}
final ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(2, metaData.getColumnCount());
assertEquals("C1", metaData.getColumnName(1));
assertEquals("C2", metaData.getColumnName(2));
for (int i = 0; i < maxRowCount || (maxRowCount == 0 && i < 3); i++) {
assertTrue(resultSet.next());
}
assertFalse(resultSet.next());
resultSet.close();
statement.close();
connection.close();
}
use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method checkStatementExecuteQuery.
private void checkStatementExecuteQuery(Connection connection, boolean prepare) throws SQLException {
final String sql = "select * from (\n" + " values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
final Statement statement;
final ResultSet resultSet;
final ParameterMetaData parameterMetaData;
if (prepare) {
final PreparedStatement ps = connection.prepareStatement(sql);
statement = ps;
parameterMetaData = ps.getParameterMetaData();
resultSet = ps.executeQuery();
} else {
statement = connection.createStatement();
parameterMetaData = null;
resultSet = statement.executeQuery(sql);
}
if (parameterMetaData != null) {
assertThat(parameterMetaData.getParameterCount(), equalTo(0));
}
final ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(2, metaData.getColumnCount());
assertEquals("C1", metaData.getColumnName(1));
assertEquals("C2", metaData.getColumnName(2));
assertTrue(resultSet.next());
assertTrue(resultSet.next());
assertTrue(resultSet.next());
assertFalse(resultSet.next());
resultSet.close();
statement.close();
connection.close();
}
use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method testSignedParameters.
@Test
public void testSignedParameters() throws Exception {
final String tableName = "signedParameters";
try (Connection conn = getLocalConnection();
Statement stmt = conn.createStatement()) {
assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 integer)";
assertFalse(stmt.execute(sql));
// Insert a single decimal
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
ParameterMetaData metadata = pstmt.getParameterMetaData();
assertNotNull(metadata);
assertFalse("Varchar should not be signed", metadata.isSigned(1));
assertTrue("Integer should be signed", metadata.isSigned(2));
pstmt.setString(1, "asdfg");
pstmt.setInt(2, 10);
assertEquals(1, pstmt.executeUpdate());
}
ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
assertNotNull(results);
assertTrue(results.next());
assertEquals("asdfg", results.getString(1));
assertEquals(10, results.getInt(2));
ResultSetMetaData resultMetadata = results.getMetaData();
assertNotNull(resultMetadata);
assertFalse("Varchar should not be signed", resultMetadata.isSigned(1));
assertTrue("Integer should be signed", resultMetadata.isSigned(2));
}
}
use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method testDateParameterWithGMT0.
@Test
public void testDateParameterWithGMT0() throws Exception {
final String tableName = "dateParameters";
try (Connection conn = getLocalConnection();
Statement stmt = conn.createStatement()) {
assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 date)";
assertFalse(stmt.execute(sql));
TimeZone tzUtc = TimeZone.getTimeZone("UTC");
Calendar cUtc = Calendar.getInstance(tzUtc, Locale.ROOT);
cUtc.set(Calendar.YEAR, 1970);
cUtc.set(Calendar.MONTH, Calendar.JANUARY);
cUtc.set(Calendar.DAY_OF_MONTH, 1);
cUtc.set(Calendar.HOUR_OF_DAY, 0);
cUtc.set(Calendar.MINUTE, 0);
cUtc.set(Calendar.SECOND, 0);
cUtc.set(Calendar.MILLISECOND, 0);
Date inputDate = new Date(cUtc.getTimeInMillis());
// Insert a single date
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
ParameterMetaData metadata = pstmt.getParameterMetaData();
assertNotNull(metadata);
pstmt.setString(1, "asdfg");
pstmt.setDate(2, inputDate, cUtc);
assertEquals(1, pstmt.executeUpdate());
}
ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
assertNotNull(results);
assertTrue(results.next());
assertEquals("asdfg", results.getString(1));
Date outputDate = results.getDate(2, cUtc);
assertEquals(inputDate.getTime(), outputDate.getTime());
assertEquals(0, outputDate.getTime());
}
}
Aggregations