/**
   * Method declaration
   *
   * @param c
   * @param s
   * @throws SQLException
   */
  void write(Session c, String s) throws SQLException {

    if (bRestoring || s == null || s.length() == 0) {
      return;
    }

    if (!bReadOnly) {
      int id = 0;

      if (c != null) {
        id = c.getId();
      }

      if (id != mLastId) {
        s = "/*C" + id + "*/" + s;
        mLastId = id;
      }

      try {
        writeLine(wScript, s);

        if (bWriteDelay) {
          bNeedFlush = true;
        } else {
          wScript.flush();
        }
      } catch (IOException e) {
        throw Trace.error(Trace.FILE_IO_ERROR, sFileScript);
      }

      // fredt@users - todo - eliminate new File() calls
      if (iLogSize > 0 && iLogCount++ > 100) {
        iLogCount = 0;

        if (scriptChecker.length() > iLogSize * 1024 * 1024) {
          checkpoint(false);
        }
      }
    }
  }
  /**
   * Method declaration
   *
   * @param db
   * @param file
   * @param full
   * @param session
   * @throws SQLException
   */
  static void scriptToFile(Database db, String file, boolean full, Session session)
      throws SQLException {

    if ((new File(file)).exists()) {

      // there must be no such file; overwriting not allowed for security
      throw Trace.error(Trace.FILE_IO_ERROR, file);
    }

    try {
      long time = 0;

      if (Trace.TRACE) {
        time = System.currentTimeMillis();
      }

      // only ddl commands; needs not so much memory
      Result r;

      if (full) {

        // no drop, no insert, and no positions for cached tables
        r = db.getScript(false, false, false, session);
      } else {

        // no drop, no insert, but positions for cached tables
        r = db.getScript(false, false, true, session);
      }

      Record n = r.rRoot;
      FileWriter w = new FileWriter(file);

      while (n != null) {
        writeLine(w, (String) n.data[0]);

        n = n.next;
      }

      // inserts are done separetely to save memory
      HsqlArrayList tables = db.getTables();

      for (int i = 0; i < tables.size(); i++) {
        Table t = (Table) tables.get(i);

        // cached tables have the index roots set in the ddl script
        if ((full || !t.isCached())
            && !t.isTemp()
            && !t.isView()
            && (!t.isText() || !t.isDataReadOnly())) {
          Index primary = t.getPrimaryIndex();
          Node x = primary.first();

          while (x != null) {
            writeLine(w, t.getInsertStatement(x.getData()));

            x = primary.next(x);
          }
        }

        // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
        if (t.isDataReadOnly() && !t.isTemp() && !t.isText()) {
          HsqlStringBuffer a = new HsqlStringBuffer("SET TABLE ");

          a.append(t.getName().statementName);
          a.append(" READONLY TRUE");
          writeLine(w, a.toString());
        }
      }

      w.close();

      if (Trace.TRACE) {
        Trace.trace(time - System.currentTimeMillis());
      }
    } catch (IOException e) {
      throw Trace.error(Trace.FILE_IO_ERROR, file + " " + e);
    }
  }