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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations