下面列出了org.hibernate.boot.model.naming.Identifier#isQuoted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Identifier normalizeQuoting(Identifier identifier) {
log.tracef( "Normalizing identifier quoting [%s]", identifier );
if ( identifier == null ) {
return null;
}
if ( identifier.isQuoted() ) {
return identifier;
}
if ( globallyQuoteIdentifiers ) {
log.tracef( "Forcing identifier [%s] to quoted for global quoting", identifier );
return Identifier.toIdentifier( identifier.getText(), true );
}
if ( autoQuoteKeywords && isReservedWord( identifier.getText() ) ) {
log.tracef( "Forcing identifier [%s] to quoted as recognized reserved word", identifier );
return Identifier.toIdentifier( identifier.getText(), true );
}
return identifier;
}
private void bindLogicalToPhysical(Identifier logicalName, String physicalName) throws DuplicateMappingException {
final String existingPhysicalNameMapping = logicalToPhysical.put( logicalName, physicalName );
if ( existingPhysicalNameMapping != null ) {
final boolean areSame = logicalName.isQuoted()
? physicalName.equals( existingPhysicalNameMapping )
: physicalName.equalsIgnoreCase( existingPhysicalNameMapping );
if ( !areSame ) {
throw new DuplicateMappingException(
String.format(
Locale.ENGLISH,
"Table [%s] contains logical column name [%s] referring to multiple physical " +
"column names: [%s], [%s]",
tableName,
logicalName,
existingPhysicalNameMapping,
physicalName
),
DuplicateMappingException.Type.COLUMN_BINDING,
tableName + "." + logicalName
);
}
}
}
private Identifier getIdentifier(Identifier name) {
if (name == null) {
return null;
}
return new Identifier(
NamingStrategy.DEFAULT.mappedName(name.getText()),
name.isQuoted()
);
}
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
if (name.isQuoted()) {
return name;
}
return jdbcEnvironment.getIdentifierHelper().toIdentifier(name.getText(), /* quoted= */ true);
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
if (name.isQuoted()) {
return name;
}
// Convert the lowerCamelCase field name into the snake_case column name
return jdbcEnvironment
.getIdentifierHelper()
.toIdentifier(
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name.getText()),
/* quoted= */ false);
}
@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 toMetaDataText(Identifier identifier) {
if ( identifier == null ) {
throw new IllegalArgumentException( "Identifier cannot be null; bad usage" );
}
if ( identifier instanceof DatabaseIdentifier ) {
return identifier.getText();
}
if ( identifier.isQuoted() ) {
switch ( quotedCaseStrategy ) {
case UPPER: {
log.tracef( "Rendering quoted identifier [%s] in upper case for use in DatabaseMetaData", identifier );
return identifier.getText().toUpperCase( Locale.ROOT );
}
case LOWER: {
log.tracef( "Rendering quoted identifier [%s] in lower case for use in DatabaseMetaData", identifier );
return identifier.getText().toLowerCase( Locale.ROOT );
}
default: {
// default is mixed case
log.tracef( "Rendering quoted identifier [%s] in mixed case for use in DatabaseMetaData", identifier );
return identifier.getText();
}
}
}
else {
switch ( unquotedCaseStrategy ) {
case MIXED: {
log.tracef( "Rendering unquoted identifier [%s] in mixed case for use in DatabaseMetaData", identifier );
return identifier.getText();
}
case LOWER: {
log.tracef( "Rendering unquoted identifier [%s] in lower case for use in DatabaseMetaData", identifier );
return identifier.getText().toLowerCase( Locale.ROOT );
}
default: {
// default is upper case
log.tracef( "Rendering unquoted identifier [%s] in upper case for use in DatabaseMetaData", identifier );
return identifier.getText().toUpperCase( Locale.ROOT );
}
}
}
}
/**
* 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 )
);
}
/**
* 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 )
);
}
/**
* 自定义 entity 名称与 table 名称的映射关系
* @param name
* @param context
* @return
*/
@Override
public Identifier toPhysicalTableName(Identifier name,JdbcEnvironment context) {
//数据库小写表名
return new Identifier(name.getText().toLowerCase(), name.isQuoted());
}