Search in sources :

Example 16 with IdentifierFactory

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

the class ElementContainerTable 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 (elementMapping instanceof EmbeddedElementPCMapping) {
        // Add all candidate keys required by fields of the embedded element
        EmbeddedElementPCMapping embMapping = (EmbeddedElementPCMapping) elementMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    }
    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);
            String unimdName = unimd.getName();
            if (!StringUtils.isWhitespace(unimdName)) {
                uniKey.setName(unimd.getName());
            }
            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 : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) 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) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 17 with IdentifierFactory

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

the class TableImpl method getSQLAddFKStatements.

/**
 * Get SQL statements to add expected Foreign Keys that are not yet at the table.
 * If the returned Map is empty, the current FK setup is correct.
 * @param actualForeignKeysByName Actual Map of foreign keys
 * @param clr The ClassLoaderResolver
 * @return a Map with the SQL statements
 */
protected Map<String, String> getSQLAddFKStatements(Map actualForeignKeysByName, ClassLoaderResolver clr) {
    assertIsInitialized();
    Map<String, String> stmtsByFKName = new HashMap<>();
    List<ForeignKey> expectedForeignKeys = getExpectedForeignKeys(clr);
    Iterator<ForeignKey> i = expectedForeignKeys.iterator();
    int n = 1;
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    while (i.hasNext()) {
        ForeignKey fk = i.next();
        if (!actualForeignKeysByName.containsValue(fk)) {
            // If no name assigned, make one up
            if (fk.getName() == null) {
                // Use the ForeignKeyIdentifier to generate the name
                DatastoreIdentifier fkName;
                do {
                    fkName = idFactory.newForeignKeyIdentifier(this, n++);
                } while (actualForeignKeysByName.containsKey(fkName));
                fk.setName(fkName.getName());
            }
            String stmtText = dba.getAddForeignKeyStatement(fk, idFactory);
            if (stmtText != null) {
                stmtsByFKName.put(fk.getName(), stmtText);
            }
        }
    }
    return stmtsByFKName;
}
Also used : HashMap(java.util.HashMap) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 18 with IdentifierFactory

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

the class TableImpl method getExistingForeignKeys.

/**
 * Accessor for the foreign keys for this table.
 * @param conn The JDBC Connection
 * @return Map of foreign keys
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
private Map<DatastoreIdentifier, ForeignKey> getExistingForeignKeys(Connection conn) throws SQLException {
    Map<DatastoreIdentifier, ForeignKey> foreignKeysByName = new HashMap<>();
    if (tableExistsInDatastore(conn)) {
        StoreSchemaHandler handler = storeMgr.getSchemaHandler();
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        RDBMSTableFKInfo tableFkInfo = (RDBMSTableFKInfo) handler.getSchemaData(conn, "foreign-keys", new Object[] { this });
        Iterator fksIter = tableFkInfo.getChildren().iterator();
        while (fksIter.hasNext()) {
            ForeignKeyInfo fkInfo = (ForeignKeyInfo) fksIter.next();
            DatastoreIdentifier fkIdentifier;
            String fkName = (String) fkInfo.getProperty("fk_name");
            if (fkName == null) {
                fkIdentifier = idFactory.newForeignKeyIdentifier(this, foreignKeysByName.size());
            } else {
                fkIdentifier = idFactory.newIdentifier(IdentifierType.FOREIGN_KEY, fkName);
            }
            short deferrability = ((Short) fkInfo.getProperty("deferrability")).shortValue();
            boolean initiallyDeferred = deferrability == DatabaseMetaData.importedKeyInitiallyDeferred;
            ForeignKey fk = foreignKeysByName.get(fkIdentifier);
            if (fk == null) {
                fk = new ForeignKey(dba, initiallyDeferred);
                fk.setName(fkIdentifier.getName());
                foreignKeysByName.put(fkIdentifier, fk);
            }
            // Find the referenced table from the provided name
            String pkTableName = (String) fkInfo.getProperty("pk_table_name");
            DatastoreIdentifier refTableId = idFactory.newTableIdentifier(pkTableName);
            DatastoreClass refTable = storeMgr.getDatastoreClass(refTableId);
            if (refTable == null) {
                // Try with same catalog/schema as this table since some JDBC don't provide this info
                if (getSchemaName() != null) {
                    refTableId.setSchemaName(getSchemaName());
                }
                if (getCatalogName() != null) {
                    refTableId.setCatalogName(getCatalogName());
                }
                refTable = storeMgr.getDatastoreClass(refTableId);
            }
            if (refTable != null) {
                String fkColumnName = (String) fkInfo.getProperty("fk_column_name");
                String pkColumnName = (String) fkInfo.getProperty("pk_column_name");
                DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN, fkColumnName);
                DatastoreIdentifier refColName = idFactory.newIdentifier(IdentifierType.COLUMN, pkColumnName);
                Column col = columnsByIdentifier.get(colName);
                Column refCol = refTable.getColumn(refColName);
                if (col != null && refCol != null) {
                    fk.addColumn(col, refCol);
                } else {
                // TODO throw exception?
                }
            } else {
                NucleusLogger.DATASTORE_SCHEMA.warn("Retrieved ForeignKey from datastore for table=" + toString() + " referencing table " + pkTableName + " but not found internally. Is there some catalog/schema or quoting causing problems?");
            }
        }
    }
    return foreignKeysByName;
}
Also used : HashMap(java.util.HashMap) RDBMSTableFKInfo(org.datanucleus.store.rdbms.schema.RDBMSTableFKInfo) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) ForeignKeyInfo(org.datanucleus.store.rdbms.schema.ForeignKeyInfo) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator)

Example 19 with IdentifierFactory

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

the class TableImpl method getSQLCreateIndexStatements.

/**
 * Accessor for the CREATE INDEX statements for this table.
 * @param actualIndicesByName Map of actual indexes
 * @param clr The ClassLoaderResolver
 * @return Map of statements
 */
