static Value getSpannerValue()

in src/main/java/com/google/cloud/spanner/pgadapter/utils/CsvCopyParser.java [135:240]


    static Value getSpannerValue(SessionState sessionState, Type type, String recordValue)
        throws SpannerException {
      try {
        switch (type.getCode()) {
          case STRING:
            return Value.string(recordValue);
          case PG_JSONB:
            return Value.pgJsonb(recordValue);
          case BOOL:
            return Value.bool(recordValue == null ? null : BooleanParser.toBoolean(recordValue));
          case INT64:
            return Value.int64(recordValue == null ? null : Long.parseLong(recordValue));
          case FLOAT32:
            return Value.float32(recordValue == null ? null : Float.parseFloat(recordValue));
          case FLOAT64:
            return Value.float64(recordValue == null ? null : Double.parseDouble(recordValue));
          case PG_NUMERIC:
            return Value.pgNumeric(recordValue);
          case BYTES:
            if (recordValue == null) {
              return Value.bytes(null);
            } else if (recordValue.startsWith("\\x")) {
              return Value.bytes(ByteArray.copyFrom(Hex.decodeHex(recordValue.substring(2))));
            } else {
              throw SpannerExceptionFactory.newSpannerException(
                  ErrorCode.INVALID_ARGUMENT,
                  "COPY only supports the Hex format for bytea columns");
            }
          case DATE:
            return Value.date(recordValue == null ? null : Date.parseDate(recordValue));
          case TIMESTAMP:
            Timestamp timestamp =
                recordValue == null
                    ? null
                    : TimestampParser.toTimestamp(recordValue, sessionState.getTimezone());
            return Value.timestamp(timestamp);
          case ARRAY:
            switch (type.getArrayElementType().getCode()) {
              case STRING:
                return Value.stringArray(
                    cast(ArrayParser.stringArrayToList(recordValue, Oid.TEXT, sessionState, true)));
              case PG_JSONB:
                return Value.pgJsonbArray(
                    cast(
                        ArrayParser.stringArrayToList(recordValue, Oid.JSONB, sessionState, true)));
              case BOOL:
                return Value.boolArray(
                    cast(ArrayParser.stringArrayToList(recordValue, Oid.BOOL, sessionState, true)));
              case INT64:
                return Value.int64Array(
                    cast(ArrayParser.stringArrayToList(recordValue, Oid.INT8, sessionState, true)));
              case FLOAT32:
                return Value.float32Array(
                    cast(
                        ArrayParser.stringArrayToList(
                            recordValue, Oid.FLOAT4, sessionState, true)));
              case FLOAT64:
                return Value.float64Array(
                    cast(
                        ArrayParser.stringArrayToList(
                            recordValue, Oid.FLOAT8, sessionState, true)));
              case PG_NUMERIC:
                return Value.pgNumericArray(
                    cast(
                        ArrayParser.stringArrayToList(
                            recordValue, Oid.NUMERIC, sessionState, true)));
              case BYTES:
                return Value.bytesArray(
                    cast(
                        ArrayParser.stringArrayToList(recordValue, Oid.BYTEA, sessionState, true)));
              case DATE:
                return Value.dateArray(
                    cast(ArrayParser.stringArrayToList(recordValue, Oid.DATE, sessionState, true)));
              case TIMESTAMP:
                return Value.timestampArray(
                    cast(
                        ArrayParser.stringArrayToList(
                            recordValue, Oid.TIMESTAMPTZ, sessionState, true)));
            }
          default:
            SpannerException spannerException =
                SpannerExceptionFactory.newSpannerException(
                    ErrorCode.INVALID_ARGUMENT, "Unknown or unsupported type: " + type);
            logger.log(Level.SEVERE, spannerException.getMessage(), spannerException);
            throw spannerException;
        }
      } catch (NumberFormatException | DateTimeParseException e) {
        SpannerException spannerException =
            SpannerExceptionFactory.newSpannerException(
                ErrorCode.INVALID_ARGUMENT,
                "Invalid input syntax for type " + type + ":" + "\"" + recordValue + "\"",
                e);
        logger.log(Level.SEVERE, spannerException.getMessage(), spannerException);
        throw spannerException;
      } catch (IllegalArgumentException e) {
        SpannerException spannerException =
            SpannerExceptionFactory.newSpannerException(
                ErrorCode.INVALID_ARGUMENT, "Invalid input syntax", e);
        logger.log(Level.SEVERE, spannerException.getMessage(), spannerException);
        throw spannerException;
      } catch (Exception e) {
        SpannerException spannerException = SpannerExceptionFactory.asSpannerException(e);
        logger.log(Level.SEVERE, spannerException.getMessage(), spannerException);
        throw spannerException;
      }
    }