Ejemplo n.º 1
0
 /** EXEC, EXECUTE and EXECUTE IMMEDIATE statement to execute dynamic SQL */
 public Integer exec(HplsqlParser.Exec_stmtContext ctx) {
   if (trace) {
     trace(ctx, "EXECUTE");
   }
   Var vsql = evalPop(ctx.expr());
   String sql = vsql.toString();
   if (trace) {
     trace(ctx, "Query: " + sql);
   }
   Query query = exec.executeSql(ctx, sql, exec.conf.defaultConnection);
   if (query.error()) {
     exec.signal(query);
     return 1;
   }
   ResultSet rs = query.getResultSet();
   if (rs != null) {
     try {
       ResultSetMetaData rsm = rs.getMetaData();
       // Assign to variables
       if (ctx.T_INTO() != null) {
         int cols = ctx.L_ID().size();
         if (rs.next()) {
           for (int i = 0; i < cols; i++) {
             Var var = exec.findVariable(ctx.L_ID(i).getText());
             if (var != null) {
               var.setValue(rs, rsm, i + 1);
               if (trace) {
                 trace(
                     ctx,
                     "COLUMN: " + rsm.getColumnName(i + 1) + ", " + rsm.getColumnTypeName(i + 1));
                 trace(ctx, "SET " + var.getName() + " = " + var.toString());
               }
             } else if (trace) {
               trace(ctx, "Variable not found: " + ctx.L_ID(i).getText());
             }
           }
           exec.setSqlCode(0);
         }
       }
       // Print the results
       else {
         int cols = rsm.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.setSqlCode(e);
     }
   }
   exec.closeQuery(query, exec.conf.defaultConnection);
   return 0;
 }
Ejemplo n.º 2
0
 @Override
 public String toString() {
   final TokenBuilder tb = new TokenBuilder().add(var.toString()).add(' ').add(ASSIGN);
   tb.add(' ').add(expr.toString());
   if (coll != null) tb.add(' ').add(COLLATION).add(" \"").add(coll.uri()).add('"');
   return tb.toString();
 }
Ejemplo n.º 3
0
 /** FETCH cursor statement */
 public Integer fetch(HplsqlParser.Fetch_stmtContext ctx) {
   trace(ctx, "FETCH");
   String name = ctx.L_ID(0).toString();
   Var cursor = exec.findVariable(name);
   if (cursor == null || cursor.type != Type.CURSOR) {
     trace(ctx, "Cursor not found: " + 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()) {
         for (int i = 1; i <= cols; i++) {
           Var var = exec.findVariable(ctx.L_ID(i).getText());
           if (var != null) {
             var.setValue(rs, rsm, i);
             if (trace) {
               trace(ctx, "COLUMN: " + rsm.getColumnName(i) + ", " + rsm.getColumnTypeName(i));
               trace(ctx, "SET " + var.getName() + " = " + var.toString());
             }
           } else if (trace) {
             trace(ctx, "Variable not found: " + ctx.L_ID(i).getText());
           }
         }
         exec.incRowCount();
         exec.setSqlSuccess();
       } else {
         exec.setSqlCode(100);
         exec.signal(Signal.Type.NOTFOUND);
       }
     }
   } catch (SQLException e) {
     exec.setSqlCode(e);
     exec.signal(Signal.Type.SQLEXCEPTION, e.getMessage(), e);
   }
   return 0;
 }
Ejemplo n.º 4
0
 /** VALUES statement */
 public Integer values(HplsqlParser.Values_into_stmtContext ctx) {
   trace(ctx, "VALUES statement");
   int cnt = ctx.ident().size(); // Number of variables and assignment expressions
   int ecnt = ctx.expr().size();
   for (int i = 0; i < cnt; i++) {
     String name = ctx.ident(i).getText();
     if (i < ecnt) {
       visit(ctx.expr(i));
       Var var = exec.setVariable(name);
       if (trace) {
         trace(ctx, "SET " + name + " = " + var.toString());
       }
     }
   }
   return 0;
 }
Ejemplo n.º 5
0
 /** 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;
 }
Ejemplo n.º 6
0
 /** String concatenation operator */
 public void operatorConcat(HplsqlParser.Expr_concatContext ctx) {
   StringBuilder val = new StringBuilder();
   int cnt = ctx.expr_concat_item().size();
   boolean nulls = true;
   for (int i = 0; i < cnt; i++) {
     Var c = evalPop(ctx.expr_concat_item(i));
     if (!c.isNull()) {
       val.append(c.toString());
       nulls = false;
     }
   }
   if (nulls) {
     evalNull();
   } else {
     evalString(val);
   }
 }
Ejemplo n.º 7
0
 public String toString() {
   String r = "{\r\n";
   if (this.lable != null) r = r + "lable in generated code";
   r = r + "type:function\r\n";
   r = r + "parameter:\r\n";
   if (par != null) {
     for (int i = 0; i < par.size(); i++) r += par.get(i).toString() + "\r\n";
   }
   if (rvalue != null) {
     r += "return:\r\n";
     r += rvalue.toString() + "\r\n";
   }
   if (table != null) {
     r += "symbolTable:\r\n";
     r += table.toString() + "\r\n";
   }
   String rr = "";
   for (int i = 0; i < r.length(); i++) {
     rr += r.charAt(i);
     if (r.charAt(i) == '\n') rr += '\t';
   }
   rr += "\r\n}";
   return rr;
 }
Ejemplo n.º 8
0
 /** 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;
 }