From: Henri Sara Date: Wed, 25 Jul 2012 10:15:39 +0000 (+0000) Subject: #9148 SQLContainer extensible null value support using custom StatementHelper classes X-Git-Tag: 7.0.0.beta1~79^2~69 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6a04a73a812000c8d1f5f9a994475c571b88c498;p=vaadin-framework.git #9148 SQLContainer extensible null value support using custom StatementHelper classes svn changeset:24023/svn branch:6.8 --- 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,10 +24,24 @@ import com.vaadin.data.util.sqlcontainer.query.generator.filter.StringDecorator; @SuppressWarnings("serial") public class DefaultSQLGenerator implements SQLGenerator { + private Class statementHelperClass = null; + public DefaultSQLGenerator() { } + /** + * Create a new DefaultSqlGenerator instance that uses the given + * implementation of {@link StatementHelper} + * + * @param statementHelper + */ + public DefaultSQLGenerator( + Class 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 @@ -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 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 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 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> dataTypes) + throws SQLException { + return false; + } }