예제 #1
0
 protected String makeConstraintName(String consName) throws SchemaException {
   boolean prev_constr = constraintList.contains(consName);
   if (prev_constr) { // need to find new name --
     Debugger.trace("Changing duplicate constraint name " + consName, Debugger.SHORT);
     int i = 1;
     while (constraintList.contains(consName + i)) i++;
     consName += i;
   }
   constraintList.add(consName);
   return consName;
 }
예제 #2
0
 protected String convertFields(String fields, JClass currentClass)
     throws SchemaException, IOException {
   Vector tokens = Tokenizer.tokenize(fields, ",", "/");
   Debugger.trace(fields + " token-size=" + tokens.size(), Debugger.VERBOSE);
   String sOut;
   if (tokens == null || tokens.size() < 1)
     throw new SchemaException(
         "Invalid constraint specification ", currentClass.getSchemaFileName());
   else sOut = jToSql((String) tokens.elementAt(0), currentClass);
   for (int j = 1; j < tokens.size(); j++) {
     sOut += "," + jToSql((String) tokens.elementAt(j), currentClass);
   }
   return sOut;
 }
예제 #3
0
  public void generate(JClass currentClass) throws IOException, SchemaException {

    schemaFile.write("\ncreate table " + currentClass.getTableName() + "(");

    String primaryKey = currentClass.getPrimaryKey(); // the sql name

    Vector allFields = new Vector(currentClass.getAllFields().values());

    boolean is_first = true;

    JField primField = null;
    HashSet fieldNames = new HashSet();

    if (Checks.exists(primaryKey)) { // set prim key first
      primField = currentClass.getFieldByName(currentClass.getPrimaryKeyJava());
      writeFieldSchema(currentClass, primField, primaryKey, is_first);
      is_first = false;
      fieldNames.add(primField.getSqlName());
    }

    for (int i = 0; i < allFields.size(); i++) {
      JField jf = (JField) allFields.elementAt(i);
      if (jf.isDbColumn() && jf != primField) {
        if (jf instanceof JCompositeField && ((JCompositeField) jf).isInline()) {
          // deal with inlining..
          inlineField(currentClass, (JCompositeField) jf, fieldNames);
        } else {
          if (!fieldNames.contains(jf.getSqlName())) fieldNames.add(jf.getSqlName());
          else
            Debugger.trace(
                "Duplicate column " + jf.getSqlName() + " in Class " + currentClass.getClassName(),
                Debugger.ERROR);
          writeFieldSchema(currentClass, jf, primaryKey, is_first);
        }
        is_first = false;
      }
    }

    if (currentClass.isEndDateable()) schemaFile.write(",\n\t" + END_DATE + " " + END_DATE_TYPE);
    if (currentClass.isVersioned()) schemaFile.write(",\n\t" + VERSION_NUMBER + " int");
    schemaFile.write(");\n");

    if (!currentClass.isManyToMany()) {
      if (needsSequence()) {
        cleanup.write(getDropSeqStmt(currentClass));
        schemaFile.write("\ncreate sequence " + currentClass.getTableName() + "_seq;\n");
      }
    } else {
      String columns = "";
      boolean first_column = true;
      for (int i = 0; i < allFields.size(); i++) {
        JField jf = (JField) allFields.elementAt(i);
        if (!jf.getSqlName().equalsIgnoreCase("")) {
          if (!first_column) columns += ",";
          else first_column = false;
          columns += jf.getSqlName();
        }
      }
      if (allFields.size() > 0) // && !isPostgres)
      {
        constraints +=
            ("\nalter table "
                + currentClass.getTableName()
                + " add constraint "
                + currentClass.getTableName()
                + "_unq primary key "
                + " ("
                + columns
                + ");\n");
      }
    }

    Iterator cons = currentClass.getConstraints();
    while (cons.hasNext()) {
      JConstraint jcons = (JConstraint) cons.next();
      String sOut = convertFields(jcons.getConstraint(), currentClass);
      String consName = makeConstraintName(jcons.getName());

      cleanUpConstraint(currentClass, consName);

      addConstraint(currentClass, consName, jcons, sOut);
    }
    Iterator indexes = currentClass.getIndexes().iterator();
    while (indexes.hasNext()) {
      JIndex index = (JIndex) indexes.next();
      String sOut = convertFields(index.getFields(), currentClass);

      cleanUpIndex(currentClass, index);

      addIndex(currentClass, sOut, index);
    }
  }