-
Notifications
You must be signed in to change notification settings - Fork 819
/
PgArray.java
508 lines (434 loc) · 15.7 KB
/
PgArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright (c) 2004, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.jdbc;
import static org.postgresql.util.internal.Nullness.castNonNull;
import org.postgresql.Driver;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.BaseStatement;
import org.postgresql.core.Field;
import org.postgresql.core.Oid;
import org.postgresql.core.Tuple;
import org.postgresql.jdbc.ArrayDecoding.PgArrayList;
import org.postgresql.jdbc2.ArrayAssistantRegistry;
import org.postgresql.util.ByteConverter;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* <p>Array is used collect one column of query result data.</p>
*
* <p>Read a field of type Array into either a natively-typed Java array object or a ResultSet.
* Accessor methods provide the ability to capture array slices.</p>
*
* <p>Other than the constructor all methods are direct implementations of those specified for
* java.sql.Array. Please refer to the javadoc for java.sql.Array for detailed descriptions of the
* functionality and parameters of the methods of this class.</p>
*
* @see ResultSet#getArray
*/
public class PgArray implements Array {
static {
ArrayAssistantRegistry.register(Oid.UUID, new UUIDArrayAssistant());
ArrayAssistantRegistry.register(Oid.UUID_ARRAY, new UUIDArrayAssistant());
}
/**
* A database connection.
*/
protected @Nullable BaseConnection connection;
/**
* The OID of this field.
*/
private final int oid;
/**
* Field value as String.
*/
protected @Nullable String fieldString;
/**
* Value of field as {@link PgArrayList}. Will be initialized only once within
* {@link #buildArrayList(String)}.
*/
protected ArrayDecoding.@Nullable PgArrayList arrayList;
protected byte @Nullable [] fieldBytes;
private final ResourceLock lock = new ResourceLock();
private PgArray(BaseConnection connection, int oid) throws SQLException {
this.connection = connection;
this.oid = oid;
}
/**
* Create a new Array.
*
* @param connection a database connection
* @param oid the oid of the array datatype
* @param fieldString the array data in string form
* @throws SQLException if something wrong happens
*/
public PgArray(BaseConnection connection, int oid, @Nullable String fieldString)
throws SQLException {
this(connection, oid);
this.fieldString = fieldString;
}
/**
* Create a new Array.
*
* @param connection a database connection
* @param oid the oid of the array datatype
* @param fieldBytes the array data in byte form
* @throws SQLException if something wrong happens
*/
public PgArray(BaseConnection connection, int oid, byte @Nullable [] fieldBytes)
throws SQLException {
this(connection, oid);
this.fieldBytes = fieldBytes;
}
private BaseConnection getConnection() {
return castNonNull(connection);
}
@Override
@SuppressWarnings("return")
public Object getArray() throws SQLException {
return getArrayImpl(1, 0, null);
}
@Override
@SuppressWarnings("return")
public Object getArray(long index, int count) throws SQLException {
return getArrayImpl(index, count, null);
}
@SuppressWarnings("return")
public Object getArrayImpl(Map<String, Class<?>> map) throws SQLException {
return getArrayImpl(1, 0, map);
}
@Override
@SuppressWarnings("return")
public Object getArray(Map<String, Class<?>> map) throws SQLException {
return getArrayImpl(map);
}
@Override
@SuppressWarnings("return")
public Object getArray(long index, int count, @Nullable Map<String, Class<?>> map)
throws SQLException {
return getArrayImpl(index, count, map);
}
public @Nullable Object getArrayImpl(long index, int count, @Nullable Map<String, Class<?>> map)
throws SQLException {
// for now maps aren't supported.
if (map != null && !map.isEmpty()) {
throw 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(fieldBytes, (int) index, count);
}
if (fieldString == null) {
return null;
}
final PgArrayList arrayList = buildArrayList(fieldString);
if (count == 0) {
count = arrayList.size();
}
// array index out of range
if ((index - 1) + 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);
}
private Object readBinaryArray(byte[] fieldBytes, int index, int count) throws SQLException {
return ArrayDecoding.readBinaryArray(index, count, fieldBytes, getConnection());
}
private ResultSet readBinaryResultSet(byte[] fieldBytes, 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 (count > 0 && dimensions > 0) {
dims[0] = Math.min(count, dims[0]);
}
List<Tuple> rows = new ArrayList<>();
Field[] fields = new Field[2];
storeValues(fieldBytes, rows, fields, elementOid, dims, pos, 0, index);
BaseStatement stat = (BaseStatement) getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return stat.createDriverResultSet(fields, rows);
}
private int storeValues(byte[] fieldBytes, List<Tuple> rows, Field[] fields, int elementOid,
final int[] dims,
int pos, final int thisDimension, int index) throws SQLException {
// handle an empty array
if (dims.length == 0) {
fields[0] = new Field("INDEX", Oid.INT4);
fields[0].setFormat(Field.BINARY_FORMAT);
fields[1] = new Field("VALUE", elementOid);
fields[1].setFormat(Field.BINARY_FORMAT);
for (int i = 1; i < index; i++) {
int len = ByteConverter.int4(fieldBytes, pos);
pos += 4;
if (len != -1) {
pos += len;
}
}
} else if (thisDimension == dims.length - 1) {
fields[0] = new Field("INDEX", Oid.INT4);
fields[0].setFormat(Field.BINARY_FORMAT);
fields[1] = new Field("VALUE", elementOid);
fields[1].setFormat(Field.BINARY_FORMAT);
for (int i = 1; i < index; i++) {
int len = ByteConverter.int4(fieldBytes, pos);
pos += 4;
if (len != -1) {
pos += len;
}
}
for (int i = 0; i < dims[thisDimension]; i++) {
byte[][] rowData = new byte[2][];
rowData[0] = new byte[4];
ByteConverter.int4(rowData[0], 0, i + index);
rows.add(new Tuple(rowData));
int len = ByteConverter.int4(fieldBytes, pos);
pos += 4;
if (len == -1) {
continue;
}
rowData[1] = new byte[len];
System.arraycopy(fieldBytes, pos, rowData[1], 0, rowData[1].length);
pos += len;
}
} else {
fields[0] = new Field("INDEX", Oid.INT4);
fields[0].setFormat(Field.BINARY_FORMAT);
fields[1] = new Field("VALUE", oid);
fields[1].setFormat(Field.BINARY_FORMAT);
int nextDimension = thisDimension + 1;
int dimensionsLeft = dims.length - nextDimension;
for (int i = 1; i < index; i++) {
pos = calcRemainingDataLength(fieldBytes, dims, pos, elementOid, nextDimension);
}
for (int i = 0; i < dims[thisDimension]; i++) {
byte[][] rowData = new byte[2][];
rowData[0] = new byte[4];
ByteConverter.int4(rowData[0], 0, i + index);
rows.add(new Tuple(rowData));
int dataEndPos = calcRemainingDataLength(fieldBytes, dims, pos, elementOid, nextDimension);
int dataLength = dataEndPos - pos;
rowData[1] = new byte[12 + 8 * dimensionsLeft + dataLength];
ByteConverter.int4(rowData[1], 0, dimensionsLeft);
System.arraycopy(fieldBytes, 4, rowData[1], 4, 8);
System.arraycopy(fieldBytes, 12 + nextDimension * 8, rowData[1], 12, dimensionsLeft * 8);
System.arraycopy(fieldBytes, pos, rowData[1], 12 + dimensionsLeft * 8, dataLength);
pos = dataEndPos;
}
}
return pos;
}
private int calcRemainingDataLength(byte[] fieldBytes,
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(fieldBytes, dims, elementOid, pos, thisDimension + 1);
}
return pos;
}
/**
* Build {@link ArrayList} from field's string input. As a result of this method
* {@link #arrayList} is build. Method can be called many times in order to make sure that array
* list is ready to use, however {@link #arrayList} will be set only once during first call.
*/
private PgArrayList buildArrayList(String fieldString) throws SQLException {
try (ResourceLock ignore = lock.obtain()) {
if (arrayList == null) {
arrayList = ArrayDecoding.buildArrayList(fieldString, getConnection().getTypeInfo().getArrayDelimiter(oid));
}
return arrayList;
}
}
/**
* Convert {@link ArrayList} to array.
*
* @param input list to be converted into array
*/
private Object buildArray(ArrayDecoding.PgArrayList input, int index, int count) throws SQLException {
final BaseConnection connection = getConnection();
return ArrayDecoding.readStringArray(index, count, connection.getTypeInfo().getPGArrayElement(oid), input, connection);
}
@Override
public int getBaseType() throws SQLException {
return getConnection().getTypeInfo().getSQLType(getBaseTypeName());
}
@Override
public String getBaseTypeName() throws SQLException {
int elementOID = getConnection().getTypeInfo().getPGArrayElement(oid);
return castNonNull(getConnection().getTypeInfo().getPGType(elementOID));
}
@Override
public ResultSet getResultSet() throws SQLException {
return getResultSetImpl(1, 0, null);
}
@Override
public ResultSet getResultSet(long index, int count) throws SQLException {
return getResultSetImpl(index, count, null);
}
@Override
public ResultSet getResultSet(@Nullable Map<String, Class<?>> map) throws SQLException {
return getResultSetImpl(map);
}
@Override
public ResultSet getResultSet(long index, int count, @Nullable Map<String, Class<?>> map)
throws SQLException {
return getResultSetImpl(index, count, map);
}
public ResultSet getResultSetImpl(@Nullable Map<String, Class<?>> map) throws SQLException {
return getResultSetImpl(1, 0, map);
}
public ResultSet getResultSetImpl(long index, int count, @Nullable Map<String, Class<?>> map)
throws SQLException {
// for now maps aren't supported.
if (map != null && !map.isEmpty()) {
throw Driver.notImplemented(this.getClass(), "getResultSetImpl(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 readBinaryResultSet(fieldBytes, (int) index, count);
}
final PgArrayList arrayList = buildArrayList(castNonNull(fieldString));
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);
}
List<Tuple> rows = new ArrayList<>();
Field[] fields = new Field[2];
// one dimensional array
if (arrayList.dimensionsCount <= 1) {
// array element type
final int baseOid = getConnection().getTypeInfo().getPGArrayElement(oid);
fields[0] = new Field("INDEX", Oid.INT4);
fields[1] = new Field("VALUE", baseOid);
for (int i = 0; i < count; i++) {
int offset = (int) index + i;
byte[] @Nullable [] t = new byte[2][0];
String v = (String) arrayList.get(offset);
t[0] = getConnection().encodeString(Integer.toString(offset + 1));
t[1] = v == null ? null : getConnection().encodeString(v);
rows.add(new Tuple(t));
}
} else {
// when multi-dimensional
fields[0] = new Field("INDEX", Oid.INT4);
fields[1] = new Field("VALUE", oid);
for (int i = 0; i < count; i++) {
int offset = (int) index + i;
byte[] @Nullable [] t = new byte[2][0];
Object v = arrayList.get(offset);
t[0] = getConnection().encodeString(Integer.toString(offset + 1));
t[1] = v == null ? null : getConnection().encodeString(toString((ArrayDecoding.PgArrayList) v));
rows.add(new Tuple(t));
}
}
BaseStatement stat = (BaseStatement) getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return stat.createDriverResultSet(fields, rows);
}
@Override
@SuppressWarnings("nullness")
public @Nullable String toString() {
if (fieldString == null && fieldBytes != null) {
try {
Object array = readBinaryArray(fieldBytes, 1, 0);
final ArrayEncoding.ArrayEncoder arraySupport = ArrayEncoding.getArrayEncoder(array);
assert arraySupport != null;
fieldString = arraySupport.toArrayString(connection.getTypeInfo().getArrayDelimiter(oid), array);
} catch (SQLException e) {
fieldString = "NULL"; // punt
}
}
return fieldString;
}
/**
* Convert array list to PG String representation (e.g. {0,1,2}).
*/
private String toString(ArrayDecoding.PgArrayList list) throws SQLException {
if (list == null) {
return "NULL";
}
StringBuilder b = new StringBuilder().append('{');
char delim = getConnection().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 ArrayDecoding.PgArrayList) {
b.append(toString((ArrayDecoding.PgArrayList) v));
} else {
escapeArrayElement(b, (String) v);
}
}
b.append('}');
return b.toString();
}
public static void escapeArrayElement(StringBuilder b, String s) {
b.append('"');
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (c == '"' || c == '\\') {
b.append('\\');
}
b.append(c);
}
b.append('"');
}
public boolean isBinary() {
return fieldBytes != null;
}
public byte @Nullable [] toBytes() {
return fieldBytes;
}
@Override
public void free() throws SQLException {
connection = null;
fieldString = null;
fieldBytes = null;
arrayList = null;
}
}