Java Code Examples for org.hibernate.boot.model.naming.Identifier#toIdentifier()

The following examples show how to use org.hibernate.boot.model.naming.Identifier#toIdentifier() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardTableExporter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String[] getSqlDropStrings(Table table, Metadata metadata) {
	StringBuilder buf = new StringBuilder( "drop table " );
	if ( dialect.supportsIfExistsBeforeTableName() ) {
		buf.append( "if exists " );
	}

	final QualifiedName tableName = new QualifiedNameParser.NameParts(
			Identifier.toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
			Identifier.toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
			table.getNameIdentifier()
	);
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
	buf.append( jdbcEnvironment.getQualifiedObjectNameFormatter().format( tableName, jdbcEnvironment.getDialect() ) )
			.append( dialect.getCascadeConstraintsString() );

	if ( dialect.supportsIfExistsAfterTableName() ) {
		buf.append( " if exists" );
	}

	return new String[] { buf.toString() };
}
 
Example 2
Source File: RobeHibernateNamingStrategyTest.java    From robe with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: CamelCaseToSnakeCaseNamingStrategy.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
private Identifier formatIdentifier(Identifier identifier) {
    if (identifier != null) {
        String name = identifier.getText();

        String formattedName = name.replaceAll(CAMEL_CASE_REGEX, SNAKE_CASE_PATTERN).toLowerCase();

        return !formattedName.equals(name) ?
                Identifier.toIdentifier(formattedName, identifier.isQuoted()) :
                identifier;
    } else {
        return null;
    }

}
 
Example 4
Source File: CustomPhysicalNamingStrategy.java    From tutorials with MIT License 5 votes vote down vote up
private Identifier convertToSnakeCase(final Identifier identifier) {
    if (identifier == null) {
        return identifier;
    }

    final String regex = "([a-z])([A-Z])";
    final String replacement = "$1_$2";
    final String newName = identifier.getText()
        .replaceAll(regex, replacement)
        .toLowerCase();
    return Identifier.toIdentifier(newName);
}
 
Example 5
Source File: OracleNamingStrategy.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 6
Source File: CamelCaseToSnakeCaseNamingStrategy.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
private Identifier formatIdentifier(Identifier identifier) {
    String name = identifier.getText();

    String formattedName = name.replaceAll("([a-z]+)([A-Z]+)", "$1\\_$2").toLowerCase();

    return !formattedName.equals(name) ?
            Identifier.toIdentifier(formattedName, identifier.isQuoted()) :
            identifier;
}
 
Example 7
Source File: PhysicalNamingStrategySnakeCaseImpl.java    From wallride with Apache License 2.0 5 votes vote down vote up
private Identifier convert(Identifier identifier) {
	if (identifier == null) {
		return identifier;
	}

	String regex = "([a-z])([A-Z])";
	String replacement = "$1_$2";
	String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase();
	return Identifier.toIdentifier(newName, identifier.isQuoted());
}
 
Example 8
Source File: PrefixPhysicalNamingStrategy.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    return Identifier.toIdentifier("SEQ_" + name.getText());
}
 
Example 9
Source File: Index.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setName(String name) {
	this.name = Identifier.toIdentifier( name );
}
 
Example 10
Source File: Table.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setName(String name) {
	this.name = Identifier.toIdentifier( name );
}
 
Example 11
Source File: PrefixPhysicalNamingStrategy.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    return name == null ? null : Identifier.toIdentifier("SCH_" + name.getText());
}
 
Example 12
Source File: Table.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setCatalog(String catalog) {
	this.catalog = Identifier.toIdentifier( catalog );
}
 
Example 13
Source File: PrefixPhysicalNamingStrategy.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    return name == null ? null : Identifier.toIdentifier("CTL_" + name.getText());
}
 
Example 14
Source File: DefaultPersistManager.java    From onedev with MIT License 4 votes vote down vote up
private String getVersionColumnName() {
	JdbcEnvironment environment = serviceRegistry.getService(JdbcEnvironment.class);
	Identifier identifier = Identifier.toIdentifier(getVersionFieldName());
	return physicalNamingStrategy.toPhysicalColumnName(identifier, environment).getText();
}
 
Example 15
Source File: DefaultPersistManager.java    From onedev with MIT License 4 votes vote down vote up
private String getVersionTableName() {
	JdbcEnvironment environment = serviceRegistry.getService(JdbcEnvironment.class);
	Identifier identifier = Identifier.toIdentifier(ModelVersion.class.getSimpleName());
	return physicalNamingStrategy.toPhysicalTableName(identifier, environment).getText();
}
 
Example 16
Source File: NormalizingIdentifierHelperImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Identifier applyGlobalQuoting(String text) {
	return Identifier.toIdentifier( text, globallyQuoteIdentifiers && !globallyQuoteIdentifiersSkipColumnDefinitions );
}
 
Example 17
Source File: QualifiedNameParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a textual representation of a qualified name into a NameParts
 * representation.  Explicitly looks for the form {@code catalog.schema.name}.
 *
 * @param text The simple text representation of the qualified name.
 *
 * @return The wrapped QualifiedName
 */
