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; }
@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); }
/** * * Determines the identifier length limit for the given JDBC context. * Returns null if no limitation is necessary. * * @param context * The current JDBC context * @return The identifier length limit for the given context. null * otherwise. */ protected Integer getIdentifierLengthLimit(JdbcEnvironment context) { // https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/dialect/package-summary.html String dialectName = context.getDialect().getClass().getSimpleName(); if (dialectName.startsWith("Oracle")) { // identifier limit of 30 chars --> // http://stackoverflow.com/a/756569 return LENGTH_LIMIT_ORACLE; } else if (context.getDialect() instanceof Shogun2OracleDialect) { // identifier limit of 30 chars --> return LENGTH_LIMIT_ORACLE; } else if (dialectName.startsWith("PostgreSQL")) { // identifier limit of 63 chars --> // http://stackoverflow.com/a/8218026 return LENGTH_LIMIT_POSTGRESQL; } // H2 has no limit --> http://stackoverflow.com/a/30477403 return null; }
@Override protected QualifiedName determineSequenceName(final Properties params, final Dialect dialect, final JdbcEnvironment jdbcEnv) { // Make sure sequence are lower case and corresponds to table name params.put(SEQUENCE_PARAM, StringHelper.unquote(params.getProperty("identity_tables")) + ConfigurationHelper.getString(CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, DEF_SEQUENCE_SUFFIX)); return super.determineSequenceName(params, dialect, jdbcEnv); }
private OptimizedSequenceStyleGenerator newStyleGenerator() { return new OptimizedSequenceStyleGenerator() { @Override protected DatabaseStructure buildDatabaseStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize) { return Mockito.mock(DatabaseStructure.class); } }; }
private ServiceRegistry newServiceRegistry() { JdbcEnvironment jdbcEnvironment = newJdbcEnvironment(); ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class); Mockito.when(serviceRegistry.getService(JdbcEnvironment.class)).thenReturn(jdbcEnvironment); Mockito.when(jdbcEnvironment.getDialect()).thenReturn(new MySQL55Dialect()); return serviceRegistry; }
@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"); }
@Override public void configure( Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class); final Dialect dialect = jdbcEnvironment.getDialect(); final ConfigurationService configurationService = serviceRegistry.getService(ConfigurationService.class); String globalEntityIdentifierPrefix = configurationService.getSetting( "entity.identifier.prefix", String.class, "SEQ_" ); sequencePrefix = ConfigurationHelper.getString( SEQUENCE_PREFIX, params, globalEntityIdentifierPrefix); final String sequencePerEntitySuffix = ConfigurationHelper.getString( SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX); final String defaultSequenceName = ConfigurationHelper.getBoolean( SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false) ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix : SequenceStyleGenerator.DEF_SEQUENCE_NAME; sequenceCallSyntax = dialect.getSequenceNextValString( ConfigurationHelper.getString( SequenceStyleGenerator.SEQUENCE_PARAM, params, defaultSequenceName)); }
@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; }
/** * 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; }
@Override public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) { return addUnderscores(name); }
@Override public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) { return addUnderscores(name); }
@Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) { return addUnderscores(name); }
@Override public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) { return addUnderscores(name); }
@Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) { return addUnderscores(name); }
@Override public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnv) { return convert(identifier); }
@Override public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnv) { return convert(identifier); }
@Override public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnv) { return convert(identifier); }
@Override public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnv) { return convert(identifier); }
@Override public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnv) { return convert(identifier); }
@Override public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) { return new Identifier(prefix+name.getText(), name.isQuoted()); }
@Override public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) { return new Identifier(prefix+name.getText(), name.isQuoted()); }
@Override public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) { return new Identifier(prefix+name.getText(), name.isQuoted()); }
private String getVersionTableName() { JdbcEnvironment environment = serviceRegistry.getService(JdbcEnvironment.class); Identifier identifier = Identifier.toIdentifier(ModelVersion.class.getSimpleName()); return physicalNamingStrategy.toPhysicalTableName(identifier, environment).getText(); }
private String getVersionColumnName() { JdbcEnvironment environment = serviceRegistry.getService(JdbcEnvironment.class); Identifier identifier = Identifier.toIdentifier(getVersionFieldName()); return physicalNamingStrategy.toPhysicalColumnName(identifier, environment).getText(); }
@Override public Identifier toPhysicalCatalogName(final Identifier name, final JdbcEnvironment context) { return toLowerCase(name); }
@Override public Identifier toPhysicalSchemaName(final Identifier name, final JdbcEnvironment context) { return toLowerCase(name); }