Search in sources :

Example 1 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createDatastoreMapping.

/**
 * Method to create the datastore mapping for a particular column and java type.
 * If the column is specified it is linked to the created datastore mapping.
 * @param mapping The java mapping
 * @param column The column (can be null)
 * @param javaType The java type
 * @return The datastore mapping
 */
public DatastoreMapping createDatastoreMapping(JavaTypeMapping mapping, Column column, String javaType) {
    Column col = column;
    String jdbcType = null;
    String sqlType = null;
    if (col != null && col.getColumnMetaData() != null) {
        // Utilise the jdbc and sql types if specified
        jdbcType = col.getColumnMetaData().getJdbcTypeName();
        sqlType = col.getColumnMetaData().getSqlType();
    }
    Class datastoreMappingClass = storeMgr.getDatastoreAdapter().getDatastoreMappingClass(javaType, jdbcType, sqlType, clr, null);
    DatastoreMapping datastoreMapping = DatastoreMappingFactory.createMapping(datastoreMappingClass, mapping, storeMgr, column);
    if (column != null) {
        column.setDatastoreMapping(datastoreMapping);
    }
    return datastoreMapping;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass)

Example 2 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class EmbeddedMapping method addMappingForMember.

/**
 * Method to add a mapping for the specified member to this mapping.
 * @param embCmd Class that the member belongs to
 * @param embMmd Member to be added
 * @param embMmds The metadata defining mapping information for the members (if any)
 */
private void addMappingForMember(AbstractClassMetaData embCmd, AbstractMemberMetaData embMmd, AbstractMemberMetaData[] embMmds) {
    if (emd != null && emd.getOwnerMember() != null && emd.getOwnerMember().equals(embMmd.getName())) {
    // Do nothing since we don't map owner fields (since the owner is the containing object)
    } else {
        AbstractMemberMetaData embeddedMmd = null;
        for (int j = 0; j < embMmds.length; j++) {
            // Why are these even possible ? Why are they here ? Why don't they use localised messages ?
            if (embMmds[j] == null) {
                throw new RuntimeException("embMmds[j] is null for class=" + embCmd.toString() + " type=" + typeName);
            }
            AbstractMemberMetaData embMmdForMmds = embCmd.getMetaDataForMember(embMmds[j].getName());
            if (embMmdForMmds != null) {
                if (embMmdForMmds.getAbsoluteFieldNumber() == embMmd.getAbsoluteFieldNumber()) {
                    // Same as the member we are processing, so use it
                    embeddedMmd = embMmds[j];
                }
            }
        }
        // Add mapping
        JavaTypeMapping embMmdMapping;
        MappingManager mapMgr = table.getStoreManager().getMappingManager();
        if (embeddedMmd != null) {
            // User has provided a field definition so map with that
            embMmdMapping = mapMgr.getMapping(table, embeddedMmd, clr, FieldRole.ROLE_FIELD);
        } else {
            // User hasn't provided a field definition so map with the classes own definition
            embMmdMapping = mapMgr.getMapping(table, embMmd, clr, FieldRole.ROLE_FIELD);
        }
        if (embMmd.getRelationType(clr) != RelationType.NONE && embMmdMapping instanceof AbstractContainerMapping) {
            // TODO Support 1-N (unidirectional) relationships and use owner object as the key in the join table
            NucleusLogger.PERSISTENCE.warn("Embedded object at " + getMemberMetaData().getFullFieldName() + " has a member " + embMmd.getFullFieldName() + " that is a container. Not fully supported as part of an embedded object!");
        }
        // Use field number from embMmd, since the embedded mapping info doesn't have reliable field number infos
        embMmdMapping.setAbsFieldNumber(embMmd.getAbsoluteFieldNumber());
        this.addJavaTypeMapping(embMmdMapping);
        for (int j = 0; j < embMmdMapping.getNumberOfDatastoreMappings(); j++) {
            // Register column with mapping
            DatastoreMapping datastoreMapping = embMmdMapping.getDatastoreMapping(j);
            this.addDatastoreMapping(datastoreMapping);
            if (this.mmd.isPrimaryKey()) {
                // Overall embedded field should be part of PK, so make all datastore fields part of it
                Column col = datastoreMapping.getColumn();
                if (col != null) {
                    col.setPrimaryKey();
                }
            }
        }
    }
}
Also used : DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 3 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class AbstractContainerMapping method prepareDatastoreMapping.