public NameParts parse(String text, Identifier defaultCatalog, Identifier defaultSchema) {
	if ( text == null ) {
		throw new IllegalIdentifierException( "Object name to parse must be specified, but found null" );
	}

	String catalogName = null;
	String schemaName = null;
	String name;

	boolean catalogWasQuoted = false;
	boolean schemaWasQuoted = false;
	boolean nameWasQuoted;

	// Note that we try to handle both forms of quoting,
	//		1) where the entire string was quoted
	//		2) where  one or more individual parts were quoted

	boolean wasQuotedInEntirety = text.startsWith( "`" ) && text.endsWith( "`" );
	if ( wasQuotedInEntirety ) {
		text = unquote( text );
	}

	final String[] tokens = text.split( "\\." );
	if ( tokens.length == 0 || tokens.length == 1 ) {
		// we have just a local name...
		name = text;
	}
	else if ( tokens.length == 2 ) {
		schemaName = tokens[0];
		name = tokens[1];
	}
	else if ( tokens.length == 3 ) {
		schemaName = tokens[0];
		catalogName = tokens[1];
		name = tokens[2];
	}
	else {
		throw new HibernateException( "Unable to parse object name: " + text );
	}

	nameWasQuoted = Identifier.isQuoted( name );
	if ( nameWasQuoted ) {
		name = unquote( name );
	}

	if ( schemaName != null ) {
		schemaWasQuoted = Identifier.isQuoted( schemaName );
		if ( schemaWasQuoted ) {
			schemaName = unquote( schemaName );
		}
	}
	else if ( defaultSchema != null ) {
		schemaName = defaultSchema.getText();
		schemaWasQuoted = defaultSchema.isQuoted();
	}

	if ( catalogName != null ) {
		catalogWasQuoted = Identifier.isQuoted( catalogName );
		if ( catalogWasQuoted ) {
			catalogName = unquote( catalogName );
		}
	}
	else if ( defaultCatalog != null ) {
		catalogName = defaultCatalog.getText();
		catalogWasQuoted = defaultCatalog.isQuoted();
	}

	return new NameParts(
			Identifier.toIdentifier( catalogName, wasQuotedInEntirety||catalogWasQuoted ),
			Identifier.toIdentifier( schemaName, wasQuotedInEntirety||schemaWasQuoted ),
			Identifier.toIdentifier( name, wasQuotedInEntirety||nameWasQuoted )
	);
}
 
Example 18
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void secondPassCompileForeignKeys(
		final Table table,
		Set<ForeignKey> done,
		final MetadataBuildingContext buildingContext) throws MappingException {
	table.createForeignKeys();

	Iterator itr = table.getForeignKeyIterator();
	while ( itr.hasNext() ) {
		final ForeignKey fk = (ForeignKey) itr.next();
		if ( !done.contains( fk ) ) {
			done.add( fk );
			final String referencedEntityName = fk.getReferencedEntityName();
			if ( referencedEntityName == null ) {
				throw new MappingException(
						"An association from the table " +
								fk.getTable().getName() +
								" does not specify the referenced entity"
				);
			}

			log.debugf( "Resolving reference to class: %s", referencedEntityName );
			final PersistentClass referencedClass = getEntityBinding( referencedEntityName );
			if ( referencedClass == null ) {
				throw new MappingException(
						"An association from the table " +
								fk.getTable().getName() +
								" refers to an unmapped class: " +
								referencedEntityName
				);
			}
			if ( referencedClass.isJoinedSubclass() ) {
				secondPassCompileForeignKeys( referencedClass.getSuperclass().getTable(), done, buildingContext );
			}

			fk.setReferencedTable( referencedClass.getTable() );

			Identifier nameIdentifier;

			ImplicitForeignKeyNameSource foreignKeyNameSource = new ImplicitForeignKeyNameSource() {
				final List<Identifier> columnNames = extractColumnNames( fk.getColumns() );
				List<Identifier> referencedColumnNames = null;

				@Override
				public Identifier getTableName() {
					return table.getNameIdentifier();
				}

				@Override
				public List<Identifier> getColumnNames() {
					return columnNames;
				}

				@Override
				public Identifier getReferencedTableName() {
					return fk.getReferencedTable().getNameIdentifier();
				}

				@Override
				public List<Identifier> getReferencedColumnNames() {
					if ( referencedColumnNames == null ) {
						referencedColumnNames = extractColumnNames( fk.getReferencedColumns() );
					}
					return referencedColumnNames;
				}

				@Override
				public Identifier getUserProvidedIdentifier() {
					return fk.getName() != null ? Identifier.toIdentifier( fk.getName() ) : null;
				}

				@Override
				public MetadataBuildingContext getBuildingContext() {
					return buildingContext;
				}
			};

			nameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineForeignKeyName(foreignKeyNameSource);

			fk.setName( nameIdentifier.render( getDatabase().getJdbcEnvironment().getDialect() ) );

			fk.alignColumns();
		}
	}
}
 
Example 19
Source File: RobeHibernateNamingStrategy.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Identifier convert(Identifier identifier) {
    if (identifier == null || StringUtils.isBlank(identifier.getText())) {
        return identifier;
    }
    return Identifier.toIdentifier(transformToPluralForm(identifier.getText()));
}
 
Example 20
Source File: MyPhysicalNamingStrategy.java    From Exam-Online with Apache License 2.0 4 votes vote down vote up
private Identifier convert(Identifier identifier) {
    if (identifier == null || StringUtils.isBlank(identifier.getText())) {
        return identifier;
    }
    return Identifier.toIdentifier(convert(identifier.getText()));
}