Refactor SQL type generation to allow SqlDialect to override type names

In many cases we don't need to replace the entire SqlTypeInfo object,
or even want to subclass it, when all that needs to change is what
SQL text is used for the name of the column type.  For example, its
a "BLOB" in H2 but "BYTEA" in PostgreSQL for the byte[] column type.

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/com/google/gwtorm/schema/RelationModel.java b/src/com/google/gwtorm/schema/RelationModel.java
index 1d8a004..ac0ba62 100644
--- a/src/com/google/gwtorm/schema/RelationModel.java
+++ b/src/com/google/gwtorm/schema/RelationModel.java
@@ -208,7 +208,7 @@
       final ColumnModel col = i.next();
       r.append(col.getColumnName());
       r.append(" ");
-      r.append(dialect.getSqlTypeInfo(col).getSqlType(col));
+      r.append(dialect.getSqlTypeInfo(col).getSqlType(col, dialect));
       if (i.hasNext()) {
         r.append(",");
       }
diff --git a/src/com/google/gwtorm/schema/sql/DialectPostgreSQL.java b/src/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
index eed004b..27f8a2c 100644
--- a/src/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
+++ b/src/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
@@ -1,7 +1,13 @@
 package com.google.gwtorm.schema.sql;
 
+import java.sql.Types;
+
 /** Dialect for <a href="http://www.postgresql.org/>PostgreSQL</a> */
 public class DialectPostgreSQL extends SqlDialect {
+  public DialectPostgreSQL() {
+    typeNames.put(Types.VARBINARY, "BYTEA");
+  }
+
   @Override
   public String getNextSequenceValueSql(final String seqname) {
     return "SELECT nextval('" + seqname + "')";
diff --git a/src/com/google/gwtorm/schema/sql/SqlBooleanTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlBooleanTypeInfo.java
index fae2f1b..d3c3b70 100644
--- a/src/com/google/gwtorm/schema/sql/SqlBooleanTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlBooleanTypeInfo.java
@@ -25,7 +25,7 @@
 
 public class SqlBooleanTypeInfo extends SqlTypeInfo {
   @Override
-  public String getSqlType(final ColumnModel column) {
+  public String getSqlType(final ColumnModel column, final SqlDialect dialect) {
     final String name = column.getColumnName();
     final String t = getTrueLiteralValue();
     final String f = getFalseLiteralValue();
diff --git a/src/com/google/gwtorm/schema/sql/SqlByteArrayTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlByteArrayTypeInfo.java
index 9d09100..b7f0758 100644
--- a/src/com/google/gwtorm/schema/sql/SqlByteArrayTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlByteArrayTypeInfo.java
@@ -42,10 +42,10 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel col) {
+  public String getSqlType(final ColumnModel col, final SqlDialect dialect) {
     final Column column = col.getColumnAnnotation();
     final StringBuilder r = new StringBuilder();
-    r.append("BLOB");
+    r.append(dialect.getSqlTypeName(getSqlTypeConstant()));
     if (column.notNull()) {
       r.append(" DEFAULT ''");
       r.append(" NOT NULL");
diff --git a/src/com/google/gwtorm/schema/sql/SqlCharTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlCharTypeInfo.java
index c73596c..6564218 100644
--- a/src/com/google/gwtorm/schema/sql/SqlCharTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlCharTypeInfo.java
@@ -24,7 +24,7 @@
 
 public class SqlCharTypeInfo extends SqlTypeInfo {
   @Override
-  public String getSqlType(final ColumnModel column) {
+  public String getSqlType(final ColumnModel column, final SqlDialect dialect) {
     final StringBuilder r = new StringBuilder();
     r.append("CHAR(1)");
     r.append(" DEFAULT ' '");
diff --git a/src/com/google/gwtorm/schema/sql/SqlDateTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlDateTypeInfo.java
index 256ea44..0ed0258 100644
--- a/src/com/google/gwtorm/schema/sql/SqlDateTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlDateTypeInfo.java
@@ -31,10 +31,10 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel col) {
+  public String getSqlType(final ColumnModel col, final SqlDialect dialect) {
     final Column column = col.getColumnAnnotation();
     final StringBuilder r = new StringBuilder();
-    r.append("DATE");
+    r.append(dialect.getSqlTypeName(getSqlTypeConstant()));
     if (column.notNull()) {
       r.append(" DEFAULT '1900-01-01'");
       r.append(" NOT NULL");
diff --git a/src/com/google/gwtorm/schema/sql/SqlDialect.java b/src/com/google/gwtorm/schema/sql/SqlDialect.java
index 325e9c2..82aa4b2 100644
--- a/src/com/google/gwtorm/schema/sql/SqlDialect.java
+++ b/src/com/google/gwtorm/schema/sql/SqlDialect.java
@@ -18,11 +18,13 @@
 import com.google.gwtorm.schema.ColumnModel;
 import com.google.gwtorm.schema.SequenceModel;
 
+import java.sql.Types;
 import java.util.HashMap;
 import java.util.Map;
 
 public abstract class SqlDialect {
   protected final Map<Class<?>, SqlTypeInfo> types;
+  protected final Map<Integer, String> typeNames;
 
   protected SqlDialect() {
     types = new HashMap<Class<?>, SqlTypeInfo>();
@@ -35,6 +37,20 @@
     types.put(java.sql.Date.class, new SqlDateTypeInfo());
     types.put(java.sql.Timestamp.class, new SqlTimestampTypeInfo());
     types.put(byte[].class, new SqlByteArrayTypeInfo());
+
+    typeNames = new HashMap<Integer, String>();
+    typeNames.put(Types.VARBINARY, "BLOB");
+    typeNames.put(Types.DATE, "DATE");
+    typeNames.put(Types.SMALLINT, "SMALLINT");
+    typeNames.put(Types.INTEGER, "INT");
+    typeNames.put(Types.BIGINT, "BIGINT");
+    typeNames.put(Types.LONGVARCHAR, "TEXT");
+    typeNames.put(Types.TIMESTAMP, "TIMESTAMP");
+  }
+
+  public String getSqlTypeName(final int typeCode) {
+    final String r = typeNames.get(typeCode);
+    return r != null ? r : "UNKNOWNTYPE";
   }
 
   public SqlTypeInfo getSqlTypeInfo(final ColumnModel col) {
diff --git a/src/com/google/gwtorm/schema/sql/SqlIntTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlIntTypeInfo.java
index 554c1c4..a13a4c4 100644
--- a/src/com/google/gwtorm/schema/sql/SqlIntTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlIntTypeInfo.java
@@ -30,7 +30,7 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel column) {
-    return "INT DEFAULT 0 NOT NULL";
+  public String getSqlType(final ColumnModel column, final SqlDialect dialect) {
+    return dialect.getSqlTypeName(getSqlTypeConstant()) + " DEFAULT 0 NOT NULL";
   }
 }
diff --git a/src/com/google/gwtorm/schema/sql/SqlLongTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlLongTypeInfo.java
index 1b1f50c..b266345 100644
--- a/src/com/google/gwtorm/schema/sql/SqlLongTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlLongTypeInfo.java
@@ -30,7 +30,7 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel column) {
-    return "BIGINT DEFAULT 0 NOT NULL";
+  public String getSqlType(final ColumnModel column, final SqlDialect dialect) {
+    return dialect.getSqlTypeName(getSqlTypeConstant()) + " DEFAULT 0 NOT NULL";
   }
 }
diff --git a/src/com/google/gwtorm/schema/sql/SqlShortTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlShortTypeInfo.java
index 5220f47..1686a20 100644
--- a/src/com/google/gwtorm/schema/sql/SqlShortTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlShortTypeInfo.java
@@ -30,7 +30,7 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel column) {
-    return "SMALLINT DEFAULT 0 NOT NULL";
+  public String getSqlType(final ColumnModel column, final SqlDialect dialect) {
+    return dialect.getSqlTypeName(getSqlTypeConstant()) + " DEFAULT 0 NOT NULL";
   }
 }
diff --git a/src/com/google/gwtorm/schema/sql/SqlStringTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlStringTypeInfo.java
index df68612..d23c7ea 100644
--- a/src/com/google/gwtorm/schema/sql/SqlStringTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlStringTypeInfo.java
@@ -42,7 +42,7 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel col) {
+  public String getSqlType(final ColumnModel col, final SqlDialect dialect) {
     final Column column = col.getColumnAnnotation();
     final StringBuilder r = new StringBuilder();
 
@@ -51,7 +51,7 @@
     } else if (column.length() <= 255) {
       r.append("VARCHAR(" + column.length() + ")");
     } else {
-      r.append("TEXT");
+      r.append(dialect.getSqlTypeName(Types.LONGVARCHAR));
     }
 
     if (column.notNull()) {
diff --git a/src/com/google/gwtorm/schema/sql/SqlTimestampTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlTimestampTypeInfo.java
index 7eed577..d5646a7 100644
--- a/src/com/google/gwtorm/schema/sql/SqlTimestampTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlTimestampTypeInfo.java
@@ -31,10 +31,10 @@
   }
 
   @Override
-  public String getSqlType(final ColumnModel col) {
+  public String getSqlType(final ColumnModel col, final SqlDialect dialect) {
     final Column column = col.getColumnAnnotation();
     final StringBuilder r = new StringBuilder();
-    r.append("TIMESTAMP");
+    r.append(dialect.getSqlTypeName(getSqlTypeConstant()));
     if (column.notNull()) {
       r.append(" DEFAULT '1900-01-01 00:00:00'");
       r.append(" NOT NULL");
diff --git a/src/com/google/gwtorm/schema/sql/SqlTypeInfo.java b/src/com/google/gwtorm/schema/sql/SqlTypeInfo.java
index 75e656d..35ed96a 100644
--- a/src/com/google/gwtorm/schema/sql/SqlTypeInfo.java
+++ b/src/com/google/gwtorm/schema/sql/SqlTypeInfo.java
@@ -27,7 +27,7 @@
   protected SqlTypeInfo() {
   }
 
-  public abstract String getSqlType(ColumnModel column);
+  public abstract String getSqlType(ColumnModel column, SqlDialect dialect);
 
   protected abstract String getJavaSqlTypeAlias();