svn commit: r679442 - in /openjpa/trunk/openjpa-jdbc/src/main: java/org/apache/openjpa/jdbc/sql/DBDictionary.java java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java resources/org/apache/openjpa/jdbc/sql/localizer.properties

1 message Options
Embed this post
Permalink
Pinaki Poddar

svn commit: r679442 - in /openjpa/trunk/openjpa-jdbc/src/main: java/org/apache/openjpa/jdbc/sql/DBDictionary.java java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java resources/org/apache/openjpa/jdbc/sql/localizer.properties

Reply Threaded More More options
Print post
Permalink
Author: ppoddar
Date: Thu Jul 24 09:05:10 2008
New Revision: 679442

URL: http://svn.apache.org/viewvc?rev=679442&view=rev
Log:
OPENJPA-664: Check for length of schema component name during generation of SQL DDL generation.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=679442&r1=679441&r2=679442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Thu Jul 24 09:05:10 2008
@@ -3062,7 +3062,9 @@
      */
     public String[] getCreateTableSQL(Table table) {
         StringBuffer buf = new StringBuffer();
-        buf.append("CREATE TABLE ").append(getFullName(table, false));
+        String tableName = checkNameLength(getFullName(table, false),
+         maxTableNameLength, "long-table-name");
+        buf.append("CREATE TABLE ").append(tableName);
         if (supportsComments && table.hasComment()) {
             buf.append(" ");
             comment(buf, table.getComment());
@@ -3135,7 +3137,9 @@
 
         StringBuffer buf = new StringBuffer();
         buf.append("CREATE SEQUENCE ");
-        buf.append(getFullName(seq));
+        String seqName = checkNameLength(getFullName(seq), maxTableNameLength,
+         "long-seq-name");
+        buf.append(seqName);
         if (seq.getInitialValue() != 0)
             buf.append(" START WITH ").append(seq.getInitialValue());
         if (seq.getIncrement() > 1)
@@ -3162,8 +3166,9 @@
         if (index.isUnique())
             buf.append("UNIQUE ");
         buf.append("INDEX ").append(index.getName());
-
-        buf.append(" ON ").append(getFullName(index.getTable(), false));
+        String indexName = checkNameLength(getFullName(index.getTable(), false),
+         maxIndexNameLength, "long-index-name");
+        buf.append(" ON ").append(indexName);
         buf.append(" (").append(Strings.join(index.getColumns(), ", ")).
             append(")");
 
@@ -3272,7 +3277,9 @@
      */
     protected String getDeclareColumnSQL(Column col, boolean alter) {
         StringBuffer buf = new StringBuffer();
-        buf.append(col).append(" ");
+        String columnName = checkNameLength(col.getName(), maxColumnNameLength,
+         "long-column-name");
+        buf.append(columnName).append(" ");
         buf.append(getTypeName(col));
 
         // can't add constraints to a column we're adding after table
@@ -3459,11 +3466,11 @@
         if (!supportsUniqueConstraints
             || (unq.isDeferred() && !supportsDeferredUniqueConstraints()))
             return null;
-
         StringBuffer buf = new StringBuffer();
         if (unq.getName() != null
             && CONS_NAME_BEFORE.equals(constraintNameMode))
-            buf.append("CONSTRAINT ").append(unq.getName()).append(" ");
+            buf.append("CONSTRAINT ").append(checkNameLength(unq.getName(),
+             maxConstraintNameLength, "long-constraint-name")).append(" ");
         buf.append("UNIQUE ");
         if (unq.getName() != null && CONS_NAME_MID.equals(constraintNameMode))
             buf.append(unq.getName()).append(" ");
@@ -4536,4 +4543,16 @@
     public void deleteStream(JDBCStore store, Select sel) throws SQLException {
         // Do nothing
     }
+    
+    /**
+     * Validate that the given name is no longer than given maximum length.
+     * If the given name is indeed longer then raises an UserException with the
+     * given message key otherwise returns the same name.
+     */
+    final String checkNameLength(String name, int length, String msgKey) {
+     if (name.length() > length)
+     throw new UserException(_loc.get(msgKey, name, name.length(),
+     length));
+     return name;
+    }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java?rev=679442&r1=679441&r2=679442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java Thu Jul 24 09:05:10 2008
@@ -46,9 +46,11 @@
         stringLengthFunction = "LENGTH({0})";
         substringFunctionName = "SUBSTR";
 
-        maxConstraintNameLength = 18;
+        // Derby name length restriction has been relaxed
+        // http://www.archivum.info/derby-dev@.../2004-12/msg00270.html
+        maxConstraintNameLength = 128;
         maxIndexNameLength = 128;
-        maxColumnNameLength = 30;
+        maxColumnNameLength = 128;
         maxTableNameLength = 128;
 
         useGetBytesForBlobs = true;

Modified: openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties?rev=679442&r1=679441&r2=679442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties Thu Jul 24 09:05:10 2008
@@ -175,3 +175,14 @@
 batch-update-success-count: ExecuteBatch command returns update success count {0}
 connection-defaults: Initial connection autoCommit: {0}, holdability: {1}, \
     TransactionIsolation: {2}
+long-table-name: Table name "{0}" is {1}-character long. The database allows \
+ maximum {2}-character for a table name.
+long-column-name: Column name "{0}" is {1}-character long. The database allows \
+ maximum {2}-character for a column name.
+long-index-name: Index name "{0}" is {1}-character long. The database allows \
+ maximum {2}-character for an index name.
+long-constraint-name: Constraint name "{0}" is {1}-character long. The \
+ database allows maximum {2}-character for a constraint name.
+long-seq-name: Sequence name "{0}" is {1}-character long. The database allows \
+ maximum {2}-character for a sequence name.
+
\ No newline at end of file


Pinaki