use of java.sql.ResultSetMetaData in project groovy by apache.
the class GroovyResultSetExtension method toString.
public String toString() {
try {
StringBuilder sb = new StringBuilder("[");
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount();
for (int i = 1; i <= count; i++) {
sb.append(metaData.getColumnName(i));
sb.append(":");
Object object = resultSet.getObject(i);
if (object != null) {
sb.append(object.toString());
} else {
sb.append("[null]");
}
if (i < count) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
} catch (SQLException e) {
// System.err.println("e.getMessage() = " + e.getMessage());
return super.toString();
}
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class JdbcRecordIterator method next.
@Override
public Map<String, String> next() {
try {
ResultSetMetaData metadata = rs.getMetaData();
int numColumns = metadata.getColumnCount();
Map<String, String> record = new HashMap<String, String>(numColumns);
for (int i = 0; i < numColumns; i++) {
String key = metadata.getColumnName(i + 1);
String value = rs.getString(i + 1);
if (value == null) {
value = NullWritable.get().toString();
}
record.put(key, value);
}
return record;
} catch (Exception e) {
LOGGER.warn("next() threw exception", e);
return null;
}
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class TxnDbUtil method queryToString.
public static String queryToString(String query, boolean includeHeader) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
StringBuilder sb = new StringBuilder();
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
if (includeHeader) {
for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
sb.append(rsmd.getColumnName(colPos)).append(" ");
}
sb.append('\n');
}
while (rs.next()) {
for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
sb.append(rs.getObject(colPos)).append(" ");
}
sb.append('\n');
}
} finally {
closeResources(conn, stmt, rs);
}
return sb.toString();
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class cbo_rp_TestJdbcDriver2 method testMetaDataGetColumns.
@Test
public void testMetaDataGetColumns() throws SQLException {
Map<String[], Integer> tests = new HashMap<String[], Integer>();
tests.put(new String[] { "testhivejdbcdriver\\_table", null }, 2);
tests.put(new String[] { "testhivejdbc%", null }, 7);
tests.put(new String[] { "testhiveJDBC%", null }, 7);
tests.put(new String[] { "%jdbcdriver\\_table", null }, 2);
tests.put(new String[] { "%jdbcdriver\\_table%", "under\\_col" }, 1);
// tests.put(new String[]{"%jdbcdriver\\_table%", "under\\_COL"}, 1);
tests.put(new String[] { "%jdbcdriver\\_table%", "under\\_co_" }, 1);
tests.put(new String[] { "%jdbcdriver\\_table%", "under_col" }, 1);
tests.put(new String[] { "%jdbcdriver\\_table%", "und%" }, 1);
tests.put(new String[] { "%jdbcdriver\\_table%", "%" }, 2);
tests.put(new String[] { "%jdbcdriver\\_table%", "_%" }, 2);
for (String[] checkPattern : tests.keySet()) {
ResultSet rs = con.getMetaData().getColumns(null, null, checkPattern[0], checkPattern[1]);
// validate the metadata for the getColumns result set
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals("TABLE_CAT", rsmd.getColumnName(1));
int cnt = 0;
while (rs.next()) {
String columnname = rs.getString("COLUMN_NAME");
int ordinalPos = rs.getInt("ORDINAL_POSITION");
switch(cnt) {
case 0:
assertEquals("Wrong column name found", "under_col", columnname);
assertEquals("Wrong ordinal position found", ordinalPos, 1);
break;
case 1:
assertEquals("Wrong column name found", "value", columnname);
assertEquals("Wrong ordinal position found", ordinalPos, 2);
break;
default:
break;
}
cnt++;
}
rs.close();
assertEquals("Found less columns then we test for.", tests.get(checkPattern).intValue(), cnt);
}
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class cbo_rp_TestJdbcDriver2 method testImportedKeys.
/**
* test getImportedKeys()
* @throws SQLException
*/
@Test
public void testImportedKeys() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// currently getImportedKeys always returns an empty resultset for Hive
ResultSet res = dbmd.getImportedKeys(null, null, null);
ResultSetMetaData md = res.getMetaData();
assertEquals(md.getColumnCount(), 14);
assertFalse(res.next());
}
Aggregations