Search in sources :

Example 1 with NamingFactory

use of org.datanucleus.store.schema.naming.NamingFactory in project datanucleus-core by datanucleus.

the class CompleteClassTable method processEmbeddedMember.

protected void processEmbeddedMember(List<AbstractMemberMetaData> mmds, ClassLoaderResolver clr, EmbeddedMetaData embmd, boolean ownerNested) {
    TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
    MetaDataManager mmgr = storeMgr.getMetaDataManager();
    NamingFactory namingFactory = storeMgr.getNamingFactory();
    AbstractMemberMetaData lastMmd = mmds.get(mmds.size() - 1);
    AbstractClassMetaData embCmd = null;
    if (lastMmd.hasCollection()) {
        // Embedded collection element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getCollection().getElementType(), clr);
    } else if (lastMmd.hasArray()) {
        // Embedded array element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getArray().getElementType(), clr);
    } else {
        // Embedded 1-1
        embCmd = mmgr.getMetaDataForClass(lastMmd.getType(), clr);
    }
    // Go through all members of the embedded class
    int[] memberPositions = embCmd.getAllMemberPositions();
    for (int i = 0; i < memberPositions.length; i++) {
        AbstractMemberMetaData mmd = embCmd.getMetaDataForManagedMemberAtAbsolutePosition(memberPositions[i]);
        if (mmd.getPersistenceModifier() != FieldPersistenceModifier.PERSISTENT) {
            // Don't need column if not persistent
            continue;
        }
        if (mmds.size() == 1 && embmd != null && embmd.getOwnerMember() != null && embmd.getOwnerMember().equals(mmd.getName())) {
            // Special case of this being a link back to the owner. TODO Repeat this for nested and their owners
            continue;
        }
        AbstractMemberMetaData embmdMmd = null;
        if (embmd != null) {
            AbstractMemberMetaData[] embmdMmds = embmd.getMemberMetaData();
            if (embmdMmds != null) {
                for (AbstractMemberMetaData thisMmd : embmdMmds) {
                    if (thisMmd.getName().equals(mmd.getName())) {
                        embmdMmd = thisMmd;
                        break;
                    }
                }
            }
        }
        RelationType relationType = mmd.getRelationType(clr);
        if (relationType != RelationType.NONE && MetaDataUtils.getInstance().isMemberEmbedded(mmgr, clr, mmd, relationType, lastMmd)) {
            if (RelationType.isRelationSingleValued(relationType)) {
                // Nested embedded PC, so recurse
                boolean nested = false;
                if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC_NESTED)) {
                    nested = !storeMgr.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_EMBEDDED_PC_FLAT);
                    String nestedStr = mmd.getValueForExtension("nested");
                    if (nestedStr != null && nestedStr.equalsIgnoreCase("" + !nested)) {
                        nested = !nested;
                    }
                }
                List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
                embMmds.add(mmd);
                if (nested) {
                    // Embedded object stored as nested under this in the owner table (where the datastore supports that)
                    // Add column for the owner of the embedded object, typically for the column name only
                    ColumnMetaData[] colmds = mmd.getColumnMetaData();
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(true);
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    // TODO Create mapping for the related info under the above column
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, true);
                } else {
                    // Embedded object stored flat into this table, with columns at same level as owner columns
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, false);
                }
            } else {
                if (mmd.hasCollection()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION_NESTED)) {
                    // TODO Support nested embedded collection element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasMap()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP_NESTED)) {
                    // TODO Support nested embedded map key/value
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasArray()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_ARRAY_NESTED)) {
                    // TODO Support nested embedded array element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded array. Not yet supported. Ignoring");
                    continue;
                }
            }
        } else {
            List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
            embMmds.add(mmd);
            ColumnMetaData[] colmds = mmd.getColumnMetaData();
            if (relationType != RelationType.NONE) {
                // 1-1/N-1 stored as single column with persistable-id
                // 1-N/M-N stored as single column with collection<persistable-id>
                // Create column for basic type
                String colName = namingFactory.getColumnName(embMmds, 0);
                ColumnImpl col = addEmbeddedColumn(colName, null);
                col.setNested(ownerNested);
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                    col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                    col.setPosition(colmds[0].getPosition());
                }
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                    col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                    col.setJdbcType(colmds[0].getJdbcType());
                }
                MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                col.setMemberColumnMapping(mapping);
                if (schemaVerifier != null) {
                    schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                }
                mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
            } else {
                // TODO Pass in embedded colmds if they have jdbcType info?
                TypeConverter typeConv = getTypeConverterForMember(mmd, colmds, typeMgr);
                if (typeConv != null) {
                    // Create column(s) for this TypeConverter
                    if (typeConv instanceof MultiColumnConverter) {
                        Class[] colJavaTypes = ((MultiColumnConverter) typeConv).getDatastoreColumnTypes();
                        Column[] cols = new Column[colJavaTypes.length];
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            String colName = namingFactory.getColumnName(embMmds, j);
                            ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                            col.setNested(ownerNested);
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getPosition() != null) {
                                col.setPosition(embmdMmd.getColumnMetaData()[j].getPosition());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getPosition() != null) {
                                col.setPosition(colmds[j].getPosition());
                            }
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getJdbcType() != null) {
                                col.setJdbcType(embmdMmd.getColumnMetaData()[j].getJdbcType());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getJdbcType() != null) {
                                col.setJdbcType(colmds[j].getJdbcType());
                            }
                            cols[j] = col;
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, cols, typeConv);
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            ((ColumnImpl) cols[j]).setMemberColumnMapping(mapping);
                        }
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    } else {
                        String colName = namingFactory.getColumnName(embMmds, 0);
                        ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                        col.setNested(ownerNested);
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                            col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                            col.setPosition(colmds[0].getPosition());
                        }
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                            col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                            col.setJdbcType(colmds[0].getJdbcType());
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                        col.setMemberColumnMapping(mapping);
                        mapping.setTypeConverter(typeConv);
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    }
                } else {
                    // Create column for basic type
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(ownerNested);
                    AbstractMemberMetaData theMmd = embMmds.get(0);
                    if (theMmd.isPrimaryKey()) {
                        col.setPrimaryKey();
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) TypeConverter(org.datanucleus.store.types.converters.TypeConverter) RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 2 with NamingFactory

use of org.datanucleus.store.schema.naming.NamingFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testDatastoreIdColumnName.

public void testDatastoreIdColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(MyDatastoreId.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for datastore-id is incorrect", "mydatastoreid_id", factory.getColumnName(cmd1, ColumnType.DATASTOREID_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 3 with NamingFactory

use of org.datanucleus.store.schema.naming.NamingFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testVersionColumnName.

public void testVersionColumnName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(Trade4.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Column name for version is incorrect", "version", factory.getColumnName(cmd1, ColumnType.VERSION_COLUMN));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 4 with NamingFactory

use of org.datanucleus.store.schema.naming.NamingFactory in project tests by datanucleus.

the class JPANamingFactoryTest method testTableName.

public void testTableName() {
    JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
    NucleusContext nucCtx = jpaEMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(ByteArray.class, clr);
    NamingFactory factory = new JPANamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.TABLE, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "bytearray", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.LOWER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"bytearray\"", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE);
    assertEquals("Table name is incorrect", "BYTEARRAY", factory.getTableName(cmd1));
    factory.setNamingCase(NamingCase.UPPER_CASE_QUOTED);
    assertEquals("Table name is incorrect", "\"BYTEARRAY\"", factory.getTableName(cmd1));
    AbstractClassMetaData cmd2 = mmgr.getMetaDataForClass(SimpleClass.class, clr);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    assertEquals("Table name is incorrect", "mysimpleclass", factory.getTableName(cmd2));
}
Also used : JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 5 with NamingFactory

use of org.datanucleus.store.schema.naming.NamingFactory in project datanucleus-core by datanucleus.

the class AbstractStoreManager method getNamingFactory.

public NamingFactory getNamingFactory() {
    if (namingFactory == null) {
        // Create the NamingFactory
        String namingFactoryName = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_NAMING_FACTORY);
        if ("datanucleus2".equalsIgnoreCase(namingFactoryName)) {
            namingFactory = new DN2NamingFactory(nucleusContext);
        } else if ("jpa".equalsIgnoreCase(namingFactoryName)) {
            namingFactory = new JPANamingFactory(nucleusContext);
        } else {
            // Fallback to the plugin mechanism
            String namingFactoryClassName = nucleusContext.getPluginManager().getAttributeValueForExtension("org.datanucleus.identifier_namingfactory", "name", namingFactoryName, "class-name");
            if (namingFactoryClassName == null) {
                // TODO Localise this
                throw new NucleusUserException("Error in specified NamingFactory " + namingFactoryName + " not found");
            }
            try {
                Class[] argTypes = new Class[] { ClassConstants.NUCLEUS_CONTEXT };
                Object[] args = new Object[] { nucleusContext };
                namingFactory = (NamingFactory) nucleusContext.getPluginManager().createExecutableExtension("org.datanucleus.identifier_namingfactory", "name", namingFactoryName, "class-name", argTypes, args);
            } catch (Throwable thr) {
                throw new NucleusUserException("Exception creating NamingFactory for datastore : " + thr.getMessage(), thr);
            }
        }
        // Set the case TODO Handle quoted cases (not specifiable via this property currently)
        String identifierCase = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
        if (identifierCase != null) {
            if (identifierCase.equalsIgnoreCase("lowercase")) {
                namingFactory.setNamingCase(NamingCase.LOWER_CASE);
            } else if (identifierCase.equalsIgnoreCase("UPPERCASE")) {
                namingFactory.setNamingCase(NamingCase.UPPER_CASE);
            } else {
                namingFactory.setNamingCase(NamingCase.MIXED_CASE);
            }
        }
    }
    return namingFactory;
}
Also used : DN2NamingFactory(org.datanucleus.store.schema.naming.DN2NamingFactory) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) DN2NamingFactory(org.datanucleus.store.schema.naming.DN2NamingFactory) JPANamingFactory(org.datanucleus.store.schema.naming.JPANamingFactory) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory)

Aggregations

NamingFactory (org.datanucleus.store.schema.naming.NamingFactory)6 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 MetaDataManager (org.datanucleus.metadata.MetaDataManager)5 JPANamingFactory (org.datanucleus.store.schema.naming.JPANamingFactory)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 NucleusContext (org.datanucleus.NucleusContext)4 JPAEntityManagerFactory (org.datanucleus.api.jpa.JPAEntityManagerFactory)4 ArrayList (java.util.ArrayList)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 RelationType (org.datanucleus.metadata.RelationType)1 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)1 TypeManager (org.datanucleus.store.types.TypeManager)1 MultiColumnConverter (org.datanucleus.store.types.converters.MultiColumnConverter)1 TypeConverter (org.datanucleus.store.types.converters.TypeConverter)1