private void setInternalObject()

in src/main/java/org/mariadb/jdbc/BasePrepareStatement.java [947:1154]


  private void setInternalObject(
      final int parameterIndex, final Object obj, final int targetSqlType, final long scaleOrLength)
      throws SQLException {
    switch (targetSqlType) {
      case Types.ARRAY:
      case Types.DATALINK:
      case Types.JAVA_OBJECT:
      case Types.REF:
      case Types.ROWID:
      case Types.SQLXML:
      case Types.STRUCT:
        throw exceptionFactory.notSupported("Type not supported");
      default:
        break;
    }

    if (obj == null) {
      setNull(parameterIndex, Types.INTEGER);
    } else if (obj instanceof String || obj instanceof Character) {
      if (targetSqlType == Types.BLOB) {
        throw exceptionFactory.create(
            String.format(
                "Cannot convert a %s to a Blob", obj instanceof String ? "string" : "character"));
      }
      String str = obj instanceof String ? (String) obj : ((Character) obj).toString();
      try {
        switch (targetSqlType) {
          case Types.BIT:
          case Types.BOOLEAN:
            setBoolean(parameterIndex, !("false".equalsIgnoreCase(str) || "0".equals(str)));
            break;
          case Types.TINYINT:
            setByte(parameterIndex, Byte.parseByte(str));
            break;
          case Types.SMALLINT:
            setShort(parameterIndex, Short.parseShort(str));
            break;
          case Types.INTEGER:
            setInt(parameterIndex, Integer.parseInt(str));
            break;
          case Types.DOUBLE:
          case Types.FLOAT:
            setDouble(parameterIndex, Double.valueOf(str));
            break;
          case Types.REAL:
            setFloat(parameterIndex, Float.valueOf(str));
            break;
          case Types.BIGINT:
            setLong(parameterIndex, Long.valueOf(str));
            break;
          case Types.DECIMAL:
          case Types.NUMERIC:
            setBigDecimal(parameterIndex, new BigDecimal(str));
            break;
          case Types.CLOB:
          case Types.NCLOB:
          case Types.CHAR:
          case Types.VARCHAR:
          case Types.LONGVARCHAR:
          case Types.NCHAR:
          case Types.NVARCHAR:
          case Types.LONGNVARCHAR:
            setString(parameterIndex, str);
            break;
          case Types.TIMESTAMP:
            if (str.startsWith("0000-00-00")) {
              setTimestamp(parameterIndex, null);
            } else {
              setTimestamp(parameterIndex, Timestamp.valueOf(str));
            }
            break;
          case Types.TIME:
            setTime(parameterIndex, Time.valueOf((String) obj));
            break;
          case Types.TIME_WITH_TIMEZONE:
            setParameter(
                parameterIndex,
                new OffsetTimeParameter(
                    OffsetTime.parse(str), protocol.getTimeZone(), useFractionalSeconds, options));
            break;
          case Types.TIMESTAMP_WITH_TIMEZONE:
            setParameter(
                parameterIndex,
                new ZonedDateTimeParameter(
                    ZonedDateTime.parse(str, SPEC_ISO_ZONED_DATE_TIME),
                    protocol.getTimeZone(),
                    useFractionalSeconds,
                    options));
            break;
          default:
            throw exceptionFactory.create(
                String.format("Could not convert [%s] to %s", str, targetSqlType));
        }
      } catch (IllegalArgumentException e) {
        throw exceptionFactory.create(
            String.format("Could not convert [%s] to %s", str, targetSqlType), e);
      }
    } else if (obj instanceof Number) {
      Number bd = (Number) obj;
      switch (targetSqlType) {
        case Types.TINYINT:
          setByte(parameterIndex, bd.byteValue());
          break;
        case Types.SMALLINT:
          setShort(parameterIndex, bd.shortValue());
          break;
        case Types.INTEGER:
          setInt(parameterIndex, bd.intValue());
          break;
        case Types.BIGINT:
          setLong(parameterIndex, bd.longValue());
          break;
        case Types.FLOAT:
        case Types.DOUBLE:
          setDouble(parameterIndex, bd.doubleValue());
          break;
        case Types.REAL:
          setFloat(parameterIndex, bd.floatValue());
          break;
        case Types.DECIMAL:
        case Types.NUMERIC:
          if (obj instanceof BigDecimal) {
            setBigDecimal(parameterIndex, (BigDecimal) obj);
          } else if (obj instanceof Double || obj instanceof Float) {
            setDouble(parameterIndex, bd.doubleValue());
          } else {
            setLong(parameterIndex, bd.longValue());
          }
          break;
        case Types.BIT:
          setBoolean(parameterIndex, bd.shortValue() != 0);
          break;
        case Types.CHAR:
        case Types.VARCHAR:
          setString(parameterIndex, bd.toString());
          break;
        default:
          throw exceptionFactory.create(
              String.format("Could not convert [%s] to %s", bd, targetSqlType));
      }
    } else if (obj instanceof byte[]) {
      if (targetSqlType == Types.BINARY
          || targetSqlType == Types.VARBINARY
          || targetSqlType == Types.LONGVARBINARY) {
        setBytes(parameterIndex, (byte[]) obj);
      } else {
        throw exceptionFactory.create(
            "Can only convert a byte[] to BINARY, VARBINARY or LONGVARBINARY");
      }

    } else if (obj instanceof Time) {
      setTime(parameterIndex, (Time) obj); // it is just a string anyway
    } else if (obj instanceof Timestamp) {
      setTimestamp(parameterIndex, (Timestamp) obj);
    } else if (obj instanceof Date) {
      setDate(parameterIndex, (Date) obj);
    } else if (obj instanceof java.util.Date) {
      long timemillis = ((java.util.Date) obj).getTime();
      if (targetSqlType == Types.DATE) {
        setDate(parameterIndex, new Date(timemillis));
      } else if (targetSqlType == Types.TIME) {
        setTime(parameterIndex, new Time(timemillis));
      } else if (targetSqlType == Types.TIMESTAMP) {
        setTimestamp(parameterIndex, new Timestamp(timemillis));
      }
    } else if (obj instanceof Boolean) {
      setBoolean(parameterIndex, (Boolean) obj);
    } else if (obj instanceof Blob) {
      setBlob(parameterIndex, (Blob) obj);
    } else if (obj instanceof Clob) {
      setClob(parameterIndex, (Clob) obj);
    } else if (obj instanceof InputStream) {
      setBinaryStream(parameterIndex, (InputStream) obj, scaleOrLength);
    } else if (obj instanceof Reader) {
      setCharacterStream(parameterIndex, (Reader) obj, scaleOrLength);
    } else if (obj instanceof LocalDateTime) {
      setTimestamp(parameterIndex, Timestamp.valueOf((LocalDateTime) obj));
    } else if (obj instanceof Instant) {
      setTimestamp(parameterIndex, Timestamp.from((Instant) obj));
    } else if (obj instanceof LocalDate) {
      setDate(parameterIndex, Date.valueOf((LocalDate) obj));
    } else if (obj instanceof OffsetDateTime) {
      setParameter(
          parameterIndex,
          new ZonedDateTimeParameter(
              ((OffsetDateTime) obj).toZonedDateTime(),
              protocol.getTimeZone(),
              useFractionalSeconds,
              options));
    } else if (obj instanceof OffsetTime) {
      setParameter(
          parameterIndex,
          new OffsetTimeParameter(
              (OffsetTime) obj, protocol.getTimeZone(), useFractionalSeconds, options));
    } else if (obj instanceof ZonedDateTime) {
      setParameter(
          parameterIndex,
          new ZonedDateTimeParameter(
              (ZonedDateTime) obj, protocol.getTimeZone(), useFractionalSeconds, options));
    } else if (obj instanceof LocalTime) {
      setParameter(parameterIndex, new LocalTimeParameter((LocalTime) obj, useFractionalSeconds));
    } else {
      throw exceptionFactory.create(
          String.format(
              "Could not set parameter in setObject, could not convert: %s to %s",
              obj.getClass(), targetSqlType));
    }
  }