use of java.sql.SQLException in project hive by apache.
the class FunctionMisc method partCountBy.
/**
* PART_COUNT_BY function
*/
public void partCountBy(HplsqlParser.Expr_func_paramsContext ctx) {
int cnt = ctx.func_param().size();
if (cnt < 1 || exec.getOffline()) {
return;
}
String tabname = evalPop(ctx.func_param(0).expr()).toString();
ArrayList<String> keys = null;
if (cnt > 1) {
keys = new ArrayList<String>();
for (int i = 1; i < cnt; i++) {
keys.add(evalPop(ctx.func_param(i).expr()).toString().toUpperCase());
}
}
String sql = "SHOW PARTITIONS " + tabname;
Query query = exec.executeQuery(ctx, sql, exec.conf.defaultConnection);
if (query.error()) {
exec.closeQuery(query, exec.conf.defaultConnection);
return;
}
ResultSet rs = query.getResultSet();
HashMap<String, Integer> group = new HashMap<String, Integer>();
try {
while (rs.next()) {
String part = rs.getString(1);
String[] parts = part.split("/");
String key = parts[0];
if (cnt > 1) {
StringBuilder k = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (keys.contains(parts[i].split("=")[0].toUpperCase())) {
if (k.length() > 0) {
k.append("/");
}
k.append(parts[i]);
}
}
key = k.toString();
}
Integer count = group.get(key);
if (count == null) {
count = new Integer(0);
}
group.put(key, count + 1);
}
} catch (SQLException e) {
exec.closeQuery(query, exec.conf.defaultConnection);
return;
}
if (cnt == 1) {
evalInt(group.size());
} else {
for (Map.Entry<String, Integer> i : group.entrySet()) {
System.out.println(i.getKey() + '\t' + i.getValue());
}
}
exec.closeQuery(query, exec.conf.defaultConnection);
}
use of java.sql.SQLException 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.SQLException in project hive by apache.
the class Stmt method resignal.
/**
* RESIGNAL statement
*/
public Integer resignal(HplsqlParser.Resignal_stmtContext ctx) {
trace(ctx, "RESIGNAL");
if (ctx.T_SQLSTATE() != null) {
String sqlstate = evalPop(ctx.expr(0)).toString();
String text = "";
if (ctx.T_MESSAGE_TEXT() != null) {
text = evalPop(ctx.expr(1)).toString();
}
SQLException exception = new SQLException(text, sqlstate, -1);
Signal signal = new Signal(Signal.Type.SQLEXCEPTION, text, exception);
exec.setSqlCode(exception);
exec.resignal(signal);
} else {
exec.resignal();
}
return 0;
}
use of java.sql.SQLException 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.SQLException 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