]> source.dussan.org Git - iciql.git/commitdiff
Fixes #15: String field not properly trimmed on discrete set
authorJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 20:29:28 +0000 (16:29 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 20:29:34 +0000 (16:29 -0400)
src/main/java/com/iciql/Query.java
src/test/java/com/iciql/test/ModelsTest.java
src/test/java/com/iciql/test/models/SupportedTypes.java

index 882fea1df2a85ff6e0865c536812e577cb650212..1dbbc4ea22a061b0009c4a657e0b0ca5461a0c55 100644 (file)
@@ -847,13 +847,24 @@ public class Query<T> {
                SelectColumn<T> col = getColumnByReference(alias);
                if (col != null && value != null && value.getClass().isEnum()) {
                        // enum
-                       EnumType type = col.getFieldDefinition().enumType;
+                       TableDefinition.FieldDefinition field = col.getFieldDefinition();
+                       EnumType type = field.enumType;
                        Enum<?> anEnum = (Enum<?>) value;
                        Object y = Utils.convertEnum(anEnum, type);
                        stat.addParameter(y);
                } else if (col != null) {
                        // object
-                       Class<? extends DataTypeAdapter<?>> typeAdapter = col.getFieldDefinition().typeAdapter;
+                       TableDefinition.FieldDefinition field = col.getFieldDefinition();
+                       Class<? extends DataTypeAdapter<?>> typeAdapter = field.typeAdapter;
+                       if (value != null && value instanceof String) {
+                               if (field.trim && field.length > 0) {
+                                       // clip strings (issue-15)
+                                       String s = (String) value;
+                                       if (s.length() > field.length) {
+                                               value = s.substring(0, field.length);
+                                       }
+                               }
+                       }
                        Object parameter = db.getDialect().serialize(value, typeAdapter);
                        stat.addParameter(parameter);
                } else {
index 683e49470f4da1a89f294325516539ac698b9fb1..ff27bf5090e347cb7005c3595a1573667a3e9f62 100644 (file)
@@ -147,4 +147,17 @@ public class ModelsTest {
                        assertEquals(0, models.get(0).length());
                }
        }
+
+       @Test
+       public void testDiscreteUpdateStringTrimming() {
+               List<SupportedTypes> original = SupportedTypes.createList();
+               db.insertAll(original);
+               SupportedTypes s1 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
+               db.from(SupportedTypes.SAMPLE)
+                               .set(SupportedTypes.SAMPLE.myString)
+                               .to(s1.myString + s1.myString + s1.myString + s1.myString)
+                               .update();
+               SupportedTypes s2 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
+               assertEquals(40, s2.myString.length());
+       }
 }
index 0383d7f0ec4dcc134c5ea4709f1586819383f55d..3b0e694e0112c288e3d294b4fe0161d85f85c7c3 100644 (file)
@@ -89,7 +89,7 @@ public class SupportedTypes implements Serializable {
        @IQColumn(length = 10, scale = 5)
        private BigDecimal myBigDecimal;
 
-       @IQColumn(length = 40)
+       @IQColumn(length = 40, trim = true)
        public String myString;
 
        @IQColumn