Search in sources :

Example 6 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class TableUtils method getIndexForField.

/**
 * Convenience method to create an Index for a field.
 * @param table Container for the index
 * @param imd The Index MetaData
 * @param fieldMapping Mapping for the field
 * @return The Index
 */
public static Index getIndexForField(Table table, IndexMetaData imd, JavaTypeMapping fieldMapping) {
    if (fieldMapping.getNumberOfDatastoreMappings() == 0) {
        // No columns in this mapping so we can hardly index it!
        return null;
    }
    if (!table.getStoreManager().getDatastoreAdapter().validToIndexMapping(fieldMapping)) {
        return null;
    }
    // Verify if a unique index is needed
    boolean unique = (imd == null ? false : imd.isUnique());
    Index index = new Index(table, unique, (imd != null ? imd.getExtensions() : null));
    // Set the index name if required
    if (imd != null && imd.getName() != null) {
        IdentifierFactory idFactory = table.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier idxId = idFactory.newIdentifier(IdentifierType.INDEX, imd.getName());
        index.setName(idxId.toString());
    }
    // Field-level index so use all columns for the field
    int countFields = fieldMapping.getNumberOfDatastoreMappings();
    for (int j = 0; j < countFields; j++) {
        index.addColumn(fieldMapping.getDatastoreMapping(j).getColumn());
    }
    return index;
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Index(org.datanucleus.store.rdbms.key.Index) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 7 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class TableUtils method getCandidateKeyForField.

/**
 * Convenience method to return the candidate key (if any) for a field.
 * @param table The table
 * @param umd The Unique MetaData
 * @param fieldMapping Mapping for the field
 * @return The Candidate Key
 */
public static CandidateKey getCandidateKeyForField(Table table, UniqueMetaData umd, JavaTypeMapping fieldMapping) {
    CandidateKey ck = new CandidateKey(table, umd != null ? umd.getExtensions() : null);
    // Set the key name if required
    if (umd.getName() != null) {
        IdentifierFactory idFactory = table.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier ckId = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, umd.getName());
        ck.setName(ckId.toString());
    }
    // Field-level index so use all columns for the field
    int countFields = fieldMapping.getNumberOfDatastoreMappings();
    for (int j = 0; j < countFields; j++) {
        ck.addColumn(fieldMapping.getDatastoreMapping(j).getColumn());
    }
    return ck;
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 8 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class SequenceTable method initialize.

/**
 * Method to initialise the table.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    // "SEQUENCE_NAME" column
    sequenceNameMapping = storeMgr.getMappingManager().getMapping(String.class);
    Column colSequenceName = addColumn(String.class.getName(), idFactory.newColumnIdentifier(sequenceNameColumnName), sequenceNameMapping, null);
    colSequenceName.setPrimaryKey();
    colSequenceName.getColumnMetaData().setLength(Integer.valueOf("255"));
    colSequenceName.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    getStoreManager().getMappingManager().createDatastoreMapping(sequenceNameMapping, colSequenceName, String.class.getName());
    // "NEXT_VAL" column
    nextValMapping = storeMgr.getMappingManager().getMapping(Long.class);
    Column colNextVal = addColumn(Long.class.getName(), idFactory.newColumnIdentifier(nextValColumnName), nextValMapping, null);
    getStoreManager().getMappingManager().createDatastoreMapping(nextValMapping, colNextVal, Long.class.getName());
    // Set up JDBC statements for supported operations
    insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + colSequenceName.getIdentifier() + "," + colNextVal.getIdentifier() + ") VALUES (?,?)";
    incrementByStmt = "UPDATE " + identifier.getFullyQualifiedName(false) + " SET " + colNextVal.getIdentifier() + "=(" + colNextVal.getIdentifier() + "+?) WHERE " + colSequenceName.getIdentifier() + "=?";
    deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + colSequenceName.getIdentifier() + "=?";
    deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
    fetchStmt = "SELECT " + colNextVal.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + colSequenceName.getIdentifier() + "=?";
    if (dba.supportsOption(DatastoreAdapter.LOCK_WITH_SELECT_FOR_UPDATE)) {
        fetchStmt += " FOR UPDATE";
    }
    fetchAllStmt = "SELECT " + colNextVal.getIdentifier() + "," + colSequenceName.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + colSequenceName.getIdentifier();
    storeMgr.registerTableInitialized(this);
    state = TABLE_STATE_INITIALIZED;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 9 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method initialiseIdentifierFactory.

/**
 * Method to create the IdentifierFactory to be used by this store.
 * Relies on the datastore adapter existing before creation
 * @param nucleusContext context
 */
protected void initialiseIdentifierFactory(NucleusContext nucleusContext) {
    if (dba == null) {
        throw new NucleusException("DatastoreAdapter not yet created so cannot create IdentifierFactory!");
    }
    String idFactoryName = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_FACTORY);
    try {
        // Create the control properties for identifier generation
        Map props = new HashMap();
        if (catalogName != null) {
            props.put("DefaultCatalog", catalogName);
        }
        if (schemaName != null) {
            props.put("DefaultSchema", schemaName);
        }
        String val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
        props.put("RequiredCase", val != null ? val : getDefaultIdentifierCase());
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_WORD_SEPARATOR);
        if (val != null) {
            props.put("WordSeparator", val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_PREFIX);
        if (val != null) {
            props.put("TablePrefix", val);
        }
        val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_SUFFIX);
        if (val != null) {
            props.put("TableSuffix", val);
        }
        props.put("NamingFactory", getNamingFactory());
        // Create the IdentifierFactory
        ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
        if ("datanucleus2".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new DN2IdentifierFactory(dba, clr, props);
        } else if ("jpa".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new JPAIdentifierFactory(dba, clr, props);
        } else if ("datanucleus1".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new DNIdentifierFactory(dba, clr, props);
        } else if ("jpox".equalsIgnoreCase(idFactoryName)) {
            identifierFactory = new JPOXIdentifierFactory(dba, clr, props);
        } else {
            // Fallback to the plugin mechanism
            Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassConstants.CLASS_LOADER_RESOLVER, Map.class };
            Object[] args = new Object[] { dba, nucleusContext.getClassLoaderResolver(null), props };
            identifierFactory = (IdentifierFactory) nucleusContext.getPluginManager().createExecutableExtension("org.datanucleus.store.rdbms.identifierfactory", "name", idFactoryName, "class-name", argTypes, args);
        }
    } catch (ClassNotFoundException cnfe) {
        throw new NucleusUserException(Localiser.msg("039004", idFactoryName), cnfe).setFatal();
    } catch (Exception e) {
        NucleusLogger.PERSISTENCE.error("Exception creating IdentifierFactory", e);
        throw new NucleusException(Localiser.msg("039005", idFactoryName), e).setFatal();
    }
}
Also used : JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DN2IdentifierFactory(org.datanucleus.store.rdbms.identifier.DN2IdentifierFactory) MacroString(org.datanucleus.util.MacroString) DN2IdentifierFactory(org.datanucleus.store.rdbms.identifier.DN2IdentifierFactory) JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) DNIdentifierFactory(org.datanucleus.store.rdbms.identifier.DNIdentifierFactory) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) JPAIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory) SQLException(java.sql.SQLException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) IOException(java.io.IOException) NucleusException(org.datanucleus.exceptions.NucleusException) UnsupportedDataTypeException(org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IncompatibleFieldTypeException(org.datanucleus.store.types.IncompatibleFieldTypeException) DNIdentifierFactory(org.datanucleus.store.rdbms.identifier.DNIdentifierFactory) JPAIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map) MultiMap(org.datanucleus.util.MultiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 10 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class SchemaTable method initialize.

/**
 * Method to initialise the table.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    MappingManager mapMgr = getStoreManager().getMappingManager();
    classMapping = mapMgr.getMapping(String.class);
    Column class_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("CLASS_NAME"), classMapping, null);
    mapMgr.createDatastoreMapping(classMapping, class_column, String.class.getName());
    class_column.getColumnMetaData().setLength(128);
    class_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    class_column.setPrimaryKey();
    tableMapping = mapMgr.getMapping(String.class);
    Column table_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TABLE_NAME"), tableMapping, null);
    mapMgr.createDatastoreMapping(tableMapping, table_column, String.class.getName());
    table_column.getColumnMetaData().setLength(128);
    table_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    typeMapping = mapMgr.getMapping(String.class);
    Column type_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TYPE"), typeMapping, null);
    mapMgr.createDatastoreMapping(typeMapping, type_column, String.class.getName());
    type_column.getColumnMetaData().setLength(4);
    type_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    // TODO Change type to SMALLINT/BIT
    ownerMapping = mapMgr.getMapping(String.class);
    Column owner_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("OWNER"), ownerMapping, null);
    mapMgr.createDatastoreMapping(ownerMapping, owner_column, String.class.getName());
    owner_column.getColumnMetaData().setLength(2);
    owner_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    versionMapping = mapMgr.getMapping(String.class);
    Column version_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("VERSION"), versionMapping, null);
    mapMgr.createDatastoreMapping(versionMapping, version_column, String.class.getName());
    version_column.getColumnMetaData().setLength(20);
    version_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    interfaceNameMapping = mapMgr.getMapping(String.class);
    Column interfaceName_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("INTERFACE_NAME"), interfaceNameMapping, null);
    mapMgr.createDatastoreMapping(interfaceNameMapping, interfaceName_column, String.class.getName());
    interfaceName_column.getColumnMetaData().setLength(255);
    interfaceName_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    interfaceName_column.setNullable(true);
    // Set up JDBC statements for supported operations
    insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + ") VALUES (?,?,?,?,?,?)";
    deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + "=?";
    deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
    fetchAllStmt = "SELECT " + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + table_column.getIdentifier();
    fetchStmt = "SELECT 1 FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + " = ? ";
    state = TABLE_STATE_INITIALIZED;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Aggregations

IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)28 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)19 HashMap (java.util.HashMap)11 Iterator (java.util.Iterator)8 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)7 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)5 Column (org.datanucleus.store.rdbms.table.Column)5 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)5 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 JPOXIdentifierFactory (org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory)4 CandidateKey (org.datanucleus.store.rdbms.key.CandidateKey)4 Index (org.datanucleus.store.rdbms.key.Index)4 MacroString (org.datanucleus.util.MacroString)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3