use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverMockTest 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 testDateParameterWithGMTN.
@Test
public void testDateParameterWithGMTN() 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("GMT+8");
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, "gfdsa");
pstmt.setDate(2, inputDate, cUtc);
assertEquals(1, pstmt.executeUpdate());
}
ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
assertNotNull(results);
assertTrue(results.next());
assertEquals("gfdsa", results.getString(1));
Date outputDate = results.getDate(2, cUtc);
assertEquals(inputDate.getTime(), outputDate.getTime());
assertEquals(-28800000, outputDate.getTime());
}
}
use of java.sql.ParameterMetaData in project calcite-avatica by apache.
the class RemoteDriverTest method testPrepareBindExecuteFetchVarbinary.
@Test
public void testPrepareBindExecuteFetchVarbinary() throws Exception {
ConnectionSpec.getDatabaseLock().lock();
try {
final Connection connection = getLocalConnection();
final String sql = "select x'de' || ? as c from (values (1, 'a'))";
final PreparedStatement ps = connection.prepareStatement(sql);
final ParameterMetaData parameterMetaData = ps.getParameterMetaData();
assertThat(parameterMetaData.getParameterCount(), equalTo(1));
ps.setBytes(1, new byte[] { 65, 0, 66 });
final ResultSet resultSet = ps.executeQuery();
assertTrue(resultSet.next());
assertThat(resultSet.getBytes(1), equalTo(new byte[] { (byte) 0xDE, 65, 0, 66 }));
resultSet.close();
ps.close();
connection.close();
} finally {
ConnectionSpec.getDatabaseLock().unlock();
}
}
use of java.sql.ParameterMetaData in project ignite by apache.
the class OdbcRequestHandler method getParamsMeta.
/**
* {@link OdbcQueryGetParamsMetaRequest} command handler.
*
* @param req Get params metadata request.
* @return Response.
*/
private SqlListenerResponse getParamsMeta(OdbcQueryGetParamsMetaRequest req) {
try {
PreparedStatement stmt = ctx.query().prepareNativeStatement(req.cacheName(), req.query());
ParameterMetaData pmd = stmt.getParameterMetaData();
byte[] typeIds = new byte[pmd.getParameterCount()];
for (int i = 1; i <= pmd.getParameterCount(); ++i) {
int sqlType = pmd.getParameterType(i);
typeIds[i - 1] = sqlTypeToBinary(sqlType);
}
OdbcQueryGetParamsMetaResult res = new OdbcQueryGetParamsMetaResult(typeIds);
return new OdbcResponse(res);
} catch (Exception e) {
U.error(log, "Failed to get params metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
return new OdbcResponse(SqlListenerResponse.STATUS_FAILED, e.toString());
}
}
use of java.sql.ParameterMetaData in project phoenix by apache.
the class PhoenixConnection method executeStatements.
public int executeStatements(Reader reader, List<Object> binds, PrintStream out) throws IOException, SQLException {
int bindsOffset = 0;
int nStatements = 0;
PhoenixStatementParser parser = new PhoenixStatementParser(reader);
try {
while (true) {
PhoenixPreparedStatement stmt = null;
try {
stmt = new PhoenixPreparedStatement(this, parser);
ParameterMetaData paramMetaData = stmt.getParameterMetaData();
for (int i = 0; i < paramMetaData.getParameterCount(); i++) {
stmt.setObject(i + 1, binds.get(bindsOffset + i));
}
long start = System.currentTimeMillis();
boolean isQuery = stmt.execute();
if (isQuery) {
ResultSet rs = stmt.getResultSet();
if (!rs.next()) {
if (out != null) {
out.println("no rows selected");
}
} else {
int columnCount = 0;
if (out != null) {
ResultSetMetaData md = rs.getMetaData();
columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
int displayWidth = md.getColumnDisplaySize(i);
String label = md.getColumnLabel(i);
if (md.isSigned(i)) {
out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padStart(label, displayWidth, ' '));
out.print(' ');
} else {
out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padEnd(md.getColumnLabel(i), displayWidth, ' '));
out.print(' ');
}
}
out.println();
for (int i = 1; i <= columnCount; i++) {
int displayWidth = md.getColumnDisplaySize(i);
out.print(Strings.padStart("", displayWidth, '-'));
out.print(' ');
}
out.println();
}
do {
if (out != null) {
ResultSetMetaData md = rs.getMetaData();
for (int i = 1; i <= columnCount; i++) {
int displayWidth = md.getColumnDisplaySize(i);
String value = rs.getString(i);
String valueString = value == null ? QueryConstants.NULL_DISPLAY_TEXT : value;
if (md.isSigned(i)) {
out.print(Strings.padStart(valueString, displayWidth, ' '));
} else {
out.print(Strings.padEnd(valueString, displayWidth, ' '));
}
out.print(' ');
}
out.println();
}
} while (rs.next());
}
} else if (out != null) {
int updateCount = stmt.getUpdateCount();
if (updateCount >= 0) {
out.println((updateCount == 0 ? "no" : updateCount) + (updateCount == 1 ? " row " : " rows ") + stmt.getUpdateOperation().toString());
}
}
bindsOffset += paramMetaData.getParameterCount();
double elapsedDuration = ((System.currentTimeMillis() - start) / 1000.0);
out.println("Time: " + elapsedDuration + " sec(s)\n");
nStatements++;
} finally {
if (stmt != null) {
stmt.close();
}
}
}
} catch (EOFException e) {
}
return nStatements;
}
Aggregations