/**
 * Method to prepare a field mapping for use in the datastore.
 * This creates the column in the table.
 */
protected void prepareDatastoreMapping() {
    if (containerIsStoredInSingleColumn()) {
        // Serialised collections/maps/arrays should just create a (typically BLOB) column as normal in the owning table
        MappingManager mmgr = storeMgr.getMappingManager();
        ColumnMetaData colmd = null;
        ColumnMetaData[] colmds = mmd.getColumnMetaData();
        if (colmds != null && colmds.length > 0) {
            // Try the field column info
            colmd = colmds[0];
        } else if (mmd.hasCollection() || mmd.hasArray()) {
            // Fallback to the element column info
            colmds = (mmd.getElementMetaData() != null) ? mmd.getElementMetaData().getColumnMetaData() : null;
            if (colmds != null && colmds.length > 0) {
                colmd = colmds[0];
            }
        }
        Column col = mmgr.createColumn(this, getJavaTypeForDatastoreMapping(0), colmd);
        mmgr.createDatastoreMapping(this, mmd, 0, col);
    }
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 4 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class ColumnOrderedKey method getColumnList.

/**
 * Method to return the list of columns which the key applies to.
 * @param includeOrdering Whether to include ordering in the column list when it is specified
 * @return The column list.
 */
public String getColumnList(boolean includeOrdering) {
    StringBuilder s = new StringBuilder("(");
    Iterator<Column> colIter = columns.iterator();
    Iterator<Boolean> colOrderIter = columnOrdering.iterator();
    while (colIter.hasNext()) {
        Column col = colIter.next();
        s.append(col != null ? col.getIdentifier() : "?");
        if (includeOrdering) {
            Boolean colOrder = colOrderIter.next();
            if (colOrder != null) {
                s.append(colOrder ? " ASC" : " DESC");
            }
        }
        if (colIter.hasNext()) {
            s.append(',');
        }
    }
    s.append(')');
    return s.toString();
}
Also used : Column(org.datanucleus.store.rdbms.table.Column)

Example 5 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class ColumnOrderedKey method setColumnOrdering.

public void setColumnOrdering(String ordering) {
    if (StringUtils.isWhitespace(ordering)) {
        return;
    }
    StringTokenizer tokeniser = new StringTokenizer(ordering, ",");
    if (tokeniser.countTokens() != columns.size()) {
        NucleusLogger.DATASTORE_SCHEMA.warn("Attempt to specify orderings of index with name=" + name + " but incorrect number of orderings (" + tokeniser.countTokens() + ") for columns (" + columns.size() + "). IGNORED");
        return;
    }
    Iterator<Column> colIter = columns.iterator();
    int i = 0;
    while (tokeniser.hasMoreTokens()) {
        String orderingToken = tokeniser.nextToken();
        colIter.next();
        columnOrdering.set(i, orderingToken.equalsIgnoreCase("ASC") ? Boolean.TRUE : orderingToken.equalsIgnoreCase("DESC") ? Boolean.FALSE : null);
        i++;
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Column(org.datanucleus.store.rdbms.table.Column)

Aggregations

Column (org.datanucleus.store.rdbms.table.Column)24 MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)5 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)3 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)3 DatastoreMapping (org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ForeignKey (org.datanucleus.store.rdbms.key.ForeignKey)2 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)2 Table (org.datanucleus.store.rdbms.table.Table)2 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1