/**
   * Releases the link betwen the session and all compiled statement objects it is linked to.
   *
   * <p>If any such statement is not linked with any other session, it is removed from management.
   *
   * @param sid the session identifier
   */
  synchronized void removeSession(int sid) {

    IntKeyIntValueHashMap scsMap;
    int csid;
    Iterator i;

    scsMap = (IntKeyIntValueHashMap) sessionMap.remove(sid);

    if (scsMap == null) {
      return;
    }

    i = scsMap.keySet().iterator();

    while (i.hasNext()) {
      csid = i.nextInt();

      int usecount = useMap.get(csid, 1) - 1;

      if (usecount == 0) {
        String sql = (String) sqlLookup.remove(csid);

        sqlMap.remove(sql);
        csidMap.remove(csid);
        useMap.remove(csid);
      } else {
        useMap.put(csid, usecount);
      }
    }
  }
  /**
   * Used after a DDL change that could impact the compiled statements. Clears references to
   * CompiledStatement objects while keeping the counts and references to the sql strings.
   */
  synchronized void resetStatements() {

    Iterator it = csidMap.keySet().iterator();

    while (it.hasNext()) {
      int key = it.nextInt();

      csidMap.put(key, null);
    }
  }
  /** Clears all internal data structures, removing any references to compiled statements. */
  synchronized void reset() {

    sqlMap.clear();
    sqlLookup.clear();
    csidMap.clear();
    sessionMap.clear();
    useMap.clear();

    next_cs_id = 0;
  }
  /**
   * Registers a compiled statement to be managed.
   *
   * <p>The only caller should be a Session that is attempting to prepare a statement for the first
   * time, or a statement that has been invalidated due to DDL changes.
   *
   * @param csid existing id or negative if the statement is not yet managed
   * @param cs The CompiledStatement to add
   * @return The compiled statement id assigned to the CompiledStatement object
   */
  synchronized int registerStatement(int csid, CompiledStatement cs) {

    if (csid < 0) {
      csid = nextID();

      sqlMap.put(cs.sql, csid);
      sqlLookup.put(csid, cs.sql);
    }

    csidMap.put(csid, cs);

    return csid;
  }
  /**
   * Links a session with a registered compiled statement.
   *
   * <p>If this session has not already been linked with the given statement, then the statement use
   * count is incremented.
   *
   * @param csid the compiled statement identifier
   * @param sid the session identifier
   */
  synchronized void linkSession(int csid, int sid) {

    IntKeyIntValueHashMap scsMap;

    scsMap = (IntKeyIntValueHashMap) sessionMap.get(sid);

    if (scsMap == null) {
      scsMap = new IntKeyIntValueHashMap();

      sessionMap.put(sid, scsMap);
    }

    int count = scsMap.get(csid, 0);

    scsMap.put(csid, count + 1);

    if (count == 0) {
      useMap.put(csid, useMap.get(csid, 0) + 1);
    }
  }
  /**
   * Removes the link between a session and a compiled statement.
   *
   * <p>If the statement is not linked with any other session, it is removed from management.
   *
   * @param csid the compiled statment identifier
   * @param sid the session identifier
   */
  synchronized void freeStatement(int csid, int sid) {

    IntKeyIntValueHashMap scsMap = (IntKeyIntValueHashMap) sessionMap.get(sid);
    int count = scsMap.get(csid) - 1;

    if (count != 0) {
      scsMap.put(csid, count);
    } else {
      scsMap.remove(csid);

      int usecount = useMap.get(csid, 1) - 1;

      if (usecount == 0) {
        String sql = (String) sqlLookup.remove(csid);

        sqlMap.remove(sql);
        csidMap.remove(csid);
        useMap.remove(csid);
      } else {
        useMap.put(csid, usecount);
      }
    }
  }
  /**
   * 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);
    }
  }
Ejemplo n.º 8
0
 /** Returns SQL type string for a java.sql.Types int value */
 static String getTypeString(int type) {
   return (String) typeNames.get(type);
 }
