private void mockContext(final ImplicitNameSource source) { final MetadataBuildingContext context = Mockito.mock(MetadataBuildingContext.class); final InFlightMetadataCollector collector = Mockito.mock(InFlightMetadataCollector.class); final Database database = Mockito.mock(Database.class); final JdbcEnvironment jdbcEnvironment = Mockito.mock(JdbcEnvironment.class); final IdentifierHelper identifierHelper = Mockito.mock(IdentifierHelper.class); Mockito.when(identifierHelper.toIdentifier(ArgumentMatchers.anyString())).then(new Answer<Identifier>() { @Override public Identifier answer(final InvocationOnMock invocation) { return DatabaseIdentifier.toIdentifier((String)invocation.getArguments()[0]) ; } }); Mockito.when(jdbcEnvironment.getIdentifierHelper()).thenReturn(identifierHelper); Mockito.when(database.getJdbcEnvironment()).thenReturn(jdbcEnvironment); Mockito.when(collector.getDatabase()).thenReturn(database); Mockito.when(context.getMetadataCollector()).thenReturn(collector); Mockito.when(source.getBuildingContext()).thenReturn(context); }
private JdbcEnvironment newJdbcEnvironment() { JdbcEnvironment jdbcEnvironment = Mockito.mock(JdbcEnvironment.class); IdentifierHelper identifierHelper = Mockito.mock(IdentifierHelper.class); Mockito.when(identifierHelper.toIdentifier(ArgumentMatchers.anyString())).then(new Answer<Identifier>() { @Override public Identifier answer(InvocationOnMock invocation) { if (invocation.getArguments()[0] == null) return null; return new Identifier((String) invocation.getArguments()[0], false); } }); Mockito.when(jdbcEnvironment.getIdentifierHelper()).thenReturn(identifierHelper); return jdbcEnvironment; }
@Test public void namingStrategy() { RobeHibernateNamingStrategy strategy = new RobeHibernateNamingStrategy("V_"); Identifier identifier = Identifier.toIdentifier("A_NAME"); Identifier expectedIdentifier = Identifier.toIdentifier("V_A_NAME"); Identifier expectedIdentifierColumn = Identifier.toIdentifier("A_NAME"); Identifier none = strategy.toPhysicalTableName(null, null); Identifier physicalCatalogName = strategy.toPhysicalCatalogName(identifier, null); Identifier physicalColumnName = strategy.toPhysicalColumnName(identifier, null); Identifier physicalSchemaName = strategy.toPhysicalSchemaName(identifier, null); Identifier physicalSequenceName = strategy.toPhysicalSequenceName(identifier, null); Identifier physicalTableName = strategy.toPhysicalTableName(identifier, null); Assert.assertEquals(null, none); Assert.assertEquals(expectedIdentifier, physicalCatalogName); Assert.assertEquals(expectedIdentifierColumn, physicalColumnName); Assert.assertEquals(expectedIdentifier, physicalSchemaName); Assert.assertEquals(expectedIdentifier, physicalSequenceName); Assert.assertEquals(expectedIdentifier, physicalTableName); }
@Override public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { String name = identifier.getText(); if (name.startsWith("m_") || "hibernate_sequence".equals(name)) { return identifier; } name = name.substring(1); name = "m_" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name); name = RUtil.fixDBSchemaObjectNameLength(name); Identifier i = new Identifier(name, identifier.isQuoted()); LOGGER.trace("toPhysicalTableName {} -> {}", identifier, i); return i; }
/** * Converts a given {@link Identifier} to the lower case representation. If * the given context has a character limit for identifiers, this will be * respected. * * @param context * The JDBC context * @param identifier * The identifier * @param prefix * Optional prefix to use for the idenifiert. Will be ignored, if * null * @return */ protected Identifier convertToLimitedLowerCase(JdbcEnvironment context, Identifier identifier, String prefix) { String identifierText = identifier.getText(); if(prefix != null) { identifierText = prefix + identifierText; } // always convert to lowercase identifierText = identifierText.toLowerCase(); // determine the length limit based on the JDBC context Integer lengthLimit = getIdentifierLengthLimit(context); // limit length if necessary if (lengthLimit != null && identifierText.length() > lengthLimit) { identifierText = StringUtils.substring(identifierText, 0, lengthLimit); } return Identifier.toIdentifier(identifierText); }
private Identifier addUnderscores(Identifier name) { if (name == null) { return null; } StringBuilder builder = new StringBuilder(name.getText().replace('.', '_')); for (int i = 1; i < builder.length() - 1; i++) { if (isUnderscoreRequired(builder.charAt(i - 1), builder.charAt(i), builder.charAt(i + 1))) { builder.insert(i++, '_'); } } return getIdentifier(builder.toString(), name.isQuoted()); }
private Identifier convert(Identifier identifier) { if (identifier == null || StringUtils.isEmpty(identifier.getText())) { return identifier; } String newName = addUnderscores(identifier.getText()); return Identifier.toIdentifier(newName); }
/** * Lower case the text, without touching the quotes. */ private Identifier toLowerCase(final Identifier name) { if (name == null) { return null; } // to lower case return new Identifier(name.getText().toLowerCase(Locale.ENGLISH), name.isQuoted()); }
@Override public Identifier determineJoinColumnName(final ImplicitJoinColumnNameSource source) { if (source.getNature() == ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION || source.getAttributePath() == null) { return source.getReferencedTableName(); } return toIdentifier(transformAttributePath(source.getAttributePath()), source.getBuildingContext()); }
@Test public void determineUniqueKeyName() { final ImplicitUniqueKeyNameSource source = Mockito.mock(ImplicitUniqueKeyNameSource.class); mockContext(source); Mockito.when(source.getTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final List<Identifier> columnsIdentifier = new ArrayList<>(); columnsIdentifier.add(DatabaseIdentifier.toIdentifier("MyCol_umn1")); columnsIdentifier.add(DatabaseIdentifier.toIdentifier("MyCol_umn2")); Mockito.when(source.getColumnNames()).thenReturn(columnsIdentifier); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineUniqueKeyName(source); Assert.assertEquals("UK_bdj7f5p3skrieson5es1km8t9", identifier.getText()); }
@Test public void determineAnyKeyColumnName() { final ImplicitAnyKeyColumnNameSource source = Mockito.mock(ImplicitAnyKeyColumnNameSource.class); mockContext(source); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAttributePath()).thenReturn(attributePath); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineAnyKeyColumnName(source); Assert.assertEquals("my_property", identifier.getText()); }
@Test public void determineJoinColumnNameCollection() { final ImplicitJoinColumnNameSource source = Mockito.mock(ImplicitJoinColumnNameSource.class); mockContext(source); Mockito.when(source.getNature()).thenReturn(ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION); Mockito.when(source.getReferencedTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinColumnName(source); Assert.assertEquals("MyTa_ble", identifier.getText()); }
@Test public void determineJoinColumnNameNoAttribute() { final ImplicitJoinColumnNameSource source = Mockito.mock(ImplicitJoinColumnNameSource.class); mockContext(source); Mockito.when(source.getNature()).thenReturn(ImplicitJoinColumnNameSource.Nature.ENTITY); Mockito.when(source.getReferencedTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinColumnName(source); Assert.assertEquals("MyTa_ble", identifier.getText()); }
@Test public void determineJoinColumnName() { final ImplicitJoinColumnNameSource source = Mockito.mock(ImplicitJoinColumnNameSource.class); mockContext(source); Mockito.when(source.getNature()).thenReturn(ImplicitJoinColumnNameSource.Nature.ENTITY); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAttributePath()).thenReturn(attributePath); Mockito.when(source.getReferencedTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinColumnName(source); Assert.assertEquals("my_property", identifier.getText()); }
@Test public void determineJoinTableName() { final ImplicitJoinTableNameSource source = Mockito.mock(ImplicitJoinTableNameSource.class); mockContext(source); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAssociationOwningAttributePath()).thenReturn(attributePath); Mockito.when(source.getOwningPhysicalTableName()).thenReturn("Table1"); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinTableName(source); Assert.assertEquals("Table1_my_property", identifier.getText()); }
@Test public void determineForeignKeyName() { final ImplicitForeignKeyNameSource source = Mockito.mock(ImplicitForeignKeyNameSource.class); mockContext(source); Mockito.when(source.getTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final List<Identifier> columnsIdentifier = new ArrayList<>(); columnsIdentifier.add(DatabaseIdentifier.toIdentifier("MyCol_umn1")); columnsIdentifier.add(DatabaseIdentifier.toIdentifier("MyCol_umn2")); Mockito.when(source.getColumnNames()).thenReturn(columnsIdentifier); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineForeignKeyName(source); Assert.assertEquals("FK_bdj7f5p3skrieson5es1km8t9", identifier.getText()); }
@Override public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) { String columnName = StringHelper.toLowerCase( StringHelper.insertUnderscoreBetweenWords(source.getAttributePath().getProperty()) ); ImplicitBasicColumnNameSource newSource = new ImplicitBasicColumnNameSource() { @Override public AttributePath getAttributePath() { return AttributePath.parse(columnName); } @Override public boolean isCollectionElement() { return source.isCollectionElement(); } @Override public MetadataBuildingContext getBuildingContext() { return source.getBuildingContext(); } }; return super.determineBasicColumnName(newSource); }
private Identifier addUnderscores(Identifier name) { if (name != null) { return Identifier.toIdentifier(addUnderscores(abbreviateName(name.getText(), maxLength))); } else { return name; } }
private Identifier convert(Identifier identifier) { if (identifier == null || StringUtils.isBlank(identifier.getText())) { return identifier; } String regex = "([a-z])([A-Z])"; String replacement = "$1_$2"; String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase(); return Identifier.toIdentifier(newName); }
private Identifier apply(Identifier name) { if (name == null) { return null; } StringBuilder text = new StringBuilder(name.getText().replace('.', '_')); for (int i = 1; i < text.length() - 1; i++) { if (isUnderscoreRequired(text.charAt(i - 1), text.charAt(i), text.charAt(i + 1))) { text.insert(i++, '_'); } } return new Identifier(text.toString().toLowerCase(Locale.ROOT), name.isQuoted()); }
private String getSchemaName(InFlightMetadataCollector mappings) { Identifier schema = mappings.getDatabase().getDefaultNamespace().getName().getSchema(); if(schema != null) { return schema.getCanonicalName(); } return null; }
private String getCatalogName(InFlightMetadataCollector mappings) { Identifier catalog = mappings.getDatabase().getDefaultNamespace().getName().getCatalog(); if(catalog != null) { return catalog.getCanonicalName(); } return null; }
protected Identifier truncateIdentifier(Identifier name) { if (name == null) { return name; } String text = name.getText(); if (text == null || text.length() <= IDENTIFIER_MAX_LENGTH) { return name; } else { return Identifier.toIdentifier(text.substring(0, IDENTIFIER_MAX_LENGTH), name.isQuoted()); } }
@Override protected QualifiedName determineSequenceName(Properties params, Dialect dialect, JdbcEnvironment jdbcEnv) { String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); String columnName = params.getProperty(PersistentIdentifierGenerator.PK); if (tableName != null && columnName != null) { StringBuilder sequenceNameBuilder = new StringBuilder(); sequenceNameBuilder.append(tableName); sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); sequenceNameBuilder.append(columnName); sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX); // todo : need to incorporate implicit catalog and schema names final Identifier catalog = jdbcEnv.getIdentifierHelper().toIdentifier( ConfigurationHelper.getString( CATALOG, params ) ); final Identifier schema = jdbcEnv.getIdentifierHelper().toIdentifier( ConfigurationHelper.getString( SCHEMA, params ) ); return new QualifiedNameParser.NameParts( catalog, schema, jdbcEnv.getIdentifierHelper().toIdentifier( sequenceNameBuilder.toString() ) ); } throw new IllegalStateException("Unable to build the sequence name"); }
/** * Convert. * * @param identifier * the identifier * @return the identifier */ private static Identifier convert(final Identifier identifier) { if (identifier == null || StringUtils.isBlank(identifier.getText())) { return identifier; } else { return Identifier.toIdentifier(identifier.getText().replaceAll(REG_EXPR, REPLACEMENT_PATTERN).toLowerCase(Locale.ENGLISH)); } }
@Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) { Identifier original = super.toPhysicalColumnName(name, context); if(original.getText().length() > 30) { return Identifier.toIdentifier(original.getText().substring(0, 30), original.isQuoted()); } return original; }
@Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) { Identifier i = super.toPhysicalColumnName(name, context); LOGGER.trace("toPhysicalColumnName {} -> {}", name, i); return i; }
@Override public Identifier determineJoinColumnName(final ImplicitJoinColumnNameSource source) { final Identifier original = super.determineJoinColumnName(source); final String name = original.getText(); // TODO why do some columns already have _id on them?? return Identifier.toIdentifier(name.endsWith("Id") || name.endsWith("_id") ? name : name + "Id", original.isQuoted()); }
/** * Check if column {@link Identifier} equals reserved word. If true, add prefix to column name * * @param name identifier to check * @param context JDBC env * * @return Identifier */ @Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) { // call superclass and get string value Identifier columnIdentifier = super.toPhysicalColumnName(name, context); String columnIdentifierText = columnIdentifier.getText(); if (StringUtils.equalsAnyIgnoreCase(columnIdentifierText, RESERVED_WORDS_ORACLE)) { columnIdentifier = convertToLimitedLowerCase(context, columnIdentifier, columnNamePrefix); } return columnIdentifier; }
/** * @param dialect * @param className * @param expectedPhysicalTableName */ private void assertExpectedPhysicalTableName(Dialect dialect, String className, String expectedPhysicalTableName) { JdbcEnvironment context = Mockito.mock(JdbcEnvironment.class); when(context.getDialect()).thenReturn(dialect); Identifier classIdentifier = Identifier.toIdentifier(className); String actualPhysicalName = namingStrategy.toPhysicalTableName(classIdentifier, context).getText(); assertEquals(expectedPhysicalTableName, actualPhysicalName); }
/** * @param dialect * @param columnName * @param expectedPhysicalColumnName */ private void assertExpectedPhysicalColumnName(Dialect dialect, String columnName, String expectedPhysicalColumnName) { JdbcEnvironment context = Mockito.mock(JdbcEnvironment.class); when(context.getDialect()).thenReturn(dialect); Identifier classIdentifier = Identifier.toIdentifier(columnName); String actualPhysicalName = namingStrategy.toPhysicalColumnName(classIdentifier, context).getText(); assertEquals(expectedPhysicalColumnName, actualPhysicalName); }
/** * @param dialect * @param columnName * @param expectedPhysicalColumnName */ private void assertExpectedPhysicalColumnName(Dialect dialect, String columnName, String expectedPhysicalColumnName) { JdbcEnvironment context = Mockito.mock(JdbcEnvironment.class); when(context.getDialect()).thenReturn(dialect); Identifier classIdentifier = Identifier.toIdentifier(columnName); String actualPhysicalName = oracleNamingStrategyShogun2.toPhysicalColumnName(classIdentifier, context).getText(); assertEquals(expectedPhysicalColumnName, actualPhysicalName); }
@Override public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { System.out.println("<<<<<<<<<<<<<<<<<< Name: "+identifier); return identifier; }
@Override public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { System.out.println("<<<<<<<<<<<<<<<<<< Name: "+identifier); return identifier; }
@Override public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { System.out.println("<<<<<<<<<<<<<<<<<< Name: "+identifier); return identifier; }
@Override public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { System.out.println("<<<<<<<<<<<<<<<<<< Name: "+identifier); return identifier; }
@Override public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnvironment) { System.out.println("<<<<<<<<<<<<<<<<<< Name: "+identifier); return identifier; }