@Test public void testString2col() { Object result = StringUtil.string2col("", "string", null); Assert.assertEquals(result, ""); result = StringUtil.string2col("123", "TINYINT", null); Assert.assertEquals(result, Byte.valueOf("123")); result = StringUtil.string2col("1", "SMALLINT", null); Assert.assertEquals(result, (short)1); result = StringUtil.string2col("1", "INT", null); Assert.assertEquals(result, 1); result = StringUtil.string2col("1", "MEDIUMINT", null); Assert.assertEquals(result, (long)1); result = StringUtil.string2col("1", "BIGINT", null); Assert.assertEquals(result, (long)1); result = StringUtil.string2col("1.1", "float", null); Assert.assertEquals(result, (float)1.1); result = StringUtil.string2col("1.1", "double", null); Assert.assertEquals(result, 1.1); result = StringUtil.string2col("value", "string", null); Assert.assertEquals(result, "value"); result = StringUtil.string2col("value", "string", null); Assert.assertEquals(result, "value"); result = StringUtil.string2col("20200312-174412", "string", new SimpleDateFormat("yyyyMMdd-HHmmss")); Assert.assertEquals(result, "2020-03-12 17:44:12"); result = StringUtil.string2col("true", "boolean", null); Assert.assertEquals(result, true); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); java.sql.Date date = new java.sql.Date(System.currentTimeMillis()); result = StringUtil.string2col(sdf.format(date), "date", sdf); Assert.assertEquals(result.toString(), date.toString()); result = StringUtil.string2col("xxx", "xxx", null); Assert.assertEquals(result, "xxx"); }
public static Object string2col(String str, String type, SimpleDateFormat customTimeFormat) { if(str == null || str.length() == 0 || type == null){ return str; } ColumnType columnType = ColumnType.getType(type.toUpperCase()); Object ret; switch(columnType) { case TINYINT: ret = Byte.valueOf(str.trim()); break; case SMALLINT: ret = Short.valueOf(str.trim()); break; case INT: ret = Integer.valueOf(str.trim()); break; case MEDIUMINT: case BIGINT: ret = Long.valueOf(str.trim()); break; case FLOAT: ret = Float.valueOf(str.trim()); break; case DOUBLE: ret = Double.valueOf(str.trim()); break; case STRING: case VARCHAR: case CHAR: if(customTimeFormat != null){ ret = DateUtil.columnToDate(str,customTimeFormat); ret = DateUtil.timestampToString((Date)ret); } else { ret = str; } break; case BOOLEAN: ret = Boolean.valueOf(str.trim().toLowerCase()); break; case DATE: ret = DateUtil.columnToDate(str,customTimeFormat); break; case TIMESTAMP: case DATETIME: ret = DateUtil.columnToTimestamp(str,customTimeFormat); break; default: ret = str; } return ret; }
StringUtil { public static Object string2col(String str, String type, SimpleDateFormat customTimeFormat) { if(str == null || str.length() == 0 || type == null){ return str; } ColumnType columnType = ColumnType.getType(type.toUpperCase()); Object ret; switch(columnType) { case TINYINT: ret = Byte.valueOf(str.trim()); break; case SMALLINT: ret = Short.valueOf(str.trim()); break; case INT: ret = Integer.valueOf(str.trim()); break; case MEDIUMINT: case BIGINT: ret = Long.valueOf(str.trim()); break; case FLOAT: ret = Float.valueOf(str.trim()); break; case DOUBLE: ret = Double.valueOf(str.trim()); break; case STRING: case VARCHAR: case CHAR: if(customTimeFormat != null){ ret = DateUtil.columnToDate(str,customTimeFormat); ret = DateUtil.timestampToString((Date)ret); } else { ret = str; } break; case BOOLEAN: ret = Boolean.valueOf(str.trim().toLowerCase()); break; case DATE: ret = DateUtil.columnToDate(str,customTimeFormat); break; case TIMESTAMP: case DATETIME: ret = DateUtil.columnToTimestamp(str,customTimeFormat); break; default: ret = str; } return ret; } }
StringUtil { public static Object string2col(String str, String type, SimpleDateFormat customTimeFormat) { if(str == null || str.length() == 0 || type == null){ return str; } ColumnType columnType = ColumnType.getType(type.toUpperCase()); Object ret; switch(columnType) { case TINYINT: ret = Byte.valueOf(str.trim()); break; case SMALLINT: ret = Short.valueOf(str.trim()); break; case INT: ret = Integer.valueOf(str.trim()); break; case MEDIUMINT: case BIGINT: ret = Long.valueOf(str.trim()); break; case FLOAT: ret = Float.valueOf(str.trim()); break; case DOUBLE: ret = Double.valueOf(str.trim()); break; case STRING: case VARCHAR: case CHAR: if(customTimeFormat != null){ ret = DateUtil.columnToDate(str,customTimeFormat); ret = DateUtil.timestampToString((Date)ret); } else { ret = str; } break; case BOOLEAN: ret = Boolean.valueOf(str.trim().toLowerCase()); break; case DATE: ret = DateUtil.columnToDate(str,customTimeFormat); break; case TIMESTAMP: case DATETIME: ret = DateUtil.columnToTimestamp(str,customTimeFormat); break; default: ret = str; } return ret; }  }
StringUtil { public static Object string2col(String str, String type, SimpleDateFormat customTimeFormat) { if(str == null || str.length() == 0 || type == null){ return str; } ColumnType columnType = ColumnType.getType(type.toUpperCase()); Object ret; switch(columnType) { case TINYINT: ret = Byte.valueOf(str.trim()); break; case SMALLINT: ret = Short.valueOf(str.trim()); break; case INT: ret = Integer.valueOf(str.trim()); break; case MEDIUMINT: case BIGINT: ret = Long.valueOf(str.trim()); break; case FLOAT: ret = Float.valueOf(str.trim()); break; case DOUBLE: ret = Double.valueOf(str.trim()); break; case STRING: case VARCHAR: case CHAR: if(customTimeFormat != null){ ret = DateUtil.columnToDate(str,customTimeFormat); ret = DateUtil.timestampToString((Date)ret); } else { ret = str; } break; case BOOLEAN: ret = Boolean.valueOf(str.trim().toLowerCase()); break; case DATE: ret = DateUtil.columnToDate(str,customTimeFormat); break; case TIMESTAMP: case DATETIME: ret = DateUtil.columnToTimestamp(str,customTimeFormat); break; default: ret = str; } return ret; }  static String convertRegularExpr(String str); static Object string2col(String str, String type, SimpleDateFormat customTimeFormat); static String col2string(Object column, String type); static String row2string(Row row, List<String> columnTypes, String delimiter, List<String> columnNames); static byte[] hexStringToByteArray(String hexString); }
StringUtil { public static Object string2col(String str, String type, SimpleDateFormat customTimeFormat) { if(str == null || str.length() == 0 || type == null){ return str; } ColumnType columnType = ColumnType.getType(type.toUpperCase()); Object ret; switch(columnType) { case TINYINT: ret = Byte.valueOf(str.trim()); break; case SMALLINT: ret = Short.valueOf(str.trim()); break; case INT: ret = Integer.valueOf(str.trim()); break; case MEDIUMINT: case BIGINT: ret = Long.valueOf(str.trim()); break; case FLOAT: ret = Float.valueOf(str.trim()); break; case DOUBLE: ret = Double.valueOf(str.trim()); break; case STRING: case VARCHAR: case CHAR: if(customTimeFormat != null){ ret = DateUtil.columnToDate(str,customTimeFormat); ret = DateUtil.timestampToString((Date)ret); } else { ret = str; } break; case BOOLEAN: ret = Boolean.valueOf(str.trim().toLowerCase()); break; case DATE: ret = DateUtil.columnToDate(str,customTimeFormat); break; case TIMESTAMP: case DATETIME: ret = DateUtil.columnToTimestamp(str,customTimeFormat); break; default: ret = str; } return ret; }  static String convertRegularExpr(String str); static Object string2col(String str, String type, SimpleDateFormat customTimeFormat); static String col2string(Object column, String type); static String row2string(Row row, List<String> columnTypes, String delimiter, List<String> columnNames); static byte[] hexStringToByteArray(String hexString);  }