/**
   * INTERNAL: This methods clones all the fields and ensures that each collection refers to the
   * same clones.
   */
  @Override
  public Object clone() {
    VariableOneToOneMapping clone = (VariableOneToOneMapping) super.clone();
    Map setOfKeys = new HashMap(getSourceToTargetQueryKeyNames().size());
    Map sourceToTarget = new HashMap(getSourceToTargetQueryKeyNames().size());
    Vector foreignKeys =
        org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(
            getForeignKeyFields().size());

    if (getTypeField() != null) {
      clone.setTypeField((DatabaseField) this.getTypeField().clone());
    }

    for (Iterator enumtr = getSourceToTargetQueryKeyNames().keySet().iterator();
        enumtr.hasNext(); ) {
      // Clone the SourceKeyFields
      DatabaseField field = (DatabaseField) enumtr.next();
      DatabaseField clonedField = (DatabaseField) field.clone();
      setOfKeys.put(field, clonedField);
      // on the next line I'm cloning the query key names
      sourceToTarget.put(clonedField, getSourceToTargetQueryKeyNames().get(field));
    }

    for (Enumeration enumtr = getForeignKeyFields().elements(); enumtr.hasMoreElements(); ) {
      DatabaseField field = (DatabaseField) enumtr.nextElement();
      foreignKeys.addElement(setOfKeys.get(field));
    }
    clone.setSourceToTargetQueryKeyFields(sourceToTarget);
    clone.setForeignKeyFields(foreignKeys);
    clone.setTypeIndicatorTranslation(new HashMap(this.getTypeIndicatorTranslation()));
    return clone;
  }
 protected Hashtable removeDuplicateAndValidateInfos(AbstractSession session) {
   Hashtable infoHashtable = new Hashtable();
   for (int i = 0; i < infos.size(); i++) {
     Info info1 = infos.get(i);
     info1 = (Info) info1.clone();
     DatabaseField descField = getDescriptor().buildField(info1.getField());
     if (info1.getField().getType() == null) {
       info1.setField(descField);
     } else {
       // keep the original type if specified
       info1.getField().setName(descField.getName());
       info1
           .getField()
           .setTableName(
               getDescriptor().getDefaultTable().getQualifiedNameDelimited(session.getPlatform()));
     }
     Info info2 = (Info) infoHashtable.get(info1.getField());
     if (info2 == null) {
       infoHashtable.put(info1.getField(), info1);
     } else {
       Info infoMerged = mergeInfos(info1, info2, session, getDescriptor());
       if (infoMerged != null) {
         // substitute info2 with infoMerged
         infoHashtable.put(infoMerged.getField(), infoMerged);
       } else {
         // couldn't merge info1 and info2 due to a conflict.
         // substitute info2 with info1
         infoHashtable.put(info1.getField(), info1);
       }
     }
   }
   return infoHashtable;
 }
  /** INTERNAL: Print SQL onto the stream, using the ExpressionPrinter for context */
  public void printSQL(ExpressionSQLPrinter printer) {
    if (isAttribute()) {
      printer.printField(getAliasedField());
    }

    // If the mapping is a direct collection then this falls into a gray area.
    // It must be treated as an attribute at this moment for it has a direct field.
    // However it is not an attribute in the sense that it also represents a foreign
    // reference and a mapping criteria has been added.
    // For bug 2900974 these are now handled as non-attributes during normalize but
    // as attributes when printing SQL.
    //
    if ((!isAttribute()) && (getMapping() != null) && getMapping().isDirectCollectionMapping()) {
      DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping) getMapping();

      // The aliased table comes for free as it was a required part of the join criteria.
      TableExpression table =
          (TableExpression) getTable(directCollectionMapping.getReferenceTable());
      DatabaseTable aliasedTable = table.aliasForTable(table.getTable());
      DatabaseField aliasedField = (DatabaseField) directCollectionMapping.getDirectField().clone();
      aliasedField.setTable(aliasedTable);
      printer.printField(aliasedField);
    }

    if ((getMapping() != null) && getMapping().isNestedTableMapping()) {
      DatabaseTable tableAlias = aliasForTable(new NestedTable(this));
      printer.printString(tableAlias.getName());
    }
  }
 /** INTERNAL: */
 protected Vector<DatabaseField> getVectorOfFieldsToGenerate(int operation, DatabaseTable table) {
   if (this.main[operation][ALL] == null) {
     return null;
   }
   if (this.tableToFieldsForGenerationMap == null) {
     // the method is called for the first time
     tableToFieldsForGenerationMap = new HashMap[NUM_OPERATIONS];
   }
   if (this.tableToFieldsForGenerationMap[operation] == null) {
     // the method is called for the first time for this operation
     this.tableToFieldsForGenerationMap[operation] = new HashMap();
   }
   Vector<DatabaseField> fieldsForGeneration =
       this.tableToFieldsForGenerationMap[operation].get(table);
   if (fieldsForGeneration == null) {
     // the method is called for the first time for this operation and this table
     fieldsForGeneration = new NonSynchronizedVector();
     Iterator it = this.main[operation][ALL].iterator();
     while (it.hasNext()) {
       DatabaseField field = (DatabaseField) it.next();
       if (field.getTable().equals(table)) {
         fieldsForGeneration.add(field);
       }
     }
     this.tableToFieldsForGenerationMap[operation].put(table, fieldsForGeneration);
   }
   return fieldsForGeneration;
 }
  /**
   * INTERNAL: Returns the first field from each of the owned tables, used for fine-grained
   * pessimistic locking.
   */
  protected Vector getForUpdateOfFields() {
    Vector allFields = getFields();
    int expected = getTableAliases().size();
    Vector firstFields = new Vector(expected);
    DatabaseTable lastTable = null;
    DatabaseField field = null;
    int i = 0;

    // The following loop takes O(n*m) time.  n=# of fields. m=#tables.
    // However, in the m=1 case this will take one pass only.
    // Also assuming that fields are generally sorted by table, this will
    // take O(n) time.
    // An even faster way may be to go getDescriptor().getAdditionalPrimaryKeyFields.
    while ((i < allFields.size()) && (firstFields.size() < expected)) {
      field = (DatabaseField) allFields.elementAt(i++);
      if ((lastTable == null) || !field.getTable().equals(lastTable)) {
        lastTable = field.getTable();
        int j = 0;
        while (j < firstFields.size()) {
          if (lastTable.equals(((DatabaseField) firstFields.elementAt(j)).getTable())) {
            break;
          }
          j++;
        }
        if (j == firstFields.size()) {
          firstFields.addElement(field);
        }
      }
    }
    return firstFields;
  }
