mirror of https://github.com/apache/openjpa.git
OPENJPA-1725: Ignore schema when checking the length of a table name
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1101243 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
26aa776627
commit
c159730005
|
@ -178,7 +178,7 @@ public class Table
|
|||
/**
|
||||
* Set the name of the table. This method can only be called on tables
|
||||
* that are not part of a schema.
|
||||
* @deprecated
|
||||
* @deprecated (use setIdentifier(DBIdentifier) instead.
|
||||
*/
|
||||
public void setName(String name) {
|
||||
setIdentifier(DBIdentifier.newTable(name));
|
||||
|
|
|
@ -3317,8 +3317,7 @@ public class DBDictionary
|
|||
*/
|
||||
public String[] getCreateTableSQL(Table table) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
String tableName = checkNameLength(getFullName(table, false),
|
||||
maxTableNameLength, "long-table-name");
|
||||
String tableName = checkNameLength(getFullIdentifier(table, false), maxTableNameLength, "long-table-name");
|
||||
buf.append("CREATE TABLE ").append(tableName);
|
||||
if (supportsComments && table.hasComment()) {
|
||||
buf.append(" ");
|
||||
|
@ -5258,9 +5257,48 @@ public class DBDictionary
|
|||
* 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));
|
||||
if (name.length() > length) {
|
||||
throw new UserException(_loc.get(msgKey, name, name.length(), length));
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the given name is not longer than given maximum length. Uses the unqualified name
|
||||
* from the supplied {@link DBIdentifier} by default..
|
||||
*
|
||||
* @param identifer The database identifier to check.
|
||||
* @param length Max length for this type of identifier
|
||||
* @param msgKey message identifier for the exception.
|
||||
* @param qualified If true the qualified name of the DBIdentifier will be used.
|
||||
*
|
||||
* @throws @{link UserException} with the given message key if the given name is indeed longer.
|
||||
* @return the same name.
|
||||
*/
|
||||
final String checkNameLength(DBIdentifier identifier, int length, String msgKey) {
|
||||
return checkNameLength(identifier, length, msgKey, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the given name is not longer than given maximum length. Conditionally uses the unqualified name
|
||||
* from the supplied {@link DBIdentifier}.
|
||||
*
|
||||
* @param identifer The database identifier to check.
|
||||
* @param length Max length for this type of identifier
|
||||
* @param msgKey message identifier for the exception.
|
||||
* @param qualified If true the qualified name of the DBIdentifier will be used.
|
||||
*
|
||||
* @throws @{link UserException} with the given message key if the given name is indeed longer.
|
||||
* @return the same name.
|
||||
*/
|
||||
final String checkNameLength(DBIdentifier identifier, int length, String msgKey, boolean qualified) {
|
||||
// always return the input name,
|
||||
String name = toDBName(identifier);
|
||||
String compareName = qualified ? name : toDBName(identifier.getUnqualifiedName());
|
||||
|
||||
if (compareName.length() > length) {
|
||||
throw new UserException(_loc.get(msgKey, name, name.length(), length));
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* 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 org.apache.openjpa.jdbc.sql;
|
||||
|
||||
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
||||
import org.apache.openjpa.jdbc.identifier.DBIdentifier;
|
||||
import org.apache.openjpa.jdbc.identifier.DBIdentifierUtilImpl;
|
||||
import org.apache.openjpa.jdbc.schema.Table;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.integration.junit3.MockObjectTestCase;
|
||||
|
||||
public class TestDBDictionaryGeneratedSQL extends MockObjectTestCase {
|
||||
|
||||
public void testCreateTableLongNameException() {
|
||||
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
|
||||
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
|
||||
|
||||
checking(new Expectations() {
|
||||
{
|
||||
allowing(mockConfiguration).getIdentifierUtilInstance();
|
||||
will(returnValue(idImpl));
|
||||
|
||||
allowing(mockConfiguration);
|
||||
}
|
||||
});
|
||||
|
||||
DBDictionary dict = new DBDictionary();
|
||||
dict.setConfiguration(mockConfiguration);
|
||||
dict.maxTableNameLength = 10;
|
||||
|
||||
Table table = new Table();
|
||||
table.setIdentifier(DBIdentifier.newTable("NameIsTooLong"));
|
||||
|
||||
try {
|
||||
dict.getCreateTableSQL(table);
|
||||
fail("Expected a UserException");
|
||||
} catch (UserException ue) {
|
||||
// expected - check message incase a different UserException is thrown.
|
||||
assertTrue(ue.getMessage().contains("Table name \"NameIsTooLong\""));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testThrowsExceptionWithSchemaSet() {
|
||||
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
|
||||
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
|
||||
|
||||
checking(new Expectations() {
|
||||
{
|
||||
allowing(mockConfiguration).getIdentifierUtilInstance();
|
||||
will(returnValue(idImpl));
|
||||
|
||||
allowing(mockConfiguration);
|
||||
}
|
||||
});
|
||||
|
||||
DBDictionary dict = new DBDictionary();
|
||||
dict.setConfiguration(mockConfiguration);
|
||||
dict.maxTableNameLength = 10;
|
||||
|
||||
Table table = new Table();
|
||||
table.setIdentifier(DBIdentifier.newTable("NameIsTooLong"));
|
||||
table.setSchemaIdentifier(DBIdentifier.newSchema("IAmASchema"));
|
||||
|
||||
try {
|
||||
dict.getCreateTableSQL(table);
|
||||
fail("Expected a UserException");
|
||||
} catch (UserException ue) {
|
||||
// expected - check message incase a different UserException is thrown.
|
||||
assertTrue(ue.getMessage().contains("Table name \"IAmASchema.NameIsTooLong\""));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSchemaNameIsNotConsidered() {
|
||||
final JDBCConfiguration mockConfiguration = mock(JDBCConfiguration.class);
|
||||
final DBIdentifierUtilImpl idImpl = new DBIdentifierUtilImpl();
|
||||
|
||||
checking(new Expectations() {
|
||||
{
|
||||
allowing(mockConfiguration).getIdentifierUtilInstance();
|
||||
will(returnValue(idImpl));
|
||||
|
||||
allowing(mockConfiguration);
|
||||
}
|
||||
});
|
||||
|
||||
DBDictionary dict = new DBDictionary();
|
||||
dict.setConfiguration(mockConfiguration);
|
||||
dict.maxTableNameLength = 12;
|
||||
|
||||
Table table = new Table();
|
||||
table.setIdentifier(DBIdentifier.newTable("NameIsRight"));
|
||||
table.setSchemaIdentifier(DBIdentifier.newSchema("IAmASchema"));
|
||||
|
||||
String[] sqls = dict.getCreateTableSQL(table);
|
||||
assertEquals(1, sqls.length);
|
||||
assertTrue(sqls[0].contains("NameIsRight"));
|
||||
assertTrue(sqls[0].contains("IAmASchema"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue