@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
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)
*
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));
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");
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(" (");
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;
}
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
}
+ 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
super(quoteStart, quoteEnd);
}
+ public OracleGenerator(String quoteStart, String quoteEnd,
+ Class<? extends StatementHelper> statementHelperClazz) {
+ super(quoteStart, quoteEnd, statementHelperClazz);
+ }
+
/*
* (non-Javadoc)
*
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 */
sh.setQueryString(query.toString());
return sh;
}
+
}
\ No newline at end of file
} 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;
+ }
}