Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/Development/DBWS/NamingConventionTransformer
DBWS Schema Naming Convention Transformers
Document History
Date | Author | Version Description & Notes |
---|---|---|
080911 | Mike Norman | 1.0 |
DBWS utility enhancement: Naming convention of generated schema types
bug 234677
The DBWSBuilder utility should allow the user to alter the names of the generated schema types.
The algorithm currently in use is very simple:
- table name ==> to_lowercase ==> add suffix 'Type' ==> translate any characters un-supported by XML1 ==> top-level element type in .xsd file
- column name ==> to_lowercase ==> translate characters ==> element tag name2
1 - same algorithm documented as part of the SQL/X(2003) specification (a.k.a. ISO/IEC 9075-14:2003 Part 14)
2 - All columns expressed as XML elements
Proposed new Interface org.eclipse.persistence.tools.dbws.NamingConventionTransformer
The proposed fix for bug 234677 is the introduction of a new Interface class:
package org.eclipse.persistence.tools.dbws; public interface NamingConventionTransformer { public enum ElementStyle { ELEMENT, ATTRIBUTE, NONE }; public String generateSchemaName(String tableName); public String generateElementAlias(String originalElementName); public ElementStyle styleForElement(String originalElementName); } public class DefaultNamingConventionTransformer implements NamingConventionTransformer { // user-provided transformer must extended this class . . . }
The algorithm above will be converted to a series of transformers (operating as a Chain-of-Responsibility) that cooperate together to transform the database table's metadata
(table name, table columns) into the schema's top-level types.
- generateTableAlias
- allows one to alter the auto-generated Schema name
- API passes in the name of the table so that it is available for parsing (e.g. remove prefix/suffix, convert to upper/lower/CamelCase, etc), but the NamingConventionTransformer implementation does not have to use it.
- generateElementAlias
- allows one to alter the auto-generated XML schema element names
- API passes in the name of the table's column so that it is available for parsing, but the NamingConventionTransformer implementation does not have to use it
- ElementStyle styleForElement
- allows one to change the XML schema style for a given table column - transform to an XML element (default), an attribute or neither (which means that the specified table column will not be mapped)
eclipselink-dbwsutils.jar | +---META-INF | | MANIFEST.MF | | | \---services | org.eclipse.persistence.tools.dbws.NamingConventionTransformer | \---org \---eclipse \---persistence \---tools \---dbws | BindingModel.class | ... \---jdbc ... JDBCHelper.class
In the org.eclipse.persistence.tools.dbws.NamingConventionTransformer services file
- org.eclipse.persistence.tools.dbws.ToLowerTransformer
- org.eclipse.persistence.tools.dbws.TypeSuffixTransformer
- org.eclipse.persistence.tools.dbws.SQLX2003Transformer
User-Provided NamingConventionTransformer
A user may provide their own NamingConventionTransformer by sub-classing org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer. For example, for the table
CREATE TABLE IF NOT EXISTS SIMPLE_TABLE ( ID NUMERIC NOT NULL, NAME VARCHAR(25), SINCE DATE, PRIMARY KEY (ID) )
The following NamingConventionTransformer will:
- make the schema element simple
- make the element names lowercase
- have the ID field represented as a schema-attribute, and
- skip the SINCE field
import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ELEMENT; import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.NONE; public class MyNamingConventionTransformer extends DefaultNamingConventionTransformer { @Override public String generateSchemaAlias(String tableName) { return super.generateSchemaAlias("simple"); } @Override public String generateElementAlias(String originalElementName) { return super.generateElementAlias(originalElementName.toLowerCase()); } @Override public ElementStyle styleForElement(String elementName) { if ("id".equalsIgnoreCase(elementName)) { return ATTRIBUTE; } else if ("since".equalsIgnoreCase(elementName)) { return NONE; } return ELEMENT; } }
The user must package their user-provided NamingConventionTransformer in a .jar-file and identify it as a service by