Пример #1
0
  /**
   * Création d'une société à partir de la base GestionDefault
   *
   * @param baseDefault nom de la base par défaut
   * @param newBase nom de la nouvelle base
   */
  public static void dupliqueDB(String baseDefault, String newBase, StatusListener l) {
    final DBSystemRoot sysRoot = Configuration.getInstance().getSystemRoot();

    // FIXME ADD TRIGGER TO UPDATE SOLDE COMPTE_PCE
    // ComptaPropsConfiguration instance = ComptaPropsConfiguration.create();
    // Configuration.setInstance(instance);

    try {
      log(l, "Création du schéma");
      if (!sysRoot.getRootsToMap().contains(baseDefault)) {
        sysRoot.getRootsToMap().add(baseDefault);
        sysRoot.refetch(Collections.singleton(baseDefault));
      }
      final DBRoot baseSQLDefault = sysRoot.getRoot(baseDefault);
      log(l, "Traitement des " + baseSQLDefault.getChildrenNames().size() + " tables");

      final SQLCreateRoot createRoot =
          baseSQLDefault.getDefinitionSQL(sysRoot.getServer().getSQLSystem());
      final SQLDataSource ds = sysRoot.getDataSource();
      // be safe don't add DROP SCHEMA
      ds.execute(createRoot.asString(newBase, false, true));
      sysRoot.getRootsToMap().add(newBase);
      // TODO find a more functional way
      final boolean origVal = Boolean.getBoolean(SQLSchema.NOAUTO_CREATE_METADATA);
      if (!origVal) System.setProperty(SQLSchema.NOAUTO_CREATE_METADATA, "true");
      sysRoot.refetch(Collections.singleton(newBase));
      if (!origVal) System.setProperty(SQLSchema.NOAUTO_CREATE_METADATA, "false");
      final DBRoot baseSQLNew = sysRoot.getRoot(newBase);

      final Set<SQLTable> newTables = baseSQLNew.getTables();
      int i = 0;
      final SQLSyntax syntax = sysRoot.getServer().getSQLSystem().getSyntax();
      // MAYBE SQLCreateRoot can avoid creating foreign constraints, then we insert data,
      // finally SQLCreateRoot adds just the constraints
      ds.execute(syntax.disableFKChecks(baseSQLNew));
      for (final SQLTable table : newTables) {
        String tableName = table.getName();

        log(l, "Copie de la table " + tableName + " " + (i + 1) + "/" + newTables.size());
        // Dump Table
        dumpTable(baseSQLDefault, table);
        log(l, "Table " + tableName + " " + (i + 1) + "/" + newTables.size() + " OK");
        i++;
      }
      ds.execute(syntax.enableFKChecks(baseSQLNew));

      if (syntax.getSystem() == SQLSystem.POSTGRESQL) {
        log(l, "Maj des séquences des tables");
        new FixSerial(sysRoot).changeAll(baseSQLNew);
      }

      log(l, "Duplication terminée");

    } catch (Throwable e) {
      e.printStackTrace();
      ExceptionHandler.handle("Erreur pendant la création de la base!", e);
      log(l, "Erreur pendant la duplication");
    }
  }
Пример #2
0
 /**
  * copie l'integralite de la table "tableName" de la base "base" dans la nouvelle base "baseNew"
  *
  * @param base
  * @param baseNew
  * @param tableName
  */
 private static void dumpTable(DBRoot source, SQLTable newTable) {
   try {
     SQLRowValues.insertFromTable(newTable, source.getTable(newTable.getName()));
   } catch (SQLException e) {
     System.err.println("Unable to dump table " + newTable.getName());
     e.printStackTrace();
   }
 }