Пример #1
0
  /* write the schema line for one field*/
  protected void writeFieldSchema(
      JClass currentClass, JField jf, String primaryKey, boolean is_first)
      throws IOException, SchemaException {
    String sqlName = jf.getSqlName();
    String fieldType = jf.getSqlType();

    String sep = is_first ? "" : ",";
    if (sqlName.equals(primaryKey)) {
      schemaFile.write(sep);
      writePrimaryKey(currentClass, sqlName);
    } else {
      schemaFile.write(sep + "\n\t" + sqlName + " " + getSqlOutType(currentClass, jf));
      if (jf.isUnique()) writeUniqueConstraint(currentClass, jf);
      if (jf.isRequired()) schemaFile.write(" not null");
      else {
        String dbSpecificOptions = getDbSpecificOptions(currentClass, jf, primaryKey);
        if (dbSpecificOptions != null) schemaFile.write(" " + dbSpecificOptions);
      }
    }

    if (jf instanceof JCompositeField
        && ((JCompositeField) jf).getJClass() != null
        && ((JCompositeField) jf).getJClass().isLeaf()) {

      // is a foreign key and has a class to reference
      JCompositeField jcf = ((JCompositeField) jf);
      JClass foreignClass = jcf.getJClass();
      String cons_name = jcf.getConstraintName();
      if (!Checks.exists(cons_name)) {
        cons_name = currentClass.getClassName() + jf.getJavaName();
        if (cons_name.length() > 30) cons_name = cons_name.substring(cons_name.length() - 28);
      }

      cons_name = makeConstraintName(cons_name);
      writeForeignKey(currentClass, cons_name, jf, foreignClass);

    } else if (Checks.exists(jf.getConstraint())) {
      String cons_name = jf.getConstraintName();
      if (!Checks.exists(cons_name)) cons_name = "C_" + jf.getJavaName();
      cons_name = makeConstraintName(cons_name);
      writeCheckConstraint(currentClass, cons_name, jf);
    }
  }
Пример #2
0
  public void inlineField(JClass currentClass, JCompositeField jcf, HashSet fieldNames)
      throws IOException, SchemaException {

    JClass fieldClass = jcf.getJClass();
    Vector fields = new Vector(fieldClass.getAllFields().values());
    String primaryKey = fieldClass.getPrimaryKey();
    String prefix = jcf.getPrefix();
    for (int i = 0; i < fields.size(); i++) {
      JField jf = (JField) fields.elementAt(i);
      String sqlName = jf.getSqlName();

      if (!jf.isDbColumn() || sqlName.equals(primaryKey)) {
        // do nothing ..
      } else if (jf instanceof JCompositeField) {
        throw new SchemaException(
            "Composite Fields not allowed in inline objects", currentClass.getSchemaFileName());
      } else
        schemaFile.write(",\n\t" + prefix + "_" + sqlName + " " + getSqlOutType(currentClass, jf));
      fieldNames.add(prefix + "_" + sqlName);
    }
  }