Search in sources :

Example 1 with IdentifierFactory

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

the class PersistableJoinTable method getExpectedCandidateKeys.

/**
 * Accessor for the candidate keys for this table.
 * @return The indices
 */
protected List getExpectedCandidateKeys() {
    // The indices required by foreign keys (BaseTable)
    List candidateKeys = super.getExpectedCandidateKeys();
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getUniqueMetaData() != null) {
        // User has defined a unique key on the join table
        UniqueMetaData unimd = mmd.getJoinMetaData().getUniqueMetaData();
        if (unimd.getNumberOfColumns() > 0) {
            String[] columnNames = unimd.getColumnNames();
            CandidateKey uniKey = new CandidateKey(this, null);
            IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
            for (String columnName : columnNames) {
                Column col = getColumn(idFactory.newColumnIdentifier(columnName));
                if (col != null) {
                    uniKey.addColumn(col);
                } else {
                    throw new NucleusUserException("Unique key on join-table " + this + " has column " + columnName + " that is not found");
                }
            }
            candidateKeys.add(uniKey);
        }
    }
    return candidateKeys;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) List(java.util.List) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 2 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory 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, "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) {
                // Only utilise unique indexes
                short idxType = ((Short) indexInfo.getProperty("type")).shortValue();
                if (idxType == DatabaseMetaData.tableIndexStatistic) {
                    // Ignore
                    continue;
                }
                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 3 with IdentifierFactory

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

the class TableImpl method dropConstraints.

/**
 * Method to drop the constraints for the table from the datastore.
 * @param conn The JDBC Connection
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
public void dropConstraints(Connection conn) throws SQLException {
    assertIsInitialized();
    boolean drop_using_constraint = dba.supportsOption(DatastoreAdapter.ALTER_TABLE_DROP_CONSTRAINT_SYNTAX);
    boolean drop_using_foreign_key = dba.supportsOption(DatastoreAdapter.ALTER_TABLE_DROP_FOREIGN_KEY_CONSTRAINT);
    if (!drop_using_constraint && !drop_using_foreign_key) {
        return;
    }
    // There's no need to drop indices; we assume they'll go away quietly when the table is dropped.
    Set<String> fkNames = new HashSet<>();
    StoreSchemaHandler handler = storeMgr.getSchemaHandler();
    RDBMSTableFKInfo fkInfo = (RDBMSTableFKInfo) handler.getSchemaData(conn, "foreign-keys", new Object[] { this });
    Iterator iter = fkInfo.getChildren().iterator();
    while (iter.hasNext()) {
        // JDBC drivers can return null names for foreign keys, so we then skip the DROP CONSTRAINT.
        ForeignKeyInfo fki = (ForeignKeyInfo) iter.next();
        String fkName = (String) fki.getProperty("fk_name");
        if (fkName != null) {
            fkNames.add(fkName);
        }
    }
    int numFKs = fkNames.size();
    if (numFKs > 0) {
        if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
            NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("058102", "" + numFKs, this));
        }
        iter = fkNames.iterator();
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        Statement stmt = conn.createStatement();
        try {
            while (iter.hasNext()) {
                String constraintName = (String) iter.next();
                String stmtText = null;
                if (drop_using_constraint) {
                    stmtText = "ALTER TABLE " + toString() + " DROP CONSTRAINT " + idFactory.getIdentifierInAdapterCase(constraintName);
                } else {
                    stmtText = "ALTER TABLE " + toString() + " DROP FOREIGN KEY " + idFactory.getIdentifierInAdapterCase(constraintName);
                }
                executeDdlStatement(stmt, stmtText);
            }
        } finally {
            stmt.close();
        }
    }
}
Also used : ForeignKeyInfo(org.datanucleus.store.rdbms.schema.ForeignKeyInfo) RDBMSTableFKInfo(org.datanucleus.store.rdbms.schema.RDBMSTableFKInfo) Statement(java.sql.Statement) Iterator(java.util.Iterator) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) HashSet(java.util.HashSet)

Example 4 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory 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 5 with IdentifierFactory

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

the class TableImpl method getExistingIndices.

/**
 * Accessor for indices on the actual table.
 * @param conn The JDBC Connection
 * @return Map of indices (keyed by the index name)
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
private Map<DatastoreIdentifier, Index> getExistingIndices(Connection conn) throws SQLException {
    Map<DatastoreIdentifier, Index> indicesByName = new HashMap<>();
    if (tableExistsInDatastore(conn)) {
        StoreSchemaHandler handler = storeMgr.getSchemaHandler();
        RDBMSTableIndexInfo tableIndexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(conn, "indices", new Object[] { this });
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        Iterator indexIter = tableIndexInfo.getChildren().iterator();
        while (indexIter.hasNext()) {
            IndexInfo indexInfo = (IndexInfo) indexIter.next();
            short idxType = ((Short) indexInfo.getProperty("type")).shortValue();
            if (idxType == DatabaseMetaData.tableIndexStatistic) {
                // Ignore
                continue;
            }
            String indexName = (String) indexInfo.getProperty("index_name");
            DatastoreIdentifier indexIdentifier = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, indexName);
            Index idx = indicesByName.get(indexIdentifier);
            if (idx == null) {
                boolean isUnique = !((Boolean) indexInfo.getProperty("non_unique")).booleanValue();
                idx = new Index(this, isUnique, null);
                idx.setName(indexName);
                indicesByName.put(indexIdentifier, idx);
            }
            // 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) {
                idx.setColumn(colSeq, col);
            }
        }
    }
    return indicesByName;
}
Also used : HashMap(java.util.HashMap) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) Index(org.datanucleus.store.rdbms.key.Index) 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) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator)

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