This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

PLAT-4609 - Oracle DB - Handle empty string

Encode and decode empty string to avoid Oracle mapping to null
Add a single space to empty string and all strings that are only spaces
  • Loading branch information...
jodastephen committed Sep 5, 2014
1 parent 18bc23a commit 50c3a710b3b5fe2fe15504cff471e66df47a7c5c
@@ -125,7 +125,7 @@ protected DbConnector initDbConnector(ComponentRepository repo) {
*/
protected DbConnector createDbConnector(ComponentRepository repo) {
DbDialect dialect = createDialect(repo);
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(getDataSource());
NamedParameterJdbcTemplate jdbcTemplate = dialect.getNamedParameterJdbcTemplate(getDataSource());
SessionFactory hibernateSessionFactory = createHibernateSessionFactory(repo, dialect);
HibernateTemplate hibernateTemplate = createHibernateTemplate(repo, hibernateSessionFactory);
TransactionTemplate transactionTemplate = createTransactionTemplate(repo, hibernateSessionFactory);
@@ -25,9 +25,11 @@
import com.opengamma.util.ArgumentChecker;
/**
* A class to facilitate importing portfolio data from a JDBC query result
* A class to facilitate importing portfolio data from a JDBC query result.
*/
public class JdbcSheetReader extends SheetReader {
// NOTE: This class uses JdbcTemplate rather than DbConnector
// as such it may not work reliably across databases, notably Oracle
private DataSource _dataSource;
private JdbcTemplate _jdbcTemplate;
@@ -131,14 +131,12 @@ public void test_bond_withSearchByIssuer() throws Exception {
"couponType", 23.5d, SimpleFrequency.ANNUAL, DayCounts.ACT_ACT_ISDA,
zdt, zdt, zdt, 129d, 1324d, 12d, 1d, 2d, 3d);
sec1.addExternalId(ExternalId.of("abc", "def"));
sec1.setName("GovernmentBond");
SecurityDocument added1 = _secMaster.add(new SecurityDocument(sec1));
GovernmentBondSecurity sec2 = new GovernmentBondSecurity("UK GOVT", "issuerType", "issuerDomicile", "market",
Currency.GBP, SimpleYieldConvention.US_TREASURY_EQUIVALENT, new Expiry(zdt),
"couponType", 23.5d, SimpleFrequency.ANNUAL, DayCounts.ACT_ACT_ISDA,
zdt, zdt, zdt, 129d, 1324d, 12d, 1d, 2d, 3d);
sec2.addExternalId(ExternalId.of("abc", "def"));
sec2.setName("GovernmentBond");
SecurityDocument added2 = _secMaster.add(new SecurityDocument(sec2));
SecurityDocument loaded1 = _secMaster.get(added1.getUniqueId());
@@ -138,7 +138,6 @@ public void test_add_addWithMissingTypeProperty() throws Exception {
public void test_add_addWithMinimalProperties() {
ManageableSecurity security = new ManageableSecurity();
SecurityDocument doc = new SecurityDocument(security);
security.setName("Test");
_secMaster.add(doc);
}
@@ -243,7 +243,7 @@ public DbConnector createObject() {
ArgumentChecker.notNull(getName(), "name");
ArgumentChecker.notNull(getDataSource(), "dataSource");
DbDialect dialect = createDialect();
NamedParameterJdbcTemplate jdbcTemplate = createNamedParameterJdbcTemplate();
NamedParameterJdbcTemplate jdbcTemplate = createNamedParameterJdbcTemplate(dialect);
SessionFactory hbFactory = createSessionFactory(dialect);
HibernateTemplate hbTemplate = createHibernateTemplate(hbFactory);
TransactionTemplate transTemplate = createTransactionTemplate(hbFactory);
@@ -275,10 +275,11 @@ protected DbDialect createDialect() {
/**
* Creates the JDBC template, using the data source.
*
* @param dialect the dialect instance, not null
* @return the JDBC template, not null
*/
protected NamedParameterJdbcTemplate createNamedParameterJdbcTemplate() {
return new NamedParameterJdbcTemplate(getDataSource());
protected NamedParameterJdbcTemplate createNamedParameterJdbcTemplate(DbDialect dialect) {
return dialect.getNamedParameterJdbcTemplate(getDataSource());
}
//-------------------------------------------------------------------------
@@ -7,8 +7,12 @@
import java.sql.Driver;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.hibernate.dialect.Dialect;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import org.threeten.bp.temporal.ChronoUnit;
@@ -59,6 +63,27 @@ public String getName() {
*/
public abstract Class<? extends Driver> getJDBCDriverClass();
//-------------------------------------------------------------------------
/**
* Gets the named parameter Spring template.
*
* @param dataSource the data source, not null
* @return the template, not null
*/
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
/**
* Gets the basic Spring template.
*
* @param dataSource the data source, not null
* @return the template, not null
*/
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
//-------------------------------------------------------------------------
/**
* Gets the Hibernate dialect object for the database.
@@ -162,6 +187,31 @@ public String sqlWildcardAdjustValue(String value) {
return value;
}
//-------------------------------------------------------------------------
/**
* Converts the specified application format string to database format.
* <p>
* This allows databases that map empty string to null to be handled.
*
* @param str the string to convert, null returns null
* @return the converted string
*/
public String toDatabaseString(String str) {
return str;
}
/**
* Converts the specified database format string to application format.
* <p>
* This allows databases that map empty string to null to be handled.
*
* @param str the string to convert, null returns null
* @return the converted string
*/
public String fromDatabaseString(String str) {
return str;
}
//-------------------------------------------------------------------------
/**
* Builds SQL to apply paging.
@@ -7,25 +7,39 @@
import java.sql.Driver;
import javax.sql.DataSource;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.Oracle10gDialect;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import com.google.common.base.CharMatcher;
import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.elsql.ElSqlConfig;
/**
* Database dialect for Oracle databases.
* <p>
* This contains any Oracle specific SQL and is tested for version 11g express.
* <p>
* An empty string is treated as null in Oracle.
* The {@link #toDatabaseString(String)} and {@link #fromDatabaseString(String)}
* methods allow the distinction to be preserved.
* They replace an empty string by a single character string.
* Any string consisting of all spaces is replaced by a string with one additional space.
*/
public class Oracle11gDbDialect extends DbDialect {
/**
* Helper can be treated as a singleton.
*/
public static final Oracle11gDbDialect INSTANCE = new Oracle11gDbDialect();
/**
* Matcher for a string that is all spaces.
*/
private static final CharMatcher ALL_SPACES = CharMatcher.is(' ');
/**
* Restrictive constructor.
@@ -46,6 +60,16 @@ public Oracle11gDbDialect() {
// return oracle.jdbc.driver.OracleDriver.class;
}
@Override
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(DataSource dataSource) {
return new OracleNamedParameterJdbcTemplate(dataSource);
}
@Override
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
return new OracleJdbcTemplate(dataSource);
}
@Override
protected Dialect createHibernateDialect() {
return new Oracle10gDialect();
@@ -56,6 +80,33 @@ protected ElSqlConfig createElSqlConfig() {
return ElSqlConfig.ORACLE;
}
//-------------------------------------------------------------------------
@Override
public String toDatabaseString(String str) {
if (str == null) {
return null;
} else if (str.isEmpty()) {
return " ";
} else if (str.charAt(0) != ' ') {
return str;
} else if (ALL_SPACES.matchesAllOf(str)) {
return str + ' ';
} else {
return str;
}
}
@Override
public String fromDatabaseString(String str) {
if (str == null) {
return null;
} else if (str.length() > 0 && ALL_SPACES.matchesAllOf(str)) {
return str.substring(1);
} else {
return str;
}
}
//-------------------------------------------------------------------------
@Override
public String sqlNextSequenceValueSelect(final String sequenceName) {
Oops, something went wrong.

0 comments on commit 50c3a71

Please sign in to comment.