summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorMarcel Schneider2013-04-15 08:29:20 (EDT)
committer Judith Gull2013-04-15 08:29:20 (EDT)
commit8ca9002ab381205f8c4d770dc5a5971916b7fd9d (patch)
treece8e35bdb3354fc4cb31e1904a18f849530c7b9c
parenta7ebc1084964c2e41fd84100765c26dec7fb0df5 (diff)
downloadorg.eclipse.scout.rt-8ca9002ab381205f8c4d770dc5a5971916b7fd9d.zip
org.eclipse.scout.rt-8ca9002ab381205f8c4d770dc5a5971916b7fd9d.tar.gz
org.eclipse.scout.rt-8ca9002ab381205f8c4d770dc5a5971916b7fd9d.tar.bz2
Bug 405478 - AbstractSqlStyle binds for BigDecimal and Character
Applies patch by Marcel Schneider with some minor changes
-rw-r--r--org.eclipse.scout.rt.server.test/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyleTest.java138
-rw-r--r--org.eclipse.scout.rt.server/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyle.java25
2 files changed, 162 insertions, 1 deletions
diff --git a/org.eclipse.scout.rt.server.test/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyleTest.java b/org.eclipse.scout.rt.server.test/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyleTest.java
new file mode 100644
index 0000000..ca095a6
--- /dev/null
+++ b/org.eclipse.scout.rt.server.test/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyleTest.java
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * Copyright (c) 2010 BSI Business Systems Integration AG.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * BSI Business Systems Integration AG - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.scout.rt.server.services.common.jdbc.style;
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Types;
+
+import org.easymock.EasyMock;
+import org.eclipse.scout.rt.server.services.common.jdbc.SqlBind;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * JUnit tests for {@link AbstractSqlStyle}
+ *
+ * @since 3.9.0
+ * @author awe, msc
+ */
+public class AbstractSqlStyleTest {
+
+ /**
+ * SQL style for testing.
+ */
+ AbstractSqlStyle sql = new AbstractSqlStyle() {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void testConnection(Connection conn) throws SQLException {
+ }
+
+ @Override
+ public boolean isBlobEnabled() {
+ return false;
+ }
+
+ @Override
+ public boolean isClobEnabled() {
+ return false;
+ }
+
+ @Override
+ public boolean isLargeString(String s) {
+ return false;
+ }
+
+ @Override
+ protected int getMaxListSize() {
+ return 0;
+ }
+ };
+
+ /**
+ * Test for {@link AbstractSqlStyle#writeBind} for {@link BigDecimal}
+ *
+ * @throws SQLException
+ */
+ @Test
+ public void testWriteBind() throws SQLException {
+ BigDecimal bd = new BigDecimal("9.123");
+ SqlBind bind = new SqlBind(Types.DECIMAL, bd);
+ PreparedStatement ps = EasyMock.createMock(PreparedStatement.class);
+ ps.setObject(1, bd, Types.DECIMAL, 3);
+ EasyMock.expectLastCall();
+ EasyMock.replay(ps);
+ sql.writeBind(ps, 1, bind);
+ }
+
+ /**
+ * Test for {@link AbstractSqlStyle#writeBind} for {@link BigDecimal}
+ *
+ * @throws SQLException
+ */
+ @Test
+ public void testWriteBindNoScale() throws SQLException {
+ BigDecimal bd = new BigDecimal("9");
+ SqlBind bind = new SqlBind(Types.NUMERIC, bd);
+ PreparedStatement ps = EasyMock.createMock(PreparedStatement.class);
+ ps.setObject(1, bd, Types.NUMERIC, 0);
+ EasyMock.expectLastCall();
+ EasyMock.replay(ps);
+ sql.writeBind(ps, 1, bind);
+ }
+
+ /**
+ * Test for {@link AbstractSqlStyle#buildBindFor} for {@link Character} with nulltype {@link Character}.
+ */
+ @Test
+ public void testBuildBindForCharacter() {
+ Character c = Character.valueOf('x');
+ SqlBind bin = sql.buildBindFor(c, Character.class);
+ Assert.assertEquals(Types.VARCHAR, bin.getSqlType());
+ Assert.assertTrue(bin.getValue() instanceof String);
+ }
+
+ /**
+ * Test for {@link AbstractSqlStyle#buildBindFor} for null values with nulltype {@link Character}.
+ */
+ @Test
+ public void testBuildBindForNullCharacter() {
+ SqlBind bin = sql.buildBindFor(null, Character.class);
+ Assert.assertEquals(Types.VARCHAR, bin.getSqlType());
+ Assert.assertNull(bin.getValue());
+ }
+
+ /**
+ * Test for {@link AbstractSqlStyle#buildBindFor} for {@link BigDecimal} with nulltype {@link BigDecimal}.
+ */
+ @Test
+ public void testBuildBindForBigDecimal() {
+ final int testValue = 100;
+ final BigDecimal b = BigDecimal.valueOf(testValue);
+ SqlBind bin = sql.buildBindFor(b, BigDecimal.class);
+ Assert.assertEquals(Types.NUMERIC, bin.getSqlType());
+ Assert.assertTrue(bin.getValue() instanceof BigDecimal);
+ }
+
+ /**
+ * Test for {@link AbstractSqlStyle#buildBindFor} for null values with nulltype {@link BigDecimal}.
+ */
+ @Test
+ public void testBuildBindForNullBigDecimal() {
+ SqlBind bin = sql.buildBindFor(null, BigDecimal.class);
+ Assert.assertEquals(Types.NUMERIC, bin.getSqlType());
+ Assert.assertNull(bin.getValue());
+ }
+
+}
diff --git a/org.eclipse.scout.rt.server/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyle.java b/org.eclipse.scout.rt.server/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyle.java
index f3a5369..1a42292 100644
--- a/org.eclipse.scout.rt.server/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyle.java
+++ b/org.eclipse.scout.rt.server/src/org/eclipse/scout/rt/server/services/common/jdbc/style/AbstractSqlStyle.java
@@ -231,6 +231,14 @@ public abstract class AbstractSqlStyle implements ISqlStyle {
else if (BigDecimal.class.isAssignableFrom(c)) {
return new SqlBind(Types.NUMERIC, o);
}
+ else if (Character.class.isAssignableFrom(c)) {
+ if (o == null) {
+ return new SqlBind(Types.VARCHAR, o);
+ }
+ else {
+ return new SqlBind(Types.VARCHAR, o.toString());
+ }
+ }
else if (String.class.isAssignableFrom(c) || char[].class == c) {
if (o == null) {
return new SqlBind(Types.VARCHAR, o);
@@ -374,12 +382,27 @@ public abstract class AbstractSqlStyle implements ISqlStyle {
}
break;
}
+ case Types.DECIMAL:
+ case Types.NUMERIC: {
+ if (bind.getValue() instanceof BigDecimal) {
+ int scale = ((BigDecimal) bind.getValue()).scale();
+ ps.setObject(jdbcBindIndex, bind.getValue(), bind.getSqlType(), scale);
+ }
+ else {
+ writeDefaultBind(ps, jdbcBindIndex, bind);
+ }
+ break;
+ }
default: {
- ps.setObject(jdbcBindIndex, bind.getValue(), bind.getSqlType());
+ writeDefaultBind(ps, jdbcBindIndex, bind);
}
}
}
+ private void writeDefaultBind(PreparedStatement ps, int jdbcBindIndex, SqlBind bind) throws SQLException {
+ ps.setObject(jdbcBindIndex, bind.getValue(), bind.getSqlType());
+ }
+
@Override
public Object readBind(ResultSet rs, ResultSetMetaData meta, int type, int jdbcBindIndex) throws SQLException {
Object o = null;