public Object execute()

in gateway-shell/src/main/java/org/apache/knox/gateway/shell/commands/SelectCommand.java [92:185]


  public Object execute(List<String> args) {
    boolean ok = false;
    String sql = "";
    String bindVariableName = null;
    KnoxShellTable table = null;

    if (!args.isEmpty()) {
      bindVariableName = getBindingVariableNameForResultingTable(args);
    }

    String dsName = (String) getVariables().get(KNOXDATASOURCE);
    @SuppressWarnings("unchecked")
    Map<String, KnoxDataSource> dataSources = getDataSources();
    KnoxDataSource ds = null;
    if (dsName == null || dsName.isEmpty()) {
      if (dataSources == null || dataSources.isEmpty()) {
        return "please configure a datasource with ':datasources add {name} {connectStr} {driver} {authntype: none|basic}'.";
      }
      else if (dataSources.size() == 1) {
        dsName = (String) dataSources.keySet().toArray()[0];
      }
      else {
        return "mulitple datasources configured. please disambiguate with ':datasources select {name}'.";
      }
    }

    sqlHistory = getSQLHistory(dsName);
    historyIndex = (sqlHistory != null && !sqlHistory.isEmpty()) ? sqlHistory.size() - 1 : -1;

    ds = dataSources.get(dsName);
    if (ds != null) {
      JLabel jl = new JLabel("Query: ");
      sqlField = new JTextArea(5,40);
      sqlField.addKeyListener(this);
      sqlField.setLineWrap(true);
      JScrollPane scrollPane = new JScrollPane(sqlField);
      Box box = Box.createHorizontalBox();
      box.add(jl);
      box.add(scrollPane);

      // JDK-5018574 : Unable to set focus to another component in JOptionPane
      SwingUtils.workAroundFocusIssue(sqlField);

      int x = JOptionPane.showConfirmDialog(null, box,
          "SQL Query Input", JOptionPane.OK_CANCEL_OPTION);

      if (x == JOptionPane.OK_OPTION) {
        ok = true;
        sql = sqlField.getText();
        addToSQLHistory(dsName, sql);
        historyIndex = -1;
      }

      //KnoxShellTable.builder().jdbc().connect("jdbc:derby:codejava/webdb1").driver("org.apache.derby.jdbc.EmbeddedDriver").username("lmccay").pwd("xxxx").sql("SELECT * FROM book");
      try {
        if (ok) {
          System.out.println(sql);
          try {
            Connection conn = getConnectionFromSession(ds);
            if (conn == null || conn.isClosed()) {
              String username = null;
              char[] pass = null;
              if (ds.getAuthnType().equalsIgnoreCase("basic")) {
                CredentialCollector dlg = login();
                username = dlg.name();
                pass = dlg.chars();
              }
              conn = getConnection(ds, username, new String(pass));
            }
            try (Statement statement = conn.createStatement()) {
              if (statement.execute(sql)) {
                try (ResultSet resultSet = statement.getResultSet()) {
                  table = KnoxShellTable.builder().jdbc().resultSet(resultSet);
                }
              }
            }
          }
          catch (SQLException e) {
            System.out.println("SQL Exception encountered... " + e.getMessage());
          }
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    else {
      return "please select a datasource via ':datasources select {name}'.";
    }
    if (table != null && bindVariableName != null) {
      getVariables().put(bindVariableName, table);
    }
    return table;
  }