01: package simpleorm.core;
02:
03: import simpleorm.properties.*;
04: import java.sql.ResultSet;
05: import java.io.ByteArrayOutputStream;
06: import java.io.InputStream;
07:
08: /** Represents a BLOB, Binary large object. See SFieldBytes for short binary strings.
09: * Supports returning the entire blob as a byte array, but that is not normal practice.
10: * ### Untested. */
11:
12: public class SFieldBlob extends SFieldScalar {
13: public SFieldBlob(SRecordMeta meta, String columnName, int maxSize,
14: SPropertyValue[] pvals) {
15: super (meta, columnName, pvals);
16: putProperty(SCon.SBYTE_SIZE, SJSharp.newInteger(maxSize)); // for Create Table only.
17: }
18:
19: public SFieldBlob(SRecordMeta meta, String columnName, int maxSize) {
20: this (meta, columnName, maxSize, new SPropertyValue[0]);
21: }
22:
23: public SFieldBlob(SRecordMeta meta, String columnName, int maxSize,
24: SPropertyValue pval) {
25: this (meta, columnName, maxSize, new SPropertyValue[] { pval });
26: }
27:
28: public SFieldBlob(SRecordMeta meta, String columnName, int maxSize,
29: SPropertyValue pval1, SPropertyValue pval2) {
30: this (meta, columnName, maxSize, new SPropertyValue[] { pval1,
31: pval2 });
32: }
33:
34: /** Abstract specializer. Clone this key field to be a foreign key
35: to <code>rmeta</code> of the same type.*/
36: SFieldMeta makeForeignKey(SRecordMeta rmeta, String prefix,
37: SPropertyValue[] pvals) {
38: return new SFieldBlob(rmeta, (prefix == null ? "" : prefix)
39: + getString(SCon.SCOLUMN_NAME), SJSharp
40: .object2Int(getProperty(SCon.SBYTE_SIZE)), pvals);
41: }
42:
43: Object queryFieldValue(ResultSet rs, int sqlIndex) throws Exception {
44: // From Marcius. just getBytes should actually work though, but maybe not in Oracle
45: //Object res = rs.getBytes(sqlIndex);
46: //return res;
47:
48: // Get the binary content from the result set as a stream
49: InputStream is = rs.getBinaryStream(sqlIndex);
50: // Create a byte array output stream so we can convert the content to a byte[]
51: ByteArrayOutputStream baos = new ByteArrayOutputStream();
52: // A small buffer to reduce the number of reads
53: byte[] buf = new byte[1024];
54: // Read bytes from the input stream and place them in the baos
55: int bytesRead = is.read(buf);
56: while (bytesRead > 0) {
57: baos.write(buf, 0, bytesRead);
58: bytesRead = is.read(buf);
59: }
60: return baos.toByteArray();
61: }
62:
63: Object convertToField(Object raw) {
64: return raw;
65: }
66:
67: /** Specializes SFieldMeta. */
68: String defaultDataType() {
69: return "BLOB";
70: }
71:
72: }
|