Example #6
0
 /** Munge the table for all the fields in the row. */
 protected AbstractRecord convertToFullyQualifiedRow(AbstractRecord row) {
   DatabaseRecord result = new DatabaseRecord(row.size());
   for (Enumeration stream = row.keys(); stream.hasMoreElements(); ) {
     DatabaseField key = (DatabaseField) stream.nextElement();
     result.put(new DatabaseField(key.getName(), this.getRootElement()), row.get(key));
   }
   return result;
 }
 Info(DatabaseField field, boolean isInsert, boolean isInsertModeReturnOnly, boolean isUpdate) {
   this.field = field;
   if (field != null) {
     if (field.getType() != null) {
       setReferenceClass(field.getType());
     }
   }
   this.isInsert = isInsert;
   this.isInsertModeReturnOnly = isInsertModeReturnOnly;
   this.isUpdate = isUpdate;
 }
  /** Append the string containing the SQL insert string for the given table. */
  protected SQLCall buildCallWithoutReturning(AbstractSession session) {
    SQLCall call = new SQLCall();
    call.returnNothing();

    Writer writer = new CharArrayWriter(200);
    try {
      writer.write("INSERT ");
      if (getHintString() != null) {
        writer.write(getHintString());
        writer.write(" ");
      }
      writer.write("INTO ");
      writer.write(getTable().getQualifiedNameDelimited(session.getPlatform()));
      writer.write(" (");

      Vector fieldsForTable = new Vector();
      for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum.hasMoreElements(); ) {
        DatabaseField field = (DatabaseField) fieldsEnum.nextElement();
        if (field.getTable().equals(getTable()) || (!field.hasTableName())) {
          fieldsForTable.addElement(field);
        }
      }

      if (fieldsForTable.isEmpty()) {
        throw QueryException.objectToInsertIsEmpty(getTable());
      }

      for (int i = 0; i < fieldsForTable.size(); i++) {
        writer.write(
            ((DatabaseField) fieldsForTable.elementAt(i)).getNameDelimited(session.getPlatform()));
        if ((i + 1) < fieldsForTable.size()) {
          writer.write(", ");
        }
      }
      writer.write(") VALUES (");

      for (int i = 0; i < fieldsForTable.size(); i++) {
        DatabaseField field = (DatabaseField) fieldsForTable.elementAt(i);
        call.appendModify(writer, field);
        if ((i + 1) < fieldsForTable.size()) {
          writer.write(", ");
        }
      }
      writer.write(")");

      call.setSQLString(writer.toString());
    } catch (IOException exception) {
      throw ValidationException.fileError(exception);
    }
    return call;
  }
    @Override
    public int hashCode() {
      DatabaseField field = getField();
      Class type = field != null ? field.getType() : null;
      boolean isInsert = isInsert();
      boolean isInsertModeReturnOnly = isInsertModeReturnOnly();
      boolean isUpdate = isUpdate();

      int result = field != null ? field.hashCode() : 0;
      result = 31 * result + (type != null ? type.hashCode() : 0);
      result = 31 * result + (isInsert ? 1 : 0);
      result = 31 * result + (isInsertModeReturnOnly ? 1 : 0);
      result = 31 * result + (isUpdate ? 1 : 0);
      return result;
    }
 /**
  * INTERNAL Used by SQLCall.appendModify(..) If the field should be passed to
  * customModifyInDatabaseCall, return true, otherwise false. Methods
  * shouldCustomModifyInDatabaseCall and customModifyInDatabaseCall should be kept in sync:
  * shouldCustomModifyInDatabaseCall should return true if and only if the field is handled by
  * customModifyInDatabaseCall.
  */
 public boolean shouldUseCustomModifyForCall(DatabaseField field) {
   Class type = field.getType();
   if ((type != null) && isOracle9Specific(type)) {
     return true;
   }
   return super.shouldUseCustomModifyForCall(field);
 }
  /**
   * INTERNAL: Return the classification for the field contained in the mapping. This is used to
   * convert the row value to a consistent java value.
   */
  @Override
  public Class getFieldClassification(DatabaseField fieldToClassify) {
    if ((getTypeField() != null) && (fieldToClassify.equals(getTypeField()))) {
      return getTypeField().getType();
    }

    String queryKey = (String) getSourceToTargetQueryKeyNames().get(fieldToClassify);
    if (queryKey == null) {
      return null;
    }
    // Search any of the implementor descriptors for a mapping for the query-key.
    Iterator iterator =
        getReferenceDescriptor().getInterfacePolicy().getChildDescriptors().iterator();
    if (iterator.hasNext()) {
      ClassDescriptor firstChild = (ClassDescriptor) iterator.next();
      DatabaseMapping mapping = firstChild.getObjectBuilder().getMappingForAttributeName(queryKey);
      if ((mapping != null) && (mapping.isDirectToFieldMapping())) {
        return ((AbstractDirectMapping) mapping).getAttributeClassification();
      }
      QueryKey targetQueryKey = firstChild.getQueryKeyNamed(queryKey);
      if ((targetQueryKey != null) && (targetQueryKey.isDirectQueryKey())) {
        return firstChild
            .getObjectBuilder()
            .getFieldClassification(((DirectQueryKey) targetQueryKey).getField());
      }
    }
    return null;
  }
  /** INTERNAL: Both ReturningPolicies should be initialized */
  public boolean hasEqualMains(ReturningPolicy policy) {
    Collection[][] mainToCompare = policy.main;
    if (main == mainToCompare) {
      return true;
    }
    for (int operation = INSERT; operation <= UPDATE; operation++) {
      for (int state = RETURN_ONLY; state < MAIN_SIZE; state++) {
        if ((main[operation][state] == null) && (mainToCompare[operation][state] != null)) {
          return false;
        }
        if ((main[operation][state] != null) && (mainToCompare[operation][state] == null)) {
          return false;
        }
        if (!main[operation][state].equals(mainToCompare[operation][state])) {
          return false;
        }
      }
    }

    // now compare types
    Hashtable allFields = new Hashtable();
    for (int operation = INSERT; operation <= UPDATE; operation++) {
      if (main[operation][ALL] != null) {
        Iterator it = main[operation][ALL].iterator();
        while (it.hasNext()) {
          DatabaseField field = (DatabaseField) it.next();
          allFields.put(field, field);
        }
      }
    }
    for (int operation = INSERT; operation <= UPDATE; operation++) {
      if (mainToCompare[operation][ALL] != null) {
        Iterator it = mainToCompare[operation][ALL].iterator();
        while (it.hasNext()) {
          DatabaseField fieldToCompare = (DatabaseField) it.next();
          DatabaseField field = (DatabaseField) allFields.get(fieldToCompare);
          if (!field.getType().equals(fieldToCompare.getType())) {
            return false;
          }
        }
      }
    }
    return true;
  }
 protected static boolean verifyField(
     AbstractSession session, DatabaseField field, ClassDescriptor descriptor) {
   boolean ok = true;
   if (field.equals(descriptor.getSequenceNumberField())) {
     ok = false;
     session
         .getIntegrityChecker()
         .handleError(
             DescriptorException.returningPolicyFieldNotSupported(field.getName(), descriptor));
   } else if (descriptor.hasInheritance()
       && field.equals(descriptor.getInheritancePolicy().getClassIndicatorField())) {
     ok = false;
     session
         .getIntegrityChecker()
         .handleError(
             DescriptorException.returningPolicyFieldNotSupported(field.getName(), descriptor));
   } else if (descriptor.usesOptimisticLocking()) {
     OptimisticLockingPolicy optimisticLockingPolicy = descriptor.getOptimisticLockingPolicy();
     if (optimisticLockingPolicy instanceof VersionLockingPolicy) {
       VersionLockingPolicy versionLockingPolicy = (VersionLockingPolicy) optimisticLockingPolicy;
       if (field.equals(versionLockingPolicy.getWriteLockField())) {
         ok = false;
         session
             .getIntegrityChecker()
             .handleError(
                 DescriptorException.returningPolicyFieldNotSupported(
                     field.getName(), descriptor));
       }
     }
   }
   return ok;
 }
 /** INTERNAL: */
 protected void fieldIsNotFromDescriptor(DatabaseField field) {
   if (field.getTable().equals(getDescriptor().getDefaultTable())) {
     if (this.fieldsNotFromDescriptor_DefaultTable == null) {
       this.fieldsNotFromDescriptor_DefaultTable = new HashMap();
     }
     this.fieldsNotFromDescriptor_DefaultTable.put(field, field);
   } else {
     if (this.fieldsNotFromDescriptor_OtherTables == null) {
       this.fieldsNotFromDescriptor_OtherTables = new HashMap();
     }
     this.fieldsNotFromDescriptor_OtherTables.put(field, field);
   }
 }
  /** INTERNAL: Alias the database field for our current environment */
  protected void initializeAliasedField() {
    DatabaseField tempField = (DatabaseField) getField().clone();
    DatabaseTable aliasedTable = getAliasedTable();

    //  Put in a special check here so that if the aliasing does nothing we don't cache the
    // result because it's invalid. This saves us from caching premature data if e.g. debugging
    // causes us to print too early"
    //	if (aliasedTable.equals(getField().getTable())) {
    //		return;
    //	} else {
    aliasedField = tempField;
    aliasedField.setTable(aliasedTable);
    //	}
  }
 protected static boolean verifyFieldAndMapping(
     AbstractSession session,
     DatabaseField field,
     ClassDescriptor descriptor,
     DatabaseMapping mapping) {
   verifyField(session, field, descriptor);
   while (mapping.isAggregateObjectMapping()) {
     ClassDescriptor referenceDescriptor =
         ((AggregateObjectMapping) mapping).getReferenceDescriptor();
     mapping = referenceDescriptor.getObjectBuilder().getMappingForField(field);
     verifyFieldAndMapping(session, field, referenceDescriptor, mapping);
   }
   if (!mapping.isDirectToFieldMapping() && !mapping.isTransformationMapping()) {
     String mappingTypeName = Helper.getShortClassName(mapping);
     session
         .getIntegrityChecker()
         .handleError(
             DescriptorException.returningPolicyMappingNotSupported(
                 field.getName(), mappingTypeName, mapping));
     return false;
   } else {
     return true;
   }
 }
 /**
  * INTERNAL: Used by SQLCall.translate(..) The binding *must* be performed (NCHAR, NSTRING,
  * NCLOB). In these special cases the method returns a wrapper object which knows whether it
  * should be bound or appended and knows how to do that.
  */
 public Object getCustomModifyValueForCall(
     Call call, Object value, DatabaseField field, boolean shouldBind) {
   Class type = field.getType();
   if ((type != null) && isOracle9Specific(type)) {
     if (value == null) {
       return null;
     }
     if (NCHAR.equals(type) || NSTRING.equals(type)) {
       return new NTypeBindCallCustomParameter(value);
     } else if (NCLOB.equals(type)) {
       value = convertToDatabaseType(value);
       if (shouldUseLocatorForLOBWrite()) {
         if (lobValueExceedsLimit(value)) {
           ((DatabaseCall) call).addContext(field, value);
           value = new String(" ");
         }
       }
       return new NTypeBindCallCustomParameter(value);
     } else if (XMLTYPE.equals(type)) {
       return getXMLTypeFactory().createXMLTypeBindCallCustomParameter(value);
     }
   }
   return super.getCustomModifyValueForCall(call, value, field, shouldBind);
 }
  /**
   * INTERNAL: Build and return the nested rows from the specified field value. This method allows
   * the field value to be an ARRAY containing other structures such as arrays or Struct, or direct
   * values.
   */
  public static Object buildContainerFromArray(
      Array fieldValue, ObjectRelationalDatabaseField arrayField, AbstractSession session)
      throws DatabaseException {
    if (arrayField.getType() == null) {
      return fieldValue;
    }
    Object[] objects = null;
    try {
      objects = (Object[]) fieldValue.getArray();
    } catch (java.sql.SQLException ex) {
      throw DatabaseException.sqlException(ex, session, false);
    }
    if (objects == null) {
      return null;
    }

    boolean isNestedStructure = false;
    ObjectRelationalDataTypeDescriptor ord = null;
    DatabaseField nestedType = null;
    if (arrayField != null) {
      nestedType = arrayField.getNestedTypeField();
      if ((nestedType != null) && nestedType.getSqlType() == Types.STRUCT) {
        ClassDescriptor descriptor = session.getDescriptor(nestedType.getType());
        if ((descriptor != null) && (descriptor.isObjectRelationalDataTypeDescriptor())) {
          // this is used to convert non-null objects passed through stored procedures and custom
          // SQL to structs
          ord = (ObjectRelationalDataTypeDescriptor) descriptor;
        }
      } else if ((nestedType != null) && (nestedType instanceof ObjectRelationalDatabaseField)) {
        isNestedStructure = true;
      }
    }
    // handle ARRAY conversions
    ReadObjectQuery query = new ReadObjectQuery();
    query.setSession(session);
    ContainerPolicy cp = ContainerPolicy.buildPolicyFor(arrayField.getType());
    Object container = cp.containerInstance(objects.length);
    for (int i = 0; i < objects.length; i++) {
      Object arrayValue = objects[i];
      if (arrayValue == null) {
        return null;
      }
      if (ord != null) {
        AbstractRecord nestedRow = ord.buildRowFromStructure((Struct) arrayValue);
        ClassDescriptor descriptor = ord;
        if (descriptor.hasInheritance()) {
          Class newElementClass =
              descriptor.getInheritancePolicy().classFromRow(nestedRow, session);
          if (!descriptor.getJavaClass().equals(newElementClass)) {
            descriptor = session.getDescriptor(newElementClass);
            if (descriptor == null) {
              descriptor = ord;
            }
          }
        }
        arrayValue = descriptor.getObjectBuilder().buildNewInstance();
        descriptor
            .getObjectBuilder()
            .buildAttributesIntoObject(arrayValue, nestedRow, query, null, false);
      } else if (isNestedStructure && (arrayValue instanceof Array)) {
        arrayValue =
            buildContainerFromArray(
                (Array) arrayValue, (ObjectRelationalDatabaseField) nestedType, session);
      }

      cp.addInto(arrayValue, container, session);
    }
    return container;
  }
 public void setField(DatabaseField field) {
   this.field = field;
   if ((field.getType() == null) && (referenceClass != null)) {
     field.setType(referenceClass);
   }
 }
 /** INTERNAL: */
 protected DatabaseField createField(String qualifiedName, Class type) {
   DatabaseField field = new DatabaseField(qualifiedName);
   field.setType(type);
   return field;
 }
 // used only on equal fields: field1.equals(field2)
 protected static boolean isThereATypeConflict(DatabaseField field1, DatabaseField field2) {
   return (field1.getType() != null)
       && (field2.getType() != null)
       && !field1.getType().equals(field2.getType());
 }
 /** INTERNAL: */
 public void validationAfterDescriptorInitialization(AbstractSession session) {
   Hashtable mapped = new Hashtable();
   for (int operation = INSERT; operation <= UPDATE; operation++) {
     if ((main[operation][MAPPED] != null) && !main[operation][MAPPED].isEmpty()) {
       Iterator it = main[operation][MAPPED].iterator();
       while (it.hasNext()) {
         DatabaseField field = (DatabaseField) it.next();
         mapped.put(field, field);
       }
     }
   }
   if (!mapped.isEmpty()) {
     for (Enumeration fields = getDescriptor().getFields().elements();
         fields.hasMoreElements(); ) {
       DatabaseField fieldInDescriptor = (DatabaseField) fields.nextElement();
       DatabaseField fieldInMain = (DatabaseField) mapped.get(fieldInDescriptor);
       if (fieldInMain != null) {
         if (fieldInMain.getType() == null) {
           if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) {
             session
                 .getIntegrityChecker()
                 .handleError(
                     DescriptorException.returningPolicyMappedFieldTypeNotSet(
                         fieldInMain.getName(), getDescriptor()));
           }
         } else if (isThereATypeConflict(fieldInMain, fieldInDescriptor)) {
           session
               .getIntegrityChecker()
               .handleError(
                   DescriptorException.returningPolicyAndDescriptorFieldTypeConflict(
                       fieldInMain.getName(),
                       fieldInMain.getType().getName(),
                       fieldInDescriptor.getType().getName(),
                       getDescriptor()));
         }
       }
     }
   }
   if (!(session.getDatasourcePlatform() instanceof DatabasePlatform)) {
     // don't attempt further diagnostics on non-relational platforms
     return;
   }
   WriteObjectQuery[] query = {
     getDescriptor().getQueryManager().getInsertQuery(),
     getDescriptor().getQueryManager().getUpdateQuery()
   };
   String[] queryTypeName = {"InsertObjectQuery", "UpdateObjectQuery"};
   for (int operation = INSERT; operation <= UPDATE; operation++) {
     if ((main[operation][ALL] != null) && !main[operation][ALL].isEmpty()) {
       // this operation requires some fields to be returned
       if ((query[operation] == null) || (query[operation].getDatasourceCall() == null)) {
         if (!session.getPlatform().canBuildCallWithReturning()) {
           session
               .getIntegrityChecker()
               .handleError(
                   DescriptorException.noCustomQueryForReturningPolicy(
                       queryTypeName[operation],
                       Helper.getShortClassName(session.getPlatform()),
                       getDescriptor()));
         }
       } else if (query[operation].getDatasourceCall() instanceof StoredProcedureCall) {
         // SQLCall with custom SQL calculates its outputRowFields later (in prepare() method) -
         // that's why SQLCall can't be verified here.
         DatabaseCall customCall = (DatabaseCall) query[operation].getDatasourceCall();
         Enumeration outputRowFields = customCall.getOutputRowFields().elements();
         Collection notFoundInOutputRow = createCollection();
         notFoundInOutputRow.addAll(main[operation][ALL]);
         while (outputRowFields.hasMoreElements()) {
           notFoundInOutputRow.remove(outputRowFields.nextElement());
         }
         if (!notFoundInOutputRow.isEmpty()) {
           Iterator it = notFoundInOutputRow.iterator();
           while (it.hasNext()) {
             DatabaseField field = (DatabaseField) it.next();
             session
                 .getIntegrityChecker()
                 .handleError(
                     DescriptorException.customQueryAndReturningPolicyFieldConflict(
                         field.getName(), queryTypeName[operation], getDescriptor()));
           }
         }
       }
     }
   }
 }
  /** INTERNAL: */
  public void initialize(AbstractSession session) {
    clearInitialization();
    main = new Collection[NUM_OPERATIONS][MAIN_SIZE];

    // The order of descriptor initialization guarantees initialization of Parent before children.
    // main array is copied from Parent's ReturningPolicy
    if (getDescriptor().isChildDescriptor()) {
      ClassDescriptor parentDescriptor =
          getDescriptor().getInheritancePolicy().getParentDescriptor();
      if (parentDescriptor.hasReturningPolicy()) {
        copyMainFrom(parentDescriptor.getReturningPolicy());
      }
    }

    if (!infos.isEmpty()) {
      Hashtable infoHashtable = removeDuplicateAndValidateInfos(session);
      Hashtable infoHashtableUnmapped = (Hashtable) infoHashtable.clone();
      for (Enumeration fields = getDescriptor().getFields().elements();
          fields.hasMoreElements(); ) {
        DatabaseField field = (DatabaseField) fields.nextElement();
        Info info = (Info) infoHashtableUnmapped.get(field);
        if (info != null) {
          infoHashtableUnmapped.remove(field);
          if (verifyFieldAndMapping(session, field)) {
            if (info.getField().getType() == null) {
              addMappedFieldToMain(field, info);
            } else {
              addMappedFieldToMain(info.getField(), info);
              fieldIsNotFromDescriptor(info.getField());
            }
          }
        }
      }

      if (!infoHashtableUnmapped.isEmpty()) {
        Enumeration fields = infoHashtableUnmapped.keys();
        while (fields.hasMoreElements()) {
          DatabaseField field = (DatabaseField) fields.nextElement();
          Info info = (Info) infoHashtableUnmapped.get(field);
          if (verifyField(session, field, getDescriptor())) {
            if (field.getType() != null) {
              addUnmappedFieldToMain(field, info);
              fieldIsNotFromDescriptor(field);
              session.log(
                  SessionLog.FINEST,
                  SessionLog.QUERY,
                  "added_unmapped_field_to_returning_policy",
                  info.toString(),
                  getDescriptor().getJavaClassName());
            } else {
              if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) {
                session
                    .getIntegrityChecker()
                    .handleError(
                        DescriptorException.returningPolicyUnmappedFieldTypeNotSet(
                            field.getName(), getDescriptor()));
              }
            }
          }
        }
      }
    }

    initializeIsUsedToSetPrimaryKey();
  }
