Search in sources :

Example 16 with ColumnElement

use of org.netbeans.modules.dbschema.ColumnElement in project Payara by payara.

the class ClassDesc method createSecondaryTableKey.

private void createSecondaryTableKey(TableDesc table, MappingReferenceKeyElementImpl mappingSecondaryKey) {
    ColumnPairElement[] pairs = mappingSecondaryKey.getColumnPairs();
    KeyDesc referencingKey = new KeyDesc();
    KeyDesc referencedKey = new KeyDesc();
    TableDesc secondaryTable = findTableDesc(((MappingTableElementImpl) mappingSecondaryKey.getTable()).getTableObject());
    for (int i = 0; i < pairs.length; i++) {
        ColumnPairElement pair = pairs[i];
        ColumnElement lc = pair.getLocalColumn();
        ColumnElement fc = pair.getReferencedColumn();
        referencingKey.addColumn(lc);
        FieldDesc lf = getLocalFieldDesc(lc);
        referencingKey.addField(lf);
        // We need to force field for the referencing key to be in the DFG
        // so it will always be loaded. This is to facilitate updating
        // secondary tables that requires this field to be loaded
        // for constraint purposes.
        lf.fetchGroup = FieldDesc.GROUP_DEFAULT;
        referencedKey.addColumn(fc);
        referencedKey.addField(getLocalFieldDesc(fc));
    }
    table.addSecondaryTableKey(new ReferenceKeyDesc(secondaryTable, referencingKey, referencedKey));
    secondaryTable.setPrimaryTableKey(new ReferenceKeyDesc(table, referencedKey, referencingKey));
}
Also used : ColumnElement(org.netbeans.modules.dbschema.ColumnElement) ColumnPairElement(org.netbeans.modules.dbschema.ColumnPairElement)

Example 17 with ColumnElement

use of org.netbeans.modules.dbschema.ColumnElement in project Payara by payara.

the class ClassDesc method registerVersionFieldWithTable.

/**
 * Registers the version field <cod>versionField</code> with the
 * corresponding table.
 *
 * @param versionField Field used in version consistency check.
 */
private void registerVersionFieldWithTable(LocalFieldDesc versionField) {
    // Version field must be mapped to exactly one column.
    ColumnElement ce = (ColumnElement) versionField.getColumnElements().next();
    Iterator iter = tables.iterator();
    while (iter.hasNext()) {
        TableDesc table = (TableDesc) iter.next();
        if (!table.isJoinTable()) {
            if (ce.getDeclaringTable() == table.getTableElement()) {
                table.setVersionField(versionField);
                break;
            }
        }
    }
}
Also used : ColumnElement(org.netbeans.modules.dbschema.ColumnElement)

Example 18 with ColumnElement

use of org.netbeans.modules.dbschema.ColumnElement in project Payara by payara.

the class FieldDesc method compareColumns.

/**
 * This method compares the column lists between to fields to see if they match.
 * If f1 and f2 are both primitive fields, we do an exact match.
 * If f1 is primitve and f2 is a relationship field, we do an at-least-one match.
 * If both f1 and f2 are relationship fields, we do an exact match.
 * @return <code>true</code> if there is a match, <code>false</code>, otherwise.
 */
static boolean compareColumns(FieldDesc f1, FieldDesc f2) {
    ArrayList columnList1 = null;
    ArrayList columnList2 = null;
    ArrayList columnList3 = null;
    ArrayList columnList4 = null;
    boolean exactMatch = false;
    // Otherwise, we use localColumns for comparison.
    if (f1 instanceof LocalFieldDesc) {
        columnList1 = ((LocalFieldDesc) f1).columnDescs;
        if (f2 instanceof LocalFieldDesc) {
            columnList2 = ((LocalFieldDesc) f2).columnDescs;
            // Not sure yet whether we need to a exact match
            // here yet.
            exactMatch = true;
        } else {
            // We are comparing LocalFieldDesc and ForeignFieldDesc.
            // We do not need an exact match. The relationship must change,
            // if one of the LocalFieldDesc's columns is changed.
            columnList2 = ((ForeignFieldDesc) f2).localColumns;
        }
    } else {
        if (f2 instanceof LocalFieldDesc) {
            return false;
        } else {
            ForeignFieldDesc ff1 = (ForeignFieldDesc) f1;
            ForeignFieldDesc ff2 = (ForeignFieldDesc) f2;
            if (ff1.useJoinTable() && ff2.useJoinTable()) {
                columnList1 = ff1.assocLocalColumns;
                columnList2 = ff2.assocLocalColumns;
                columnList3 = ff1.assocForeignColumns;
                columnList4 = ff2.assocForeignColumns;
            } else if (!ff1.useJoinTable() && !ff2.useJoinTable()) {
                columnList1 = ff1.localColumns;
                columnList2 = ff2.localColumns;
                columnList3 = ff1.foreignColumns;
                columnList4 = ff2.foreignColumns;
            } else {
                return false;
            }
            exactMatch = true;
        }
    }
    boolean found = false;
    for (int k = 0; k < 2; k++) {
        if (k == 1) {
            if (columnList3 != null) {
                columnList1 = columnList3;
                columnList2 = columnList4;
            } else {
                break;
            }
        }
        int size1 = columnList1.size();
        int size2 = columnList2.size();
        if (exactMatch && (size1 != size2)) {
            return false;
        }
        for (int i = 0; i < size1; i++) {
            found = false;
            ColumnElement c1 = (ColumnElement) columnList1.get(i);
            // Find if any column of columnList2 matches with c1.
            for (int j = 0; j < size2; j++) {
                ColumnElement c2 = (ColumnElement) columnList2.get(j);
                if (c1.getName().getFullName().equals(c2.getName().getFullName())) {
                    found = true;
                }
            }
            // we return false.
            if (exactMatch && !found) {
                return false;
            }
            // we return true;
            if (!exactMatch && found) {
                return true;
            }
        }
    }
    // exactly. Otherwise, the two column lists don't match at all.
    return found;
}
Also used : ArrayList(java.util.ArrayList) ColumnElement(org.netbeans.modules.dbschema.ColumnElement)

