Class AbstractNamingFactory

java.lang.Object
org.datanucleus.store.schema.naming.AbstractNamingFactory
All Implemented Interfaces:
NamingFactory
Direct Known Subclasses:
DN2NamingFactory, JPANamingFactory

public abstract class AbstractNamingFactory extends Object implements NamingFactory
Abstract base for any naming factory, providing convenience facilities like truncation.
  • Field Details

    • reservedWords

      protected Set<String> reservedWords
    • wordSeparator

      protected String wordSeparator
      Separator to use for words in the identifiers.
    • quoteString

      protected String quoteString
      Quote used when the identifier case selected requires it.
    • namingCase

      protected NamingCase namingCase
    • nucCtx

      protected NucleusContext nucCtx
    • clr

      protected ClassLoaderResolver clr
    • maxLengthByComponent

      Map<SchemaComponent,Integer> maxLengthByComponent
      Map of max name length, keyed by the schema component type
    • TRUNCATE_HASH_LENGTH

      private static final int TRUNCATE_HASH_LENGTH
      The number of characters used to build the hash.
      See Also:
    • TRUNCATE_HASH_RANGE

      private static final int TRUNCATE_HASH_RANGE
      Range to use for creating hashed ending when truncating identifiers. The actual hashes have a value between 0 and HASH_RANGE - 1.
  • Constructor Details

    • AbstractNamingFactory

      public AbstractNamingFactory(NucleusContext nucCtx)
  • Method Details

    • setReservedKeywords

      public NamingFactory setReservedKeywords(Set<String> keywords)
      Description copied from interface: NamingFactory
      Method to set the provided list of keywords as names that identifiers have to surround by quotes to use.
      Specified by:
      setReservedKeywords in interface NamingFactory
      Parameters:
      keywords - The keywords
      Returns:
      This naming factory
    • setQuoteString

      public NamingFactory setQuoteString(String quote)
      Description copied from interface: NamingFactory
      Method to set the quote string to use (when the identifiers need to be quoted). See
      setIdentifierCase
      .
      Specified by:
      setQuoteString in interface NamingFactory
      Parameters:
      quote - The quote string
      Returns:
      This naming factory
    • setWordSeparator

      public NamingFactory setWordSeparator(String sep)
      Description copied from interface: NamingFactory
      Method to set the word separator of the names.
      Specified by:
      setWordSeparator in interface NamingFactory
      Parameters:
      sep - Separator
      Returns:
      This naming factory
    • setNamingCase

      public NamingFactory setNamingCase(NamingCase nameCase)
      Description copied from interface: NamingFactory
      Method to set the required case of the names.
      Specified by:
      setNamingCase in interface NamingFactory
      Parameters:
      nameCase - Required case
      Returns:
      This naming factory
    • setMaximumLength

      public NamingFactory setMaximumLength(SchemaComponent cmpt, int max)
      Description copied from interface: NamingFactory
      Method to set the maximum length of the name of the specified schema component.
      Specified by:
      setMaximumLength in interface NamingFactory
      Parameters:
      cmpt - The component
      max - The maximum it accepts
      Returns:
      This naming factory
    • getMaximumLengthForComponent

      protected int getMaximumLengthForComponent(SchemaComponent cmpt)
    • getTableName

      public String getTableName(AbstractClassMetaData cmd)
      Description copied from interface: NamingFactory
      Method to return the name of the table for the specified class.
      Specified by:
      getTableName in interface NamingFactory
      Parameters:
      cmd - Metadata for the class
      Returns:
      Name of the table
    • getColumnName

      public String getColumnName(AbstractMemberMetaData mmd, ColumnType type)
      Description copied from interface: NamingFactory
      Method to return the name of the column for the specified field. If you have multiple columns for a field then call the other
      getColumnName
      method.
      Specified by:
      getColumnName in interface NamingFactory
      Parameters:
      mmd - Metadata for the field
      type - Type of column
      Returns:
      The column name
    • getColumnName

      public String getColumnName(List<AbstractMemberMetaData> mmds, int colPosition)
      Description copied from interface: NamingFactory
      Method to return the name of the column for the position of the specified EMBEDDED field, within the specified owner field. For example, say we have a class Type1 with field "field1" that is marked as embedded, and this is of type Type2. In turn Type2 has a field "field2" that is also embedded, of type Type3. Type3 has a field "name". So to get the column name for Type3.name in the table for Type1 we call "getColumnName({mmdForField1InType1, mmdForField2InType2, mmdForNameInType3}, 0)".
      Specified by:
      getColumnName in interface NamingFactory
      Parameters:
      mmds - MetaData for the field(s) with the column. The first value is the original field that is embedded, followed by fields of the embedded object(s).
      colPosition - The position of the column (where this field has multiple columns)
      Returns:
      The column name TODO Pass in something that distinguishes between map key and map value
    • getConstraintName

      public String getConstraintName(AbstractClassMetaData cmd, ConstraintMetaData cnstrmd, int position)
      Description copied from interface: NamingFactory
      Method to return the name of a constraint specified at class level.
      Specified by:
      getConstraintName in interface NamingFactory
      Parameters:
      cmd - Metadata for the class
      cnstrmd - The constraint metadata
      position - Number of the constraint at class level (first is 0)
      Returns:
      Name of the constraint
    • getConstraintName

      public String getConstraintName(String className, AbstractMemberMetaData mmd, ConstraintMetaData cnstrmd)
      Description copied from interface: NamingFactory
      Method to return the name of a constraint specified at member level.
      Specified by:
      getConstraintName in interface NamingFactory
      Parameters:
      className - Name of the class that this constraint is for.
      mmd - Metadata for the member
      cnstrmd - The constraint metadata
      Returns:
      Name of the constraint
    • getConstraintName

      public String getConstraintName(AbstractClassMetaData cmd, ConstraintMetaData cnstrmd, ColumnType type)
      Description copied from interface: NamingFactory
      Method to return the name of the constraint for the specified class (version, datastore-id, discriminator etc).
      Specified by:
      getConstraintName in interface NamingFactory
      Parameters:
      cmd - Metadata for the class
      cnstrmd - The constraint metadata
      type - Column type
      Returns:
      Name of the constraint
    • getSequenceName

      public String getSequenceName(SequenceMetaData seqmd)
      Description copied from interface: NamingFactory
      Method to return the name of sequence.
      Specified by:
      getSequenceName in interface NamingFactory
      Parameters:
      seqmd - Metadata for the sequence
      Returns:
      Name of the sequence
    • calculateHashMax

      private static final int calculateHashMax()
    • truncate

      protected static String truncate(String name, int length)
      Method to truncate a name to fit within the specified name length. If truncation is necessary will use a 4 char hashcode (defined by TRUNCATE_HASH_LENGTH) (at the end) to attempt to create uniqueness.
      Parameters:
      name - The name
      length - The (max) length to use
      Returns:
      The truncated name.
    • getNameInRequiredCase

      protected String getNameInRequiredCase(String name)
      Convenience method to convert the passed name into a name in the required "case". Also adds on any required quoting.
      Parameters:
      name - The name
      Returns:
      The updated name in the correct case
    • prepareIdentifierNameForUse

      protected String prepareIdentifierNameForUse(String name, SchemaComponent cmpt)
      Convenience method that will truncate the provided name if it is longer than the longest possible for the specified schema component, and then convert it into the required case.
      Parameters:
      name - The name
      cmpt - The schema component that it is for
      Returns:
      The prepared identifier name