diff --git a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java index e3940ca..b522ef0 100644 --- a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java @@ -173,7 +173,7 @@ public class MySQLBuilder } @Override - protected String getColumnType (FieldMarshaller fm) + protected String getColumnType (FieldMarshaller fm, int length) { if (fm instanceof ByteMarshaller) { return "TINYINT"; @@ -202,7 +202,10 @@ public class MySQLBuilder } else if (ftype.equals(Double.class)) { return "DOUBLE"; } else if (ftype.equals(String.class)) { - return "VARCHAR"; + if (length < (1 << 15)) { + return "VARCHAR(" + length + ")"; + } + return "TEXT"; } else if (ftype.equals(Date.class)) { return "DATE"; } else if (ftype.equals(Time.class)) { @@ -218,7 +221,19 @@ public class MySQLBuilder "Don't know how to create SQL for " + ftype + "."); } } 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) { return "BLOB"; } else if (fm instanceof ByteEnumMarshaller) { diff --git a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java index a1b1fbd..a102fb4 100644 --- a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java @@ -186,7 +186,7 @@ public class PostgreSQLBuilder } @Override - protected String getColumnType (FieldMarshaller fm) + protected String getColumnType (FieldMarshaller fm, int length) { if (fm instanceof ByteMarshaller) { return "SMALLINT"; @@ -215,7 +215,10 @@ public class PostgreSQLBuilder } else if (ftype.equals(Double.class)) { return "DOUBLE"; } else if (ftype.equals(String.class)) { - return "VARCHAR"; + if (length < (1 << 15)) { + return "VARCHAR(" + length + ")"; + } + return "TEXT"; } else if (ftype.equals(Date.class)) { return "DATE"; } else if (ftype.equals(Time.class)) { diff --git a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java index c3b9ecb..f6d8047 100644 --- a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java @@ -145,15 +145,10 @@ public abstract class SQLBuilder if (!typeDone) { if (type.length() == 0) { - type = getColumnType(fm); + type = getColumnType(fm, length); } 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 // 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 * the table associated with the given {@link DepotMarshaller}. This is a highly database * specific operation and must thus be implemented by each dialect subclass. - * + * * {@see Conditionals.FullTextIndex} */ public abstract boolean addFullTextSearch ( @@ -196,8 +191,9 @@ public abstract class SQLBuilder /** * Overridden by subclasses to figure the dialect-specific SQL type of the given field. + * @param length */ - protected abstract String getColumnType (FieldMarshaller fm); + protected abstract String getColumnType (FieldMarshaller fm, int length); /** The class that maps persistent classes to marshallers. */ protected DepotTypes _types;