From Zell: select text and binary column types based on the @Column(length)

annotation.
This commit is contained in:
Michael Bayne
2007-08-08 23:47:50 +00:00
parent c7150cabdc
commit e21fbd9df1
3 changed files with 27 additions and 13 deletions
@@ -173,7 +173,7 @@ public class MySQLBuilder
} }
@Override @Override
protected <T> String getColumnType (FieldMarshaller fm) protected <T> String getColumnType (FieldMarshaller fm, int length)
{ {
if (fm instanceof ByteMarshaller) { if (fm instanceof ByteMarshaller) {
return "TINYINT"; return "TINYINT";
@@ -202,7 +202,10 @@ public class MySQLBuilder
} else if (ftype.equals(Double.class)) { } else if (ftype.equals(Double.class)) {
return "DOUBLE"; return "DOUBLE";
} else if (ftype.equals(String.class)) { } else if (ftype.equals(String.class)) {
return "VARCHAR"; if (length < (1 << 15)) {
return "VARCHAR(" + length + ")";
}
return "TEXT";
} else if (ftype.equals(Date.class)) { } else if (ftype.equals(Date.class)) {
return "DATE"; return "DATE";
} else if (ftype.equals(Time.class)) { } else if (ftype.equals(Time.class)) {
@@ -218,7 +221,19 @@ public class MySQLBuilder
"Don't know how to create SQL for " + ftype + "."); "Don't know how to create SQL for " + ftype + ".");
} }
} else if (fm instanceof ByteArrayMarshaller) { } else if (fm instanceof ByteArrayMarshaller) {
return "VARBINARY"; // semi-arbitrarily use VARBINARY() up to 32767
if (length < (1 << 15)) {
return "VARBINARY(" + length + ")";
}
// use BLOB to 65535
if (length < (1 << 16)) {
return "BLOB";
}
if (length < (1 << 24)) {
return "MEDIUMBLOB";
}
return "LONGBLOB";
} else if (fm instanceof IntArrayMarshaller) { } else if (fm instanceof IntArrayMarshaller) {
return "BLOB"; return "BLOB";
} else if (fm instanceof ByteEnumMarshaller) { } else if (fm instanceof ByteEnumMarshaller) {
@@ -186,7 +186,7 @@ public class PostgreSQLBuilder
} }
@Override @Override
protected <T> String getColumnType (FieldMarshaller fm) protected <T> String getColumnType (FieldMarshaller fm, int length)
{ {
if (fm instanceof ByteMarshaller) { if (fm instanceof ByteMarshaller) {
return "SMALLINT"; return "SMALLINT";
@@ -215,7 +215,10 @@ public class PostgreSQLBuilder
} else if (ftype.equals(Double.class)) { } else if (ftype.equals(Double.class)) {
return "DOUBLE"; return "DOUBLE";
} else if (ftype.equals(String.class)) { } else if (ftype.equals(String.class)) {
return "VARCHAR"; if (length < (1 << 15)) {
return "VARCHAR(" + length + ")";
}
return "TEXT";
} else if (ftype.equals(Date.class)) { } else if (ftype.equals(Date.class)) {
return "DATE"; return "DATE";
} else if (ftype.equals(Time.class)) { } else if (ftype.equals(Time.class)) {
@@ -145,15 +145,10 @@ public abstract class SQLBuilder
if (!typeDone) { if (!typeDone) {
if (type.length() == 0) { if (type.length() == 0) {
type = getColumnType(fm); type = getColumnType(fm, length);
} }
builder.append(" ").append(type); builder.append(" ").append(type);
// if this is a VARCHAR field, add the length
if (type.equals("VARCHAR") || type.equals("VARBINARY")) {
builder.append("(").append(length).append(")");
}
// TODO: handle precision and scale // TODO: handle precision and scale
// handle nullability and uniqueness // handle nullability and uniqueness
@@ -177,7 +172,7 @@ public abstract class SQLBuilder
* Add full-text search capabilities, as defined by the provided {@link FullTextIndex}, on * Add full-text search capabilities, as defined by the provided {@link FullTextIndex}, on
* the table associated with the given {@link DepotMarshaller}. This is a highly database * the table associated with the given {@link DepotMarshaller}. This is a highly database
* specific operation and must thus be implemented by each dialect subclass. * specific operation and must thus be implemented by each dialect subclass.
* *
* {@see Conditionals.FullTextIndex} * {@see Conditionals.FullTextIndex}
*/ */
public abstract <T extends PersistentRecord> boolean addFullTextSearch ( public abstract <T extends PersistentRecord> boolean addFullTextSearch (
@@ -196,8 +191,9 @@ public abstract class SQLBuilder
/** /**
* Overridden by subclasses to figure the dialect-specific SQL type of the given field. * Overridden by subclasses to figure the dialect-specific SQL type of the given field.
* @param length
*/ */
protected abstract <T> String getColumnType (FieldMarshaller fm); protected abstract <T> String getColumnType (FieldMarshaller fm, int length);
/** The class that maps persistent classes to marshallers. */ /** The class that maps persistent classes to marshallers. */
protected DepotTypes _types; protected DepotTypes _types;