]> source.dussan.org Git - jackcess.git/commitdiff
add support for strconv function
authorJames Ahlborn <jtahlborn@yahoo.com>
Fri, 5 Oct 2018 19:11:18 +0000 (19:11 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Fri, 5 Oct 2018 19:11:18 +0000 (19:11 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1207 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/expr/package-info.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/ValueSupport.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java

index 1f461392d25a4609e559bfd23aef9f9f1ecb6ef1..1cd517f617d0f4c2b367c05322be655af819b4cb 100644 (file)
@@ -232,7 +232,7 @@ limitations under the License.
  * <tr class="TableRowColor"><td>Right[$]</td><td>Y</td></tr>
  * <tr class="TableRowColor"><td>Space[$]</td><td>Y</td></tr>
  * <tr class="TableRowColor"><td>StrComp</td><td>Y</td></tr>
- * <tr class="TableRowColor"><td>StrConv</td><td></td></tr>
+ * <tr class="TableRowColor"><td>StrConv[$]</td><td>Partial</td></tr>
  * <tr class="TableRowColor"><td>String[$]</td><td>Y</td></tr>
  * <tr class="TableRowColor"><td>StrReverse</td><td>Y</td></tr>
  * <tr class="TableRowColor"><td>UCase[$]</td><td>Y</td></tr>
index 4a6da20da6dbe363406b5f0c731b86b98e8e9523..42212cf97208a68e6517268fdd5ded1f8b84deeb 100644 (file)
@@ -23,6 +23,7 @@ import com.healthmarketscience.jackcess.expr.EvalException;
 import com.healthmarketscience.jackcess.expr.Function;
 import com.healthmarketscience.jackcess.expr.LocaleContext;
 import com.healthmarketscience.jackcess.expr.Value;
+import org.apache.commons.lang.WordUtils;
 import static com.healthmarketscience.jackcess.impl.expr.DefaultFunctions.*;
 import static com.healthmarketscience.jackcess.impl.expr.FunctionSupport.*;
 
@@ -32,6 +33,9 @@ import static com.healthmarketscience.jackcess.impl.expr.FunctionSupport.*;
  */
 public class DefaultTextFunctions
 {
+  // mask to separate the case conversion value (first two bits) from the char
+  // conversion value for the StrConv() function
+  private static final int STR_CONV_MASK = 0x03;
 
   private DefaultTextFunctions() {}
 
@@ -317,6 +321,50 @@ public class DefaultTextFunctions
     }
   });
 
+  public static final Function STRCONV = registerStringFunc(new FuncVar("StrConv", 2, 3) {
+    @Override
+    protected Value evalVar(EvalContext ctx, Value[] params) {
+      Value param1 = params[0];
+      if(param1.isNull()) {
+        return ValueSupport.NULL_VAL;
+      }
+
+      String str = param1.getAsString(ctx);
+      int conversion = params[1].getAsLongInt(ctx);
+      // TODO, for now, ignore locale id...?
+      // int localeId = params[2];
+
+      int caseConv = STR_CONV_MASK & conversion;
+      int charConv = (~STR_CONV_MASK) & conversion;
+
+      switch(caseConv) {
+      case 1:
+        // vbUpperCase
+        str = str.toUpperCase();
+        break;
+      case 2:
+        // vbLowerCase
+        str = str.toLowerCase();
+        break;
+      case 3:
+        // vbProperCase
+        str = WordUtils.capitalize(str.toLowerCase());
+        break;
+      default:
+        // do nothing
+      }
+
+      if(charConv != 0) {
+          // 64 = vbUnicode, all java strings are already unicode,so nothing to do
+        if(charConv != 64) {
+          throw new EvalException("Unsupported character conversion " + charConv);
+        }
+      }
+
+      return ValueSupport.toValue(str);
+    }
+  });
+
   public static final Function STRING = registerStringFunc(new Func2("String") {
     @Override
     protected Value eval2(EvalContext ctx, Value param1, Value param2) {
index 06833de02eac04405b99211864c9e0940a1f673c..621ed37fca2a734d6dde0d03ef447c3c4eb1c7e9 100644 (file)
@@ -96,21 +96,21 @@ public class ValueSupport
   }
 
   public static DateFormat getDateFormatForType(LocaleContext ctx, Value.Type type) {
-      String fmtStr = null;
-      switch(type) {
-      case DATE:
-        fmtStr = ctx.getTemporalConfig().getDefaultDateFormat();
-        break;
-      case TIME:
-        fmtStr = ctx.getTemporalConfig().getDefaultTimeFormat();
-        break;
-      case DATE_TIME:
-        fmtStr = ctx.getTemporalConfig().getDefaultDateTimeFormat();
-        break;
-      default:
-        throw new EvalException("Unexpected date/time type " + type);
-      }
-      return ctx.createDateFormat(fmtStr);
+    String fmtStr = null;
+    switch(type) {
+    case DATE:
+      fmtStr = ctx.getTemporalConfig().getDefaultDateFormat();
+      break;
+    case TIME:
+      fmtStr = ctx.getTemporalConfig().getDefaultTimeFormat();
+      break;
+    case DATE_TIME:
+      fmtStr = ctx.getTemporalConfig().getDefaultDateTimeFormat();
+      break;
+    default:
+      throw new EvalException("Unexpected date/time type " + type);
+    }
+    return ctx.createDateFormat(fmtStr);
   }
 
   /**
index 4051fde24eba188e5795d89dcb316eb5c72b763c..f4f037ba06592282ee9ef109724d515ee1e173e5 100644 (file)
@@ -161,6 +161,11 @@ public class DefaultFunctionsTest extends TestCase
     assertEquals(1, eval("=StrComp('bar', 'FOO', 0)"));
     assertEquals(-1, eval("=StrComp('FOO', 'foo', 0)"));
 
+    assertEquals("FOO", eval("=StrConv('foo', 1)"));
+    assertEquals("foo", eval("=StrConv('foo', 2)"));
+    assertEquals("foo", eval("=StrConv('FOO', 2)"));
+    assertEquals("Foo Bar", eval("=StrConv('FOO bar', 3)"));
+
     assertEquals("halb", eval("=StrReverse('blah')"));
 
     assertEquals("foo", eval("=Choose(1,'foo','bar','blah')"));
index d7b5a00a14ea8033817f6ec33300707bea348cf2..4c50f12e278fb3d458f501bf072db1b8729456d9 100644 (file)
@@ -322,6 +322,10 @@ public class ExpressionatorTest extends TestCase
     assertEquals(new Date(1044680400000L), eval("=#01/02/2003# + '37'"));
     assertEquals(new Date(1044680400000L), eval("='37' + #01/02/2003#"));
     assertEquals(new Date(1041508800000L), eval("=#01/02/2003 7:00:00 AM#"));
+
+    assertEquals("2/8/2003", eval("=CStr(#01/02/2003# + '37')"));
+    assertEquals("9:24:00 AM", eval("=CStr(#7:00:00 AM# + 0.1)"));
+    assertEquals("1/2/2003 1:10:00 PM", eval("=CStr(#01/02/2003# + #13:10:00#)"));
   }
 
   public void testNull() throws Exception