/** * @param tablename the table name * @param name the columnname * @param value the value * @return the mapped value */ Object toStatementValue(Tablename tablename, String name, Object value) { if (isNullOrEmpty(value)) { return null; } final DataType dataType = catalog.getColumnMetadata(tablename, name).getType(); // build in if (UDTValueMapper.isBuildInType(dataType)) { // enum if (DataTypes.isTextDataType(dataType) && Enum.class.isAssignableFrom(value.getClass())) { return value.toString(); } // byte buffer (byte[]) if (dataType.equals(DataType.blob()) && byte[].class.isAssignableFrom(value.getClass())) { return ByteBuffer.wrap((byte[]) value); } return value; // udt } else { return toUdtValue( tablename, catalog, catalog.getColumnMetadata(tablename, name).getType(), value); } }
/** * Deserializes the input parameter and returns an Object which must then be cast to a core data * type * * @param <T> type * @param in input * @param target target * @return Object object */ @SuppressWarnings("unchecked") public <T> T deserialize(Input in, Type target) { byte type = in.readDataType(); log.debug("Type: {}", type); while (type == DataTypes.CORE_SKIP) { type = in.readDataType(); log.debug("Type (skip): {}", type); } log.debug("Datatype: {}", DataTypes.toStringValue(type)); Object result; switch (type) { case DataTypes.CORE_NULL: result = in.readNull(target); break; case DataTypes.CORE_BOOLEAN: result = in.readBoolean(target); break; case DataTypes.CORE_NUMBER: result = in.readNumber(target); break; case DataTypes.CORE_STRING: result = in.readString(target); break; case DataTypes.CORE_DATE: result = in.readDate(target); break; case DataTypes.CORE_ARRAY: result = in.readArray(this, target); break; case DataTypes.CORE_MAP: result = in.readMap(this, target); break; case DataTypes.CORE_XML: result = in.readXML(target); break; case DataTypes.CORE_OBJECT: result = in.readObject(this, target); break; case DataTypes.CORE_BYTEARRAY: result = in.readByteArray(target); break; case DataTypes.OPT_REFERENCE: result = in.readReference(target); break; default: result = in.readCustom(target); break; } return (T) postProcessExtension(result, target); }
/** * Deserializes the input parameter and returns an Object which must then be cast to a core data * type * * @param in * @param target * @return Object */ public <T> T deserialize(Input in, Class<T> target) { byte type = in.readDataType(); while (type == DataTypes.CORE_SKIP) { type = in.readDataType(); } if (log.isDebugEnabled()) { log.debug("Datatype: " + DataTypes.toStringValue(type)); } Object result; switch (type) { case DataTypes.CORE_NULL: result = in.readNull(); break; case DataTypes.CORE_BOOLEAN: result = in.readBoolean(); break; case DataTypes.CORE_NUMBER: result = in.readNumber(); break; case DataTypes.CORE_STRING: result = in.readString(); break; case DataTypes.CORE_DATE: result = in.readDate(); break; case DataTypes.CORE_ARRAY: result = in.readArray(this); break; case DataTypes.CORE_MAP: result = in.readMap(this); break; case DataTypes.CORE_XML: result = in.readXML(); break; case DataTypes.CORE_OBJECT: result = in.readObject(this); break; case DataTypes.CORE_BYTEARRAY: result = in.readByteArray(); break; case DataTypes.OPT_REFERENCE: result = in.readReference(); break; default: result = in.readCustom(); break; } return (T) postProcessExtension(result, target); }
static <T, U> U getFromResultSet(ExecuteContext ctx, Field<U> field, int index) throws SQLException { @SuppressWarnings("unchecked") Converter<T, U> converter = (Converter<T, U>) DataTypes.converter(field.getType()); if (converter != null) { return converter.from(getFromResultSet(ctx, converter.fromType(), index)); } else { return getFromResultSet(ctx, field.getType(), index); } }
// Read a single field name & type, plus any enum/factor/level info private PMMLParser pDataField() { HashMap<String, String> attrs = expect("DataField").attrs(); String name = attrs.get("name"); _types.put(name, DataTypes.parse(attrs.get("dataType"))); if (peek() == '/') return expect("/>"); expect('>'); ArrayList<String> str = new ArrayList(); while (skipWS().expect('<').peek() != '/') str.add(pDataFieldValue()); String[] ss = str.toArray(new String[0]); Arrays.sort(ss, null); _enums.put(name, ss); return expect("/DataField>"); }
@SuppressWarnings("unchecked") public static <T> T getFromStatement(ExecuteContext ctx, Class<? extends T> type, int index) throws SQLException { CallableStatement stmt = (CallableStatement) ctx.statement(); if (type == Blob.class) { return (T) stmt.getBlob(index); } else if (type == Boolean.class) { return (T) checkWasNull(stmt, Boolean.valueOf(stmt.getBoolean(index))); } else if (type == BigInteger.class) { BigDecimal result = stmt.getBigDecimal(index); return (T) (result == null ? null : result.toBigInteger()); } else if (type == BigDecimal.class) { return (T) stmt.getBigDecimal(index); } else if (type == Byte.class) { return (T) checkWasNull(stmt, Byte.valueOf(stmt.getByte(index))); } else if (type == byte[].class) { return (T) stmt.getBytes(index); } else if (type == Clob.class) { return (T) stmt.getClob(index); } else if (type == Date.class) { return (T) stmt.getDate(index); } else if (type == Double.class) { return (T) checkWasNull(stmt, Double.valueOf(stmt.getDouble(index))); } else if (type == Float.class) { return (T) checkWasNull(stmt, Float.valueOf(stmt.getFloat(index))); } else if (type == Integer.class) { return (T) checkWasNull(stmt, Integer.valueOf(stmt.getInt(index))); } else if (type == Long.class) { return (T) checkWasNull(stmt, Long.valueOf(stmt.getLong(index))); } else if (type == Short.class) { return (T) checkWasNull(stmt, Short.valueOf(stmt.getShort(index))); } else if (type == String.class) { return (T) stmt.getString(index); } else if (type == Time.class) { return (T) stmt.getTime(index); } else if (type == Timestamp.class) { return (T) stmt.getTimestamp(index); } else if (type == YearToMonth.class) { if (ctx.getDialect() == POSTGRES) { Object object = stmt.getObject(index); return (T) (object == null ? null : PostgresUtils.toYearToMonth(object)); } else { String string = stmt.getString(index); return (T) (string == null ? null : YearToMonth.valueOf(string)); } } else if (type == DayToSecond.class) { if (ctx.getDialect() == POSTGRES) { Object object = stmt.getObject(index); return (T) (object == null ? null : PostgresUtils.toDayToSecond(object)); } else { String string = stmt.getString(index); return (T) (string == null ? null : DayToSecond.valueOf(string)); } } else if (type == UByte.class) { String string = stmt.getString(index); return (T) (string == null ? null : UByte.valueOf(string)); } else if (type == UShort.class) { String string = stmt.getString(index); return (T) (string == null ? null : UShort.valueOf(string)); } else if (type == UInteger.class) { String string = stmt.getString(index); return (T) (string == null ? null : UInteger.valueOf(string)); } else if (type == ULong.class) { String string = stmt.getString(index); return (T) (string == null ? null : ULong.valueOf(string)); } // The type byte[] is handled earlier. byte[][] can be handled here else if (type.isArray()) { return (T) convertArray(stmt.getObject(index), (Class<? extends Object[]>) type); } else if (ArrayRecord.class.isAssignableFrom(type)) { return (T) getArrayRecord(ctx, stmt.getArray(index), (Class<? extends ArrayRecord<?>>) type); } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, stmt.getString(index)); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, stmt.getString(index)); } else if (UDTRecord.class.isAssignableFrom(type)) { switch (ctx.getDialect()) { case POSTGRES: return (T) pgNewUDTRecord(type, stmt.getObject(index)); } return (T) stmt.getObject(index, DataTypes.udtRecords()); } else if (Result.class.isAssignableFrom(type)) { ResultSet nested = (ResultSet) stmt.getObject(index); return (T) getNewFactory(ctx).fetch(nested); } else { return (T) stmt.getObject(index); } }
@SuppressWarnings("unchecked") private static <T> T getFromResultSet(ExecuteContext ctx, Class<? extends T> type, int index) throws SQLException { ResultSet rs = ctx.resultSet(); if (type == Blob.class) { return (T) rs.getBlob(index); } else if (type == Boolean.class) { return (T) checkWasNull(rs, Boolean.valueOf(rs.getBoolean(index))); } else if (type == BigInteger.class) { // The SQLite JDBC driver doesn't support BigDecimals if (ctx.getDialect() == SQLDialect.SQLITE) { return Convert.convert(rs.getString(index), (Class<? extends T>) BigInteger.class); } else { BigDecimal result = rs.getBigDecimal(index); return (T) (result == null ? null : result.toBigInteger()); } } else if (type == BigDecimal.class) { // The SQLite JDBC driver doesn't support BigDecimals if (ctx.getDialect() == SQLDialect.SQLITE) { return Convert.convert(rs.getString(index), (Class<? extends T>) BigDecimal.class); } else { return (T) rs.getBigDecimal(index); } } else if (type == Byte.class) { return (T) checkWasNull(rs, Byte.valueOf(rs.getByte(index))); } else if (type == byte[].class) { return (T) rs.getBytes(index); } else if (type == Clob.class) { return (T) rs.getClob(index); } else if (type == Date.class) { return (T) getDate(ctx.getDialect(), rs, index); } else if (type == Double.class) { return (T) checkWasNull(rs, Double.valueOf(rs.getDouble(index))); } else if (type == Float.class) { return (T) checkWasNull(rs, Float.valueOf(rs.getFloat(index))); } else if (type == Integer.class) { return (T) checkWasNull(rs, Integer.valueOf(rs.getInt(index))); } else if (type == Long.class) { return (T) checkWasNull(rs, Long.valueOf(rs.getLong(index))); } else if (type == Short.class) { return (T) checkWasNull(rs, Short.valueOf(rs.getShort(index))); } else if (type == String.class) { return (T) rs.getString(index); } else if (type == Time.class) { return (T) getTime(ctx.getDialect(), rs, index); } else if (type == Timestamp.class) { return (T) getTimestamp(ctx.getDialect(), rs, index); } else if (type == YearToMonth.class) { if (ctx.getDialect() == POSTGRES) { Object object = rs.getObject(index); return (T) (object == null ? null : PostgresUtils.toYearToMonth(object)); } else { String string = rs.getString(index); return (T) (string == null ? null : YearToMonth.valueOf(string)); } } else if (type == DayToSecond.class) { if (ctx.getDialect() == POSTGRES) { Object object = rs.getObject(index); return (T) (object == null ? null : PostgresUtils.toDayToSecond(object)); } else { String string = rs.getString(index); return (T) (string == null ? null : DayToSecond.valueOf(string)); } } else if (type == UByte.class) { String string = rs.getString(index); return (T) (string == null ? null : UByte.valueOf(string)); } else if (type == UShort.class) { String string = rs.getString(index); return (T) (string == null ? null : UShort.valueOf(string)); } else if (type == UInteger.class) { String string = rs.getString(index); return (T) (string == null ? null : UInteger.valueOf(string)); } else if (type == ULong.class) { String string = rs.getString(index); return (T) (string == null ? null : ULong.valueOf(string)); } // The type byte[] is handled earlier. byte[][] can be handled here else if (type.isArray()) { switch (ctx.getDialect()) { case POSTGRES: { return pgGetArray(ctx, type, index); } default: // Note: due to a HSQLDB bug, it is not recommended to call rs.getObject() here: // See https://sourceforge.net/tracker/?func=detail&aid=3181365&group_id=23316&atid=378131 return (T) convertArray(rs.getArray(index), (Class<? extends Object[]>) type); } } else if (ArrayRecord.class.isAssignableFrom(type)) { return (T) getArrayRecord(ctx, rs.getArray(index), (Class<? extends ArrayRecord<?>>) type); } else if (EnumType.class.isAssignableFrom(type)) { return getEnumType(type, rs.getString(index)); } else if (MasterDataType.class.isAssignableFrom(type)) { return (T) getMasterDataType(type, rs.getObject(index)); } else if (UDTRecord.class.isAssignableFrom(type)) { switch (ctx.getDialect()) { case POSTGRES: return (T) pgNewUDTRecord(type, rs.getObject(index)); } return (T) rs.getObject(index, DataTypes.udtRecords()); } else if (Result.class.isAssignableFrom(type)) { ResultSet nested = (ResultSet) rs.getObject(index); return (T) getNewFactory(ctx).fetch(nested); } else { return (T) rs.getObject(index); } }
static { try { xmlTypeToDataType.put("string", DataTypes.getTypeString(DataTypes.STRING)); xmlTypeToDataType.put("byte", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("decimal", DataTypes.getTypeString(DataTypes.BIGDECIMAL)); xmlTypeToDataType.put("double", DataTypes.getTypeString(DataTypes.DOUBLE)); xmlTypeToDataType.put("float", DataTypes.getTypeString(DataTypes.DOUBLE)); xmlTypeToDataType.put("int", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("integer", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("negativeInteger", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("nonNegativeInteger", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("nonPositiveInteger", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("positiveInteger", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("short", DataTypes.getTypeString(DataTypes.INT)); xmlTypeToDataType.put("date", DataTypes.getTypeString(DataTypes.DATE)); xmlTypeToDataType.put("dateTime", DataTypes.getTypeString(DataTypes.TIMESTAMP)); xmlTypeToDataType.put("time", DataTypes.getTypeString(DataTypes.TIME)); } catch (OdaException e) { // Should not arrive here } }
public static DataTypes parse(String s) { return DataTypes.valueOf(s.toUpperCase()); }