Package org.castor.ddlgen.schemaobject

Examples of org.castor.ddlgen.schemaobject.PrimaryKey


        Table table = _schemaFactory.createTable();
        table.setName(tableName);
        table.setConfiguration(_configuration);
        table.setSchema(_schema);
       
        PrimaryKey primarykey = _schemaFactory.createPrimaryKey();
        primarykey.setConfiguration(_configuration);
        primarykey.setTable(table);
        primarykey.setName("pk_" + tableName);
        table.setPrimaryKey(primarykey);

        // Return if there are no field in the table.
        if (cm.getClassChoice() == null) { return table; }

        boolean isUseFieldIdentity = _mappingHelper.isUseFieldIdentity(cm);
        Enumeration ef = cm.getClassChoice().enumerateFieldMapping();

        // Process key generator.
        String keygenerator = cm.getKeyGenerator();
        KeyGenerator keyGen = null;
        if (keygenerator != null) {
            keyGen = _keyGenRegistry.getKeyGenerator(keygenerator.toUpperCase());
        }
        table.setKeyGenerator(keyGen);

        while (ef.hasMoreElements()) {
            FieldMapping fm = (FieldMapping) ef.nextElement();

            // Skip if <sql> tag is not defined and we have no mapping to DB.
            if (fm.getSql() == null) { continue; }

            boolean isFieldIdentity = fm.getIdentity();
            if (!isUseFieldIdentity) {
                isFieldIdentity = _mappingHelper.isIdentity(cm, fm);
            }
           
            // Checke for many-key, many-table definition.
            if (fm.getSql().getManyTable() != null) {
                    // Generate resolving table for many-many relationship
                    addResolveField(fm, cm);
            }
                   
            // Process column creation if sql name is defined.
            String[] sqlnames = fm.getSql().getName();             
            if ((sqlnames != null) && (sqlnames.length > 0)
                    && (fm.getSql().getManyTable() == null)) {
                // Normal case, using sql name as column name.
                String sqltype = fm.getSql().getType();

                TypeInfo typeInfo = null;
                ClassMapping cmRef = null;
                String[] refIdTypes = null;
                boolean isUseReferenceType = false;

                // Get type info.
                if (sqltype != null) {
                    typeInfo = _typeMapper.getType(sqltype);
                }

                // If typeInfo is null, this table has a reference to another one.
                if (typeInfo == null) {
                    cmRef = _mappingHelper.getClassMappingByName(fm.getType());
                    // Use field type if reference class could not be found.
                    if (cmRef == null) {                       
                        typeInfo = _typeMapper.getType(fm.getType());
                       
                        if (typeInfo == null) {
                            throw new TypeNotFoundException("can not resolve type "
                                + fm.getType() + " in class '" + cm.getName() + "'");
                        }
                    } else {
                        isUseReferenceType = true;
                        refIdTypes = _mappingHelper.resolveTypeReferenceForIds(
                                fm.getType());

                        // If number of reference table's Id's differ from number of
                        // field elements.
                        if (refIdTypes.length != sqlnames.length) {
                            throw new TypeNotFoundException(
                                    "number of reference table's Id differs"
                                            + " to number of field elements '"
                                            + fm.getName() + "' of class '"
                                            + cm.getName() + "'"
                                            + refIdTypes.length + "," + sqlnames.length);
                        }
                    }
                }

                // Create fields.
                for (int i = 0; i < sqlnames.length; i++) {
                    Field field = _schemaFactory.createField();
                    field.setConfiguration(_configuration);

                    if (isUseReferenceType) {
                        // Each sqlname correspond to a identity of the reference table.
                        // Should be able to get the original type of the reference
                        // field.
                        typeInfo = _typeMapper.getType(refIdTypes[i]);
                        if (typeInfo == null) {
                            throw new TypeNotFoundException(
                                    "can not find reference type "
                                    + refIdTypes[i] + " of class " + cm.getName());
                        }
                    }

                    // process attributes of field
                    field.setName(sqlnames[i]);
                    field.setTable(table);
                    field.setType(typeInfo);
                    field.setIdentity(isFieldIdentity);
                    field.setRequired(fm.getRequired());
                    field.setKeyGenerator(keyGen);

                    table.addField(field);
                   
                    if (isFieldIdentity) {
                       primarykey.addField(field);
                    }
                }
               
                // Create foreign keys.
                if (isUseReferenceType) { addOneOneForeignKey(table, fm); }
View Full Code Here

TOP

Related Classes of org.castor.ddlgen.schemaobject.PrimaryKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.