/**
   * Records a Log entry representing a new connection action on the specified Session object.
   *
   * @param session the Session object for which to record the log entry
   * @throws HsqlException if there is a problem recording the Log entry
   */
  public synchronized void logConnectUser(Session session) throws HsqlException {

    if (logStatements) {
      writeToLog(session, session.getUser().getConnectUserSQL());
    }
  }
  /**
   * Determines if the authorizations are adequate to execute the compiled object. Completion
   * requires the list of all database objects in a compiled statement.
   */
  void checkAccessRights(Session session) {

    if (targetTable != null && !targetTable.isTemp()) {
      Grantee owner = targetTable.getOwner();

      if (owner != null && owner.isSystem()) {
        if (!session.getUser().isSystem()) {
          throw Error.error(ErrorCode.X_42501, targetTable.getName().name);
        }
      }

      if (!session.isProcessingScript) {
        targetTable.checkDataReadOnly();
      }

      session.checkReadWrite();
    }

    if (session.isAdmin()) {
      return;
    }

    for (int i = 0; i < sequences.length; i++) {
      session.getGrantee().checkAccess(sequences[i]);
    }

    for (int i = 0; i < routines.length; i++) {
      if (routines[i].isLibraryRoutine()) {
        continue;
      }

      session.getGrantee().checkAccess(routines[i]);
    }

    for (int i = 0; i < rangeVariables.length; i++) {
      RangeVariable range = rangeVariables[i];

      if (range.rangeTable.getSchemaName() == SqlInvariants.SYSTEM_SCHEMA_HSQLNAME) {
        continue;
      }

      session.getGrantee().checkSelect(range.rangeTable, range.usedColumns);
    }

    switch (type) {
      case StatementTypes.CALL:
        {
          break;
        }
      case StatementTypes.INSERT:
        {
          session.getGrantee().checkInsert(targetTable, insertCheckColumns);

          break;
        }
      case StatementTypes.SELECT_CURSOR:
        break;

      case StatementTypes.DELETE_WHERE:
        {
          session.getGrantee().checkDelete(targetTable);

          break;
        }
      case StatementTypes.UPDATE_WHERE:
        {
          session.getGrantee().checkUpdate(targetTable, updateCheckColumns);

          break;
        }
      case StatementTypes.MERGE:
        {
          session.getGrantee().checkInsert(targetTable, insertCheckColumns);
          session.getGrantee().checkUpdate(targetTable, updateCheckColumns);

          break;
        }
    }
  }
 /**
  * Records a Log entry representing a new connection action on the specified Session object.
  *
  * @param session the Session object for which to record the log entry
  * @throws HsqlException if there is a problem recording the Log entry
  */
 void logConnectUser(Session session) throws HsqlException {
   writeToLog(session, session.getUser().getConnectStatement());
 }
  /**
   * This is used to read the *.log file and manage any necessary transaction rollback.
   *
   * @throws HsqlException
   */
  public static void runScript(Database database, String logFilename, int logType)
      throws HsqlException {

    IntKeyHashMap sessionMap = new IntKeyHashMap();
    Session sysSession = database.getSessionManager().getSysSession(null, false);
    Session current = sysSession;
    int currentId = 0;

    database.setReferentialIntegrity(false);

    ScriptReaderBase scr = null;

    try {
      StopWatch sw = new StopWatch();

      scr =
          ScriptReaderBase.newScriptReader(database, logFilename, ScriptWriterBase.SCRIPT_TEXT_170);

      while (scr.readLoggedStatement(current)) {
        int sessionId = scr.getSessionNumber();

        if (currentId != sessionId) {
          currentId = sessionId;
          current = (Session) sessionMap.get(currentId);

          if (current == null) {
            current =
                database
                    .getSessionManager()
                    .newSession(database, sysSession.getUser(), false, true);

            sessionMap.put(currentId, current);
          }
        }

        if (current.isClosed()) {
          sessionMap.remove(currentId);

          continue;
        }

        String schema = current.currentSchema.name;
        Result result = null;

        switch (scr.getStatementType()) {
          case ScriptReaderBase.ANY_STATEMENT:
            result = current.sqlExecuteDirectNoPreChecks(scr.getLoggedStatement());

            if (result != null && result.mode == ResultConstants.ERROR) {
              if (result.getException() != null) {
                throw result.getException();
              }

              throw Trace.error(result);
            }
            break;

          case ScriptReaderBase.SEQUENCE_STATEMENT:
            scr.getCurrentSequence().reset(scr.getSequenceValue());
            break;

          case ScriptReaderBase.COMMIT_STATEMENT:
            current.commit();
            break;

          case ScriptReaderBase.INSERT_STATEMENT:
            {
              Object[] data = scr.getData();

              scr.getCurrentTable().insertNoCheckFromLog(current, data);

              break;
            }
          case ScriptReaderBase.DELETE_STATEMENT:
            {
              Object[] data = scr.getData();

              scr.getCurrentTable().deleteNoCheckFromLog(current, data);

              break;
            }
          case ScriptReaderBase.SCHEMA_STATEMENT:
            {
              current.setSchema(scr.getCurrentSchema());
            }
        }

        if (current.isClosed()) {
          sessionMap.remove(currentId);
        }
      }
    } catch (Throwable e) {
      String message;

      // catch out-of-memory errors and terminate
      if (e instanceof OutOfMemoryError) {
        message = "out of memory processing " + logFilename + " line: " + scr.getLineNumber();

        database.logger.appLog.logContext(message);

        throw Trace.error(Trace.OUT_OF_MEMORY);
      }

      // stop processing on bad log line
      message = logFilename + " line: " + scr.getLineNumber() + " " + e.getMessage();

      database.logger.appLog.logContext(message);
      Trace.printSystemOut(message);
    } finally {
      if (scr != null) {
        scr.close();
      }

      database.getSessionManager().closeAllSessions();
      database.setReferentialIntegrity(true);
    }
  }