tfischer 2005/02/23 09:32:09 Modified: src/generator/src/dtd database.dtd src/generator/src/test/org/apache/torque/engine/database/model NameFactoryTest.java src/generator/src/java/org/apache/torque/engine/database/model NameGenerator.java JavaNameGenerator.java xdocs changes.xml schema-howto.xml Log: Added support for specifying database schema names at generate time, i.e. tablenames like "my_schema.my_table" can now be used in schema.xml To do this, the java naming methods "underscore" and "javaname" in the generator were changed such that, in addition to removing underscores, they remove dots as well. Also, a new java naming method called underscoreOmitSchema was introduced, which works like the underscore method but omits everything before the last dot. As an example, the tablename "my_schema.my_TABLE" would produce the java name "MySchemaMyTable" with the method "underscore", "MySchemaMyTABLE" with the method "javaName", and "MyTable" with the method UnderscoreOmitSchema. I did not know how to proceed with the "nochange" method, it will produce unvalid java names if schemata are used (java name "my_schema.my_TABLE"). Also some docs were added in the schema-howto. The changes were tested for oracle, I will do tests for mysql and postgresql as well. Revision Changes Path 1.5 +11 -6 db-torque/src/generator/src/dtd/database.dtd Index: database.dtd =================================================================== RCS file: /home/cvs/db-torque/src/generator/src/dtd/database.dtd,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- database.dtd 25 Nov 2003 16:57:44 -0000 1.4 +++ database.dtd 23 Feb 2005 17:32:09 -0000 1.5 @@ -23,10 +23,15 @@ Java class or method name. nochange - indicates not change is performed. - underscore - Underscores are removed, First letter is + underscore - Underscores and dots are removed, first letter is capitalized, first letter after an underscore - is capitalized, the rest of the letters are - converted to lowercase. + is capitalized, first letter after a dot is capitalized, + the rest of the letters are converted to lowercase. + underscoreOmitSchema - The section of the name before and including + the last dot in the name is removed. For the remaining part, + underscores are removed, first letter is capitalized, + first letter after an underscore is capitalized, + the rest of the letters are converted to lowercase. javaname - same as underscore, but no letters are converted to lowercase. --> @@ -39,7 +44,7 @@ package CDATA #IMPLIED baseClass CDATA #IMPLIED basePeer CDATA #IMPLIED - defaultJavaNamingMethod (nochange|underscore|javaname) "underscore" + defaultJavaNamingMethod (nochange|underscore|underscoreOmitSchema|javaname) "underscore" heavyIndexing (true|false) "false" > @@ -82,7 +87,7 @@ basePeer CDATA #IMPLIED alias CDATA #IMPLIED interface CDATA #IMPLIED - javaNamingMethod (nochange|underscore|javaname) #IMPLIED + javaNamingMethod (nochange|underscore|underscoreOmitSchema|javaname) #IMPLIED heavyIndexing (true|false) #IMPLIED description CDATA #IMPLIED > 1.6 +26 -5 db-torque/src/generator/src/test/org/apache/torque/engine/database/model/NameFactoryTest.java Index: NameFactoryTest.java =================================================================== RCS file: /home/cvs/db-torque/src/generator/src/test/org/apache/torque/engine/database/model/NameFactoryTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- NameFactoryTest.java 22 Feb 2004 06:29:38 -0000 1.5 +++ NameFactoryTest.java 23 Feb 2005 17:32:09 -0000 1.6 @@ -68,9 +68,22 @@ } }, { { - "MY_USER", NameGenerator.CONV_METHOD_UNDERSCORE }, { - "MY_USER", NameGenerator.CONV_METHOD_JAVANAME }, { - "MY_USER", NameGenerator.CONV_METHOD_NOCHANGE } + "MY_USER", + NameGenerator.CONV_METHOD_UNDERSCORE }, { + "MY_USER", + NameGenerator.CONV_METHOD_UNDERSCORE_OMIT_SCHEMA }, { + "MY_USER", + NameGenerator.CONV_METHOD_JAVANAME }, { + "MY_USER", + NameGenerator.CONV_METHOD_NOCHANGE }, { + "MY_SCHEMA.MY_USER", + NameGenerator.CONV_METHOD_UNDERSCORE }, { + "MY_SCHEMA.MY_USER", + NameGenerator.CONV_METHOD_UNDERSCORE_OMIT_SCHEMA }, { + "MY_SCHEMA.MY_USER", + NameGenerator.CONV_METHOD_JAVANAME } , { + "MY_SCHEMA.MY_USER", + NameGenerator.CONV_METHOD_NOCHANGE } } }; @@ -86,7 +99,15 @@ makeString(4) + "_FK_1", makeString(5) + "_FK_2" }, { - "MyUser", "MYUSER", "MY_USER" } + "MyUser", + "MyUser", + "MYUSER", + "MY_USER", + "MySchemaMyUser", + "MyUser", + "MYSCHEMAMYUSER", + "MY_SCHEMA.MY_USER" + } }; /** 1.3 +14 -1 db-torque/src/generator/src/java/org/apache/torque/engine/database/model/NameGenerator.java Index: NameGenerator.java =================================================================== RCS file: /home/cvs/db-torque/src/generator/src/java/org/apache/torque/engine/database/model/NameGenerator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- NameGenerator.java 22 Feb 2004 06:27:19 -0000 1.2 +++ NameGenerator.java 23 Feb 2005 17:32:09 -0000 1.3 @@ -36,6 +36,11 @@ char STD_SEPARATOR_CHAR = '_'; /** + * The character which separates the schema name from the table name + */ + char SCHEMA_SEPARATOR_CHAR = '.'; + + /** * Traditional method for converting schema table and column names * to java names. The <code>CONV_METHOD_XXX</code> constants * define how names for columns and tables in the database schema @@ -46,6 +51,14 @@ String CONV_METHOD_UNDERSCORE = "underscore"; /** + * Similar to [EMAIL PROTECTED] #CONV_METHOD_UNDERSCORE} except a possible + * schema name (preceding a dot (.) )is omitted + * + * @see JavaNameGenerator#underscoreOmitSchemaMethod(String) + */ + String CONV_METHOD_UNDERSCORE_OMIT_SCHEMA = "underscoreOmitSchema"; + + /** * Similar to [EMAIL PROTECTED] #CONV_METHOD_UNDERSCORE} except nothing is * converted to lowercase. * 1.4 +67 -3 db-torque/src/generator/src/java/org/apache/torque/engine/database/model/JavaNameGenerator.java Index: JavaNameGenerator.java =================================================================== RCS file: /home/cvs/db-torque/src/generator/src/java/org/apache/torque/engine/database/model/JavaNameGenerator.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JavaNameGenerator.java 22 Feb 2004 06:27:19 -0000 1.3 +++ JavaNameGenerator.java 23 Feb 2005 17:32:09 -0000 1.4 @@ -57,6 +57,10 @@ { javaName = underscoreMethod(schemaName); } + else if (CONV_METHOD_UNDERSCORE_OMIT_SCHEMA.equals(method)) + { + javaName = underscoreOmitSchemaMethod(schemaName); + } else if (CONV_METHOD_JAVANAME.equals(method)) { javaName = javanameMethod(schemaName); @@ -77,8 +81,9 @@ /** * Converts a database schema name to java object name. Removes - * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of - * name and each letter after the <code>STD_SEPERATOR</code>, + * <code>STD_SEPARATOR_CHAR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>, + * capitilizes first letter of name and each letter after the + * <code>STD_SEPERATOR</code> and <code>SCHEMA_SEPARATOR_CHAR</code>, * converts the rest of the letters to lowercase. * * @param schemaName name to be converted. @@ -89,6 +94,52 @@ protected String underscoreMethod(String schemaName) { StringBuffer name = new StringBuffer(); + + // remove the STD_SEPARATOR_CHARs and capitalize + // the tokens + StringTokenizer tok = new StringTokenizer + (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); + while (tok.hasMoreTokens()) + { + String namePart = ((String) tok.nextElement()).toLowerCase(); + name.append(StringUtils.capitalize(namePart)); + } + + // remove the SCHEMA_SEPARATOR_CHARs and capitalize + // the tokens + schemaName = name.toString(); + name = new StringBuffer(); + tok = new StringTokenizer + (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); + while (tok.hasMoreTokens()) + { + String namePart = (String) tok.nextElement(); + name.append(StringUtils.capitalize(namePart)); + } + return name.toString(); + } + + /** + * Converts a database schema name to java object name. + * First, it removes all characters before the last occurence of + * .<code>SCHEMA_SEPARATOR_CHAR</code>. Then, in a second step, removes + * <code>STD_SEPARATOR_CHAR</code>, capitilizes first letter of + * name and each letter after the <code>STD_SEPERATOR</code>, + * and converts the rest of the letters to lowercase. + * + * @param schemaName name to be converted. + * @return converted name. + * @see org.apache.torque.engine.database.model.NameGenerator + * @see #underscoreOmitSchemaMethod(String) + */ + protected String underscoreOmitSchemaMethod(String schemaName) + { + // take only part after last dot + int lastDotPos = schemaName.lastIndexOf(SCHEMA_SEPARATOR_CHAR); + if (lastDotPos != -1) { + schemaName = schemaName.substring(lastDotPos + 1); + } + StringBuffer name = new StringBuffer(); StringTokenizer tok = new StringTokenizer (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); while (tok.hasMoreTokens()) @@ -119,6 +170,19 @@ String namePart = (String) tok.nextElement(); name.append(StringUtils.capitalize(namePart)); } + + // remove the SCHEMA_SEPARATOR_CHARs and capitalize + // the tokens + schemaName = name.toString(); + name = new StringBuffer(); + + tok = new StringTokenizer + (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); + while (tok.hasMoreTokens()) + { + String namePart = (String) tok.nextElement(); + name.append(StringUtils.capitalize(namePart)); + } return name.toString(); } 1.145 +4 -0 db-torque/xdocs/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/db-torque/xdocs/changes.xml,v retrieving revision 1.144 retrieving revision 1.145 diff -u -r1.144 -r1.145 --- changes.xml 18 Feb 2005 10:06:22 -0000 1.144 +++ changes.xml 23 Feb 2005 17:32:09 -0000 1.145 @@ -28,6 +28,10 @@ <body> <release version="3.2-dev" date="in CVS"> + <action type="add" dev="tfischer"> + Added support for schema support at generate time. + See <a href="schema-howto.html">Schema Support Howto</a>. + </action> <action type="remove" dev="tfischer" issue="TRQS222"> Removed Oracle Update patch again. Creates invalid scripts and causes ant to fail. 1.3 +63 -4 db-torque/xdocs/schema-howto.xml Index: schema-howto.xml =================================================================== RCS file: /home/cvs/db-torque/xdocs/schema-howto.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- schema-howto.xml 31 Jan 2005 19:43:56 -0000 1.2 +++ schema-howto.xml 23 Feb 2005 17:32:09 -0000 1.3 @@ -20,14 +20,15 @@ <p> Torque has some rudimentary support for Database schemas when -accessing a database. The current generator can not create schema -specific SQL files for you, all the table names will be unqualified -(i.e. end up in the 'public' schema). +accessing a database. Schema names can either be specified when +generating classes via the generator, or they can be specified +in the runtime. Note that specifying explicit schema names both at +generate time and at runtime is not tested and most likely causes problems. </p> </section> -<section name="Configuring Schema Names"> +<section name="Configuring Schema Names at Runtime"> <p> Schema support happens "per-DataSourceFactory". This might @@ -87,5 +88,63 @@ </section> + <section name="Using Schema Names at generate time"> + + <p> + To define the schema of a table in the schema.xml, use the + fully qualified table name as name attribute in the <table> + element of your schema.xml. + For example, to use the schema "bookstore" for the table "book", + use the following table definition: + </p> + + <source><![CDATA[ +... +<table name="bookstore.book" description="Book table"> +... +]]> + </source> + + <p> + If the standard naming method is used, the resulting + java class will be named BookstoreBook. If you want to omit + the schema name in the java name + (i.e. the resulting java class should be named "Book"), + you can either use the javaName attribute of the table definition: + </p> + + <source><![CDATA[ +... +<table name="bookstore.book" javaName="Book" description="Book table"> +... +]]> + </source> + + <p> + or you can use the attribute + defaultJavaNamingMethod="underscoreOmitSchema" + in the database definition: + </p> + + <source><![CDATA[ +... +<database name="bookstore" defaultJavaNamingMethod="underscoreOmitSchema"> +... +]]> + </source> + + <p> + Note that the defaultJavaNamingMethod attribute of a table + will only affect the column names in the table and cannot + be used to change the name of the table itself. + </p> + + <p> + If you use a sequence to autogenerate ids, the sequence will be generated in the same schema + as the table. + </p> + + </section> + </body> </document>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]