Ejemplo n.º 9
0
  static {
    typeAliases = new IntValueHashMap(67, 1);

    typeAliases.put("INTEGER", Types.INTEGER);
    typeAliases.put("INT", Types.INTEGER);
    typeAliases.put("int", Types.INTEGER);
    typeAliases.put("java.lang.Integer", Types.INTEGER);
    typeAliases.put("IDENTITY", Types.INTEGER);
    typeAliases.put("DOUBLE", Types.DOUBLE);
    typeAliases.put("double", Types.DOUBLE);
    typeAliases.put("java.lang.Double", Types.DOUBLE);
    typeAliases.put("FLOAT", Types.FLOAT);
    typeAliases.put("REAL", Types.REAL);
    typeAliases.put("VARCHAR", Types.VARCHAR);
    typeAliases.put("java.lang.String", Types.VARCHAR);
    typeAliases.put("CHAR", Types.CHAR);
    typeAliases.put("CHARACTER", Types.CHAR);
    typeAliases.put("LONGVARCHAR", Types.LONGVARCHAR);
    typeAliases.put("VARCHAR_IGNORECASE", VARCHAR_IGNORECASE);
    typeAliases.put("DATE", Types.DATE);
    typeAliases.put("java.sql.Date", Types.DATE);
    typeAliases.put("TIME", Types.TIME);
    typeAliases.put("java.sql.Time", Types.TIME);
    typeAliases.put("TIMESTAMP", Types.TIMESTAMP);
    typeAliases.put("java.sql.Timestamp", Types.TIMESTAMP);
    typeAliases.put("DATETIME", Types.TIMESTAMP);
    typeAliases.put("DECIMAL", Types.DECIMAL);
    typeAliases.put("java.math.BigDecimal", Types.DECIMAL);
    typeAliases.put("NUMERIC", Types.NUMERIC);
    typeAliases.put("BIT", Types.BIT);
    typeAliases.put("boolean", Types.BIT);
    typeAliases.put("java.lang.Boolean", Types.BIT);
    typeAliases.put("TINYINT", Types.TINYINT);
    typeAliases.put("byte", Types.TINYINT);
    typeAliases.put("java.lang.Byte", Types.TINYINT);
    typeAliases.put("SMALLINT", Types.SMALLINT);
    typeAliases.put("short", Types.SMALLINT);
    typeAliases.put("java.lang.Short", Types.SMALLINT);
    typeAliases.put("BIGINT", Types.BIGINT);
    typeAliases.put("long", Types.BIGINT);
    typeAliases.put("java.lang.Long", Types.BIGINT);
    typeAliases.put("BINARY", Types.BINARY);
    typeAliases.put("[B", Types.BINARY);
    typeAliases.put("VARBINARY", Types.VARBINARY);
    typeAliases.put("LONGVARBINARY", Types.LONGVARBINARY);
    typeAliases.put("OTHER", Types.OTHER);
    typeAliases.put("OBJECT", Types.OTHER);
    typeAliases.put("java.lang.Object", Types.OTHER);
    typeAliases.put("NULL", Types.NULL);
    typeAliases.put("void", Types.NULL);
    typeAliases.put("java.lang.Void", Types.NULL);

    //
    typeNames = new IntKeyHashMap(37);

    typeNames.put(Types.NULL, "NULL");
    typeNames.put(Types.INTEGER, "INTEGER");
    typeNames.put(Types.DOUBLE, "DOUBLE");
    typeNames.put(VARCHAR_IGNORECASE, "VARCHAR_IGNORECASE");
    typeNames.put(Types.VARCHAR, "VARCHAR");
    typeNames.put(Types.CHAR, "CHAR");
    typeNames.put(Types.LONGVARCHAR, "LONGVARCHAR");
    typeNames.put(Types.DATE, "DATE");
    typeNames.put(Types.TIME, "TIME");
    typeNames.put(Types.DECIMAL, "DECIMAL");
    typeNames.put(Types.BIT, "BIT");
    typeNames.put(Types.TINYINT, "TINYINT");
    typeNames.put(Types.SMALLINT, "SMALLINT");
    typeNames.put(Types.BIGINT, "BIGINT");
    typeNames.put(Types.REAL, "REAL");
    typeNames.put(Types.FLOAT, "FLOAT");
    typeNames.put(Types.NUMERIC, "NUMERIC");
    typeNames.put(Types.TIMESTAMP, "TIMESTAMP");
    typeNames.put(Types.BINARY, "BINARY");
    typeNames.put(Types.VARBINARY, "VARBINARY");
    typeNames.put(Types.LONGVARBINARY, "LONGVARBINARY");
    typeNames.put(Types.OTHER, "OBJECT");

    //
    illegalParameterClasses = new org.hsqldb.lib.HashSet(13);

    illegalParameterClasses.add(Byte.TYPE);
    illegalParameterClasses.add(Short.TYPE);
    illegalParameterClasses.add(Float.TYPE);
    illegalParameterClasses.add(Byte.class);
    illegalParameterClasses.add(Short.class);
    illegalParameterClasses.add(Float.class);
  }
 /**
  * Retreives the sql statement for a registered compiled statement.
  *
  * @param csid the compiled statement identifier
  * @return sql string
  */
 synchronized String getSql(int csid) {
   return (String) sqlLookup.get(csid);
 }
 /**
  * Retrieves the CompiledStatement object having the specified compiled statement identifier, or
  * null if the CompiledStatement object has been invalidated.
  *
  * @param csid the identifier of the requested CompiledStatement object
  * @return the requested CompiledStatement object
  */
 synchronized CompiledStatement getStatement(int csid) {
   return (CompiledStatement) csidMap.get(csid);
 }