|
| 1 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | +// Copyright (c) 2012-2014 Monty Program Ab |
| 3 | +// Copyright (c) 2015-2024 MariaDB Corporation Ab |
| 4 | +package org.mariadb.jdbc.plugin.array; |
| 5 | + |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.sql.*; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Map; |
| 10 | +import org.mariadb.jdbc.client.ColumnDecoder; |
| 11 | +import org.mariadb.jdbc.client.Context; |
| 12 | +import org.mariadb.jdbc.client.DataType; |
| 13 | +import org.mariadb.jdbc.client.result.CompleteResult; |
| 14 | +import org.mariadb.jdbc.util.constants.ColumnFlags; |
| 15 | + |
| 16 | +public class FloatArray implements Array { |
| 17 | + |
| 18 | + private final float[] val; |
| 19 | + private Context context; |
| 20 | + |
| 21 | + public FloatArray(float[] val, Context context) { |
| 22 | + this.val = val; |
| 23 | + this.context = context; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public String getBaseTypeName() throws SQLException { |
| 28 | + return "float[]"; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public int getBaseType() throws SQLException { |
| 33 | + return Types.FLOAT; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Object getArray() throws SQLException { |
| 38 | + return this.val; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Object getArray(Map<String, Class<?>> map) throws SQLException { |
| 43 | + throw new SQLFeatureNotSupportedException( |
| 44 | + "getArray(Map<String, Class<?>> map) is not supported"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Object getArray(long index, int count) throws SQLException { |
| 49 | + if (index < 1 || index > val.length) { |
| 50 | + throw new SQLException( |
| 51 | + String.format( |
| 52 | + "Wrong index position. Is %s but must be in 1-%s range", index, val.length)); |
| 53 | + } |
| 54 | + if (count < 0 || (index - 1 + count) > val.length) { |
| 55 | + throw new SQLException( |
| 56 | + String.format( |
| 57 | + "Count value is too big. Count is %s but cannot be > to %s", |
| 58 | + count, val.length - (index - 1))); |
| 59 | + } |
| 60 | + |
| 61 | + return Arrays.copyOfRange(val, (int) index - 1, (int) (index - 1) + count); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException { |
| 66 | + throw new SQLFeatureNotSupportedException( |
| 67 | + "getArray(long index, int count, Map<String, Class<?>> map) is not supported"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public ResultSet getResultSet() throws SQLException { |
| 72 | + return getResultSet(1, this.val.length); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException { |
| 77 | + throw new SQLFeatureNotSupportedException( |
| 78 | + "getResultSet(Map<String, Class<?>> map) is not supported"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public ResultSet getResultSet(long index, int count) throws SQLException { |
| 83 | + byte[][] rows = new byte[count][]; |
| 84 | + for (int i = 0; i < count; i++) { |
| 85 | + rows[i] = Float.toString(this.val[(int) index - 1 + i]).getBytes(StandardCharsets.US_ASCII); |
| 86 | + } |
| 87 | + |
| 88 | + return new CompleteResult( |
| 89 | + new ColumnDecoder[] {ColumnDecoder.create("Array", DataType.FLOAT, ColumnFlags.NOT_NULL)}, |
| 90 | + rows, |
| 91 | + context, |
| 92 | + ResultSet.TYPE_SCROLL_INSENSITIVE); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) |
| 97 | + throws SQLException { |
| 98 | + throw new SQLFeatureNotSupportedException( |
| 99 | + "getResultSet(long index, int count, Map<String, Class<?>> map) is not supported"); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void free() {} |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean equals(Object o) { |
| 107 | + if (this == o) return true; |
| 108 | + if (o == null || getClass() != o.getClass()) return false; |
| 109 | + |
| 110 | + FloatArray that = (FloatArray) o; |
| 111 | + |
| 112 | + return Arrays.equals(val, that.val); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int hashCode() { |
| 117 | + return Arrays.hashCode(val); |
| 118 | + } |
| 119 | +} |
0 commit comments