/** * This method transforms a value of the source type into a value * of the target type. * @param value Incoming value of source type * @return Outgoing value of target type * @throws TransformationException if value is an incorrect input type or * the transformation fails */ public Object transformDirect(Object value) throws TransformationException { String contents = (String)value; //TODO: if the value is too large, we should store in a file buffer return new ClobType(new ClobImpl(contents)); }
} else if (value instanceof ClobType) { json.writeString(ClobType.getString((Clob) value)); } else if (value instanceof BlobType) { json.writeString(Base64.encodeBytes(
@TeiidFunction(category=FunctionCategoryConstants.JSON) public static ClobType jsonParse(ClobType val, boolean wellformed) throws SQLException, IOException, ParseException { Reader r = null; if (val.getType() == Type.JSON) { return val; } if (!wellformed) { r = val.getCharacterStream(); } try { if (!wellformed) { JSONParser parser = new JSONParser(); parser.parse(r, validatingContentHandler); } ClobType ct = new ClobType(val.getReference()); ct.setType(Type.JSON); return ct; } finally { if (r != null) { r.close(); } } }
@Override protected Object readObject(ObjectInput in, List<Object> cache, byte version) throws IOException, ClassNotFoundException { ClobType ct = new ClobType(); ct.readExternal(in); return ct; } }
/** * This method transforms a value of the source type into a value * of the target type. * @param value Incoming value of source type * @return Outgoing value of target type * @throws TransformationException if value is an incorrect input type or * the transformation fails */ public Object transformDirect(Object value) throws TransformationException { JsonType json = (JsonType)value; ClobType clob = new ClobType(json.getReference()); clob.setType(Type.JSON); return clob; }
@Test public void testClobValuePersistence() throws Exception { String testString = "this is test clob"; //$NON-NLS-1$ SerialClob clob = new SerialClob(testString.toCharArray()); ClobType cv = new ClobType(clob); String key = cv.getReferenceStreamId(); // now force to serialize ClobType read = UnitTestUtil.helpSerialize(cv); assertTrue(read.length() > 0); // make sure we have kept the reference stream id assertEquals(key, read.getReferenceStreamId()); // and lost the original object assertNull(read.getReference()); }
public static ClobType concat(CommandContext context, ClobType str1, ClobType str2) throws IOException, SQLException { BufferManager bm = context.getBufferManager(); FileStore fs = bm.createFileStore("clob"); //$NON-NLS-1$ FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING); boolean remove = true; try (Reader characterStream = str1.getCharacterStream(); Reader characterStream2 = str2.getCharacterStream();){ Writer writer = fsisf.getWriter(); int chars = ObjectConverterUtil.write(writer, characterStream, -1, false); chars += ObjectConverterUtil.write(writer, characterStream2, -1, false); writer.close(); if (fsisf.getStorageMode() == StorageMode.MEMORY) { //detach if just in memory byte[] bytes = fsisf.getMemoryBytes(); return new ClobType(new ClobImpl(new String(bytes, Streamable.ENCODING))); } remove = false; context.addCreatedLob(fsisf); return new ClobType(new ClobImpl(fsisf, chars)); } finally { if (remove) { fs.remove(); } } }
FileStore fs = buffMgr.createFileStore("temp"); ClobType clob = new ClobType(new ClobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { Streamable<?>lob = lobManager.getLobReference(clob.getReferenceStreamId()); assertTrue(lob.getClass().isAssignableFrom(ClobType.class)); ClobType clobRead = (ClobType)lob; assertEquals(ClobType.getString(clob), ClobType.getString(clobRead)); assertTrue(clobRead.length() != -1);
@TeiidFunction(name=SourceSystemFunctions.REGEXP_REPLACE, category=FunctionCategoryConstants.STRING, nullOnNull=true) public static ClobType regexpReplace(CommandContext context, ClobType source, String regex, String replacement, String flags) throws FunctionExecutionException { //TODO: this is not very memory safe - we can write out to the buffermanger if needed String result = regexpReplace(context, source.getCharSequence(), regex, replacement, flags); return new ClobType(new ClobImpl(result)); }
@Test public void testReferencePersistence() throws Exception { String testString = "this is test clob"; //$NON-NLS-1$ SerialClob clob = new SerialClob(testString.toCharArray()); ClobType cv = new ClobType(clob); cv.setReferenceStreamId(null); // now force to serialize ClobType read = UnitTestUtil.helpSerialize(cv); assertTrue(read.length() > 0); assertEquals(testString, read.getSubString(1, testString.length())); }
@Test public void testClobValue() throws Exception { String testString = "this is test clob"; //$NON-NLS-1$ SerialClob clob = new SerialClob(testString.toCharArray()); ClobType cv = new ClobType(clob); assertEquals(testString, cv.getSubString(1L, (int)cv.length())); }
@Test public void testUpperLowerClob() throws Exception { char[] val = new char[] {87, 122, 147, 0xD801, 0xDC37}; assertEquals(new String(val).toUpperCase(), ClobType.getString(FunctionMethods.upperCase(new ClobType(ClobImpl.createClob(val))))); assertEquals(new String(val).toLowerCase(), ClobType.getString(FunctionMethods.lowerCase(new ClobType(ClobImpl.createClob(val))))); }
@TeiidFunction(category=FunctionCategoryConstants.CONVERSION, name="to_bytes") public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException { Charset cs = CharsetUtils.getCharset(encoding); ClobInputStreamFactory cisf = new ClobInputStreamFactory(value.getReference()); cisf.setCharset(cs); if (!wellFormed || CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) || CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) { //validate that the binary conversion is possible //TODO: cache the result in a filestore InputStream is = new ReaderInputStream(value.getCharacterStream(), cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT)); try { while (is.read() != -1) { } } catch (IOException e) { CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class); if (cce != null) { throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, cs.displayName()), cce); } throw e; } finally { is.close(); } } return new BlobType(new BlobImpl(cisf)); }
@TeiidFunction(category=FunctionCategoryConstants.JSON) public static JsonType jsonParse(ClobType val, boolean wellformed) throws SQLException, IOException, ParseException { Reader r = null; if (val.getType() == Type.JSON) { return new JsonType(val.getReference()); } if (!wellformed) { r = val.getCharacterStream(); } try { if (!wellformed) { JSONParser parser = new JSONParser(); parser.parse(r, validatingContentHandler); } return new JsonType(val.getReference()); } finally { if (r != null) { r.close(); } } }
@Test public void testClobCompare() throws Exception { String testString = "this is test clob"; //$NON-NLS-1$ SerialClob clob = new SerialClob(testString.toCharArray()); ClobType ct = new ClobType(clob); SerialClob clob1 = new SerialClob(testString.toCharArray()); ClobType ct1 = new ClobType(clob1); assertEquals(0, ct1.compareTo(ct)); }
@Test public void testLobHandling() throws Exception { ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$ x.setType(DataTypeManager.DefaultDataClasses.CLOB); List<ElementSymbol> schema = Arrays.asList(x); TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$ tb.setInlineLobs(false); ClobType c = new ClobType(new SerialClob(new char[0])); TupleBatch batch = new TupleBatch(1, new List[] {Arrays.asList(c)}); tb.addTupleBatch(batch, false); assertNotNull(tb.getLobReference(c.getReferenceStreamId())); }
public static GeometryType geometryFromGml(ClobType gml, Integer srid) throws FunctionExecutionException { try { return geometryFromGml(gml.getCharacterStream(), srid); } catch (SQLException e) { throw new FunctionExecutionException(e); } }
} else if (object instanceof ClobType) { ClobType clob = (ClobType)object; if (clob.getType() == Type.JSON) { Reader r = clob.getCharacterStream(); try { ObjectConverterUtil.write(writer, r, -1, false); JSONParser.escape(clob.getCharSequence(), writer); writer.append('"');