/**
   * Opens this database. The database should be opened after construction. or reopened by the
   * close(int closemode) method during a "shutdown compact". Closes the log if there is an error.
   */
  void reopen() {

    boolean isNew = false;

    setState(DATABASE_OPENING);

    try {
      nameManager = new HsqlNameManager(this);
      granteeManager = new GranteeManager(this);
      userManager = new UserManager(this);
      schemaManager = new SchemaManager(this);
      persistentStoreCollection = new PersistentStoreCollectionDatabase(this);
      isReferentialIntegrity = true;
      sessionManager = new SessionManager(this);
      collation = collation.newDatabaseInstance();
      dbInfo = DatabaseInformation.newDatabaseInformation(this);
      txManager = new TransactionManager2PL(this);

      lobManager.createSchema();
      sessionManager.getSysLobSession().setSchema(SqlInvariants.LOBS_SCHEMA);
      schemaManager.setSchemaChangeTimestamp();
      schemaManager.createSystemTables();

      // completed metadata
      logger.open();

      isNew = logger.isNewDatabase;

      if (isNew) {
        String username = urlProperties.getProperty("user", "SA");
        String password = urlProperties.getProperty("password", "");

        userManager.createFirstUser(username, password);
        schemaManager.createPublicSchema();
        logger.checkpoint(false);
      }

      lobManager.open();
      dbInfo.setWithContent(true);

      checkpointRunner = new CheckpointRunner();
      timeoutRunner = new TimeoutRunner();
    } catch (Throwable e) {
      logger.close(Database.CLOSEMODE_IMMEDIATELY);
      logger.releaseLock();
      setState(DATABASE_SHUTDOWN);
      clearStructures();
      DatabaseManager.removeDatabase(this);

      if (!(e instanceof HsqlException)) {
        e = Error.error(ErrorCode.GENERAL_ERROR, e);
      }

      logger.logSevereEvent("could not reopen database", e);

      throw (HsqlException) e;
    }

    setState(DATABASE_ONLINE);
  }
  public String[] getSettingsSQL() {

    HsqlArrayList list = new HsqlArrayList();
    StringBuffer sb = new StringBuffer();

    if (!getCatalogName().name.equals(HsqlNameManager.DEFAULT_CATALOG_NAME)) {
      String name = getCatalogName().statementName;

      sb.append("ALTER CATALOG PUBLIC RENAME TO ").append(name);
      list.add(sb.toString());
      sb.setLength(0);
    }

    if (!collation.isDefaultCollation()) {
      list.add(collation.getDatabaseCollationSQL());
    }

    HashMappedList lobTables = schemaManager.getTables(SqlInvariants.LOBS_SCHEMA);

    for (int i = 0; i < lobTables.size(); i++) {
      Table table = (Table) lobTables.get(i);

      if (table.isCached()) {
        sb.append(Tokens.T_SET).append(' ').append(Tokens.T_TABLE);
        sb.append(' ');
        sb.append(table.getName().getSchemaQualifiedStatementName());
        sb.append(' ').append(Tokens.T_TYPE).append(' ');
        sb.append(Tokens.T_CACHED);
        list.add(sb.toString());
        sb.setLength(0);
      }
    }

    String[] array = new String[list.size()];

    list.toArray(array);

    return array;
  }
  static {
    SqlInvariants.isSystemSchemaName(SqlInvariants.SYSTEM_SCHEMA);
    Charset.getDefaultInstance();
    Collation.getDefaultInstance();

    sessionAttributesMetaData = ResultMetaData.newResultMetaData(SessionInterface.INFO_LIMIT);

    for (int i = 0; i < Session.INFO_LIMIT; i++) {
      sessionAttributesMetaData.columns[i] = new ColumnBase(null, null, null, null);
    }

    sessionAttributesMetaData.columns[Session.INFO_ID].setType(Type.SQL_INTEGER);
    sessionAttributesMetaData.columns[Session.INFO_INTEGER].setType(Type.SQL_INTEGER);
    sessionAttributesMetaData.columns[Session.INFO_BOOLEAN].setType(Type.SQL_BOOLEAN);
    sessionAttributesMetaData.columns[Session.INFO_VARCHAR].setType(Type.SQL_VARCHAR);
    sessionAttributesMetaData.prepareData();
  }