Example #24
0
 public void createDDL() {
   DatabaseConvertGUI.setReadSuccess(true);
   databaseName = generateDatabaseName();
   sb.append("CREATE DATABASE " + databaseName + ";\r\n");
   sb.append("USE " + databaseName + ";\r\n");
   for (int boundCount = 0;
       boundCount <= maxBound;
       boundCount++) { // process tables in order from least dependent (least number of bound
                       // tables) to most dependent
     for (int tableCount = 0;
         tableCount < numBoundTables.length;
         tableCount++) { // step through list of tables
       if (numBoundTables[tableCount] == boundCount) { //
         sb.append("CREATE TABLE " + tables[tableCount].getName() + " (\r\n");
         int[] nativeFields = tables[tableCount].getNativeFieldsArray();
         int[] relatedFields = tables[tableCount].getRelatedFieldsArray();
         boolean[] primaryKey = new boolean[nativeFields.length];
         int numPrimaryKey = 0;
         int numForeignKey = 0;
         for (int nativeFieldCount = 0;
             nativeFieldCount < nativeFields.length;
             nativeFieldCount++) { // print out the fields
           DatabaseField currentField = getField(nativeFields[nativeFieldCount]);
           sb.append(
               "\t" + currentField.getName() + " " + strDataType[currentField.getDataType()]);
           if (currentField.getDataType() == 0) { // varchar
             sb.append(
                 "("
                     + currentField.getVarcharValue()
                     + ")"); // append varchar length in () if data type is varchar
           }
           if (currentField.getDisallowNull()) {
             sb.append(" NOT NULL");
           }
           if (!currentField.getDefaultValue().equals("")) {
             if (currentField.getDataType() == 1) { // boolean data type
               sb.append(" DEFAULT " + convertStrBooleanToInt(currentField.getDefaultValue()));
             } else { // any other data type
               sb.append(" DEFAULT " + currentField.getDefaultValue());
             }
           }
           if (currentField.getIsPrimaryKey()) {
             primaryKey[nativeFieldCount] = true;
             numPrimaryKey++;
           } else {
             primaryKey[nativeFieldCount] = false;
           }
           if (currentField.getFieldBound() != 0) {
             numForeignKey++;
           }
           sb.append(",\r\n"); // end of field
         }
         if (numPrimaryKey > 0) { // table has primary key(s)
           sb.append("CONSTRAINT " + tables[tableCount].getName() + "_PK PRIMARY KEY (");
           for (int i = 0; i < primaryKey.length; i++) {
             if (primaryKey[i]) {
               sb.append(getField(nativeFields[i]).getName());
               numPrimaryKey--;
               if (numPrimaryKey > 0) {
                 sb.append(", ");
               }
             }
           }
           sb.append(")");
           if (numForeignKey > 0) {
             sb.append(",");
           }
           sb.append("\r\n");
         }
         if (numForeignKey > 0) { // table has foreign keys
           int currentFK = 1;
           for (int i = 0; i < relatedFields.length; i++) {
             if (relatedFields[i] != 0) {
               sb.append(
                   "CONSTRAINT "
                       + tables[tableCount].getName()
                       + "_FK"
                       + currentFK
                       + " FOREIGN KEY("
                       + getField(nativeFields[i]).getName()
                       + ") REFERENCES "
                       + getTable(getField(nativeFields[i]).getTableBound()).getName()
                       + "("
                       + getField(relatedFields[i]).getName()
                       + ")");
               if (currentFK < numForeignKey) {
                 sb.append(",\r\n");
               }
               currentFK++;
             }
           }
           sb.append("\r\n");
         }
         sb.append(");\r\n\r\n"); // end of table
       }
     }
   }
 }