Example 19 with ColumnElement

use of org.netbeans.modules.dbschema.ColumnElement in project Payara by payara.

the class ForeignFieldDesc method initializeFieldLists.

/**
 * Initialize the field lists based on column list information.
 */
private void initializeFieldLists() {
    ClassDesc theConfig = classDesc;
    for (int i = 0; i < 4; i++) {
        ArrayList fields = null;
        ArrayList columns = null;
        switch(i) {
            case 0:
                columns = localColumns;
                fields = getLocalFields();
                break;
            case 1:
                columns = assocLocalColumns;
                fields = getAssocLocalFields();
                break;
            case 2:
                columns = assocForeignColumns;
                fields = getAssocForeignFields();
                break;
            case 3:
                columns = foreignColumns;
                fields = getForeignFields();
                theConfig = foreignConfig;
                break;
        }
        if (columns == null)
            continue;
        for (int j = 0; j < columns.size(); j++) {
            ColumnElement ce = (ColumnElement) columns.get(j);
            TableElement te = ce.getDeclaringTable();
            if (te == null) {
                throw new JDOFatalInternalException(I18NHelper.getMessage(messages, // NOI18N
                "core.configuration.columnnotable"));
            }
            fields.add(theConfig.getLocalFieldDesc(ce));
        }
    }
}
Also used : JDOFatalInternalException(com.sun.jdo.api.persistence.support.JDOFatalInternalException) ArrayList(java.util.ArrayList) ColumnElement(org.netbeans.modules.dbschema.ColumnElement) TableElement(org.netbeans.modules.dbschema.TableElement)

Example 20 with ColumnElement

use of org.netbeans.modules.dbschema.ColumnElement in project Payara by payara.

the class LocalFieldDesc method isPrimitiveMappedToNullableColumn.

public boolean isPrimitiveMappedToNullableColumn() {
    if (primitiveMappedToNullableColumn == null) {
        boolean rc = getType().isPrimitive();
        for (Iterator iter = columnDescs.iterator(); iter.hasNext() && rc; ) {
            ColumnElement c = (ColumnElement) iter.next();
            rc = c.isNullable();
        }
        primitiveMappedToNullableColumn = new Boolean(rc);
    }
    return primitiveMappedToNullableColumn.booleanValue();
}
Also used : Iterator(java.util.Iterator) ColumnElement(org.netbeans.modules.dbschema.ColumnElement)

Aggregations

ColumnElement (org.netbeans.modules.dbschema.ColumnElement)24 JDOFatalInternalException (com.sun.jdo.api.persistence.support.JDOFatalInternalException)4 Iterator (java.util.Iterator)4 LocalFieldDesc (com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc)3 ArrayList (java.util.ArrayList)3 TableElement (org.netbeans.modules.dbschema.TableElement)3 ColumnPairElement (org.netbeans.modules.dbschema.ColumnPairElement)2 JDOFatalDataStoreException (com.sun.jdo.api.persistence.support.JDOFatalDataStoreException)1 TableDesc (com.sun.jdo.spi.persistence.support.sqlstore.model.TableDesc)1 ResultDesc (com.sun.jdo.spi.persistence.support.sqlstore.sql.ResultDesc)1 UpdateJoinTableDesc (com.sun.jdo.spi.persistence.support.sqlstore.sql.UpdateJoinTableDesc)1 com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint (com.sun.jdo.spi.persistence.support.sqlstore.sql.constraint)1 Collection (java.util.Collection)1