summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-01-19 11:01:52 -0800
committerShawn O. Pearce <sop@google.com>2009-01-19 11:01:52 -0800
commit32f21701afcd96f08be35b764e7ee7055952d080 (patch)
treee2b5ada2b8b857dea2e19027fdbe38c3e381493d
parent254d72922f1f9467ad2e9a603bce1350a332a0c2 (diff)
downloadgwtorm-32f21701afcd96f08be35b764e7ee7055952d080.tar.gz
Allow dialects to refine themselves based on connection specific data
Once we have the connection open and have roughly selected the dialect based on the JDBC URL its useful to allow the dialect to refine itself to a server-specific version. This might then later be used to refine how SQL is generated for that database, like when syntax is changed or new useful syntax has been added in later revisions of the server software. Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gwtorm/jdbc/Database.java24
-rw-r--r--src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java6
2 files changed, 19 insertions, 11 deletions
diff --git a/src/main/java/com/google/gwtorm/jdbc/Database.java b/src/main/java/com/google/gwtorm/jdbc/Database.java
index 0b9a39f..753ea15 100644
--- a/src/main/java/com/google/gwtorm/jdbc/Database.java
+++ b/src/main/java/com/google/gwtorm/jdbc/Database.java
@@ -76,11 +76,22 @@ public class Database<T extends Schema> implements SchemaFactory<T> {
throws OrmException {
dataSource = ds;
- final String url;
+ SqlDialect dialect;
try {
final Connection c = ds.getConnection();
try {
- url = c.getMetaData().getURL();
+ final String url = c.getMetaData().getURL();
+ if (url.startsWith("jdbc:postgresql:")) {
+ dialect = new DialectPostgreSQL();
+
+ } else if (url.startsWith("jdbc:h2:")) {
+ dialect = new DialectH2();
+
+ } else {
+ throw new OrmException("No dialect known for " + url);
+ }
+
+ dialect = dialect.refine(c);
} finally {
c.close();
}
@@ -88,15 +99,6 @@ public class Database<T extends Schema> implements SchemaFactory<T> {
throw new OrmException("Unable to determine driver URL", e);
}
- final SqlDialect dialect;
- if (url.startsWith("jdbc:postgresql:")) {
- dialect = new DialectPostgreSQL();
- } else if (url.startsWith("jdbc:h2:")) {
- dialect = new DialectH2();
- } else {
- throw new OrmException("No dialect known for " + url);
- }
-
schemaModel = new JavaSchemaModel(schema);
final GeneratedClassLoader loader = newLoader(schema);
final String cachedName = schemaFactoryNames.get(schema);
diff --git a/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java b/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
index 2565513..5aa2e13 100644
--- a/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
+++ b/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
@@ -19,6 +19,7 @@ import com.google.gwtorm.client.Sequence;
import com.google.gwtorm.schema.ColumnModel;
import com.google.gwtorm.schema.SequenceModel;
+import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
@@ -50,6 +51,11 @@ public abstract class SqlDialect {
typeNames.put(Types.TIMESTAMP, "TIMESTAMP");
}
+ /** Select a better dialect definition for this connection */
+ public SqlDialect refine(final Connection c) throws SQLException {
+ return this;
+ }
+
public String getSqlTypeName(final int typeCode) {
final String r = typeNames.get(typeCode);
return r != null ? r : "UNKNOWNTYPE";