/**
   * Executes a SELECT statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.SELECT
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeSelectStatement(CompiledStatement cs) throws HsqlException {

    Select select = cs.select;
    Result result;

    if (select.sIntoTable != null) {

      // session level user rights
      session.checkDDLWrite();

      if (session.getDatabase().findUserTable(session, select.sIntoTable.name) != null
          || session.getDatabase().dInfo.getSystemTable(session, select.sIntoTable.name) != null) {
        throw Trace.error(Trace.TABLE_ALREADY_EXISTS, select.sIntoTable.name);
      }

      result = select.getResult(session.getMaxRows(), session);
      result =
          session.dbCommandInterpreter.processSelectInto(
              result, select.sIntoTable, select.intoType);

      session.getDatabase().setMetaDirty(false);
    } else {
      result = select.getResult(session.getMaxRows(), session);
    }

    return result;
  }
  /** Initializes this connection. */
  private void init() {

    runnerThread = Thread.currentThread();
    keepAlive = true;

    try {
      socket.setTcpNoDelay(true);

      dataInput = new DataInputStream(socket.getInputStream());
      dataOutput = new BufferedOutputStream(socket.getOutputStream());

      Result resultIn = Result.read(rowIn, dataInput);
      Result resultOut;

      try {
        int dbIndex = ArrayUtil.find(server.dbAlias, resultIn.subSubString);

        dbID = server.dbID[dbIndex];
        user = resultIn.getMainString();
        password = resultIn.getSubString();

        if (!server.isSilent()) {
          server.printWithThread(mThread + ":trying to connect user " + user);
        }

        session =
            DatabaseManager.newSession(dbID, resultIn.getMainString(), resultIn.getSubString());
        resultOut = new Result(ResultConstants.UPDATECOUNT);
        resultOut.databaseID = session.getDatabase().databaseID;
        resultOut.sessionID = session.getId();
      } catch (HsqlException e) {
        session = null;
        resultOut = new Result(e, null);
      } catch (ArrayIndexOutOfBoundsException e) {
        session = null;
        resultOut = new Result(Trace.error(Trace.DATABASE_NOT_EXISTS), resultIn.subSubString);
      }

      Result.write(resultOut, rowOut, dataOutput);

      return;
    } catch (Exception e) {
      server.printWithThread(mThread + ":couldn't connect " + user);
    }

    close();
  }
  /** Initializes this connection. */
  private void init() {

    runnerThread = Thread.currentThread();
    keepAlive = true;

    try {
      socket.setTcpNoDelay(true);

      dataInput = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      dataOutput = new BufferedOutputStream(socket.getOutputStream());

      Result resultIn = Result.read(rowIn, dataInput);
      Result resultOut;

      try {
        dbID = server.getDBID(resultIn.subSubString);
        user = resultIn.getMainString();
        password = resultIn.getSubString();

        if (!server.isSilent()) {
          server.printWithThread(mThread + ":trying to connect user " + user);
        }

        session =
            DatabaseManager.newSession(dbID, resultIn.getMainString(), resultIn.getSubString());
        resultOut = new Result(ResultConstants.UPDATECOUNT);
        resultOut.databaseID = session.getDatabase().databaseID;
        resultOut.sessionID = session.getId();
      } catch (HsqlException e) {
        session = null;
        resultOut = new Result(e, null);
      } catch (RuntimeException e) {
        session = null;
        resultOut = new Result(e, null);
      }

      Result.write(resultOut, rowOut, dataOutput);

      return;
    } catch (Exception e) {
      server.printWithThread(mThread + ":couldn't connect " + user);
    }

    close();
  }
  /** Used by pooled connections to close the existing SQL session and open a new one. */
  private Result resetSession() {

    Result resultOut;

    if (!server.isSilent()) {
      server.printWithThread(mThread + ":trying to connect user " + user);
    }

    try {
      session.close();

      session = DatabaseManager.newSession(dbID, user, password);
      resultOut = new Result(ResultConstants.UPDATECOUNT);
      resultOut.databaseID = session.getDatabase().databaseID;
      resultOut.sessionID = session.getId();
    } catch (HsqlException e) {
      session = null;
      resultOut = new Result(e, null);
    }

    return resultOut;
  }