Exemplo n.º 1
0
 /**
  * Create a temporary CLOB value from a stream.
  *
  * @param in the reader
  * @param length the number of characters to read, or -1 for no limit
  * @param handler the data handler
  * @return the lob value
  */
 public static ValueLobDb createTempClob(Reader in, long length, DataHandler handler) {
   BufferedReader reader;
   if (in instanceof BufferedReader) {
     reader = (BufferedReader) in;
   } else {
     reader = new BufferedReader(in, Constants.IO_BUFFER_SIZE);
   }
   try {
     boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null;
     long remaining = Long.MAX_VALUE;
     if (length >= 0 && length < remaining) {
       remaining = length;
     }
     int len = getBufferSize(handler, compress, remaining);
     char[] buff;
     if (len >= Integer.MAX_VALUE) {
       String data = IOUtils.readStringAndClose(reader, -1);
       buff = data.toCharArray();
       len = buff.length;
     } else {
       buff = new char[len];
       reader.mark(len);
       len = IOUtils.readFully(reader, buff, len);
     }
     if (len <= handler.getMaxLengthInplaceLob()) {
       byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8);
       return ValueLobDb.createSmallLob(Value.CLOB, small, len);
     }
     reader.reset();
     ValueLobDb lob = new ValueLobDb(handler, reader, remaining);
     return lob;
   } catch (IOException e) {
     throw DbException.convertIOException(e, null);
   }
 }
Exemplo n.º 2
0
 @Override
 public Value convertPrecision(long precision, boolean force) {
   if (this.precision <= precision) {
     return this;
   }
   ValueLobDb lob;
   if (type == CLOB) {
     if (handler == null) {
       try {
         int p = MathUtils.convertLongToInt(precision);
         String s = IOUtils.readStringAndClose(getReader(), p);
         byte[] data = s.getBytes(Constants.UTF8);
         lob = ValueLobDb.createSmallLob(type, data, s.length());
       } catch (IOException e) {
         throw DbException.convertIOException(e, null);
       }
     } else {
       lob = ValueLobDb.createTempClob(getReader(), precision, handler);
     }
   } else {
     if (handler == null) {
       try {
         int p = MathUtils.convertLongToInt(precision);
         byte[] data = IOUtils.readBytesAndClose(getInputStream(), p);
         lob = ValueLobDb.createSmallLob(type, data, data.length);
       } catch (IOException e) {
         throw DbException.convertIOException(e, null);
       }
     } else {
       lob = ValueLobDb.createTempBlob(getInputStream(), precision, handler);
     }
   }
   return lob;
 }
Exemplo n.º 3
0
 @Override
 public String getString() {
   int len = precision > Integer.MAX_VALUE || precision == 0 ? Integer.MAX_VALUE : (int) precision;
   try {
     if (type == Value.CLOB) {
       if (small != null) {
         return new String(small, Constants.UTF8);
       }
       return IOUtils.readStringAndClose(getReader(), len);
     }
     byte[] buff;
     if (small != null) {
       buff = small;
     } else {
       buff = IOUtils.readBytesAndClose(getInputStream(), len);
     }
     return StringUtils.convertBytesToHex(buff);
   } catch (IOException e) {
     throw DbException.convertIOException(e, toString());
   }
 }
Exemplo n.º 4
0
 public static Object convert(Object o, Class<?> targetType) {
   if (o == null) {
     return null;
   }
   Class<?> currentType = o.getClass();
   if (targetType.isAssignableFrom(currentType)) {
     return o;
   }
   if (targetType == String.class) {
     if (Clob.class.isAssignableFrom(currentType)) {
       Clob c = (Clob) o;
       try {
         Reader r = c.getCharacterStream();
         return IOUtils.readStringAndClose(r, -1);
       } catch (Exception e) {
         throw new RuntimeException("Error converting CLOB to String: " + e.toString(), e);
       }
     }
     return o.toString();
   }
   if (Number.class.isAssignableFrom(currentType)) {
     Number n = (Number) o;
     if (targetType == Byte.class) {
       return n.byteValue();
     } else if (targetType == Short.class) {
       return n.shortValue();
     } else if (targetType == Integer.class) {
       return n.intValue();
     } else if (targetType == Long.class) {
       return n.longValue();
     } else if (targetType == Double.class) {
       return n.doubleValue();
     } else if (targetType == Float.class) {
       return n.floatValue();
     }
   }
   throw new RuntimeException(
       "Can not convert the value " + o + " from " + currentType + " to " + targetType);
 }
Exemplo n.º 5
0
  private void testInsertRowWithUpdatableResultSetDefault() throws Exception {
    stat.execute("create table test(id int primary key, " + "data varchar(255) default 'Hello')");
    PreparedStatement prep =
        conn.prepareStatement(
            "select * from test", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = prep.executeQuery();
    rs.moveToInsertRow();
    rs.updateInt(1, 1);
    rs.insertRow();
    rs.close();
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    assertEquals("Hello", rs.getString(2));
    assertEquals("Hello", rs.getString("data"));
    assertEquals("Hello", rs.getNString(2));
    assertEquals("Hello", rs.getNString("data"));
    assertEquals("Hello", IOUtils.readStringAndClose(rs.getNCharacterStream(2), -1));
    assertEquals("Hello", IOUtils.readStringAndClose(rs.getNCharacterStream("data"), -1));
    assertEquals("Hello", IOUtils.readStringAndClose(rs.getNClob(2).getCharacterStream(), -1));
    assertEquals("Hello", IOUtils.readStringAndClose(rs.getNClob("data").getCharacterStream(), -1));

    rs = prep.executeQuery();

    rs.moveToInsertRow();
    rs.updateInt(1, 2);
    rs.updateNString(2, "Hello");
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 3);
    rs.updateNString("data", "Hello");
    rs.insertRow();

    Clob c;
    Writer w;

    rs.moveToInsertRow();
    rs.updateInt(1, 4);
    c = conn.createClob();
    w = c.setCharacterStream(1);
    w.write("Hello");
    w.close();
    rs.updateClob(2, c);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 5);
    c = conn.createClob();
    w = c.setCharacterStream(1);
    w.write("Hello");
    w.close();
    rs.updateClob("data", c);
    rs.insertRow();

    InputStream in;

    rs.moveToInsertRow();
    rs.updateInt(1, 6);
    in = new ByteArrayInputStream("Hello".getBytes("UTF-8"));
    rs.updateAsciiStream(2, in);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 7);
    in = new ByteArrayInputStream("Hello".getBytes("UTF-8"));
    rs.updateAsciiStream("data", in);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 8);
    in = new ByteArrayInputStream("Hello-".getBytes("UTF-8"));
    rs.updateAsciiStream(2, in, 5);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 9);
    in = new ByteArrayInputStream("Hello-".getBytes("UTF-8"));
    rs.updateAsciiStream("data", in, 5);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 10);
    in = new ByteArrayInputStream("Hello-".getBytes("UTF-8"));
    rs.updateAsciiStream(2, in, 5L);
    rs.insertRow();

    rs.moveToInsertRow();
    rs.updateInt(1, 11);
    in = new ByteArrayInputStream("Hello-".getBytes("UTF-8"));
    rs.updateAsciiStream("data", in, 5L);
    rs.insertRow();

    rs = stat.executeQuery("select * from test");
    while (rs.next()) {
      assertEquals("Hello", rs.getString(2));
    }

    stat.execute("drop table test");
  }