- Add the Codota plugin to your IDE and get smart completions
private void myMethod () {}
protected Array makeArray(int oid, String fieldString) throws SQLException { return new PgArray(this, oid, fieldString); }
private int calcRemainingDataLength(int[] dims, int pos, int elementOid, int thisDimension) { if (thisDimension == dims.length - 1) { for (int i = 0; i < dims[thisDimension]; ++i) { int len = ByteConverter.int4(fieldBytes, pos); pos += 4; if (len == -1) { continue; } pos += len; } } else { pos = calcRemainingDataLength(dims, elementOid, pos, thisDimension + 1); } return pos; }
/** * {@inheritDoc} */ @Override public void appendArray(StringBuilder sb, char delim, String[] array) { sb.append('{'); for (int i = 0; i < array.length; ++i) { if (i > 0) { sb.append(delim); } if (array[i] == null) { sb.append('N'); sb.append('U'); sb.append('L'); sb.append('L'); } else { PgArray.escapeArrayElement(sb, array[i]); } } sb.append('}'); }
private Object getArray(PgProto.DatumMessage datumMessage, PgConnectionSupplier connection, int columnType) { // Currently the logical decoding plugin sends unhandled types as a byte array containing the string // representation (in Postgres) of the array value. // The approach to decode this is sub-optimal but the only way to improve this is to update the plugin. // Reasons for it being sub-optimal include: // 1. It requires a Postgres JDBC connection to deserialize // 2. The byte-array is a serialised string but we make the assumption its UTF-8 encoded (which it will // be in most cases) // 3. For larger arrays and especially 64-bit integers and the like it is less efficient sending string // representations over the wire. try { byte[] data = datumMessage.hasDatumBytes()? datumMessage.getDatumBytes().toByteArray() : null; if (data == null) return null; String dataString = new String(data, Charset.forName("UTF-8")); PgArray arrayData = new PgArray(connection.get(), columnType, dataString); Object deserializedArray = arrayData.getArray(); return Arrays.asList((Object[])deserializedArray); } catch (SQLException e) { LOGGER.warn("Unexpected exception trying to process PgArray column '{}'", datumMessage.getColumnName(), e); } return null; } }
return readBinaryResultSet((int) index, count); buildArrayList(); t[1] = v == null ? null : connection.encodeString(toString((PgArrayList) v)); rows.add(t);
/** * Convert array list to PG String representation (e.g. {0,1,2}). */ private String toString(PgArrayList list) throws SQLException { if (list == null) { return "NULL"; } StringBuilder b = new StringBuilder().append('{'); char delim = connection.getTypeInfo().getArrayDelimiter(oid); for (int i = 0; i < list.size(); i++) { Object v = list.get(i); if (i > 0) { b.append(delim); } if (v == null) { b.append("NULL"); } else if (v instanceof PgArrayList) { b.append(toString((PgArrayList) v)); } else { escapeArrayElement(b, (String) v); } } b.append('}'); return b.toString(); }
public String toString() { if (fieldString == null && fieldBytes != null) { try { Object array = readBinaryArray(1, 0); final PrimitiveArraySupport arraySupport = PrimitiveArraySupport.getArraySupport(array); if (arraySupport != null) { fieldString = arraySupport.toArrayString(connection.getTypeInfo().getArrayDelimiter(oid), array); } else { java.sql.Array tmpArray = connection.createArrayOf(getBaseTypeName(), (Object[]) array); fieldString = tmpArray.toString(); } } catch (SQLException e) { fieldString = "NULL"; // punt } } return fieldString; }
final String typeName = arrayObj.getBaseTypeName(); final String value = arrayObj.toString(); node = new ArrayResultNode(name, value, typeName, typeId, pgSet.getStatement().getConnection()); } else {
public Object getArrayImpl(long index, int count, Map<String, Class<?>> map) throws SQLException { // for now maps aren't supported. if (map != null && !map.isEmpty()) { throw org.postgresql.Driver.notImplemented(this.getClass(), "getArrayImpl(long,int,Map)"); } // array index is out of range if (index < 1) { throw new PSQLException(GT.tr("The array index is out of range: {0}", index), PSQLState.DATA_ERROR); } if (fieldBytes != null) { return readBinaryArray((int) index, count); } if (fieldString == null) { return null; } buildArrayList(); if (count == 0) { count = arrayList.size(); } // array index out of range if ((--index) + count > arrayList.size()) { throw new PSQLException( GT.tr("The array index is out of range: {0}, number of elements: {1}.", index + count, (long) arrayList.size()), PSQLState.DATA_ERROR); } return buildArray(arrayList, (int) index, count); }
: (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : BooleanTypeUtil.castToBoolean((String) o)); } else { pa[length++] = o == null ? false : BooleanTypeUtil.castToBoolean((String) o); : (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : PgResultSet.toShort((String) o)); } else { pa[length++] = o == null ? 0 : PgResultSet.toShort((String) o); : (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : PgResultSet.toInt((String) o)); } else { pa[length++] = o == null ? 0 : PgResultSet.toInt((String) o); : (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : PgResultSet.toLong((String) o)); } else { pa[length++] = o == null ? 0L : PgResultSet.toLong((String) o); oa[length++] = dims > 1 && v != null ? buildArray((PgArrayList) v, 0, -1) : (v == null ? null : PgResultSet.toBigDecimal((String) v)); : (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : PgResultSet.toFloat((String) o)); } else { pa[length++] = o == null ? 0f : PgResultSet.toFloat((String) o); : (dims > 1 ? buildArray((PgArrayList) o, 0, -1) : PgResultSet.toDouble((String) o)); } else { pa[length++] = o == null ? 0d : PgResultSet.toDouble((String) o); oa[length++] = dims > 1 && v != null ? buildArray((PgArrayList) v, 0, -1) : v;
public int getBaseType() throws SQLException { return connection.getTypeInfo().getSQLType(getBaseTypeName()); }
private Object readBinaryArray(int index, int count) throws SQLException { int dimensions = ByteConverter.int4(fieldBytes, 0); // int flags = ByteConverter.int4(fieldBytes, 4); // bit 0: 0=no-nulls, 1=has-nulls int elementOid = ByteConverter.int4(fieldBytes, 8); int pos = 12; int[] dims = new int[dimensions]; for (int d = 0; d < dimensions; ++d) { dims[d] = ByteConverter.int4(fieldBytes, pos); pos += 4; /* int lbound = ByteConverter.int4(fieldBytes, pos); */ pos += 4; } if (dimensions == 0) { return java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), 0); } if (count > 0) { dims[0] = Math.min(count, dims[0]); } Object arr = java.lang.reflect.Array.newInstance(elementOidToClass(elementOid), dims); try { storeValues((Object[]) arr, elementOid, dims, pos, 0, index); } catch (IOException ioe) { throw new PSQLException( GT.tr( "Invalid character data was found. This is most likely caused by stored data containing characters that are invalid for the character set the database was created in. The most common example of this is storing 8bit data in a SQL_ASCII database."), PSQLState.DATA_ERROR, ioe); } return arr; }
public Object getArray() throws SQLException { return getArrayImpl(1, 0, null); }
public String getBaseTypeName() throws SQLException { buildArrayList(); int elementOID = connection.getTypeInfo().getPGArrayElement(oid); return connection.getTypeInfo().getPGType(elementOID); }
public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException { return getArrayImpl(index, count, map); }
private Object getArray(PgProto.DatumMessage datumMessage, PgConnectionSupplier connection, int columnType) { // Currently the logical decoding plugin sends unhandled types as a byte array containing the string // representation (in Postgres) of the array value. // The approach to decode this is sub-optimal but the only way to improve this is to update the plugin. // Reasons for it being sub-optimal include: // 1. It requires a Postgres JDBC connection to deserialize // 2. The byte-array is a serialised string but we make the assumption its UTF-8 encoded (which it will // be in most cases) // 3. For larger arrays and especially 64-bit integers and the like it is less efficient sending string // representations over the wire. try { byte[] data = datumMessage.hasDatumBytes()? datumMessage.getDatumBytes().toByteArray() : null; if (data == null) return null; String dataString = new String(data, Charset.forName("UTF-8")); PgArray arrayData = new PgArray(connection.get(), columnType, dataString); Object deserializedArray = arrayData.getArray(); return Arrays.asList((Object[])deserializedArray); } catch (SQLException e) { LOGGER.warn("Unexpected exception trying to process PgArray column '{}'", datumMessage.getColumnName(), e); } return null; } }
protected Array makeArray(int oid, byte[] value) throws SQLException { return new PgArray(connection, oid, value); }
public Object getArray(long index, int count) throws SQLException { return getArrayImpl(index, count, null); }
int dimensionsLeft = dims.length - nextDimension; for (int i = 1; i < index; ++i) { pos = calcRemainingDataLength(dims, pos, elementOid, nextDimension); ByteConverter.int4(rowData[0], 0, i + index); rows.add(rowData); int dataEndPos = calcRemainingDataLength(dims, pos, elementOid, nextDimension); int dataLength = dataEndPos - pos; rowData[1] = new byte[12 + 8 * dimensionsLeft + dataLength];