Search in sources :

Example 86 with SQLException

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);
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 87 with SQLException

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 88 with SQLException

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;
}
Also used : SQLException(java.sql.SQLException)

Example 89 with SQLException

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 90 with SQLException

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Aggregations

SQLException (java.sql.SQLException)6792 PreparedStatement (java.sql.PreparedStatement)3048 ResultSet (java.sql.ResultSet)2426 Connection (java.sql.Connection)1871 ArrayList (java.util.ArrayList)972 Test (org.junit.Test)873 Statement (java.sql.Statement)779 IOException (java.io.IOException)341 List (java.util.List)335 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)298 Properties (java.util.Properties)255 DatabaseException (net.jforum.exceptions.DatabaseException)249 HashMap (java.util.HashMap)232 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)184 Timestamp (java.sql.Timestamp)171 CallableStatement (java.sql.CallableStatement)165 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)160 DalHints (com.ctrip.platform.dal.dao.DalHints)159 Map (java.util.Map)125 Date (java.util.Date)123