summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHenri Sara <henri.sara@itmill.com>2012-07-25 10:15:39 +0000
committerHenri Sara <henri.sara@itmill.com>2012-07-25 10:15:39 +0000
commit6a04a73a812000c8d1f5f9a994475c571b88c498 (patch)
tree659bca80b27fd2cce5991a77fa648f6ec3c9c4c9 /src
parentb6ea64e917b69a80bd61c0541947fee8faf0e46c (diff)
downloadvaadin-framework-6a04a73a812000c8d1f5f9a994475c571b88c498.tar.gz
vaadin-framework-6a04a73a812000c8d1f5f9a994475c571b88c498.zip
#9148 SQLContainer extensible null value support using custom StatementHelper classes
svn changeset:24023/svn branch:6.8
Diffstat (limited to 'src')
-rw-r--r--src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java60
-rw-r--r--src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java2
-rw-r--r--src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java12
-rw-r--r--src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java24
4 files changed, 92 insertions, 6 deletions
diff --git a/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java b/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java
index 24d283c9ab..d1700fc0d2 100644
--- a/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java
+++ b/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java
@@ -24,11 +24,25 @@ import com.vaadin.data.util.sqlcontainer.query.generator.filter.StringDecorator;
@SuppressWarnings("serial")
public class DefaultSQLGenerator implements SQLGenerator {
+ private Class<? extends StatementHelper> statementHelperClass = null;
+
public DefaultSQLGenerator() {
}
/**
+ * Create a new DefaultSqlGenerator instance that uses the given
+ * implementation of {@link StatementHelper}
+ *
+ * @param statementHelper
+ */
+ public DefaultSQLGenerator(
+ Class<? extends StatementHelper> statementHelperClazz) {
+ this();
+ statementHelperClass = statementHelperClazz;
+ }
+
+ /**
* Construct a DefaultSQLGenerator with the specified identifiers for start
* and end of quoted strings. The identifiers may be different depending on
* the database engine and it's settings.
@@ -44,6 +58,20 @@ public class DefaultSQLGenerator implements SQLGenerator {
quoteEnd));
}
+ /**
+ * Same as {@link #DefaultSQLGenerator(String, String)} but with support for
+ * custom {@link StatementHelper} implementation.
+ *
+ * @param quoteStart
+ * @param quoteEnd
+ * @param statementHelperClazz
+ */
+ public DefaultSQLGenerator(String quoteStart, String quoteEnd,
+ Class<? extends StatementHelper> statementHelperClazz) {
+ this(quoteStart, quoteEnd);
+ statementHelperClass = statementHelperClazz;
+ }
+
/*
* (non-Javadoc)
*
@@ -58,7 +86,7 @@ public class DefaultSQLGenerator implements SQLGenerator {
throw new IllegalArgumentException("Table name must be given.");
}
toSelect = toSelect == null ? "*" : toSelect;
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
query.append("SELECT " + toSelect + " FROM ").append(
SQLUtil.escapeSQL(tableName));
@@ -91,7 +119,7 @@ public class DefaultSQLGenerator implements SQLGenerator {
if (item == null) {
throw new IllegalArgumentException("Updated item must be given.");
}
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
query.append("UPDATE ").append(tableName).append(" SET");
@@ -144,7 +172,7 @@ public class DefaultSQLGenerator implements SQLGenerator {
throw new IllegalArgumentException(
"Cannot generate an insert query for item already in database.");
}
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
query.append("INSERT INTO ").append(tableName).append(" (");
@@ -197,7 +225,7 @@ public class DefaultSQLGenerator implements SQLGenerator {
throw new IllegalArgumentException(
"Valid keyColumnNames must be provided.");
}
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
query.append("DELETE FROM ").append(tableName).append(" WHERE ");
int count = 1;
@@ -308,4 +336,28 @@ public class DefaultSQLGenerator implements SQLGenerator {
}
return rowIdentifiers;
}
+
+ /**
+ * Returns the statement helper for the generator. Override this to handle
+ * platform specific data types.
+ *
+ * @see http://dev.vaadin.com/ticket/9148
+ * @return a new instance of the statement helper
+ */
+ protected StatementHelper getStatementHelper() {
+ if (statementHelperClass == null) {
+ return new StatementHelper();
+ }
+
+ try {
+ return statementHelperClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new RuntimeException(
+ "Unable to instantiate custom StatementHelper", e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(
+ "Unable to instantiate custom StatementHelper", e);
+ }
+ }
+
} \ No newline at end of file
diff --git a/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java b/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java
index 52c13feecb..13ef1d0090 100644
--- a/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java
+++ b/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java
@@ -50,7 +50,7 @@ public class MSSQLGenerator extends DefaultSQLGenerator {
offset = pagelength > 1 ? ++offset : offset;
pagelength = pagelength > 1 ? --pagelength : pagelength;
toSelect = toSelect == null ? "*" : toSelect;
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
/* Row count request is handled here */
diff --git a/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java b/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java
index 8e33211154..43a562d3a8 100644
--- a/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java
+++ b/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java
@@ -16,6 +16,10 @@ public class OracleGenerator extends DefaultSQLGenerator {
}
+ public OracleGenerator(Class<? extends StatementHelper> statementHelperClazz) {
+ super(statementHelperClazz);
+ }
+
/**
* Construct an OracleSQLGenerator with the specified identifiers for start
* and end of quoted strings. The identifiers may be different depending on
@@ -31,6 +35,11 @@ public class OracleGenerator extends DefaultSQLGenerator {
super(quoteStart, quoteEnd);
}
+ public OracleGenerator(String quoteStart, String quoteEnd,
+ Class<? extends StatementHelper> statementHelperClazz) {
+ super(quoteStart, quoteEnd, statementHelperClazz);
+ }
+
/*
* (non-Javadoc)
*
@@ -50,7 +59,7 @@ public class OracleGenerator extends DefaultSQLGenerator {
offset = pagelength > 1 ? ++offset : offset;
pagelength = pagelength > 1 ? --pagelength : pagelength;
toSelect = toSelect == null ? "*" : toSelect;
- StatementHelper sh = new StatementHelper();
+ StatementHelper sh = getStatementHelper();
StringBuffer query = new StringBuffer();
/* Row count request is handled here */
@@ -99,4 +108,5 @@ public class OracleGenerator extends DefaultSQLGenerator {
sh.setQueryString(query.toString());
return sh;
}
+
} \ No newline at end of file
diff --git a/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java b/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java
index 3fd92e920d..f9458609c3 100644
--- a/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java
+++ b/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java
@@ -128,8 +128,32 @@ public class StatementHelper implements Serializable {
} else if (Timestamp.class.equals(dataTypes.get(i))) {
pstmt.setTimestamp(i + 1, null);
} else {
+
+ if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) {
+ return;
+ }
+
throw new SQLException("Data type not supported by SQLContainer: "
+ parameters.get(i).getClass().toString());
}
}
+
+ /**
+ * Handle unrecognized null values. Override this to handle null values for
+ * platform specific data types that are not handled by the default
+ * implementation of the {@link StatementHelper}.
+ *
+ * @param i
+ * @param pstmt
+ * @param dataTypes2
+ *
+ * @return true if handled, false otherwise
+ *
+ * @see {@link http://dev.vaadin.com/ticket/9148}
+ */
+ protected boolean handleUnrecognizedTypeNullValue(int i,
+ PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes)
+ throws SQLException {
+ return false;
+ }
}