You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2010/07/16 21:38:28 UTC
svn commit: r964921 - in /openjpa/trunk: openjpa-examples/src/main/java/
openjpa-examples/src/main/java/META-INF/
openjpa-examples/src/main/java/mapping/
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/
openjpa-jdbc/src/main/java/org/apache/open...
Author: dwoods
Date: Fri Jul 16 19:38:27 2010
New Revision: 964921
URL: http://svn.apache.org/viewvc?rev=964921&view=rev
Log:
OPENJPA-1724 Allow MappingTool to generate SQL files in a different encoding than the local JVM
Added:
openjpa/trunk/openjpa-examples/src/main/java/mapping/
openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java (with props)
openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java (with props)
openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml (with props)
Modified:
openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml
openjpa/trunk/openjpa-examples/src/main/java/build.xml
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/MappingToolTask.java
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingTool.java
openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Files.java
openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_mapping.xml
Modified: openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml (original)
+++ openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml Fri Jul 16 19:38:27 2010
@@ -25,6 +25,12 @@
We need to enumerate each persistent class first in the persistence.xml
See: http://issues.apache.org/jira/browse/OPENJPA-78
-->
+ <persistence-unit name="mapping" transaction-type="RESOURCE_LOCAL">
+ <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+ <class>mapping.BuildSchemaPC</class>
+ <class>mapping.BuildUTF8SchemaPC</class>
+ </persistence-unit>
+
<persistence-unit name="none" transaction-type="RESOURCE_LOCAL">
<mapping-file>reversemapping/orm.xml</mapping-file>
<class>hellojpa.Message</class>
Modified: openjpa/trunk/openjpa-examples/src/main/java/build.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/build.xml?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/build.xml (original)
+++ openjpa/trunk/openjpa-examples/src/main/java/build.xml Fri Jul 16 19:38:27 2010
@@ -54,6 +54,7 @@ Please traverse to a sub-directory, like
<exec dir="${basedir}/hellojpa" executable="ant${platform.script.ext}"/>
<exec dir="${basedir}/embeddables" executable="ant${platform.script.ext}"/>
<exec dir="${basedir}/relations" executable="ant${platform.script.ext}"/>
+ <exec dir="${basedir}/mapping" executable="ant${platform.script.ext}"/>
<exec dir="${basedir}/reversemapping" executable="ant${platform.script.ext}"/>
</target>
Added: openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java?rev=964921&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java Fri Jul 16 19:38:27 2010
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package mapping;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ * <p>Persistent type used in testing the mappingtool's buildSchema action
+ * with files that use ASCII column names.</p>
+ *
+ */
+@Entity
+public class BuildSchemaPC {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private int id;
+
+ @Basic
+ @Column(name = "chain", length = 50)
+ private String stringField = null;
+
+
+ public BuildSchemaPC() {}
+
+ public BuildSchemaPC(String chain)
+ {
+ stringField = chain;
+ }
+
+ public String getStringField() {
+ return stringField;
+ }
+
+ public void setStringField(String chain) {
+ stringField = chain;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+}
Propchange: openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildSchemaPC.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java?rev=964921&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java Fri Jul 16 19:38:27 2010
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package mapping;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ * <p>Persistent type used in testing the mappingtool's buildSchema action
+ * with files that use UTF-8 column names.</p>
+ *
+ */
+@Entity
+public class BuildUTF8SchemaPC {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private int id;
+
+ @Basic
+ @Column(name = "cha\u00EEne", length = 50)
+ private String stringField = null;
+
+
+ public BuildUTF8SchemaPC() {}
+
+ public BuildUTF8SchemaPC(String chain)
+ {
+ stringField = chain;
+ }
+
+ public String getStringField() {
+ return stringField;
+ }
+
+ public void setStringField(String chain) {
+ stringField = chain;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+}
Propchange: openjpa/trunk/openjpa-examples/src/main/java/mapping/BuildUTF8SchemaPC.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml?rev=964921&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml Fri Jul 16 19:38:27 2010
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project name="mapping" default="run">
+ <property name="example" value="mapping"/>
+ <import file="../build.xml"/>
+
+ <!-- when we clean, also remove all of the auto-generated files -->
+ <target name="pre-clean" description="Pre-clean phase">
+ <delete includeemptydirs="true">
+ <fileset dir="${basedir}">
+ <include name="create.sql"/>
+ <include name="createUTF8.sql"/>
+ <include name="schema.xml"/>
+ </fileset>
+ </delete>
+ </target>
+
+ <!--
+ Run the mapping tool
+ -->
+ <target name="run" depends="compile"
+ unless="mapping-schema-skip"
+ description="Generate SQL DDL files">
+
+ <taskdef name="mappingtool" classpathref="classpath"
+ classname="org.apache.openjpa.jdbc.ant.MappingToolTask"/>
+
+ <!-- This can also be performed from a cmdline as -
+ java -cp .:openjpa-all-<version>.jar:derby-10.5.3.0_1.jar
+ -Dopenjpa.ConnectionDriverName="org.apache.derby.jdbc.EmbeddedDriver"
+ -Dopenjpa.ConnectionURL="jdbc:derby:mapping-database;create=true"
+ -Dopenjpa.ConnectionUserName= -Dopenjpa.ConnectionPassword=
+ org.apache.openjpa.jdbc.meta.MappingTool BuildSchemaPC.class
+ -a buildSchema
+ -->
+ <echo message="Using MappingTool to generate the XML schema"/>
+ <mappingtool action="buildSchema"
+ schemafile="schema.xml">
+ <fileset dir="${basedir}">
+ <include name="${basedir}/**/*.class"/>
+ </fileset>
+ <config connectiondrivername="${dbdriver}" connectionurl="${dburl}"
+ connectionusername="${dbuser}" connectionpassword="${dbpass}"/>
+ </mappingtool>
+
+ <!-- This can also be performed from a cmdline as -
+ java -cp .:openjpa-all-<version>.jar:derby-10.5.3.0_1.jar
+ -Dopenjpa.ConnectionDriverName="org.apache.derby.jdbc.EmbeddedDriver"
+ -Dopenjpa.ConnectionURL="jdbc:derby:mapping-database;create=true"
+ -Dopenjpa.ConnectionUserName= -Dopenjpa.ConnectionPassword=
+ org.apache.openjpa.jdbc.meta.MappingTool BuildUTF8SchemaPC.class
+ -sa build -sql createUTF8.sql -se UTF-8
+ -->
+ <echo message="Using MappingTool to generate the UTF-8 SQL DDL"/>
+ <mappingtool schemaaction="build"
+ sqlfile="createUTF8.sql" sqlEncode="UTF-8">
+ <fileset dir="${basedir}">
+ <include name="${basedir}/**/*.class"/>
+ </fileset>
+ <config connectiondrivername="${dbdriver}" connectionurl="${dburl}"
+ connectionusername="${dbuser}" connectionpassword="${dbpass}"/>
+ </mappingtool>
+
+ <!-- This can also be performed from a cmdline as -
+ java -cp .:openjpa-all-<version>.jar:derby-10.5.3.0_1.jar
+ -Dopenjpa.ConnectionDriverName="org.apache.derby.jdbc.EmbeddedDriver"
+ -Dopenjpa.ConnectionURL="jdbc:derby:mapping-database;create=true"
+ -Dopenjpa.ConnectionUserName= -Dopenjpa.ConnectionPassword=
+ org.apache.openjpa.jdbc.meta.MappingTool BuildSchemaPC.class
+ -sa build -sql create.sql
+ -->
+ <echo message="Using MappingTool to generate the SQL DDL"/>
+ <mappingtool schemaaction="build"
+ sqlfile="create.sql">
+ <fileset dir="${basedir}">
+ <include name="${basedir}/**/*.class"/>
+ </fileset>
+ <config connectiondrivername="${dbdriver}" connectionurl="${dburl}"
+ connectionusername="${dbuser}" connectionpassword="${dbpass}"/>
+ </mappingtool>
+ </target>
+</project>
Propchange: openjpa/trunk/openjpa-examples/src/main/java/mapping/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/MappingToolTask.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/MappingToolTask.java?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/MappingToolTask.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/ant/MappingToolTask.java Fri Jul 16 19:38:27 2010
@@ -49,6 +49,7 @@ import org.apache.openjpa.util.MultiLoad
* <li><code>file</code></li>
* <li><code>schemaFile</code></li>
* <li><code>sqlFile</code></li>
+ * <li><code>sqlEncode</code></li>
* <li><code>tmpClassLoader</code></li>
* </ul> Of these arguments, only <code>action</code> is required.
*/
@@ -62,6 +63,7 @@ public class MappingToolTask
protected String file = null;
protected String schemaFile = null;
protected String sqlFile = null;
+ protected String sqlEncode = null;
protected boolean tmpClassLoader = true;
/**
@@ -163,6 +165,13 @@ public class MappingToolTask
}
/**
+ * Set the output file charset encoding we want the MappingTool to use.
+ */
+ public void setSQLEncode(String sqlEncode) {
+ this.sqlEncode = sqlEncode;
+ }
+
+ /**
* Set whether this action applies to metadata as well as mappings.
*/
public void setMeta(boolean meta) {
@@ -197,7 +206,7 @@ public class MappingToolTask
flags.mappingWriter = Files.getWriter(file, loader);
flags.schemaWriter = Files.getWriter(schemaFile, loader);
- flags.sqlWriter = Files.getWriter(sqlFile, loader);
+ flags.sqlWriter = Files.getWriter(sqlFile, loader, sqlEncode);
JDBCConfiguration conf = (JDBCConfiguration) getConfiguration();
conf.setClassResolver(resolver);
Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingTool.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingTool.java?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingTool.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingTool.java Fri Jul 16 19:38:27 2010
@@ -456,10 +456,11 @@ public class MappingTool
record(null);
}
- private void record(MappingTool.Flags flags) {
+ public void record(MappingTool.Flags flags) {
MappingRepository repos = getRepository();
MetaDataFactory io = repos.getMetaDataFactory();
ClassMapping[] mappings;
+
if (!ACTION_DROP.equals(_action))
mappings = repos.getMappings();
else if (_dropMap != null)
@@ -837,6 +838,10 @@ public class MappingTool
* <li><i>-sqlFile/-sql <stdout | output file or resource></i>: Use
* this option to write the planned schema changes as a SQL
* script rather than modifying the data store.</li>
+ * <li><i>-sqlEncode/-se <encoding></i>: Use
+ * this option with the <code>-sqlFile</code> flag to write the SQL script
+ * in a different Java character set encoding than the default JVM locale,
+ * such as <code>UTF-8</code>.</li>
* <li><i>-dropTables/-dt <true/t | false/f></i>: Corresponds to the
* same-named option in the {@link SchemaTool}.</li>
* <li><i>-dropSequences/-dsq <true/t | false/f></i>: Corresponds
@@ -972,6 +977,7 @@ public class MappingTool
String fileName = opts.removeProperty("file", "f", null);
String schemaFileName = opts.removeProperty("schemaFile", "sf", null);
String sqlFileName = opts.removeProperty("sqlFile", "sql", null);
+ String sqlEncoding = opts.removeProperty("sqlEncode", "se", null);
String schemas = opts.removeProperty("s");
if (schemas != null)
opts.setProperty("schemas", schemas);
@@ -984,7 +990,10 @@ public class MappingTool
else
flags.mappingWriter = Files.getWriter(fileName, loader);
flags.schemaWriter = Files.getWriter(schemaFileName, loader);
- flags.sqlWriter = Files.getWriter(sqlFileName, loader);
+ if (sqlEncoding != null)
+ flags.sqlWriter = Files.getWriter(sqlFileName, loader, sqlEncoding);
+ else
+ flags.sqlWriter = Files.getWriter(sqlFileName, loader);
return run(conf, args, flags, loader);
}
Modified: openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties Fri Jul 16 19:38:27 2010
@@ -276,6 +276,7 @@ tool-usage: Usage: java org.apache.openj
\t[-file/-f <stdout | output file or resource>]\n\
\t[-schemaFile/-sf <stdout | output file or resource>]\n\
\t[-sqlFile/-sql <stdout | output file or resource>]\n\
+ \t[-sqlEncode/-se <Java character set encoding like UTF-8>]\n\
\t[-schemaAction/-sa <add | retain | drop | refresh | build | none>]\n\
\t[-schemas/-s <schemas and tables>]\n\
\t[-readSchema/-rs <true/t | false/f>]\n\
@@ -417,4 +418,4 @@ unique-no-table: A unique constraint on
table "{2}" nor any of its secondary table(s) "{3}".
bad-version-column-table: One of the version column "{0}" has been associated \
with table "{1}", but no primary or secondary table of such name exists.
-version-type-unsupported: Version field "{0}" of {1} is not supported.
\ No newline at end of file
+version-type-unsupported: Version field "{0}" of {1} is not supported.
Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Files.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Files.java?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Files.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/Files.java Fri Jul 16 19:38:27 2010
@@ -20,6 +20,7 @@ package org.apache.openjpa.lib.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -27,7 +28,9 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLDecoder;
@@ -240,6 +243,33 @@ public class Files {
}
/**
+ * Return a writer to the stream(stdout or stderr) or file named by the
+ * given string set to the provided charset encoding.
+ *
+ * @see #getOutputStream
+ */
+ public static Writer getWriter(String file, ClassLoader loader, String enc)
+ throws IOException {
+ if (file == null)
+ return null;
+ if (enc == null) {
+ // call the non-encoded version of the method
+ return getWriter(file, loader);
+ }
+
+ try {
+ if ("stdout".equals(file))
+ return new PrintWriter(new OutputStreamWriter(System.out, enc));
+ else if ("stderr".equals(file))
+ return new PrintWriter(new OutputStreamWriter(System.err, enc));
+ else
+ return new BufferedWriter(new OutputStreamWriter(getOutputStream(file, loader), enc));
+ } catch (IOException ioe) {
+ throw new NestableRuntimeException(ioe);
+ }
+ }
+
+ /**
* Return an output stream to the stream(stdout or stderr) or file named
* by the given string.
*
Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_mapping.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_mapping.xml?rev=964921&r1=964920&r2=964921&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_mapping.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_mapping.xml Fri Jul 16 19:38:27 2010
@@ -122,6 +122,14 @@ mappings, even if the schema already exi
</listitem>
<listitem>
<para>
+<literal>-sqlEncode/-se <encoding></literal>: Use this option
+with <literal>-sqlFile</literal> to write the SQL script in a different
+Java character set encoding than the default JVM locale, such as
+<literal>UTF-8</literal>.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
<literal>-dropTables/-dt <true/t | false/f></literal>: Corresponds to the
same-named option on the schema tool.
</para>