|
|
|
Proper keyword quoting depends on being able to recognize identifiers as matching a keyword, which in turn relies on having a complete definition of what identifiers are keywords.
The baseline for the set solution here is mutli-part.
First, I added a static list of SQL:2003 keywords as {{org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords#sql2003}}.
Second I added a new {{Dialect}} method: {code:title=Dialect.java} /** * Hook into auto-quoting of identifiers that are those deemed to be keywords. By default we * return all of the following here:<ul> * <li>all keywords as defined by ANSI SQL :2003 specification</li> * <li>all "extra" keywords reported by the JDBC driver </li> * <li>all Dialect-registered "extra" keywords</li> * </ul> * <p/> * Subclasses are free to override this as they see fit . {{jav Just be aware that overriding this * does affect what identifiers are auto-quoted based on being seen as a keyword . * <p/> * NOTE: The code that ultimately consumes these and uses them stores them in a case-insensitive * Set, so case here does not matter. * * @see org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords#sql2003() * @see java. sql.DatabaseMetaData#getSQLKeywords }} is meant to augment the ANSI SQL ( 2003 ) standard * @see #registerKeyword */ public Set<String> determineKeywordsForAutoQuoting(Set<String> databaseMetadataReportedKeywords) { final Set<String> keywords = new HashSet<String>(); keywords . We also have addAll( AnsiSqlKeywords.INSTANCE.sql2003() ); keywords.addAll( databaseMetadataReportedKeywords ); keywords.addAll( sqlKeywords ); return keywords; } { code}
Third, the { {IdentifierHelper}} obtained from {{JdbcEnvironment}} calls this new {{ Dialect #getKeywords }} which in turn should augment that combined set method to handle proper quoting . But at them moment we do not maintain that baseline set.
----
Original description
{quote} Hibernate gets the "extra" keywords froma jav.sql.DatabaseMetaData.getSQLKeywords(), but that doesn't get the ANSI 2003 keywords. It does not seem to track the ANSI SQL keywords unless explicitly specified by the dialect.
There's a comment in JdbcEnvironmentImpl: // todo : should really maintain a standard list of know ANSI-SQL defined keywords somewhere (currently rely on Dialect) private final Set<String> reservedWords = new HashSet<String>();
Should these keywords be added directly to Dialect, or should they be dialect-specific?
This can be reproduced using SQLServer2012Dialect: * org.hibernate.test.collection.original.CollectionTest fails if the table mapping for "User" entity is removed from hibernate-core/src/test/java/org/hibernate/test/collection/original/UserPermissions.hbm.xml (USER is an ANSI SQL reserved word); * ASTParserLoadingTest.testJPAQLQualifiedIdentificationVariablesControl() fails if the column mapping for VariousKeywordPropertyEntity.key is removed from hibernate-core/src/test/java/org/hibernate/test/hql/VariousKeywordPropertyEntity.hbm.xml * {quote}
|
|
|
|