Exemplo n.º 1
0
  /**
   * Execute query.
   *
   * @param sql the sql
   * @param async the asynch
   * @param queryName the query name
   * @return the string
   */
  @CliCommand(
      value = "query execute",
      help = "Execute query <query-string>. " + ASYNC_DOC + " " + QUERY_NAME_DOC)
  public String executeQuery(
      @CliOption(
              key = {"", "query"},
              mandatory = true,
              help = "<query-string>")
          String sql,
      @CliOption(
              key = {"async"},
              mandatory = false,
              unspecifiedDefaultValue = "false",
              specifiedDefaultValue = "true",
              help = "<async>")
          boolean async,
      @CliOption(
              key = {"name"},
              mandatory = false,
              help = "<query-name>")
          String queryName) {

    PrettyPrintable cliOutput;

    try {
      if (async) {
        QueryHandle queryHandle = getClient().executeQueryAsynch(sql, queryName).getData();
        return queryHandle.getHandleIdString();
      } else {
        return formatResultSet(getClient().getResults(sql, queryName));
      }
    } catch (final LensAPIException e) {

      BriefError briefError = new BriefError(e.getLensAPIErrorCode(), e.getLensAPIErrorMessage());
      cliOutput =
          new IdBriefErrorTemplate(
              IdBriefErrorTemplateKey.REQUEST_ID, e.getLensAPIRequestId(), briefError);

    } catch (final LensBriefErrorException e) {
      cliOutput = e.getIdBriefErrorTemplate();
    }

    return cliOutput.toPrettyString();
  }