OPENJPA-455

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@599192 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2007-11-28 23:13:50 +00:00
parent 5472692b41
commit 55720f220b
2 changed files with 62 additions and 12 deletions

View File

@ -313,6 +313,7 @@ public class DBDictionary
protected final Set systemSchemaSet = new HashSet();
protected final Set systemTableSet = new HashSet();
protected final Set fixedSizeTypeNameSet = new HashSet();
protected final Set typeModifierSet = new HashSet();
// when we store values that lose precion, track the types so that the
// first time it happens we can warn the user
@ -1596,6 +1597,14 @@ public class DBDictionary
* override this method to return the unaltered type name for columns of
* those types (or add the type names to the
* <code>fixedSizeTypeNameSet</code>).
*
* <P>Some databases support "type modifiers" for example the unsigned
* "modifier" in MySQL. In these cases the size should go between the type
* and the "modifier", instead of after the modifier. For example
* CREATE table FOO ( myint INT (10) UNSIGNED . . .) instead of
* CREATE table FOO ( myint INT UNSIGNED (10) . . .).
* Type modifiers should be added to <code>typeModifierSet</code> in
* subclasses.
*/
protected String appendSize(Column col, String typeName) {
if (fixedSizeTypeNameSet.contains(typeName.toUpperCase()))
@ -1613,12 +1622,28 @@ public class DBDictionary
size = buf.toString();
}
int idx = typeName.indexOf("{0}");
if (idx == -1 && size != null)
return typeName + size;
if (idx == -1)
return typeName;
return insertSize(typeName, size);
}
/**
* Helper method that inserts a size clause for a given SQL type.
*
* @see appendSize
*
* @param typeName The SQL type ie INT
* @param size The size clause ie (10)
* @return The typeName + size clause. Usually the size clause will
* be appended to typeName. If the typeName contains a
* marker : {0} or if typeName contains a modifier the
* size clause will be inserted appropriately.
*/
protected String insertSize(String typeName, String size) {
if(StringUtils.isEmpty(size)) {
return typeName;
}
int idx = typeName.indexOf("{0}");
if (idx != -1) {
// replace '{0}' with size
String ret = typeName.substring(0, idx);
if (size != null)
@ -1627,6 +1652,28 @@ public class DBDictionary
ret = ret + typeName.substring(idx + 3);
return ret;
}
if (! typeModifierSet.isEmpty()) {
String s;
idx = typeName.length();
int curIdx = -1;
for (Iterator i = typeModifierSet.iterator(); i.hasNext();) {
s = (String) i.next();
if (typeName.toUpperCase().contains(s)) {
curIdx = typeName.toUpperCase().indexOf(s);
if (curIdx != -1 && curIdx < idx) {
idx = curIdx;
}
}
}
if(idx != typeName.length()) {
String ret = typeName.substring(0, idx);
ret = ret + size;
ret = ret + ' ' + typeName.substring(idx);
return ret;
}
}
return typeName + size;
}
///////////
// Selects

View File

@ -107,6 +107,9 @@ public class MySQLDictionary
// MySQL requires double-escape for strings
searchStringEscape = "\\\\";
typeModifierSet.addAll(Arrays.asList(new String[] { "UNSIGNED",
"ZEROFILL" }));
}
public String[] getCreateTableSQL(Table table) {