コード例 #1
0
  /* override to insert constraint in table definition */
  protected void writeCheckConstraint(JClass currentClass, String cons_name, JField jf)
      throws IOException {

    constraints +=
        "\nalter table "
            + currentClass.getTableName()
            + " add constraint "
            + cons_name
            + " check ("
            + jf.getSqlName()
            + jf.getConstraint()
            + ");\n";

    cleanupConstraints +=
        "\nalter table " + currentClass.getTableName() + " drop constraint " + cons_name + " ;\n";
  }
コード例 #2
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);
    }
  }