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