use of java.sql.ResultSetMetaData in project hive by apache.
the class TestTableOutputFormat method setupMockData.
private void setupMockData() throws SQLException {
mockBeeline = new BeelineMock();
mockResultSet = mock(ResultSet.class);
ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
when(mockResultSetMetaData.getColumnCount()).thenReturn(2);
when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Key");
when(mockResultSetMetaData.getColumnLabel(2)).thenReturn("Value");
when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
mockRow = new TestBufferedRows.MockRow();
// returns true as long as there is more data in mockResultData array
when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {
private int mockRowDataIndex = 0;
@Override
public Boolean answer(final InvocationOnMock invocation) {
if (mockRowDataIndex < mockRowData.length) {
mockRow.setCurrentRowData(mockRowData[mockRowDataIndex]);
mockRowDataIndex++;
return true;
} else {
return false;
}
}
});
when(mockResultSet.getString(Matchers.anyInt())).thenAnswer(new Answer<String>() {
@Override
public String answer(final InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
int index = ((Integer) args[0]);
return mockRow.getColumn(index);
}
});
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class Copy method copyToFile.
/**
* Copy the query results to a file
* @throws Exception
*/
void copyToFile(HplsqlParser.Copy_stmtContext ctx, Query query) throws Exception {
ResultSet rs = query.getResultSet();
if (rs == null) {
return;
}
ResultSetMetaData rm = rs.getMetaData();
String filename = null;
if (ctx.copy_target().expr() != null) {
filename = evalPop(ctx.copy_target().expr()).toString();
} else {
filename = ctx.copy_target().getText();
}
byte[] del = delimiter.getBytes();
byte[] rowdel = "\n".getBytes();
byte[] nullstr = "NULL".getBytes();
int cols = rm.getColumnCount();
int rows = 0;
long bytes = 0;
if (trace || info) {
String mes = "Query executed: " + cols + " columns, output file: " + filename;
if (trace) {
trace(ctx, mes);
} else {
info(ctx, mes);
}
}
java.io.File file = null;
File hdfsFile = null;
if (ctx.T_HDFS() == null) {
file = new java.io.File(filename);
} else {
hdfsFile = new File();
}
OutputStream out = null;
timer.start();
try {
if (file != null) {
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file, false);
} else {
out = hdfsFile.create(filename, true);
}
String col;
String sql = "";
if (sqlInsert) {
sql = "INSERT INTO " + sqlInsertName + " VALUES (";
rowdel = ");\n".getBytes();
}
while (rs.next()) {
if (sqlInsert) {
out.write(sql.getBytes());
}
for (int i = 1; i <= cols; i++) {
if (i > 1) {
out.write(del);
bytes += del.length;
}
col = rs.getString(i);
if (col != null) {
if (sqlInsert) {
col = Utils.quoteString(col);
}
byte[] b = col.getBytes();
out.write(b);
bytes += b.length;
} else if (sqlInsert) {
out.write(nullstr);
}
}
out.write(rowdel);
bytes += rowdel.length;
rows++;
}
exec.setRowCount(rows);
} finally {
if (out != null) {
out.close();
}
}
long elapsed = timer.stop();
if (info) {
info(ctx, "COPY completed: " + rows + " row(s), " + Utils.formatSizeInBytes(bytes) + ", " + timer.format() + ", " + rows / elapsed / 1000 + " rows/sec");
}
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class Stmt method assignFromSelect.
/**
* Assignment from SELECT statement
*/
public Integer assignFromSelect(HplsqlParser.Assignment_stmt_select_itemContext ctx) {
String sql = evalPop(ctx.select_stmt()).toString();
if (trace) {
trace(ctx, sql.toString());
}
String conn = exec.getStatementConnection();
Query query = exec.executeQuery(ctx, sql.toString(), conn);
if (query.error()) {
exec.signal(query);
return 1;
}
exec.setSqlSuccess();
try {
ResultSet rs = query.getResultSet();
ResultSetMetaData rm = null;
if (rs != null) {
rm = rs.getMetaData();
int cnt = ctx.ident().size();
if (rs.next()) {
for (int i = 1; i <= cnt; i++) {
Var var = exec.findVariable(ctx.ident(i - 1).getText());
if (var != null) {
var.setValue(rs, rm, i);
if (trace) {
trace(ctx, "COLUMN: " + rm.getColumnName(i) + ", " + rm.getColumnTypeName(i));
trace(ctx, "SET " + var.getName() + " = " + var.toString());
}
} else if (trace) {
trace(ctx, "Variable not found: " + ctx.ident(i - 1).getText());
}
}
exec.incRowCount();
exec.setSqlSuccess();
} else {
exec.setSqlCode(100);
exec.signal(Signal.Type.NOTFOUND);
}
}
} catch (SQLException e) {
exec.signal(query);
return 1;
} finally {
exec.closeQuery(query, conn);
}
return 0;
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class Stmt method fetch.
/**
* FETCH cursor statement
*/
public Integer fetch(HplsqlParser.Fetch_stmtContext ctx) {
trace(ctx, "FETCH");
String name = ctx.L_ID(0).toString();
Var cursor = exec.findCursor(name);
if (cursor == null) {
trace(ctx, "Cursor not found: " + name);
exec.setSqlCode(-1);
exec.signal(Signal.Type.SQLEXCEPTION);
return 1;
} else if (cursor.value == null) {
trace(ctx, "Cursor not open: " + name);
exec.setSqlCode(-1);
exec.signal(Signal.Type.SQLEXCEPTION);
return 1;
} else if (exec.getOffline()) {
exec.setSqlCode(100);
exec.signal(Signal.Type.NOTFOUND);
return 0;
}
// Assign values from the row to local variables
try {
Query query = (Query) cursor.value;
ResultSet rs = query.getResultSet();
ResultSetMetaData rsm = null;
if (rs != null) {
rsm = rs.getMetaData();
}
if (rs != null && rsm != null) {
int cols = ctx.L_ID().size() - 1;
if (rs.next()) {
query.setFetch(true);
for (int i = 1; i <= cols; i++) {
Var var = exec.findVariable(ctx.L_ID(i).getText());
if (var != null) {
if (var.type != Var.Type.ROW) {
var.setValue(rs, rsm, i);
} else {
var.setValues(rs, rsm);
}
if (trace) {
trace(ctx, var, rs, rsm, i);
}
} else if (trace) {
trace(ctx, "Variable not found: " + ctx.L_ID(i).getText());
}
}
exec.incRowCount();
exec.setSqlSuccess();
} else {
query.setFetch(false);
exec.setSqlCode(100);
}
} else {
exec.setSqlCode(-1);
}
} catch (SQLException e) {
exec.setSqlCode(e);
exec.signal(Signal.Type.SQLEXCEPTION, e.getMessage(), e);
}
return 0;
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class Stmt method describe.
/**
* DESCRIBE statement
*/
public Integer describe(HplsqlParser.Describe_stmtContext ctx) {
trace(ctx, "DESCRIBE");
String sql = "DESCRIBE " + evalPop(ctx.table_name()).toString();
trace(ctx, sql);
Query query = exec.executeSql(ctx, sql, exec.conf.defaultConnection);
if (query.error()) {
exec.signal(query);
return 1;
}
try {
ResultSet rs = query.getResultSet();
ResultSetMetaData rm = null;
if (rs != null) {
rm = rs.getMetaData();
int cols = rm.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
if (i > 1) {
System.out.print("\t");
}
System.out.print(rs.getString(i));
}
System.out.println("");
}
}
} catch (SQLException e) {
exec.signal(query);
exec.closeQuery(query, exec.conf.defaultConnection);
return 1;
}
exec.setSqlSuccess();
exec.closeQuery(query, exec.conf.defaultConnection);
return 0;
}
Aggregations