Exemplo n.º 1
0
  public void voltQueueSQL(final String sql, Object... args) {
    if (sql == null || sql.isEmpty()) {
      throw new IllegalArgumentException("SQL statement '" + sql + "' is null or the empty string");
    }

    try {
      AdHocPlannedStmtBatch paw =
          m_csp
              .plan(sql, !m_catProc.getSinglepartition(), ProcedureInvocationType.ORIGINAL, 0, 0)
              .get();
      if (paw.errorMsg != null) {
        throw new VoltAbortException("Failed to plan sql '" + sql + "' error: " + paw.errorMsg);
      }

      if (m_catProc.getReadonly() && !paw.isReadOnly()) {
        throw new VoltAbortException(
            "Attempted to queue DML adhoc sql '" + sql + "' from read only procedure");
      }

      assert (1 == paw.plannedStatements.size());

      QueuedSQL queuedSQL = new QueuedSQL();
      AdHocPlannedStatement plannedStatement = paw.plannedStatements.get(0);
      queuedSQL.stmt =
          SQLStmtAdHocHelper.createWithPlan(
              plannedStatement.sql,
              plannedStatement.core.aggregatorFragment,
              plannedStatement.core.collectorFragment,
              plannedStatement.core.isReplicatedTableDML,
              plannedStatement.core.readOnly,
              plannedStatement.core.parameterTypes);
      if (plannedStatement.extractedParamValues.size() == 0) {
        // case handles if there were parameters OR
        // if there were no constants to pull out
        queuedSQL.params = getCleanParams(queuedSQL.stmt, args);
      } else {
        if (args.length > 0) {
          throw new ExpectedProcedureException(
              "Number of arguments provided was "
                  + args.length
                  + " where 0 were expected for statement "
                  + sql);
        }
        Object[] extractedParams = plannedStatement.extractedParamValues.toArray();
        if (extractedParams.length != queuedSQL.stmt.numStatementParamJavaTypes) {
          String msg =
              String.format("Wrong number of extracted param for parameterized statement: %s", sql);
          throw new VoltAbortException(msg);
        }
        queuedSQL.params = getCleanParams(queuedSQL.stmt, extractedParams);
      }

      updateCRC(queuedSQL);
      m_batch.add(queuedSQL);
    } catch (Exception e) {
      if (e instanceof ExecutionException) {
        throw new VoltAbortException(e.getCause());
      }
      if (e instanceof VoltAbortException) {
        throw (VoltAbortException) e;
      }
      throw new VoltAbortException(e);
    }
  }