} else if (DataType.Name.LIST.toString().equalsIgnoreCase(mainTypeString)) { DataType listType = DataType.list(firstParamType); statement.setList(paramIndex, (List) codecRegistry.codecFor(listType).parse(paramValue)); return;
@Override public <T> QueryBinder<ENTITY> setList(int i, List<T> v) { for (BoundStatement statement : statements.values()) { statement.setList(i, v); } return this; }
@Test(groups = "short") public void should_work_with_prepared_statements_2() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, pk) .setList(1, v, elementsType) // variant with element type explicitly set ); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, pk)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_use_custom_codecs_with_prepared_statements() { PreparedStatement ps = session().prepare(insertQuery); session() .execute( ps.bind() .setInt(0, 42) .setList(1, newArrayList(42)) .setString( 2, "42") // here we have the CQL type so VarcharCodec will be used even if // IntToStringCodec accepts it ); session() .execute( ps.bind() .setString(0, "42") .setList(1, newArrayList("42"), String.class) .setString( 2, "42") // here we have the CQL type so VarcharCodec will be used even if // IntToStringCodec accepts it ); ps = session().prepare(selectQuery); assertRow(session().execute(ps.bind().setInt(0, 42)).one()); assertRow(session().execute(ps.bind().setString(0, "42")).one()); }
@Test(groups = "short") public void should_use_collection_codecs_with_prepared_statements_2() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, n_int) .setList(1, l_int) .setList(2, l_bigint, Long.class) // variant with element type explicitly set .setSet(3, s_float) .setSet( 4, s_double, TypeToken.of(Double.class)) // variant with element type explicitly set .setMap(5, m_varint) .setMap( 6, m_decimal, Integer.class, BigDecimal.class) // variant with element type explicitly set ); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, n_int)); Row row = rows.one(); assertRow(row); }
} else if (value.getValue() instanceof List) { if (value.isNamed()) boundStatement.setList(value.getName(), (List) value.getValue()); else boundStatement.setList(value.getIndex() - 1, (List) value.getValue()); } else { throw new IllegalArgumentException("bind parameter type [" + value.getValue().getClass()
byName.setInt("k", byNameKey); setValue(byName, "v", mapping.outerType, mapping.value); byName.setList("l", list, mapping.javaType); byName.setMap("m", map, mapping.javaType, mapping.javaType); if (is21) { byIndex.setInt(0, byIndexKey); setValue(byIndex, 1, mapping.outerType, mapping.value); byIndex.setList(2, list, mapping.javaType); byIndex.setMap(3, map, mapping.javaType, mapping.javaType); if (is21) {
@Override protected ResultSetFuture newFuture() { return factory.session.executeAsync(factory.preparedStatement.bind().setList("days", days)); }
@Override protected ResultSetFuture newFuture() { return factory.session.executeAsync(factory.preparedStatement.bind().setList("days", days)); }
@Override public <T> QueryBinder<ENTITY> setList(String name, List<T> v) { for (BoundStatement statement : statements.values()) { if (statement.preparedStatement().getVariables().contains(name)) { statement.setList(name, v); } } return this; }
/** * Perform a single write operation * * @param key * @return * @throws Exception */ @Override public String writeSingle(String key) throws Exception { BoundStatement bStmt = writePstmt.bind(); bStmt.setString("\"_id\"", key); bStmt.setList("name", Arrays.asList(this.dataGenerator.getRandomValue())) ; bStmt.setConsistencyLevel(this.WriteConsistencyLevel); session.execute(bStmt); return ResultOK; }
/** * Perform a single write operation * * @param key * @return * @throws Exception */ @Override public String writeSingle(String key) throws Exception { BoundStatement bStmt = writePstmt.bind(); bStmt.setString("\"_id\"", key); bStmt.setList("name", Arrays.asList(this.dataGenerator.getRandomValue())) ; bStmt.setConsistencyLevel(this.WriteConsistencyLevel); session.execute(bStmt); return ResultOK; }
public ListenableFuture<List<ByteBuffer>> getDependencies(long startEpochDayMillis, long endEpochDayMillis) { List<Date> days = getDays(startEpochDayMillis, endEpochDayMillis); try { BoundStatement bound = selectDependencies.bind().setList("days", days); if (LOG.isDebugEnabled()) { LOG.debug(debugSelectDependencies(days)); } return Futures.transform( session.executeAsync(bound), (ResultSet input) -> { List<ByteBuffer> dependencies = new ArrayList<>(); for (Row row : input) { dependencies.add(row.getBytes("dependencies")); } return dependencies; } ); } catch (RuntimeException ex) { LOG.error("failed " + debugSelectDependencies(days), ex); return Futures.immediateFailedFuture(ex); } }
@Override public ListenableFuture<List<DependencyLink>> getDependencies(long endTs, @Nullable Long lookback) { List<Date> days = getDays(endTs, lookback); try { BoundStatement bound = CassandraUtil.bindWithName(selectDependencies, "select-dependencies") .setList("days", days); return transform(session.executeAsync(bound), ConvertDependenciesResponse.INSTANCE); } catch (RuntimeException ex) { return immediateFailedFuture(ex); } }
public void setString(int parameterIndex, String value) throws SQLException { checkNotClosed(); checkIndex(parameterIndex); try{ this.statement.setString(parameterIndex-1, value); }catch(CodecNotFoundException e){ // Big ugly hack in order to parse string representations of collections // Yes, I'm ashamed... if(e.getMessage().contains("set<")){ String itemType = e.getMessage().substring(e.getMessage().indexOf("<")+1, e.getMessage().indexOf(">")); this.statement.setSet(parameterIndex-1, Utils.parseSet(itemType, value)); }else if(e.getMessage().contains("list<")){ String itemType = e.getMessage().substring(e.getMessage().indexOf("<")+1, e.getMessage().indexOf(">")); this.statement.setList(parameterIndex-1, Utils.parseList(itemType, value)); }else if(e.getMessage().contains("map<")){ String[] kvTypes = e.getMessage().substring(e.getMessage().indexOf("<")+1, e.getMessage().indexOf(">")).replace(" ", "").split(","); this.statement.setMap(parameterIndex-1, Utils.parseMap(kvTypes[0],kvTypes[1], value)); } } }
private BoundStatement boundWriteStatement(MailboxMessage message, Pair<BlobId, BlobId> pair) { CassandraMessageId messageId = (CassandraMessageId) message.getMessageId(); return insert.bind() .setUUID(MESSAGE_ID, messageId.get()) .setTimestamp(INTERNAL_DATE, message.getInternalDate()) .setInt(BODY_START_OCTET, (int) (message.getHeaderOctets())) .setLong(FULL_CONTENT_OCTETS, message.getFullContentOctets()) .setLong(BODY_OCTECTS, message.getBodyOctets()) .setString(BODY_CONTENT, pair.getRight().asString()) .setString(HEADER_CONTENT, pair.getLeft().asString()) .setLong(TEXTUAL_LINE_COUNT, Optional.ofNullable(message.getTextualLineCount()).orElse(DEFAULT_LONG_VALUE)) .setList(PROPERTIES, buildPropertiesUdt(message)) .setList(ATTACHMENTS, buildAttachmentUdt(message)); }
@Override protected ResultSetFuture newFuture() { Statement bound = factory .preparedStatement .bind() .setList("service_names", input.service_names()) .setBytesUnsafe("start_ts", factory.timestampCodec.serialize(input.start_ts())) .setBytesUnsafe("end_ts", factory.timestampCodec.serialize(input.end_ts())) .setInt("limit_", input.limit_()) .setFetchSize(Integer.MAX_VALUE); // NOTE in the new driver, we also set this to limit return factory.session.executeAsync(bound); }
@Test(groups = "short") public void should_work_with_prepared_statements_2() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, pk) .setList(1, v, elementsType) // variant with element type explicitly set ); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, pk)); Row row = rows.one(); assertRow(row); }
/** * Validates that geometry values can be inserted as a list and verifies that the list is stored * correctly by retrieving it and ensuring it matches. * * @test_category dse:geospatial */ @Test(groups = "short") public void should_insert_as_list() throws Exception { UUID key = UUIDs.random(); PreparedStatement prepared = session().prepare("INSERT INTO tbl (k, l) values (?, ?)"); BoundStatement bs = prepared.bind(); bs.setUUID(0, key); bs.setList(1, sampleData); session().execute(bs); validate(key, "l", sampleData, TypeTokens.listOf(selfType)); }