/** Get number of elements in INTO or var=col assignment clause */ int getIntoCount(HplsqlParser.Select_stmtContext ctx) { HplsqlParser.Into_clauseContext into = getIntoClause(ctx); if (into != null) { return into.ident().size(); } List<HplsqlParser.Select_list_itemContext> sl = ctx.fullselect_stmt() .fullselect_stmt_item(0) .subselect_stmt() .select_list() .select_list_item(); if (sl.get(0).T_EQUAL() != null) { return sl.size(); } return 0; }
/** Get variable name assigned in INTO or var=col clause by index */ String getIntoVariable(HplsqlParser.Select_stmtContext ctx, int idx) { HplsqlParser.Into_clauseContext into = getIntoClause(ctx); if (into != null) { return into.ident(idx).getText(); } HplsqlParser.Select_list_itemContext sl = ctx.fullselect_stmt() .fullselect_stmt_item(0) .subselect_stmt() .select_list() .select_list_item(idx); if (sl != null) { return sl.ident().getText(); } return null; }
/** Executing or building SELECT statement */ public Integer select(HplsqlParser.Select_stmtContext ctx) { if (ctx.parent instanceof HplsqlParser.StmtContext) { exec.stmtConnList.clear(); trace(ctx, "SELECT"); } boolean oldBuildSql = exec.buildSql; exec.buildSql = true; StringBuilder sql = new StringBuilder(); if (ctx.cte_select_stmt() != null) { sql.append(evalPop(ctx.cte_select_stmt()).toString()); sql.append("\n"); } sql.append(evalPop(ctx.fullselect_stmt()).toString()); exec.buildSql = oldBuildSql; if (!(ctx.parent instanceof HplsqlParser.StmtContext)) { // No need to execute at this stage exec.stackPush(sql); return 0; } if (trace && ctx.parent instanceof HplsqlParser.StmtContext) { trace(ctx, sql.toString()); } if (exec.getOffline()) { trace(ctx, "Not executed - offline mode set"); return 0; } String conn = exec.getStatementConnection(); Query query = exec.executeQuery(ctx, sql.toString(), conn); if (query.error()) { exec.signal(query); return 1; } trace(ctx, "SELECT completed successfully"); exec.setSqlSuccess(); try { ResultSet rs = query.getResultSet(); ResultSetMetaData rm = null; if (rs != null) { rm = rs.getMetaData(); } int into_cnt = getIntoCount(ctx); if (into_cnt > 0) { trace(ctx, "SELECT INTO statement executed"); if (rs.next()) { for (int i = 1; i <= into_cnt; i++) { String into_name = getIntoVariable(ctx, i - 1); Var var = exec.findVariable(into_name); 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 { trace(ctx, "Variable not found: " + into_name); } } exec.incRowCount(); exec.setSqlSuccess(); } else { exec.setSqlCode(100); exec.signal(Signal.Type.NOTFOUND); } } // Print all results for standalone SELECT statement else if (ctx.parent instanceof HplsqlParser.StmtContext) { int cols = rm.getColumnCount(); if (trace) { trace(ctx, "Standalone SELECT executed: " + cols + " columns in the result set"); } 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(""); exec.incRowCount(); } } // Scalar subquery else { trace(ctx, "Scalar subquery executed, first row and first column fetched only"); if (rs.next()) { exec.stackPush(new Var().setValue(rs, rm, 1)); exec.setSqlSuccess(); } else { evalNull(); exec.setSqlCode(100); } } } catch (SQLException e) { exec.signal(query); exec.closeQuery(query, exec.conf.defaultConnection); return 1; } exec.closeQuery(query, exec.conf.defaultConnection); return 0; }
/** Get INTO clause */ HplsqlParser.Into_clauseContext getIntoClause(HplsqlParser.Select_stmtContext ctx) { if (ctx.fullselect_stmt().fullselect_stmt_item(0).subselect_stmt() != null) { return ctx.fullselect_stmt().fullselect_stmt_item(0).subselect_stmt().into_clause(); } return null; }