Пример #1
0
  private static String getType(SQLField field) {
    String columnName = field.getName();
    String columnType = field.getType().getTypeName();
    StringBuffer result = new StringBuffer();
    // NULL OR NOT NULL
    // field.getType().getSize();
    // String nullable = tableMetaData.getString("IS_NULLABLE");
    String nullString = "NULL";
    DBStructureItemDB db = field.getDB();
    // if ("NO".equalsIgnoreCase(nullable)) {
    // nullString = "NOT NULL";
    // }

    // DEFAULT
    Object defaultValueO = field.getDefaultValue();
    String defaultValue = (defaultValueO == null) ? null : defaultValueO.toString();
    String defaultValueString = "";
    if (defaultValue != null) {
      defaultValueString = " default " + defaultValue;
    }

    int columnSize = field.getType().getSize();
    Integer decimalDigit = (Integer) field.getMetadata("DECIMAL_DIGITS");
    String stringColumnSize = "";
    if (Integer.valueOf(columnSize).intValue() > 0) {
      stringColumnSize = " (" + columnSize;

      if (decimalDigit != null && Integer.valueOf(decimalDigit).intValue() > 0) {
        stringColumnSize += ", " + decimalDigit;
      }

      stringColumnSize += ")";
    }

    if ((columnType.trim().equalsIgnoreCase("character varying")
            || columnType.trim().equalsIgnoreCase("varchar")
            || columnType.trim().equalsIgnoreCase("numeric"))
        && stringColumnSize.length() > 0) {
      result.append(" " + columnType + stringColumnSize + " ");
      result.append(defaultValueString);
    } else {
      result.append(" " + columnType + " ");
      result.append(defaultValueString);
    }
    return result.toString();
  }
Пример #2
0
  public static void fixUserCommon(int base) {

    if (Configuration.getInstance() == null) {
      Configuration.setInstance(ComptaPropsConfiguration.create());
    }
    Configuration instance = Configuration.getInstance();
    SQLTable tableSociete = Configuration.getInstance().getBase().getTable("SOCIETE_COMMON");

    String baseName = tableSociete.getRow(base).getString("DATABASE_NAME");

    instance.getBase().getDBSystemRoot().getRootsToMap().clear();
    try {
      Set<String> s = new HashSet<String>();
      s.add(baseName);
      instance.getBase().fetchTables(s);
    } catch (SQLException e) {
      throw new IllegalStateException("could not access societe base", e);
    }

    System.err.println("baseName" + baseName);
    instance.getSystemRoot().prependToRootPath("Common");
    instance.getSystemRoot().prependToRootPath(baseName);

    SQLSchema baseSQL = instance.getBase().getSchema(baseName);

    DatabaseMetaData dbMetaDataSociete;
    try {
      dbMetaDataSociete = baseSQL.getBase().getDataSource().getConnection().getMetaData();

      String[] type = new String[1];
      type[0] = "TABLE";
      ResultSet rs = dbMetaDataSociete.getTables("", baseSQL.getName(), "%", null);

      System.err.println("Start " + rs.getFetchSize());
      int i = 0;
      while (rs.next()) {

        if (rs.getString("TABLE_TYPE") != null
            && rs.getString("TABLE_TYPE").equalsIgnoreCase("TABLE")) {
          // System.err.println("FIND TABLE");
          // baseSQL.getTableNames();
          final SQLTable table = baseSQL.getTable(rs.getString("TABLE_NAME"));
          Set<SQLField> s = table.getFields();
          for (SQLField field : s) {
            if (field.getName().equalsIgnoreCase("ID_USER_COMMON_CREATE")
                || field.getName().equalsIgnoreCase("ID_USER_COMMON_MODIFY")) {
              Object o = field.getDefaultValue();
              if (o == null || (o instanceof Integer && ((Integer) o) == 0)) {

                System.err.println("Bad default on " + field);
                baseSQL
                    .getBase()
                    .execute(
                        "ALTER TABLE \""
                            + field.getTable().getSchema().getName()
                            + "\".\""
                            + field.getTable().getName()
                            + "\" ALTER COLUMN \""
                            + field.getName()
                            + "\" SET DEFAULT 1;");

                baseSQL
                    .getBase()
                    .execute(
                        "UPDATE \""
                            + field.getTable().getSchema().getName()
                            + "\".\""
                            + field.getTable().getName()
                            + "\" SET \""
                            + field.getName()
                            + "\"=1 WHERE \""
                            + field.getName()
                            + "\"=0 OR \""
                            + field.getName()
                            + "\" IS NULL;");
              }
            }
          }
        }
        // System.err.println(i++ + " " + rs.getString("TABLE_TYPE"));
      }
      rs.close();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }