if (first)
first = false;
else
buf.append(", ");
DbAttribute at = it.next();
// attribute may not be fully valid, do a simple check
if (at.getType() == TypesMapping.NOT_DEFINED) {
throw new CayenneRuntimeException("Undefined type for attribute '"
+ ent.getFullyQualifiedName()
+ "."
+ at.getName()
+ "'.");
}
String[] types = externalTypesForJdbcType(at.getType());
if (types == null || types.length == 0) {
throw new CayenneRuntimeException("Undefined type for attribute '"
+ ent.getFullyQualifiedName()
+ "."
+ at.getName()
+ "': "
+ at.getType());
}
String type = types[0];
buf.append(at.getName()).append(' ').append(type);
// append size and precision (if applicable)
if (typeSupportsLength(at.getType())) {
int len = at.getMaxLength();
int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1;
// sanity check
if (scale > len) {
scale = -1;
}
if (len > 0) {
buf.append('(').append(len);
if (scale >= 0) {
buf.append(", ").append(scale);
}
buf.append(')');
}
}
if (at.isMandatory()) {
buf.append(" NOT NULL");
}
else {
buf.append(" NULL");
}
}
// primary key clause
Iterator<DbAttribute> pkit = ent.getPrimaryKeys().iterator();
if (pkit.hasNext()) {
if (first)
first = false;
else
buf.append(", ");
buf.append("PRIMARY KEY (");
boolean firstPk = true;
while (pkit.hasNext()) {
if (firstPk)
firstPk = false;
else
buf.append(", ");
DbAttribute at = pkit.next();
buf.append(at.getName());
}
buf.append(')');
}
buf.append(')');
return buf.toString();