Implemented the ColumnTyper interface for our various builders.

This commit is contained in:
Michael Bayne
2010-06-19 17:41:10 +00:00
parent 5587d134bd
commit 45ec7cf77c
3 changed files with 128 additions and 161 deletions
@@ -246,62 +246,51 @@ public class HSQLBuilder
@Override @Override
protected <T> String getColumnType (FieldMarshaller<?> fm, int length) protected <T> String getColumnType (FieldMarshaller<?> fm, int length)
{ {
if (fm instanceof FieldMarshaller.ByteMarshaller) { return fm.getColumnType(TYPER, length);
return "TINYINT";
} else if (fm instanceof FieldMarshaller.ShortMarshaller) {
return "SMALLINT";
} else if (fm instanceof FieldMarshaller.IntMarshaller) {
return "INTEGER";
} else if (fm instanceof FieldMarshaller.LongMarshaller) {
return "BIGINT";
} else if (fm instanceof FieldMarshaller.FloatMarshaller) {
return "REAL";
} else if (fm instanceof FieldMarshaller.DoubleMarshaller) {
return "DOUBLE PRECISION";
} else if (fm instanceof FieldMarshaller.ObjectMarshaller) {
Class<?> ftype = fm.getField().getType();
if (ftype.equals(Byte.class)) {
return "TINYINT";
} else if (ftype.equals(Short.class)) {
return "SMALLINT";
} else if (ftype.equals(Integer.class)) {
return "INTEGER";
} else if (ftype.equals(Long.class)) {
return "BIGINT";
} else if (ftype.equals(Float.class)) {
return "FLOAT";
} else if (ftype.equals(Double.class)) {
return "DOUBLE PRECISION";
} else if (ftype.equals(String.class)) {
return "VARCHAR(" + length + ")";
} else if (ftype.equals(Date.class)) {
return "DATE";
} else if (ftype.equals(Time.class)) {
return "TIME";
} else if (ftype.equals(Timestamp.class)) {
return "TIMESTAMP";
} else if (ftype.equals(Blob.class)) {
return "VARBINARY";
} else if (ftype.equals(Clob.class)) {
return "VARCHAR";
} else {
throw new IllegalArgumentException(
"Don't know how to create SQL for " + ftype + ".");
}
} else if (fm instanceof FieldMarshaller.ByteArrayMarshaller) {
return "VARBINARY";
} else if (fm instanceof FieldMarshaller.IntArrayMarshaller) {
return "VARBINARY";
} else if (fm instanceof FieldMarshaller.ByteEnumMarshaller<?>) {
return "TINYINT";
} else if (fm instanceof FieldMarshaller.BooleanMarshaller) {
return "BOOLEAN";
} else {
throw new IllegalArgumentException("Unknown field marshaller type: " + fm.getClass());
}
} }
/** Holds the Full Text Seach condition between build and bind phases. */ /** Holds the Full Text Seach condition between build and bind phases. */
protected SQLExpression _ftsCondition; protected SQLExpression _ftsCondition;
}
protected static final FieldMarshaller.ColumnTyper TYPER = new FieldMarshaller.ColumnTyper() {
public String getBooleanType (int length) {
return "BOOLEAN";
}
public String getByteType (int length) {
return "TINYINT";
}
public String getShortType (int length) {
return "SMALLINT";
}
public String getIntType (int length) {
return "INTEGER";
}
public String getLongType (int length) {
return "BIGINT";
}
public String getFloatType (int length) {
return "REAL";
}
public String getDoubleType (int length) {
return "DOUBLE PRECISION";
}
public String getStringType (int length) {
return "VARCHAR(" + length + ")";
}
public String getDateType (int length) {
return "DATE";
}
public String getTimeType (int length) {
return "TIME";
}
public String getTimestampType (int length) {
return "TIMESTAMP";
}
public String getBlobType (int length) {
return "VARBINARY";
}
public String getClobType (int length) {
return "VARCHAR";
}
};
}
@@ -268,56 +268,44 @@ public class MySQLBuilder
@Override @Override
protected <T> String getColumnType (FieldMarshaller<?> fm, int length) protected <T> String getColumnType (FieldMarshaller<?> fm, int length)
{ {
if (fm instanceof ByteMarshaller) { return fm.getColumnType(TYPER, length);
}
protected static final FieldMarshaller.ColumnTyper TYPER = new FieldMarshaller.ColumnTyper() {
public String getBooleanType (int length) {
return "TINYINT"; return "TINYINT";
} else if (fm instanceof ShortMarshaller) { }
public String getByteType (int length) {
return "TINYINT";
}
public String getShortType (int length) {
return "SMALLINT"; return "SMALLINT";
} else if (fm instanceof IntMarshaller) { }
public String getIntType (int length) {
return "INTEGER"; return "INTEGER";
} else if (fm instanceof LongMarshaller) { }
public String getLongType (int length) {
return "BIGINT"; return "BIGINT";
} else if (fm instanceof FloatMarshaller) { }
public String getFloatType (int length) {
return "FLOAT"; return "FLOAT";
} else if (fm instanceof DoubleMarshaller) { }
public String getDoubleType (int length) {
return "DOUBLE"; return "DOUBLE";
} else if (fm instanceof ObjectMarshaller) { }
Class<?> ftype = fm.getField().getType(); public String getStringType (int length) {
if (ftype.equals(Byte.class)) { return (length < (1 << 15)) ? "VARCHAR(" + length + ")" : "TEXT";
return "SMALLINT"; }
} else if (ftype.equals(Short.class)) { public String getDateType (int length) {
return "SMALLINT"; return "DATE";
} else if (ftype.equals(Integer.class)) { }
return "INTEGER"; public String getTimeType (int length) {
} else if (ftype.equals(Long.class)) { return "DATETIME";
return "BIGINT"; }
} else if (ftype.equals(Float.class)) { public String getTimestampType (int length) {
return "FLOAT"; return "DATETIME";
} else if (ftype.equals(Double.class)) { }
return "DOUBLE"; public String getBlobType (int length) {
} else if (ftype.equals(String.class)) {
if (length < (1 << 15)) {
return "VARCHAR(" + length + ")";
}
return "TEXT";
} else if (ftype.equals(Date.class)) {
return "DATE";
} else if (ftype.equals(Time.class)) {
return "DATETIME";
} else if (ftype.equals(Timestamp.class)) {
return "DATETIME";
} else if (ftype.equals(Blob.class)) {
return "BYTEA";
} else if (ftype.equals(Clob.class)) {
return "TEXT";
} else {
throw new IllegalArgumentException(
"Don't know how to create SQL for " + ftype + ".");
}
} else if (fm instanceof ByteArrayMarshaller ||
fm instanceof IntArrayMarshaller) {
if (fm instanceof IntArrayMarshaller) {
length *= 4;
}
// semi-arbitrarily use VARBINARY() up to 32767 // semi-arbitrarily use VARBINARY() up to 32767
if (length < (1 << 15)) { if (length < (1 << 15)) {
return "VARBINARY(" + length + ")"; return "VARBINARY(" + length + ")";
@@ -330,12 +318,9 @@ public class MySQLBuilder
return "MEDIUMBLOB"; return "MEDIUMBLOB";
} }
return "LONGBLOB"; return "LONGBLOB";
} else if (fm instanceof ByteEnumMarshaller<?>) {
return "TINYINT";
} else if (fm instanceof BooleanMarshaller) {
return "TINYINT";
} else {
throw new IllegalArgumentException("Unknown field marshaller type: " + fm.getClass());
} }
} public String getClobType (int length) {
return "TEXT";
}
};
} }
@@ -263,62 +263,7 @@ public class PostgreSQLBuilder
@Override @Override
protected <T> String getColumnType (FieldMarshaller<?> fm, int length) protected <T> String getColumnType (FieldMarshaller<?> fm, int length)
{ {
if (fm instanceof FieldMarshaller.ByteMarshaller) { return fm.getColumnType(TYPER, length);
return "SMALLINT";
} else if (fm instanceof FieldMarshaller.ShortMarshaller) {
return "SMALLINT";
} else if (fm instanceof FieldMarshaller.IntMarshaller) {
return "INTEGER";
} else if (fm instanceof FieldMarshaller.LongMarshaller) {
return "BIGINT";
} else if (fm instanceof FieldMarshaller.FloatMarshaller) {
return "REAL";
} else if (fm instanceof FieldMarshaller.DoubleMarshaller) {
return "DOUBLE PRECISION";
} else if (fm instanceof FieldMarshaller.ObjectMarshaller) {
Class<?> ftype = fm.getField().getType();
if (ftype.equals(Byte.class)) {
return "SMALLINT";
} else if (ftype.equals(Short.class)) {
return "SMALLINT";
} else if (ftype.equals(Integer.class)) {
return "INTEGER";
} else if (ftype.equals(Long.class)) {
return "BIGINT";
} else if (ftype.equals(Float.class)) {
return "FLOAT";
} else if (ftype.equals(Double.class)) {
return "DOUBLE";
} else if (ftype.equals(String.class)) {
if (length < (1 << 15)) {
return "VARCHAR(" + length + ")";
}
return "TEXT";
} else if (ftype.equals(Date.class)) {
return "DATE";
} else if (ftype.equals(Time.class)) {
return "TIME";
} else if (ftype.equals(Timestamp.class)) {
return "TIMESTAMP";
} else if (ftype.equals(Blob.class)) {
return "BYTEA";
} else if (ftype.equals(Clob.class)) {
return "TEXT";
} else {
throw new IllegalArgumentException(
"Don't know how to create SQL for " + ftype + ".");
}
} else if (fm instanceof FieldMarshaller.ByteArrayMarshaller) {
return "BYTEA";
} else if (fm instanceof FieldMarshaller.IntArrayMarshaller) {
return "BYTEA";
} else if (fm instanceof FieldMarshaller.ByteEnumMarshaller<?>) {
return "SMALLINT";
} else if (fm instanceof FieldMarshaller.BooleanMarshaller) {
return "BOOLEAN";
} else {
throw new IllegalArgumentException("Unknown field marshaller type: " + fm.getClass());
}
} }
protected static String massageFTQuery (FullText match) protected static String massageFTQuery (FullText match)
@@ -354,4 +299,52 @@ public class PostgreSQLBuilder
"Unknown full text configuration: " + configuration); "Unknown full text configuration: " + configuration);
} }
} }
protected static final FieldMarshaller.ColumnTyper TYPER = new FieldMarshaller.ColumnTyper() {
public String getBooleanType (int length) {
return "BOOLEAN";
}
public String getByteType (int length) {
return "SMALLINT";
}
public String getShortType (int length) {
return "SMALLINT";
}
public String getIntType (int length) {
return "INTEGER";
}
public String getLongType (int length) {
return "BIGINT";
}
public String getFloatType (int length) {
return "REAL";
}
public String getDoubleType (int length) {
return "DOUBLE PRECISION";
}
public String getByteArrayType (int length) {
return "BYTEA";
}
public String getIntArrayType (int length) {
return "BYTEA";
}
public String getStringType (int length) {
return "VARCHAR(" + length + ")";
}
public String getDateType (int length) {
return "DATE";
}
public String getTimeType (int length) {
return "TIME";
}
public String getTimestampType (int length) {
return "TIMESTAMP";
}
public String getBlobType (int length) {
return "BYTEA";
}
public String getClobType (int length) {
return "TEXT";
}
};
} }