private void testUnsupportedOperations() throws Exception { Connection conn = getConnection(); stat = conn.createStatement(); stat.execute("create table test(id int, c clob, b blob)"); stat.execute("insert into test values(1, 'x', x'00')"); ResultSet rs = stat.executeQuery("select * from test order by id"); rs.next(); Clob clob = rs.getClob(2); byte[] data = IOUtils.readBytesAndClose(clob.getAsciiStream(), -1); assertEquals("x", new String(data, "UTF-8")); assertTrue(clob.toString().endsWith("'x'")); clob.free(); assertTrue(clob.toString().endsWith("null")); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).truncate(0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).setAsciiStream(1); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).setString(1, "", 0, 1); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).position("", 0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).position((Clob) null, 0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).getCharacterStream(1, 1); Blob blob = rs.getBlob(3); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).truncate(0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).setBytes(1, new byte[0], 0, 0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).position(new byte[1], 0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).position((Blob) null, 0); assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).getBinaryStream(1, 1); assertTrue(blob.toString().endsWith("X'00'")); blob.free(); assertTrue(blob.toString().endsWith("null")); stat.execute("drop table test"); conn.close(); }
public InputStream getAsciiStream() throws SQLException { checkValidation(); if (clob == null) { throw new IllegalStateException(); } return clob.getAsciiStream(); }
/** * Tests that the InputStream got from a empty Clob reflects new data in the underlying Clob. * * @throws Exception */ public void testGetAsciiStreamCreateClob() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str = "Hi I am the insert String"; // Create the InputStream that will // be used for comparing the Stream // that is obtained from the Blob after // the update. ByteArrayInputStream str_is = new ByteArrayInputStream(str.getBytes("US-ASCII")); // create the empty Clob. Clob clob = getConnection().createClob(); // Get the InputStream from this // Clob InputStream is = clob.getAsciiStream(); // set the String into the clob. clob.setString(1, str); // Ensure that the Stream obtained from // the clob contains the expected bytes assertEquals(str_is, is); }
@Override public InputStream getAsciiStream(final int columnIndex) throws SQLException { final Clob clob = getClob(columnIndex); if (clob == null) { return null; } return clob.getAsciiStream(); }
/** * Tests that the data updated in a Clob is always reflected in the InputStream got. Here the * updates into the Clob are done using both an OutputStream obtained from this Clob as well as * using Clob.setString. * * @throws Exception */ public void testGetAsciiStreamClobUpdates() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str1 = "Hi I am the insert string"; // Stores the byte array representation of // the insert string. byte[] str1_bytes = str1.getBytes(); // The String that will be used in the // second series of updates String str2 = "Hi I am the update string"; // create the empty Clob. Clob clob = getConnection().createClob(); // Get the InputStream from this // Clob before any writes happen. InputStream is_BeforeWrite = clob.getAsciiStream(); // Get an OutputStream from this Clob // into which the data can be written OutputStream os = clob.setAsciiStream(1); os.write(str1_bytes); // Doing a setString now on the Clob // should reflect the same extension // in the InputStream also. clob.setString((str1_bytes.length) + 1, str2); // Get the input stream from the // Clob after the update InputStream is_AfterWrite = clob.getAsciiStream(); // Now check if the two InputStreams // match assertEquals(is_BeforeWrite, is_AfterWrite); }
private void testReadClob(boolean close) throws Exception { byte[] result = null; String data = "ZIP"; when(clob.getAsciiStream()).thenReturn(new ByteArrayInputStream(data.getBytes())); if (close == true) { result = TypeHandlerUtils.readClob(clob); } else { result = TypeHandlerUtils.readClob(clob, false); } Assert.assertEquals(data, new String(result)); }
public HarmonySerialClob(Clob clob) throws SQLException { Reader characterStream; if (clob == null) { throw new IllegalArgumentException(); } if ((characterStream = clob.getCharacterStream()) == null && clob.getAsciiStream() == null) { throw new IllegalArgumentException(); } this.clob = clob; origLen = clob.length(); len = origLen; buf = new char[(int) len]; try { characterStream.read(buf); } catch (IOException e) { SQLException se = new SQLException("SerialClob: " + e.getMessage()); se.initCause(e); throw se; } }
/** * Tests the implementation for the free() method in the Clob interface. * * @throws SQLException if an error occurs during releasing the Clob resources */ public void testFreeandMethodsAfterCallingFree() throws IllegalAccessException, InvocationTargetException, SQLException { clob = BlobClobTestSetup.getSampleClob(getConnection()); // call the buildHashSetMethod to initialize the // HashSet with the method signatures that are exempted // from throwing a SQLException after free has been called // on the Clob object. buildHashSet(); InputStream asciiStream = clob.getAsciiStream(); Reader charStream = clob.getCharacterStream(); clob.free(); // testing the idempotence of the free() method // the method can be called multiple times on // the first are treated as no-ops clob.free(); // to the free method so testing calling // a method on this invalid object should throw // an SQLException buildMethodList(clob); }
protected static Element createRow(ResultSet rs, ResultSetMetaData metaData, Element rootElement) throws SQLException, IOException { Document doc = rootElement.getOwnerDocument(); Element rowElement = doc.createElement(SQLSchemaGenerator.ROW_ELEMENT); rootElement.appendChild(rowElement); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { int type = metaData.getColumnType(i); String columnName = metaData.getColumnName(i); if (columnName == null) columnName = ""; Element colElement = doc.createElement(columnName.toLowerCase()); rowElement.appendChild(colElement); switch (type) { case java.sql.Types.CHAR: case java.sql.Types.VARCHAR: case java.sql.Types.LONGVARCHAR: // case java.sql.Types.NCHAR: // case java.sql.Types.NVARCHAR: // case java.sql.Types.LONGNVARCHAR: String content = rs.getString(i); colElement.appendChild(doc.createTextNode(content == null ? "" : content)); break; case java.sql.Types.NUMERIC: case java.sql.Types.DECIMAL: int scale = metaData.getScale(i); if (scale <= 0) { // 按照整数处理 Long l = rs.getLong(i); colElement.appendChild(doc.createTextNode(l == null ? "" : l.toString())); } else { // 按照double处理 Double d = rs.getDouble(i); colElement.appendChild(doc.createTextNode(d == null ? "" : d.toString())); } break; case java.sql.Types.BOOLEAN: case java.sql.Types.BIT: Boolean b = rs.getBoolean(i); colElement.appendChild(doc.createTextNode(b == null ? "" : b.toString())); break; case java.sql.Types.TINYINT: case java.sql.Types.SMALLINT: case java.sql.Types.INTEGER: Integer theInt = rs.getInt(i); colElement.appendChild(doc.createTextNode(theInt == null ? "" : theInt.toString())); break; case java.sql.Types.BIGINT: Long theLong = rs.getLong(i); colElement.appendChild(doc.createTextNode(theLong == null ? "" : theLong.toString())); break; case java.sql.Types.REAL: Float f = rs.getFloat(i); colElement.appendChild(doc.createTextNode((f == null ? "" : f.toString()))); break; case java.sql.Types.FLOAT: case java.sql.Types.DOUBLE: Double db = rs.getDouble(i); colElement.appendChild(doc.createTextNode(db == null ? "" : db.toString())); break; case java.sql.Types.BINARY: case java.sql.Types.VARBINARY: case java.sql.Types.LONGVARBINARY: byte[] bytes = rs.getBytes(i); if (bytes != null) { String base64 = new String(Base64.encodeBase64(bytes), "UTF-8"); colElement.appendChild(doc.createCDATASection(base64)); } case java.sql.Types.BLOB: Blob blob = rs.getBlob(i); if (blob != null) { InputStream in = blob.getBinaryStream(); if (in != null) { byte[] tyeBytes = new byte[in.available()]; int offset = 0; int numRead = 0; while (offset < tyeBytes.length && (numRead = in.read(tyeBytes, offset, tyeBytes.length - offset)) >= 0) { offset += numRead; } String base64_2 = new String(Base64.encodeBase64(tyeBytes), "UTF-8"); colElement.appendChild(doc.createCDATASection(base64_2)); } } break; case java.sql.Types.CLOB: Clob clob = rs.getClob(i); if (clob != null) { InputStream clobIn = clob.getAsciiStream(); if (clobIn != null) { byte[] clobBytes = new byte[clobIn.available()]; int clob_offset = 0; int clob_numRead = 0; while (clob_offset < clobBytes.length && (clob_numRead = clobIn.read(clobBytes, clob_offset, clobBytes.length - clob_offset)) >= 0) { clob_offset += clob_numRead; } String base64_3 = new String(Base64.encodeBase64(clobBytes), "UTF-8"); colElement.appendChild(doc.createCDATASection(base64_3)); } } break; case java.sql.Types.DATE: java.sql.Date sqlDate = rs.getDate(i); colElement.appendChild(doc.createTextNode(sqlDate == null ? "" : sqlDate.toString())); break; case java.sql.Types.TIME: java.sql.Time t = rs.getTime(i); colElement.appendChild(doc.createTextNode(t == null ? "" : t.toString())); break; case java.sql.Types.TIMESTAMP: java.sql.Timestamp ts = rs.getTimestamp(i); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); colElement.appendChild(doc.createTextNode(ts == null ? "" : df.format(ts))); break; } } return rowElement; }