Package org.castor.ddlgen.schemaobject

Examples of org.castor.ddlgen.schemaobject.Table


     * @param writer DDLWriter to write schema objects to.
     * @throws GeneratorException If failed to generate DDL.
     */
    public final void generateKeyGenerator(final DDLWriter writer) throws GeneratorException {
        for (int i = 0; i < _schema.getTableCount(); i++) {
            Table table = _schema.getTable(i);
            if (table.getKeyGenerator() != null) {
                table.getKeyGenerator().setTable(table);
                table.getKeyGenerator().toCreateDDL(writer);
            }
        }
    }
View Full Code Here


        generateHeader(writer);
       
        if (genSchema) { _schema.toCreateDDL(writer); }
       
        for (int i = 0; i < _schema.getTableCount(); i++) {
            Table table = _schema.getTable(i);

            if (genDrop) { table.toDropDDL(writer); }
            if (genCreate) { table.toCreateDDL(writer); }
            if (genPrimaryKey) { table.getPrimaryKey().toCreateDDL(writer); }
            if (genForeignKey) { createForeignKeyDDL(table, writer); }
            if (genIndex) { createIndex(table, writer); }
            if (genKeyGen && (table.getKeyGenerator() != null)) {
                table.getKeyGenerator().setTable(table);
                table.getKeyGenerator().toCreateDDL(writer);
            }
        }
    }
View Full Code Here

        // Create tables.
        Enumeration ec = root.enumerateClassMapping();
        while (ec.hasMoreElements()) {
            ClassMapping cm = (ClassMapping) ec.nextElement();
            Table table = createTable(cm);
            if (table != null) { _schema.addTable(table); }
        }
       
        // Create N:M relation tables.
        Iterator i = _resolveTable.keySet().iterator();
        while (i.hasNext()) {
            ClassMapping cm = (ClassMapping) _resolveTable.get(i.next());
            Table table = createTable(cm);
            if (table != null) { _schema.addTable(table); }
        }
    }
View Full Code Here

     */
    private Table createTable(final ClassMapping cm) throws GeneratorException {
        String tableName = cm.getMapTo().getTable();
        if (tableName == null) { return null; }

        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);
                    }
                }
View Full Code Here

        if (cm == null) {
            throw new GeneratorException("can not find class " + fm.getType());
        }
       
        String referenceTableName = cm.getMapTo().getTable();
        Table referenceTable = null;
        referenceTable = table.getSchema().getTable(referenceTableName);
        fk.setReferenceTable(referenceTable);
       
        String[] manykeys = fm.getSql().getManyKey();
        if (manykeys == null || manykeys.length == 0) {
            manykeys = _mappingHelper.getClassMappingSqlIdentity(cm, true);
        }
        for (int i = 0; i < manykeys.length; i++) {
            for (int j = 0; j < referenceTable.getFieldCount(); j++) {
                Field field = referenceTable.getField(j);
                if (manykeys[i].equals(field.getName())) { fk.addReferenceField(field); }
            }
        }

        fk.setRelationType(ForeignKey.ONE_ONE);
View Full Code Here

TOP

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

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.