Class AbstractIdentifierFactory
- java.lang.Object
-
- org.datanucleus.store.rdbms.identifier.AbstractIdentifierFactory
-
- All Implemented Interfaces:
IdentifierFactory
- Direct Known Subclasses:
DNIdentifierFactory,JPAIdentifierFactory
public abstract class AbstractIdentifierFactory extends java.lang.Object implements IdentifierFactory
Abstract representation of an identifier factory for ORM datastores. To be extended to generate the identifiers.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,DatastoreIdentifier>candidatesstatic intCASE_LOWERstatic intCASE_PRESERVEstatic intCASE_UPPERprotected org.datanucleus.ClassLoaderResolverclrprotected java.util.Map<java.lang.String,DatastoreIdentifier>columnsprotected DatastoreAdapterdbaprotected java.lang.StringdefaultCatalogNameDefault catalog name for any created identifiers.protected java.lang.StringdefaultSchemaNameDefault schema name for any created identifiers.protected java.util.Map<java.lang.String,DatastoreIdentifier>foreignkeysprivate static intHASH_LENGTHThe number of characters used to build the hash.private static intHASH_RANGERange to use for creating hashed ending when truncating identifiers.protected java.util.Map<java.lang.String,DatastoreIdentifier>indexesprotected org.datanucleus.store.schema.naming.NamingCasenamingCaseCase to use for identifiers.protected org.datanucleus.store.schema.naming.NamingFactorynamingFactoryprotected java.util.Map<java.lang.String,DatastoreIdentifier>primarykeysprotected java.lang.StringquoteStringprotected java.util.Map<java.lang.String,DatastoreIdentifier>referencesprotected java.util.Map<java.lang.String,DatastoreIdentifier>sequencesprotected java.util.Map<java.lang.String,DatastoreIdentifier>tablesprotected java.lang.StringwordSeparatorSeparator to use for words in the identifiers.-
Fields inherited from interface org.datanucleus.store.rdbms.identifier.IdentifierFactory
PROPERTY_DEFAULT_CATALOG, PROPERTY_DEFAULT_SCHEMA, PROPERTY_NAMING_FACTORY, PROPERTY_REQUIRED_CASE, PROPERTY_TABLE_PREFIX, PROPERTY_TABLE_SUFFIX, PROPERTY_WORD_SEPARATOR
-
-
Constructor Summary
Constructors Constructor Description AbstractIdentifierFactory(DatastoreAdapter dba, org.datanucleus.ClassLoaderResolver clr, java.util.Map props)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static intcalculateHashMax()protected abstract java.lang.StringgenerateIdentifierNameForJavaName(java.lang.String javaName)Generate a datastore identifier from a Java identifier.protected abstract java.lang.StringgetColumnIdentifierSuffix(org.datanucleus.metadata.FieldRole role, boolean embedded)Accessor for the suffix to add to any column identifier, based on the role type.DatastoreAdaptergetDatastoreAdapter()Accessor for the datastore adapter that we are creating identifiers for.java.lang.StringgetIdentifierInAdapterCase(java.lang.String identifier)Convenience method to convert the passed identifier into an identifier in the correct case, and with any required quoting for the datastore adapter.protected java.lang.String[]getIdentifierNamePartsFromName(java.lang.String name)Convenience method to split a fully-specified identifier name (inc catalog/schema) into its constituent parts.java.lang.StringgetIdentifierTruncatedToAdapterColumnLength(java.lang.String identifier)Method to truncate the provided identifier as required to the datastore adapter column length (if required)org.datanucleus.store.schema.naming.NamingCasegetNamingCase()Accessor for the identifier case being used.java.lang.StringgetWordSeparator()Accessor for the word separator for identifiers.DatastoreIdentifiernewCandidateKeyIdentifier(Table table, int seq)Method to generate an identifier for a candidate key in the supplied table.DatastoreIdentifiernewColumnIdentifier(java.lang.String identifierName)Method to use to generate an identifier for a column.DatastoreIdentifiernewColumnIdentifier(java.lang.String javaName, boolean embedded, org.datanucleus.metadata.FieldRole fieldRole, boolean custom)Method to create an identifier for a column where we want the name based on the supplied java name, and the field has a particular role (and so could have its naming set according to the role).DatastoreIdentifiernewForeignKeyIdentifier(Table table, int seq)Method to create a new identifier for a foreign key in the supplied table.DatastoreIdentifiernewIdentifier(DatastoreIdentifier identifier, java.lang.String suffix)Method to return a new Identifier based on the passed identifier, but adding on the passed suffixDatastoreIdentifiernewIdentifier(IdentifierType identifierType, java.lang.String name)Method to generate an identifier based on the supplied name for the requested type of identifier.DatastoreIdentifiernewIndexIdentifier(Table table, boolean isUnique, int seq)Method to create an identifier for an Index in the supplied table.DatastoreIdentifiernewPrimaryKeyIdentifier(Table table)Method to generate an identifier for a primary key for the supplied table.DatastoreIdentifiernewSequenceIdentifier(java.lang.String sequenceName)Method to generate an identifier for a sequence using the passed name.DatastoreIdentifiernewTableIdentifier(java.lang.String identifierName)Method to use to generate an identifier for a column in the default catalog/schema.DatastoreIdentifiernewTableIdentifier(java.lang.String identifierName, java.lang.String catalogName, java.lang.String schemaName)Method to use to generate an identifier for a column.protected voidsetCatalogSchemaForTable(TableIdentifier identifier)Convenience method to set the catalog/schema on the passed TableIdentifier.protected java.lang.Stringtruncate(java.lang.String identifier, int length)Method to truncate an identifier to fit within the specified identifier length.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.datanucleus.store.rdbms.identifier.IdentifierFactory
newAdapterIndexFieldIdentifier, newDiscriminatorFieldIdentifier, newForeignKeyFieldIdentifier, newIndexFieldIdentifier, newJoinTableFieldIdentifier, newReferenceFieldIdentifier, newTableIdentifier, newTableIdentifier, newVersionFieldIdentifier
-
-
-
-
Field Detail
-
CASE_PRESERVE
public static final int CASE_PRESERVE
- See Also:
- Constant Field Values
-
CASE_UPPER
public static final int CASE_UPPER
- See Also:
- Constant Field Values
-
CASE_LOWER
public static final int CASE_LOWER
- See Also:
- Constant Field Values
-
HASH_LENGTH
private static final int HASH_LENGTH
The number of characters used to build the hash.- See Also:
- Constant Field Values
-
HASH_RANGE
private static final int HASH_RANGE
Range to use for creating hashed ending when truncating identifiers. The actual hashes have a value between 0 andHASH_RANGE- 1.
-
namingFactory
protected org.datanucleus.store.schema.naming.NamingFactory namingFactory
-
dba
protected DatastoreAdapter dba
-
clr
protected org.datanucleus.ClassLoaderResolver clr
-
namingCase
protected org.datanucleus.store.schema.naming.NamingCase namingCase
Case to use for identifiers.
-
quoteString
protected java.lang.String quoteString
-
wordSeparator
protected java.lang.String wordSeparator
Separator to use for words in the identifiers.
-
tables
protected java.util.Map<java.lang.String,DatastoreIdentifier> tables
-
columns
protected java.util.Map<java.lang.String,DatastoreIdentifier> columns
-
foreignkeys
protected java.util.Map<java.lang.String,DatastoreIdentifier> foreignkeys
-
indexes
protected java.util.Map<java.lang.String,DatastoreIdentifier> indexes
-
candidates
protected java.util.Map<java.lang.String,DatastoreIdentifier> candidates
-
primarykeys
protected java.util.Map<java.lang.String,DatastoreIdentifier> primarykeys
-
sequences
protected java.util.Map<java.lang.String,DatastoreIdentifier> sequences
-
references
protected java.util.Map<java.lang.String,DatastoreIdentifier> references
-
defaultCatalogName
protected java.lang.String defaultCatalogName
Default catalog name for any created identifiers.
-
defaultSchemaName
protected java.lang.String defaultSchemaName
Default schema name for any created identifiers.
-
-
Constructor Detail
-
AbstractIdentifierFactory
public AbstractIdentifierFactory(DatastoreAdapter dba, org.datanucleus.ClassLoaderResolver clr, java.util.Map props)
Constructor. The properties accepted are- RequiredCase : what case the identifiers should be in
- DefaultCatalog : default catalog to use (if any)
- DefaultSchema : default schema to use (if any)
- Parameters:
dba- Database adapterclr- ClassLoader resolverprops- Any properties controlling identifier generation
-
-
Method Detail
-
calculateHashMax
private static final int calculateHashMax()
-
getDatastoreAdapter
public DatastoreAdapter getDatastoreAdapter()
Accessor for the datastore adapter that we are creating identifiers for.- Specified by:
getDatastoreAdapterin interfaceIdentifierFactory- Returns:
- The datastore adapter
-
getNamingCase
public org.datanucleus.store.schema.naming.NamingCase getNamingCase()
Accessor for the identifier case being used.- Specified by:
getNamingCasein interfaceIdentifierFactory- Returns:
- The identifier case
-
getWordSeparator
public java.lang.String getWordSeparator()
Accessor for the word separator for identifiers.- Returns:
- The word separator
-
getIdentifierTruncatedToAdapterColumnLength
public java.lang.String getIdentifierTruncatedToAdapterColumnLength(java.lang.String identifier)
Description copied from interface:IdentifierFactoryMethod to truncate the provided identifier as required to the datastore adapter column length (if required)- Specified by:
getIdentifierTruncatedToAdapterColumnLengthin interfaceIdentifierFactory- Parameters:
identifier- The identifier- Returns:
- The truncated variant (if the length was longer than the max column identifier length);
-
truncate
protected java.lang.String truncate(java.lang.String identifier, int length)Method to truncate an identifier to fit within the specified identifier length. If truncation is necessary will use a 4 char hashcode (defined byHASH_LENGTH) (at the end) to attempt to create uniqueness.- Parameters:
identifier- The identifierlength- The (max) length to use- Returns:
- The truncated identifier.
-
getIdentifierInAdapterCase
public java.lang.String getIdentifierInAdapterCase(java.lang.String identifier)
Convenience method to convert the passed identifier into an identifier in the correct case, and with any required quoting for the datastore adapter. If the identifier is already quoted and needs quotes then none are added.- Specified by:
getIdentifierInAdapterCasein interfaceIdentifierFactory- Parameters:
identifier- The identifier- Returns:
- The updated identifier in the correct case
-
newIdentifier
public DatastoreIdentifier newIdentifier(IdentifierType identifierType, java.lang.String name)
Method to generate an identifier based on the supplied name for the requested type of identifier.- Specified by:
newIdentifierin interfaceIdentifierFactory- Parameters:
identifierType- the type of identifier to be createdname- The Java or SQL identifier name- Returns:
- The DatastoreIdentifier
-
newIdentifier
public DatastoreIdentifier newIdentifier(DatastoreIdentifier identifier, java.lang.String suffix)
Method to return a new Identifier based on the passed identifier, but adding on the passed suffix- Specified by:
newIdentifierin interfaceIdentifierFactory- Parameters:
identifier- The current identifiersuffix- The suffix- Returns:
- The new identifier
-
newTableIdentifier
public DatastoreIdentifier newTableIdentifier(java.lang.String identifierName)
Method to use to generate an identifier for a column in the default catalog/schema. The passed name will not be changed (other than in its case) although it may be truncated to fit the maximum length permitted for a column identifier.- Specified by:
newTableIdentifierin interfaceIdentifierFactory- Parameters:
identifierName- The identifier name- Returns:
- The DatastoreIdentifier for the table
-
newTableIdentifier
public DatastoreIdentifier newTableIdentifier(java.lang.String identifierName, java.lang.String catalogName, java.lang.String schemaName)
Method to use to generate an identifier for a column. The passed name will not be changed (other than in its case) although it may be truncated to fit the maximum length permitted for a column identifier.- Specified by:
newTableIdentifierin interfaceIdentifierFactory- Parameters:
identifierName- The identifier name for the tablecatalogName- Optional catalog name (null means not set)schemaName- Optional schema name (null means not set)- Returns:
- The DatastoreIdentifier for the table
-
newColumnIdentifier
public DatastoreIdentifier newColumnIdentifier(java.lang.String identifierName)
Method to use to generate an identifier for a column. The passed name will not be changed (other than in its case) although it may be truncated to fit the maximum length permitted for a column identifier.- Specified by:
newColumnIdentifierin interfaceIdentifierFactory- Parameters:
identifierName- The identifier name- Returns:
- The DatastoreIdentifier
-
newColumnIdentifier
public DatastoreIdentifier newColumnIdentifier(java.lang.String javaName, boolean embedded, org.datanucleus.metadata.FieldRole fieldRole, boolean custom)
Method to create an identifier for a column where we want the name based on the supplied java name, and the field has a particular role (and so could have its naming set according to the role).- Specified by:
newColumnIdentifierin interfaceIdentifierFactory- Parameters:
javaName- The java field nameembedded- Whether the identifier is for a field embeddedfieldRole- The role to be performed by this column e.g FK, Index ?custom- Whether this has a user-defined name- Returns:
- The DatastoreIdentifier
-
newSequenceIdentifier
public DatastoreIdentifier newSequenceIdentifier(java.lang.String sequenceName)
Method to generate an identifier for a sequence using the passed name.- Specified by:
newSequenceIdentifierin interfaceIdentifierFactory- Parameters:
sequenceName- the name of the sequence to use- Returns:
- The DatastoreIdentifier
-
newPrimaryKeyIdentifier
public DatastoreIdentifier newPrimaryKeyIdentifier(Table table)
Method to generate an identifier for a primary key for the supplied table.- Specified by:
newPrimaryKeyIdentifierin interfaceIdentifierFactory- Parameters:
table- the table- Returns:
- The DatastoreIdentifier
-
newCandidateKeyIdentifier
public DatastoreIdentifier newCandidateKeyIdentifier(Table table, int seq)
Method to generate an identifier for a candidate key in the supplied table.- Specified by:
newCandidateKeyIdentifierin interfaceIdentifierFactory- Parameters:
table- the tableseq- the sequential number- Returns:
- The DatastoreIdentifier
-
newForeignKeyIdentifier
public DatastoreIdentifier newForeignKeyIdentifier(Table table, int seq)
Method to create a new identifier for a foreign key in the supplied table.- Specified by:
newForeignKeyIdentifierin interfaceIdentifierFactory- Parameters:
table- the tableseq- the sequential number- Returns:
- The DatastoreIdentifier
-
newIndexIdentifier
public DatastoreIdentifier newIndexIdentifier(Table table, boolean isUnique, int seq)
Method to create an identifier for an Index in the supplied table.- Specified by:
newIndexIdentifierin interfaceIdentifierFactory- Parameters:
table- the tableisUnique- if the index is uniqueseq- the sequential number- Returns:
- The DatastoreIdentifier
-
getColumnIdentifierSuffix
protected abstract java.lang.String getColumnIdentifierSuffix(org.datanucleus.metadata.FieldRole role, boolean embedded)Accessor for the suffix to add to any column identifier, based on the role type.- Parameters:
role- column roleembedded- Whether the column is stored embedded- Returns:
- The suffix (e.g _ID for id columns).
-
generateIdentifierNameForJavaName
protected abstract java.lang.String generateIdentifierNameForJavaName(java.lang.String javaName)
Generate a datastore identifier from a Java identifier. Embodies the naming rules for the factory.- Parameters:
javaName- the Java identifier.- Returns:
- The datastore identifier
-
setCatalogSchemaForTable
protected void setCatalogSchemaForTable(TableIdentifier identifier)
Convenience method to set the catalog/schema on the passed TableIdentifier.- Parameters:
identifier- The TableIdentifier
-
getIdentifierNamePartsFromName
protected java.lang.String[] getIdentifierNamePartsFromName(java.lang.String name)
Convenience method to split a fully-specified identifier name (inc catalog/schema) into its constituent parts. Returns a String array with 3 elements. The first is the catalog, second the schema, and third the identifier.- Parameters:
name- Name- Returns:
- The parts
-
-