From 13b08bb1e5a955650289c1c2d548883bfeac0de9 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 4 Apr 2016 16:29:28 -0400 Subject: [PATCH] Fixes #15: String field not properly trimmed on discrete set --- src/main/java/com/iciql/Query.java | 15 +++++++++++++-- src/test/java/com/iciql/test/ModelsTest.java | 13 +++++++++++++ .../com/iciql/test/models/SupportedTypes.java | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index 882fea1..1dbbc4e 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -847,13 +847,24 @@ public class Query { SelectColumn 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> typeAdapter = col.getFieldDefinition().typeAdapter; + TableDefinition.FieldDefinition field = col.getFieldDefinition(); + Class> 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 { diff --git a/src/test/java/com/iciql/test/ModelsTest.java b/src/test/java/com/iciql/test/ModelsTest.java index 683e494..ff27bf5 100644 --- a/src/test/java/com/iciql/test/ModelsTest.java +++ b/src/test/java/com/iciql/test/ModelsTest.java @@ -147,4 +147,17 @@ public class ModelsTest { assertEquals(0, models.get(0).length()); } } + + @Test + public void testDiscreteUpdateStringTrimming() { + List 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()); + } } diff --git a/src/test/java/com/iciql/test/models/SupportedTypes.java b/src/test/java/com/iciql/test/models/SupportedTypes.java index 0383d7f..3b0e694 100644 --- a/src/test/java/com/iciql/test/models/SupportedTypes.java +++ b/src/test/java/com/iciql/test/models/SupportedTypes.java @@ -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 -- 2.39.5