From 315cc2d1555925da6f62b808976792676ec2bcea Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Thu, 24 Jul 2008 16:05:10 +0000 Subject: [PATCH] OPENJPA-664: Check for length of schema component name during generation of SQL DDL generation. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@679442 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/DBDictionary.java | 33 +++++++++++++++---- .../openjpa/jdbc/sql/DerbyDictionary.java | 6 ++-- .../openjpa/jdbc/sql/localizer.properties | 11 +++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 62069d9c2..92b1ed7c9 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -3062,7 +3062,9 @@ public class DBDictionary */ 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 @@ public class DBDictionary 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 @@ public class DBDictionary 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 @@ public class DBDictionary */ 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 @@ public class DBDictionary 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 class DBDictionary 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; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java index 8453fe020..7a26c6934 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java @@ -46,9 +46,11 @@ public class DerbyDictionary stringLengthFunction = "LENGTH({0})"; substringFunctionName = "SUBSTR"; - maxConstraintNameLength = 18; + // Derby name length restriction has been relaxed + // http://www.archivum.info/derby-dev@db.apache.org/2004-12/msg00270.html + maxConstraintNameLength = 128; maxIndexNameLength = 128; - maxColumnNameLength = 30; + maxColumnNameLength = 128; maxTableNameLength = 128; useGetBytesForBlobs = true; diff --git a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties index 3b5bb6a49..6b6a103ec 100644 --- a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties +++ b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/sql/localizer.properties @@ -175,3 +175,14 @@ function-not-supported: The database dictionary in use ("{0}") \ 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