.values(allFields, Collections.<Object>nCopies(allFields.size(), bindMarker()));
/** * Adds multiple column/value pairs to the values inserted by this {@code INSERT} statement. * * @param names a list of column names to insert/update. * @param values a list of values to insert/update. The {@code i}th value in {@code values} will * be inserted for the {@code i}th column in {@code names}. * @return the {@code INSERT} statement those options are part of. * @throws IllegalArgumentException if {@code names.length != values.length}. */ public Insert values(String[] names, Object[] values) { return statement.values(names, values); } }
/** * Adds multiple column/value pairs to the values inserted by this INSERT statement. * * @param names a list of column names to insert/update. * @param values a list of values to insert/update. The {@code i}th value in {@code values} will * be inserted for the {@code i}th column in {@code names}. * @return this INSERT statement. * @throws IllegalArgumentException if {@code names.length != values.length}. * @throws IllegalStateException if this method is called and the {@link #json(Object)} method has * been called before, because it's not possible to mix {@code INSERT JSON} syntax with * regular {@code INSERT} syntax. */ public Insert values(String[] names, Object[] values) { return values(Arrays.asList(names), Arrays.asList(values)); }
@Test(groups = "short") public void routingKeyColumnCaseSensitivityForQuotedIdentifiersTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE_QUOTED); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; query = insertInto(table) .values( new String[] {"\"theKey\"", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3}); ByteBuffer bb = ByteBuffer.allocate(4); bb.putInt(0, 42); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); query = insertInto(table) .values(new String[] {"theKey", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3}); assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); query = insertInto(table) .values(new String[] {"theKey", "a", "b", "theKey"}, new Object[] {42, 1, 2, 3}); assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); }
/** * Adds multiple column/value pairs to the values inserted by this INSERT statement. * * @param names a list of column names to insert/update. * @param values a list of values to insert/update. The {@code i}th * value in {@code values} will be inserted for the {@code i}th column * in {@code names}. * @return the INSERT statement those options are part of. * @throws IllegalArgumentException if {@code names.length != values.length}. */ public Insert values(String[] names, Object[] values) { return statement.values(names, values); } }
/** * Adds multiple column/value pairs to the values inserted by this INSERT statement. * * @param names a list of column names to insert/update. * @param values a list of values to insert/update. The {@code i}th * value in {@code values} will be inserted for the {@code i}th column * in {@code names}. * @return the INSERT statement those options are part of. * @throws IllegalArgumentException if {@code names.length != values.length}. */ public Insert values(String[] names, Object[] values) { return statement.values(names, values); } }
/** * Adds multiple column/value pairs to the values inserted by this INSERT statement. * * @param names a list of column names to insert/update. * @param values a list of values to insert/update. The {@code i}th * value in {@code values} will be inserted for the {@code i}th column * in {@code names}. * @return the INSERT statement those options are part of. * @throws IllegalArgumentException if {@code names.length != values.length}. */ public Insert values(String[] names, Object[] values) { return statement.values(names, values); } }
@Test(groups = "short") public void textRoutingKeyTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; String txt = "If she weighs the same as a duck... she's made of wood."; query = insertInto(table).values(new String[] {"k", "a", "b"}, new Object[] {txt, 1, 2}); assertEquals( query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); session().execute(query); query = select().from(table).where(eq("k", txt)); assertEquals( query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); Row row = session().execute(query).one(); assertEquals(row.getString("k"), txt); assertEquals(row.getInt("a"), 1); assertEquals(row.getInt("b"), 2); }
protected void write(int n, boolean batch, ConsistencyLevel cl) { // We don't use insert for our test because the resultSet don't ship the queriedHost // Also note that we don't use tracing because this would trigger requests that screw up the // test for (int i = 0; i < n; ++i) if (batch) // BUG: WriteType == SIMPLE session() .execute( batch() .add(insertInto(tableName).values(new String[] {"k", "i"}, new Object[] {0, 0})) .setConsistencyLevel(cl)); else session() .execute( new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", tableName)) .setConsistencyLevel(cl)); }
@Test(groups = "short") public void intRoutingKeyTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; query = insertInto(table).values(new String[] {"k", "a", "b"}, new Object[] {42, 1, 2}); ByteBuffer bb = ByteBuffer.allocate(4); bb.putInt(0, 42); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); session().execute(query); query = select().from(table).where(eq("k", 42)); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); Row row = session().execute(query).one(); assertEquals(row.getInt("k"), 42); assertEquals(row.getInt("a"), 1); assertEquals(row.getInt("b"), 2); }
insert = insertInto("foo") .values( new String[] {"a", "b"}, new Object[] {
CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; query = insertInto(table).values(new String[] {"theKey", "a", "b"}, new Object[] {42, 1, 2}); ByteBuffer bb = ByteBuffer.allocate(4); bb.putInt(0, 42); assertEquals(row.getInt("b"), 2); query = insertInto(table).values(new String[] {"ThEkEy", "a", "b"}, new Object[] {42, 1, 2}); bb = ByteBuffer.allocate(4); bb.putInt(0, 42);
@Override public InsertBuilder<T> values(String[] names, Object[] values) { insert.values(classBean.toColumn(names), values); return this; } @Override
batch = batch() .add(insertInto(table).values(new String[] {"k", "a"}, new Object[] {42, 1})) .add(update(table).using(ttl(400))); assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb);
insert = insertInto("foo") .values( new String[] {"a", "b"}, new Object[] { insert = insertInto("foo", "bar") .values( new String[] {"a", "b"}, new Object[] { .using(timestamp(42)) .value("c", 123) .values( new String[] {"a", "b"}, new Object[] { insertInto("foo").values(new String[] {"a", "b"}, new Object[] {1, 2, 3}); fail("Expected an IllegalArgumentException"); } catch (IllegalArgumentException e) {
.add( insertInto("foo") .values( new String[] {"a", "b"}, new Object[] {
public CassandraUtils insert(String[] names, Object[] values) { execute(QueryBuilder.insertInto(keyspace, table).values(names, values)); return this; }
insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())), insertInto("foo").value("k", 1).value("v", Sets.newHashSet(uuid())), insertInto("foo").values(new String[] {"k", "v"}, new Object[] {1, fcall("token", "k")}), insertInto("foo").values(new String[] {"k", "v"}, new Object[] {1, now()}), insertInto("foo").values(new String[] {"k", "v"}, new Object[] {1, uuid()}), insertInto("foo") .values( new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of("foo", fcall("token", "k"))}), insertInto("foo") .values(new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of("foo", now())}), insertInto("foo") .values(new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of("foo", uuid())}), insertInto("foo") .values( new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of(fcall("token", "k"), "foo")}), insertInto("foo") .values(new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of(now(), "foo")}), insertInto("foo") .values(new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of(uuid(), "foo")}), update("foo").with(set("v", fcall("token", "k"))).where(eq("k", 1)), update("foo").with(set("v", now())).where(eq("k", 1)), insertInto("foo").values(new String[] {"k", "v"}, new Object[] {1, raw("foo()")}), insertInto("foo") .values( new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of("foo", raw("foo()"))}), insertInto("foo")
/** * This method return the CQL query to insert data in to the table. * refer : http://docs.datastax.com/en/cql/3.1/cql/cql_reference/insert_r.html * * @param mapping Cassandra Mapping {@link CassandraMapping} * @param fields available fields * @return CQL Query */ static String getInsertDataQuery(CassandraMapping mapping, List<String> fields) { String[] columnNames = getColumnNames(mapping, fields); String[] objects = new String[fields.size()]; Arrays.fill(objects, "?"); return QueryBuilder.insertInto(mapping.getKeySpace().getName(), mapping.getCoreName()).values(columnNames, objects).getQueryString(); }
/** * This method return the CQL query to insert data in to the table. * refer : http://docs.datastax.com/en/cql/3.1/cql/cql_reference/insert_r.html * * @param mapping Cassandra Mapping {@link CassandraMapping} * @param fields available fields * @return CQL Query */ static String getInsertDataQuery(CassandraMapping mapping, List<String> fields) { String[] columnNames = getColumnNames(mapping, fields); String[] objects = new String[fields.size()]; Arrays.fill(objects, "?"); return QueryBuilder.insertInto(mapping.getKeySpace().getName(), mapping.getCoreName()).values(columnNames, objects).getQueryString(); }