public void encodeAsBinary()

in src/main/protocol-impl/java/com/mysql/cj/protocol/a/StringValueEncoder.java [259:384]


    public void encodeAsBinary(Message msg, BindValue binding) {
        NativePacketPayload intoPacket = (NativePacketPayload) msg;

        String x = (String) binding.getValue();
        switch (binding.getMysqlType()) {
            case BOOLEAN:
            case BIT:
                Boolean b = null;
                if ("true".equalsIgnoreCase(x) || "Y".equalsIgnoreCase(x)) {
                    b = true;
                } else if ("false".equalsIgnoreCase(x) || "N".equalsIgnoreCase(x)) {
                    b = false;
                } else if (x.matches("-?\\d+\\.?\\d*")) {
                    b = !x.matches("-?[0]+[.]*[0]*");
                } else {
                    throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("PreparedStatement.66", new Object[] { x }),
                            this.exceptionInterceptor);
                }
                intoPacket.writeInteger(IntegerDataType.INT1, b ? 1L : 0L);
                return;
            case TINYINT:
            case TINYINT_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT1, Long.parseLong(x));
                return;
            case SMALLINT:
            case SMALLINT_UNSIGNED:
            case MEDIUMINT:
            case MEDIUMINT_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT2, Long.parseLong(x));
                return;
            case INT:
            case INT_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT4, Long.parseLong(x));
                return;
            case BIGINT:
            case BIGINT_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT8, Long.parseLong(x));
                return;
            case FLOAT:
            case FLOAT_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT4, Float.floatToIntBits(Float.parseFloat(x)));
                return;
            case DOUBLE:
            case DOUBLE_UNSIGNED:
                intoPacket.writeInteger(IntegerDataType.INT8, Double.doubleToLongBits(Double.parseDouble(x)));
                return;
            case DECIMAL:
            case DECIMAL_UNSIGNED:
                BigDecimal bd = getScaled(new BigDecimal(x), binding.getScaleOrLength());
                intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(bd.toPlainString(), this.charEncoding.getValue()));
                return;
            case CHAR:
            case ENUM:
            case SET:
            case VARCHAR:
            case TINYTEXT:
            case TEXT:
            case MEDIUMTEXT:
            case LONGTEXT:
            case JSON:
            case BINARY:
            case GEOMETRY:
            case VARBINARY:
            case TINYBLOB:
            case BLOB:
            case MEDIUMBLOB:
            case LONGBLOB:
                if (binding.isNational() && !this.charEncoding.getValue().equalsIgnoreCase("UTF-8") && !this.charEncoding.getValue().equalsIgnoreCase("utf8")) {
                    throw ExceptionFactory.createException(Messages.getString("ServerPreparedStatement.31"), this.exceptionInterceptor);
                }
                try {
                    intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(x, this.charEncoding.getValue()));
                } catch (CJException uEE) {
                    throw ExceptionFactory.createException(Messages.getString("ServerPreparedStatement.31") + this.charEncoding.getValue() + "'", uEE,
                            this.exceptionInterceptor);
                }
                return;
            case DATE:
                Object dt = TimeUtil.parseToDateTimeObject(x, binding.getMysqlType());
                if (dt instanceof LocalDate) {
                    writeDate(msg, InternalDate.from((LocalDate) dt));
                    return;
                } else if (dt instanceof LocalDateTime) {
                    writeDateTime(msg, InternalTimestamp.from((LocalDateTime) dt));
                    return;
                }
                break;
            case DATETIME:
            case TIMESTAMP:
                dt = TimeUtil.parseToDateTimeObject(x, binding.getMysqlType());
                if (dt instanceof LocalDate) {
                    writeDateTime(msg, InternalTimestamp.from((LocalDate) dt));
                    return;
                } else if (dt instanceof LocalDateTime) {
                    writeDateTime(msg, InternalTimestamp.from((LocalDateTime) dt));
                    return;
                }
                break;
            case TIME:
                dt = TimeUtil.parseToDateTimeObject(x, binding.getMysqlType());
                if (dt instanceof LocalTime) {
                    writeTime(msg, InternalTime.from((LocalTime) dt));
                    return;
                } else if (dt instanceof Duration) {
                    writeTime(msg, InternalTime.from(adjustDuration(Duration.ofNanos(((Duration) binding.getValue()).toNanos()), binding.getField())));
                    return;
                }
                break;
            case YEAR:
                dt = TimeUtil.parseToDateTimeObject(x, binding.getMysqlType());
                if (dt instanceof LocalDate) {
                    intoPacket.writeInteger(IntegerDataType.INT4, ((LocalDate) dt).getYear());
                    return;
                } else if (dt instanceof LocalDateTime) {
                    intoPacket.writeInteger(IntegerDataType.INT4, ((LocalDateTime) dt).getYear());
                    return;
                }
                break;

            default:
                break;
        }
        throw ExceptionFactory.createException(WrongArgumentException.class,
                Messages.getString("PreparedStatement.67", new Object[] { binding.getValue().getClass().getName(), binding.getMysqlType().toString() }),
                this.exceptionInterceptor);
    }