protected Map<String, String> getSQLCreateIndexStatements(Map actualIndicesByName, ClassLoaderResolver clr) {
    assertIsInitialized();
    Map<String, String> stmtsByIdxName = new HashMap<>();
    Set<Index> expectedIndices = getExpectedIndices(clr);
    int n = 1;
    Iterator<Index> indexIter = expectedIndices.iterator();
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    while (indexIter.hasNext()) {
        Index idx = indexIter.next();
        if (isIndexReallyNeeded(idx, actualIndicesByName.values())) {
            // If no name assigned, make one up
            if (idx.getName() == null) {
                // Use IndexIdentifier to generate the name.
                DatastoreIdentifier idxName;
                do {
                    idxName = idFactory.newIndexIdentifier(this, idx.getUnique(), n++);
                    idx.setName(idxName.getName());
                } while (actualIndicesByName.containsKey(idxName));
            }
            String stmtText = dba.getCreateIndexStatement(idx, idFactory);
            stmtsByIdxName.put(idx.getName(), stmtText);
        }
    }
    return stmtsByIdxName;
}
Also used : HashMap(java.util.HashMap) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Index(org.datanucleus.store.rdbms.key.Index) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 20 with IdentifierFactory

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

the class TableImpl method getExistingPrimaryKeys.

/**
 * Accessor for the primary keys for this table in the datastore.
 * @param conn The JDBC Connection
 * @return Map of primary keys
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
private Map<DatastoreIdentifier, PrimaryKey> getExistingPrimaryKeys(Connection conn) throws SQLException {
    Map<DatastoreIdentifier, PrimaryKey> primaryKeysByName = new HashMap<>();
    if (tableExistsInDatastore(conn)) {
        StoreSchemaHandler handler = storeMgr.getSchemaHandler();
        RDBMSTablePKInfo tablePkInfo = (RDBMSTablePKInfo) handler.getSchemaData(conn, "primary-keys", new Object[] { this });
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        Iterator pkColsIter = tablePkInfo.getChildren().iterator();
        while (pkColsIter.hasNext()) {
            PrimaryKeyInfo pkInfo = (PrimaryKeyInfo) pkColsIter.next();
            String pkName = (String) pkInfo.getProperty("pk_name");
            DatastoreIdentifier pkIdentifier;
            if (pkName == null) {
                pkIdentifier = idFactory.newPrimaryKeyIdentifier(this);
            } else {
                pkIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, pkName);
            }
            PrimaryKey pk = primaryKeysByName.get(pkIdentifier);
            if (pk == null) {
                pk = new PrimaryKey(this);
                pk.setName(pkIdentifier.getName());
                primaryKeysByName.put(pkIdentifier, pk);
            }
            int keySeq = (((Short) pkInfo.getProperty("key_seq")).shortValue()) - 1;
            String colName = (String) pkInfo.getProperty("column_name");
            DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, colName);
            Column col = columnsByIdentifier.get(colIdentifier);
            if (col == null) {
                throw new UnexpectedColumnException(this.toString(), colIdentifier.getName(), this.getSchemaName(), this.getCatalogName());
            }
            pk.setColumn(keySeq, col);
        }
    }
    return primaryKeysByName;
}
Also used : HashMap(java.util.HashMap) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) PrimaryKeyInfo(org.datanucleus.store.rdbms.schema.PrimaryKeyInfo) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) RDBMSTablePKInfo(org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo) UnexpectedColumnException(org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)

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