-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathBinaryColumnTest.java
More file actions
59 lines (53 loc) · 1.75 KB
/
Copy pathBinaryColumnTest.java
File metadata and controls
59 lines (53 loc) · 1.75 KB
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
/*
* Copyright (c) 2004-2013 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Tada AB
*/
package org.postgresql.pljava.example;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.postgresql.pljava.ResultSetProvider;
/**
* Example using {@code ResultSetProvider} to return 100 rows of two
* {@code bytea} columns each, which should be equal in each row, one being
* set by {@link java.sql.ResultSet#updateBinaryStream updateBinaryStream}
* and the other by {@link java.sql.ResultSet#updateBytes updateBytes}.
*/
public class BinaryColumnTest implements ResultSetProvider {
public static ResultSetProvider getBinaryPairs() {
return new BinaryColumnTest();
}
@Override
public boolean assignRowValues(ResultSet rs, int rowCount)
throws SQLException {
try {
if (rowCount >= 100)
return false;
int offset = rowCount * 100;
ByteArrayOutputStream bld = new ByteArrayOutputStream();
DataOutputStream da = new DataOutputStream(bld);
for (int idx = 0; idx < 100; ++idx)
da.writeInt(offset + idx);
byte[] bytes = bld.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
rs.updateBinaryStream(1, input, bytes.length);
rs.updateBytes(2, bytes);
return true;
} catch (IOException e) {
throw new SQLException(e.getMessage());
}
}
@Override
public void close() throws SQLException {
}
}