Example #1
0
  SQLEngine(
      final JDOClassDescriptor clsDesc, final PersistenceFactory factory, final String stampField)
      throws MappingException {

    _clsDesc = clsDesc;
    _factory = factory;
    _keyGen = null;

    if (_clsDesc.getExtends() == null) {
      KeyGeneratorDescriptor keyGenDesc = clsDesc.getKeyGeneratorDescriptor();
      if (keyGenDesc != null) {
        int[] tempType = ((JDOFieldDescriptor) _clsDesc.getIdentity()).getSQLType();
        _keyGen =
            keyGenDesc
                .getKeyGeneratorRegistry()
                .getKeyGenerator(_factory, keyGenDesc, (tempType == null) ? 0 : tempType[0]);

        // Does the key generator support the sql type specified in the mapping?
        _keyGen.supportsSqlType(tempType[0]);
      }
    }

    // construct field and id info
    Vector idsInfo = new Vector();
    Vector fieldsInfo = new Vector();

    /*
     * Implementation Note:
     * Extends and Depends has some special mutual exclusive
     * properties, which implementator should aware of.
     *
     * A Depended class may depends on another depended class
     * A class should either extends or depends on other class
     * A class should not depend on extending class.
     *  because, it is the same as depends on the base class
     * A class may be depended by zero or more classes
     * A class may be extended by zero or more classes
     * A class may extends only zero or one class
     * A class may depends only zero or one class
     * A class may depend on extended class
     * A class may extend a dependent class.
     * A class may extend a depended class.
     * No loop or circle should exist
     */
    // then, we put depended class ids in the back
    JDOClassDescriptor base = clsDesc;

    // walk until the base class which this class extends
    base = clsDesc;
    Stack stack = new Stack();
    stack.push(base);
    while (base.getExtends() != null) {
      // if (base.getDepends() != null) {
      //     throw new MappingException("Class should not both depends on and extended other
      // classes");
      // }
      base = (JDOClassDescriptor) base.getExtends();
      stack.push(base);
      // do we need to add loop detection?
    }

    // now base is either the base of extended class, or
    // clsDesc
    // we always put the original id info in front
    // [oleg] except for SQL name, it may differ.
    FieldDescriptor[] baseIdDescriptors = base.getIdentities();
    FieldDescriptor[] idDescriptors = clsDesc.getIdentities();

    for (int i = 0; i < baseIdDescriptors.length; i++) {
      if (baseIdDescriptors[i] instanceof JDOFieldDescriptor) {
        String name = baseIdDescriptors[i].getFieldName();
        String[] sqlName = ((JDOFieldDescriptor) baseIdDescriptors[i]).getSQLName();
        int[] sqlType = ((JDOFieldDescriptor) baseIdDescriptors[i]).getSQLType();
        FieldHandlerImpl fh = (FieldHandlerImpl) baseIdDescriptors[i].getHandler();

        // The extending class may have other SQL names for identity fields
        for (int j = 0; j < idDescriptors.length; j++) {
          if (name.equals(idDescriptors[j].getFieldName())
              && (idDescriptors[j] instanceof JDOFieldDescriptor)) {
            sqlName = ((JDOFieldDescriptor) idDescriptors[j]).getSQLName();
            break;
          }
        }
        idsInfo.add(
            new SQLColumnInfo(
                sqlName[0],
                sqlType[0],
                fh.getConvertTo(),
                fh.getConvertFrom(),
                fh.getConvertParam()));
      } else {
        throw new MappingException("Except JDOFieldDescriptor");
      }
    }

    // then do the fields
    while (!stack.empty()) {
      base = (JDOClassDescriptor) stack.pop();
      FieldDescriptor[] fieldDescriptors = base.getFields();
      for (int i = 0; i < fieldDescriptors.length; i++) {
        // fieldDescriptors[i] is persistent in db if it is not transient
        // and it is a JDOFieldDescriptor or has a ClassDescriptor
        if (!fieldDescriptors[i].isTransient()) {
          if ((fieldDescriptors[i] instanceof JDOFieldDescriptor)
              || (fieldDescriptors[i].getClassDescriptor() != null)) {

            fieldsInfo.add(
                new SQLFieldInfo(
                    clsDesc, fieldDescriptors[i], base.getTableName(), !stack.empty()));
          }
        }
      }
    }

    _ids = new SQLColumnInfo[idsInfo.size()];
    idsInfo.copyInto(_ids);

    _fields = new SQLFieldInfo[fieldsInfo.size()];
    fieldsInfo.copyInto(_fields);

    _loadStatement = new SQLStatementLoad(this, factory);
    _createStatement = new SQLStatementCreate(this, factory);
    _removeStatement = new SQLStatementRemove(this, factory);
    _storeStatement = new SQLStatementStore(this, factory, _loadStatement.getLoadStatement());
  }