Search in sources :

Example 11 with DatastoreIdentifier

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

the class TableImpl method getExistingCandidateKeys.

/**
 * Accessor for the candidate keys for this table.
 * @param conn The JDBC Connection
 * @return Map of candidate keys
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
private Map<DatastoreIdentifier, CandidateKey> getExistingCandidateKeys(Connection conn) throws SQLException {
    Map<DatastoreIdentifier, CandidateKey> candidateKeysByName = new HashMap<>();
    if (tableExistsInDatastore(conn)) {
        StoreSchemaHandler handler = storeMgr.getSchemaHandler();
        RDBMSTableIndexInfo tableIndexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(conn, RDBMSSchemaHandler.TYPE_INDICES, new Object[] { this });
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        Iterator indexIter = tableIndexInfo.getChildren().iterator();
        while (indexIter.hasNext()) {
            IndexInfo indexInfo = (IndexInfo) indexIter.next();
            boolean isUnique = !((Boolean) indexInfo.getProperty("non_unique")).booleanValue();
            if (isUnique) {
                // No idea of why this was being used, so commented out (H2 v2 fails if enabled)
                // short idxType = ((Short)indexInfo.getProperty("type")).shortValue();
                // if (idxType == DatabaseMetaData.tableIndexStatistic)
                // {
                // // Ignore
                // continue;
                // }
                // Only utilise unique indexes
                String keyName = (String) indexInfo.getProperty("index_name");
                DatastoreIdentifier idxName = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, keyName);
                CandidateKey key = candidateKeysByName.get(idxName);
                if (key == null) {
                    key = new CandidateKey(this, null);
                    key.setName(keyName);
                    candidateKeysByName.put(idxName, key);
                }
                // Set the column
                int colSeq = ((Short) indexInfo.getProperty("ordinal_position")).shortValue() - 1;
                DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN, (String) indexInfo.getProperty("column_name"));
                Column col = columnsByIdentifier.get(colName);
                if (col != null) {
                    key.setColumn(colSeq, col);
                }
            }
        }
    }
    return candidateKeysByName;
}
Also used : HashMap(java.util.HashMap) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) IndexInfo(org.datanucleus.store.rdbms.schema.IndexInfo) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator)

Example 12 with DatastoreIdentifier

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

the class TableImpl method getSQLAddCandidateKeyStatements.

/**
 * Get SQL statements to add expected Candidate Keys that are not yet on the
 * table. If the returned Map is empty, the current Candidate Key setup is correct.
 * @param actualCandidateKeysByName Actual Map of candidate keys
 * @return a Map with the SQL statements
 */
protected Map<String, String> getSQLAddCandidateKeyStatements(Map actualCandidateKeysByName) {
    assertIsInitialized();
    Map<String, String> stmtsByCKName = new HashMap<>();
    List<CandidateKey> expectedCandidateKeys = getExpectedCandidateKeys();
    Iterator<CandidateKey> i = expectedCandidateKeys.iterator();
    int n = 1;
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    while (i.hasNext()) {
        CandidateKey ck = i.next();
        if (!actualCandidateKeysByName.containsValue(ck)) {
            // If no name assigned, make one up
            if (ck.getName() == null) {
                // Use the CandidateKeyIdentifier to generate the name
                DatastoreIdentifier ckName;
                do {
                    ckName = idFactory.newCandidateKeyIdentifier(this, n++);
                } while (actualCandidateKeysByName.containsKey(ckName));
                ck.setName(ckName.getName());
            }
            String stmtText = dba.getAddCandidateKeyStatement(ck, idFactory);
            if (stmtText != null) {
                stmtsByCKName.put(ck.getName(), stmtText);
            }
        }
    }
    return stmtsByCKName;
}
Also used : HashMap(java.util.HashMap) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 13 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier 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.getNumberOfColumnMappings() == 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.getNumberOfColumnMappings();
    for (int j = 0; j < countFields; j++) {
        index.addColumn(fieldMapping.getColumnMapping(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 14 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier 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.getNumberOfColumnMappings();
    for (int j = 0; j < countFields; j++) {
        ck.addColumn(fieldMapping.getColumnMapping(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 15 with DatastoreIdentifier

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

the class TableGenerator method reserveBlock.

/**
 * Method to reserve a block of "size" identities.
 * @param size Block size
 * @return The reserved block
 */
public ValueGenerationBlock<Long> reserveBlock(long size) {
    if (size < 1) {
        return null;
    }
    // search for an ID in the database
    List<Long> oid = new ArrayList<>();
    try {
        if (sequenceTable == null) {
            initialiseSequenceTable();
        }
        DatastoreIdentifier sourceTableIdentifier = null;
        if (properties.containsKey(ValueGenerator.PROPERTY_TABLE_NAME)) {
            sourceTableIdentifier = ((RDBMSStoreManager) storeMgr).getIdentifierFactory().newTableIdentifier(properties.getProperty(ValueGenerator.PROPERTY_TABLE_NAME));
        // TODO Apply passed in catalog/schema to this identifier rather than the default for the factory
        }
        Long nextId = sequenceTable.getNextVal(sequenceName, connection, (int) size, sourceTableIdentifier, properties.getProperty(ValueGenerator.PROPERTY_COLUMN_NAME), initialValue);
        for (int i = 0; i < size; i++) {
            oid.add(nextId);
            nextId = Long.valueOf(nextId.longValue() + 1);
        }
        if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
            NucleusLogger.VALUEGENERATION.debug(Localiser.msg("040004", "" + size));
        }
        return new ValueGenerationBlock<>(oid);
    } catch (SQLException e) {
        throw new ValueGenerationException(Localiser.msg("061001", e.getMessage()));
    }
}
Also used : SQLException(java.sql.SQLException) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)45 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)19 HashMap (java.util.HashMap)18 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)12 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)11 Iterator (java.util.Iterator)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)10 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)8 MacroString (org.datanucleus.util.MacroString)6 ArrayList (java.util.ArrayList)5 NucleusException (org.datanucleus.exceptions.NucleusException)5 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 Index (org.datanucleus.store.rdbms.key.Index)4 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)4 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)4 SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3