| 1 package com.quadcap.sql.types; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedOutputStream ; 43 import java.io.Externalizable ; 44 import java.io.InputStream ; 45 import java.io.IOException ; 46 import java.io.ObjectInput ; 47 import java.io.ObjectOutput ; 48 import java.io.OutputStream ; 49 import java.io.Reader ; 50 51 import java.sql.Clob ; 53 import java.sql.SQLException ; 55 56 import com.quadcap.sql.file.BlockFile; 57 import com.quadcap.sql.file.Datafile; 58 import com.quadcap.sql.file.RandomAccess; 59 import com.quadcap.sql.file.RandomAccessInputStream; 60 import com.quadcap.sql.file.RandomAccessOutputStream; 61 62 import com.quadcap.io.AsciiInputStream; 63 64 import com.quadcap.util.text.TextMatch; 65 66 import com.quadcap.util.Debug; 67 import com.quadcap.util.Util; 68 69 74 public class ValueClob extends ValueBlob 75 implements Clob , Externalizable  79 { 81 public ValueClob() { } 82 83 public ValueClob(Datafile file, long transId, 84 Reader r, int length) 85 throws IOException  86 { 87 super(file, transId, new CharStream(r), length*2); 88 } 89 90 public ValueClob(String s) { 91 super(Util.strCharsAsBytes(s)); 92 } 93 94 public Value unop(int op) throws ValueException { 95 switch (op) { 96 case Op.NULL: return ValueBoolean.falseBoolean; 98 case Op.PATTERN: return new ValuePattern(toString(), ValuePattern.defaultEscape); 100 default: 101 throw new ValueException("Unary op: " + Op.toString(op) + 102 " not implemented for this type"); 103 } 104 } 105 106 public Value binop(int op, Value l) throws ValueException { 107 return l.binop(op, this); 108 } 109 110 public Value binop(int op, ValueString r) throws ValueException { 111 return ValueString.binop(op, convert(TypeVarChar.typeVarChar), r); 112 } 113 114 public Object asJavaObject() { 115 return this; 116 } 117 118 public void fromJavaObject(Object obj) throws ValueException { 119 throw new ValueException("bad type: " + obj); 120 } 121 122 public Type getType() throws SQLException { 123 return new TypeClob(); 124 } 125 126 129 public char[] getChars(long cpos, int clen) throws SQLException { 130 cpos--; try { 132 if (clen > 1024 * 1024) throw new SQLException ("CLOB too long"); 133 char[] buf = new char[clen]; 134 if (bytes == null) { 135 getRandomAccess(); 136 byte[] tbuf = new byte[4096]; 137 int bpos = (int)(cpos * 2); 138 int cbufpos = 0; 139 int blen = clen * 2; 140 while (blen > 0) { 141 int len = blen > 4096 ? 4096 : blen; 142 ra.read(bpos, tbuf, 0, len); 143 bpos += len; 144 blen -= len; 145 Util.bytesToChars(tbuf, 0, buf, cbufpos, len/2); 146 cbufpos += (len/2); 147 } 148 } else { 149 Util.bytesToChars(bytes, (int)(cpos*2), buf, 0, clen); 150 } 151 return buf; 152 } catch (IOException e) { 153 throw new SQLException (e.toString(), "Q001G"); 154 } 155 } 156 157 public InputStream getAsciiStream() throws SQLException { 158 return getBinaryStream(); 159 } 160 161 public Reader getCharacterStream() throws SQLException { 162 return new ByteReader(getBinaryStream()); 163 } 164 165 public String getSubString(long pos, int length) throws SQLException { 166 return new String (getChars(pos, length)); 167 } 168 169 public long length() throws SQLException { 170 return super.length() / 2; 171 } 172 173 public long position(Clob clob, long start) throws SQLException { 175 String pattern = clob.getSubString(0, (int)clob.length()); 176 return position(pattern, start); 177 } 178 180 public long position(String pattern, long start) throws SQLException { 181 return super.position(pattern.getBytes(), start); 182 } 183 184 public void readExternal(ObjectInput in) 185 throws IOException , ClassNotFoundException  186 { 187 super.readExternal(in); 188 } 189 190 public void writeExternal(ObjectOutput out) throws IOException { 191 super.writeExternal(out); 192 } 193 194 public Value convert(TypeVarChar type) throws ValueException { 195 int len = 0; 196 try { 197 len = (int)length(); 198 if (type.getMax() < 0 || type.getMax() >= len) { 199 return new ValueString(toString()); 200 } else { 201 return new ValueString(getSubString(1, type.getMax())); 202 } 203 } catch (SQLException e) { 204 Debug.print(e); 205 throw new ValueException(e.toString()); 206 } 207 } 208 209 public Value convert(TypeClob type) throws ValueException { 210 return this; 211 } 212 213 public String toString() { 214 try { 215 return getSubString(1L, (int)length()); 216 } catch (SQLException e) { 217 return ""; 218 } 219 } 220 221 222 224 239 public int setString(long pos, String str) 240 throws SQLException  241 { 242 throw new SQLException ("Not implemented"); 243 } 244 245 263 public int setString(long pos, String str, int offset, int len) 264 throws SQLException  265 { 266 throw new SQLException ("Not implemented"); 267 } 268 269 283 public java.io.OutputStream setAsciiStream(long pos) 284 throws SQLException  285 { 286 throw new SQLException ("Not implemented"); 287 } 288 289 304 public java.io.Writer setCharacterStream(long pos) 305 throws SQLException  306 { 307 throw new SQLException ("Not implemented"); 308 } 309 310 } 311 | Popular Tags |