protected void checkCassandraException(Exception e) { _mexceptions.incr(); if (e instanceof AlreadyExistsException || e instanceof AuthenticationException || e instanceof DriverException || e instanceof DriverInternalError || e instanceof InvalidConfigurationInQueryException || e instanceof InvalidQueryException || e instanceof InvalidTypeException || e instanceof QueryExecutionException || e instanceof QueryTimeoutException || e instanceof QueryValidationException || e instanceof ReadTimeoutException || e instanceof SyntaxError || e instanceof TraceRetrievalException || e instanceof TruncateException || e instanceof UnauthorizedException || e instanceof UnavailableException || e instanceof ReadTimeoutException || e instanceof WriteTimeoutException) { throw new ReportedFailedException(e); } else { throw new RuntimeException(e); } }
@SuppressWarnings("cast") public long getLong(int index) throws SQLException { checkIndex(index); try{ if(currentRow.getColumnDefinitions().getType(index-1).getName().toString().equals("int")){ return (long)currentRow.getInt(index-1); }else if(currentRow.getColumnDefinitions().getType(index-1).getName().toString().equals("varint")){ return currentRow.getVarint(index-1).longValue(); }else{ return currentRow.getLong(index-1); } }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
@SuppressWarnings("cast") public long getLong(String name) throws SQLException { checkName(name); try{ if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("int")){ return (long)currentRow.getInt(name); }else if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("varint")){ return currentRow.getVarint(name).longValue(); }else{ return currentRow.getLong(name); } }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
public long getLong(int index) throws SQLException { checkIndex(index); try{ if(currentRow.getColumnDefinitions().getType(index-1).getName().toString().equals("int")){ return currentRow.getInt(index-1); }else if(currentRow.getColumnDefinitions().getType(index-1).getName().toString().equals("varint")){ return currentRow.getVarint(index-1).longValue(); }else{ return currentRow.getLong(index-1); } }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } //return currentRow.getLong(index - 1); }
/** * Use the query set via a previous call to prepare to return a single Entity * * @return the entity */ public Entity asSingleEntity() { if (null == this.preparedQuery) { throw new DatastoreServiceException(messages.NO_QUERY_SET_ERROR); } try { List<DataStoreRow> rows = this.getClient().select(this.preparedQuery); if (rows.size() > 0) { Entity e = rows.get(0).getEntity(); return e; } } catch (DatastoreServiceException dse) { if (InvalidTypeException.class != dse.getInner().getClass()) { Log.e(LOG_TAG, dse); throw dse; } } catch(EntityNotFoundException enfe) { Log.v(LOG_TAG, enfe.toString()); } return null; }
@Override public DateTime parse(String value) { if (value == null || value.equals("NULL")) { return null; } try { return DateTime.parse(value); } catch (IllegalArgumentException iae) { throw new InvalidTypeException("Could not parse format: " + value, iae); } }
@Override public Long parse(String value) { try { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Long.parseLong(value); } catch (NumberFormatException e) { throw new InvalidTypeException(String.format("Cannot parse 64-bits long value from \"%s\"", value)); } }
@Override public long deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) { return 0; } if (bytes.remaining() != 8) { throw new InvalidTypeException("Invalid 64-bits long value, expecting 8 bytes but got " + bytes.remaining()); } return bytes.getLong(bytes.position()); }
@Override public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null || bytes.remaining() == 0) return null; return new String(bytes.array()); }
@Override public byte[] deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null || bytes.remaining() == 0) return null; return bytes.duplicate().array(); }
@Override public Time deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null || bytes.remaining() == 0) return null; long nanosOfDay = bigint().deserializeNoBoxing(bytes, protocolVersion); return new Time(LocalTime.fromMillisOfDay(nanosOfDay / 1000000L).toDateTimeToday().getMillis()); }
@Override public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null || bytes.remaining() == 0) return null; long nanosOfDay = bigint().deserializeNoBoxing(bytes, protocolVersion); return LocalTime.fromMillisOfDay(nanosOfDay / 1000000).toString(); }
@Override public String parse(String value) { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; // enclosing single quotes required, even for long literals if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("time values must be enclosed by single quotes"); return value.substring(1, value.length() - 1); }
public String format(Row row) throws IndexOutOfBoundsException, InvalidTypeException { StringBuilder retVal = new StringBuilder(); String[] stringVals = stringVals(row); retVal.append(stringVals[0]); for (int i = 1; i < parsersSize; i++) { retVal.append(delimiter).append(stringVals[i]); } return retVal.toString(); }
public String formatJson(Row row) throws IndexOutOfBoundsException, InvalidTypeException { String[] stringVals = delimParser.stringVals(row); Map<String,String> pairs = new HashMap<String,String>(); for (int i = 0; i < sbl.size(); i++) pairs.put(sbl.get(i).name, stringVals[i]); return JSONObject.toJSONString(pairs); }
public double getDouble(int index) throws SQLException { checkIndex(index); try{ if(currentRow.getColumnDefinitions().getType(index-1).getName().toString().equals("float")){ return currentRow.getFloat(index-1); } return currentRow.getDouble(index-1); }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
@SuppressWarnings("cast") public double getDouble(String name) throws SQLException { checkName(name); try{ if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("float")){ return (double)currentRow.getFloat(name); } return currentRow.getDouble(name); }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
@Override public ByteBuffer serialize(Long paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT); }
@Override public Long deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramByteBuffer == null) { return null; } // always duplicate the ByteBuffer instance before consuming it! return ByteBufferUtil.toLong(paramByteBuffer.duplicate()); }
@Override public ByteBuffer serialize(BigDecimal paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT.longValue()); }
@Override public BigDecimal deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramByteBuffer == null) { return null; } // always duplicate the ByteBuffer instance before consuming it! Long value = ByteBufferUtil.toLong(paramByteBuffer.duplicate()); return new BigDecimal(value); }
@Override public ByteBuffer serialize(Integer paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT); }
@Override public Integer deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramByteBuffer == null) { return null; } // always duplicate the ByteBuffer instance before consuming it! Long value = ByteBufferUtil.toLong(paramByteBuffer.duplicate()); return value.intValue(); }
@Override public ByteBuffer serialize(Double paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT.floatValue()); }
@Override public Double deserialize(ByteBuffer paramByteBuffer, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramByteBuffer == null) { return null; } // always duplicate the ByteBuffer instance before consuming it! Float value = ByteBufferUtil.toFloat(paramByteBuffer.duplicate()); return value.doubleValue(); }
@Override public ByteBuffer serialize(Long paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT.intValue()); }
@Override public ByteBuffer serialize(Double paramT, ProtocolVersion paramProtocolVersion) throws InvalidTypeException { if (paramT == null) { return null; } return ByteBufferUtil.bytes(paramT); }
public double getDouble(String name) throws SQLException { checkName(name); try{ if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("float")){ return currentRow.getFloat(name); } return currentRow.getDouble(name); }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
public long getLong(String name) throws SQLException { checkName(name); try{ if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("int")){ return currentRow.getInt(name); }else if(currentRow.getColumnDefinitions().getType(name).getName().toString().equals("varint")){ return currentRow.getVarint(name).longValue(); }else{ return currentRow.getLong(name); } }catch(InvalidTypeException e){ throw new SQLNonTransientException(e); } }
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2) { DataType defined = getType(i); if (name1 != defined.getName() && name2 != defined.getName()) throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined)); return defined.getName(); }
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2, DataType.Name name3) { DataType defined = getType(i); if (name1 != defined.getName() && name2 != defined.getName() && name3 != defined.getName()) throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined)); return defined.getName(); }
public static ByteBuffer serialize(TypeCodec<?> codec, int protocolVersion, Object value) { if (!codec.getJavaType().getRawType().isAssignableFrom(value.getClass())) throw new InvalidTypeException("Invalid value for CQL type " + codec.getCqlType().getName().toString()); return ((TypeCodec)codec).serialize(value, ProtocolVersion.fromInt(protocolVersion)); }
@Test(groups = "long") public void serializeDeserializeCollectionsTest() { List<String> l = Arrays.asList("foo", "bar"); DataType dt = DataType.list(DataType.text()); assertEquals(dt.deserialize(dt.serialize(l, ProtocolVersion.V3)), l); try { DataType.list(DataType.bigint()).serialize(l, ProtocolVersion.V3); fail("This should not have worked"); } catch (InvalidTypeException e) { /* That's what we want */ } }
@Override public ByteBuffer serialize(E value, ProtocolVersion protocolVersion) throws InvalidTypeException { return innerCodec.serialize(value == null ? null : value.value(), protocolVersion); }
@Override public E deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { Integer value = innerCodec.deserialize(bytes, protocolVersion); return value == null ? null : enumValueMap.get(value); }
@Override public E parse(String value) throws InvalidTypeException { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : enumValueMap.get(Integer.parseInt(value)); }
@Override public String format(E value) throws InvalidTypeException { return value == null ? "NULL" : Integer.toString(value.value()); }