/** * Ensures that a statement can be printed with and without a required custom codec. The * expectation is that if the codec is not registered, then the query string should contain bind * markers for all variables; if however all codecs are properly registered, then the query string * should contain all variables inlined and formatted properly. * * @jira_ticket JAVA-1272 */ @Test(groups = "unit") public void should_inline_custom_codec() throws Exception { assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (?);"); CodecRegistry.DEFAULT_INSTANCE.register(new FooCodec()); assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (42);"); }
.json( "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}';"); insertInto("users") .json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}") .toString()) .isEqualTo( "INSERT INTO users JSON '{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}';"); assertThat(insertInto("users").json(bindMarker()).toString()) .isEqualTo("INSERT INTO users JSON ?;"); assertThat(insertInto("users").json(bindMarker("json")).toString()) .isEqualTo("INSERT INTO users JSON :json;"); assertThat( "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .defaultNull() .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}' DEFAULT NULL;"); "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .defaultUnset() .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}' DEFAULT UNSET;");
@Test(groups = "unit") public void should_handle_from_json() throws Exception { assertThat( update("users") .with(set("age", fromJson("42"))) .where(eq("id", fromJson("\"user123\""))) .toString()) .isEqualTo("UPDATE users SET age=fromJson('42') WHERE id=fromJson('\"user123\"');"); assertThat( insertInto("users") .value("id", fromJson("\"user123\"")) .value("age", fromJson("42")) .toString()) .isEqualTo("INSERT INTO users (id,age) VALUES (fromJson('\"user123\"'),fromJson('42'));"); assertThat(insertInto("users").value("id", fromJson(bindMarker())).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(?));"); assertThat(insertInto("users").value("id", fromJson(bindMarker("id"))).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(:id));"); }
@Override public String toString() { return insert.toString(); }
@Test(groups = "unit") public void quotingTest() { assertEquals(select().from("Metrics", "epochs").toString(), "SELECT * FROM Metrics.epochs;"); assertEquals( select().from("Metrics", quote("epochs")).toString(), "SELECT * FROM Metrics.\"epochs\";"); assertEquals( select().from(quote("Metrics"), "epochs").toString(), "SELECT * FROM \"Metrics\".epochs;"); assertEquals( select().from(quote("Metrics"), quote("epochs")).toString(), "SELECT * FROM \"Metrics\".\"epochs\";"); assertEquals( insertInto("Metrics", "epochs").toString(), "INSERT INTO Metrics.epochs () VALUES ();"); assertEquals( insertInto("Metrics", quote("epochs")).toString(), "INSERT INTO Metrics.\"epochs\" () VALUES ();"); assertEquals( insertInto(quote("Metrics"), "epochs").toString(), "INSERT INTO \"Metrics\".epochs () VALUES ();"); assertEquals( insertInto(quote("Metrics"), quote("epochs")).toString(), "INSERT INTO \"Metrics\".\"epochs\" () VALUES ();"); }
.value("i", 1) .value("f", 1.1) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 2) .value("f", 2.5) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 3) .value("f", 3.7) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 4) .value("f", 5.0) .toString());
Insert insert = (Insert)CCJSqlParserUtil.parse("insert into mytable (col1) values (1)"); System.out.println(insert.toString());
System.out.println(insertStmt.toString());
/** * Ensures that a statement can be printed with and without a required custom codec. The * expectation is that if the codec is not registered, then the query string should contain bind * markers for all variables; if however all codecs are properly registered, then the query string * should contain all variables inlined and formatted properly. * * @jira_ticket JAVA-1272 */ @Test(groups = "unit") public void should_inline_custom_codec() throws Exception { assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (?);"); CodecRegistry.DEFAULT_INSTANCE.register(new FooCodec()); assertThat(insertInto("users").value("id", new Foo(42)).toString()) .isEqualTo("INSERT INTO users (id) VALUES (42);"); }
.json( "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}';"); insertInto("users") .json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}") .toString()) .isEqualTo( "INSERT INTO users JSON '{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}';"); assertThat(insertInto("users").json(bindMarker()).toString()) .isEqualTo("INSERT INTO users JSON ?;"); assertThat(insertInto("users").json(bindMarker("json")).toString()) .isEqualTo("INSERT INTO users JSON :json;"); assertThat( "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .defaultNull() .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}' DEFAULT NULL;"); "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}") .defaultUnset() .toString()) .isEqualTo( "INSERT INTO example JSON '{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}' DEFAULT UNSET;");
@Override String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<AliasedMappedProperty> columns, Collection<Mapper.Option> options) { Insert insert = table == null ? insertInto(mapper.keyspace, mapper.table) : insertInto(table); for (AliasedMappedProperty col : columns) if (!col.mappedProperty.isComputed()) insert.value(col.mappedProperty.getMappedName(), bindMarker()); for (Mapper.Option opt : options) { opt.validate(QueryType.SAVE, manager); opt.modifyQueryString(insert); } return insert.toString(); }
log.debug("Recording Event: " + insert.toString()); ResultSet resultSet = cassandraSession.execute(insert); log.debug("Recorded Event: " + resultSet.toString());
private static String insertQuery(TableMetadata tableMetadata, String... options) { Insert insert = QueryBuilder.insertInto(tableMetadata); if (options != null) { Insert.Options using = insert.using(); for (String option : options) { if ("TTL".equals(option)) { using.and(QueryBuilder.ttl(QueryBuilder.bindMarker())); } else { using.and(QueryBuilder.timestamp(QueryBuilder.bindMarker())); } } } List<ColumnMetadata> columns = tableMetadata.getColumns(); for(ColumnMetadata column : columns) { insert.value(column.getName(), QueryBuilder.bindMarker()); } return insert.toString(); }
@Test(groups = "unit") public void should_handle_from_json() throws Exception { assertThat( update("users") .with(set("age", fromJson("42"))) .where(eq("id", fromJson("\"user123\""))) .toString()) .isEqualTo("UPDATE users SET age=fromJson('42') WHERE id=fromJson('\"user123\"');"); assertThat( insertInto("users") .value("id", fromJson("\"user123\"")) .value("age", fromJson("42")) .toString()) .isEqualTo("INSERT INTO users (id,age) VALUES (fromJson('\"user123\"'),fromJson('42'));"); assertThat(insertInto("users").value("id", fromJson(bindMarker())).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(?));"); assertThat(insertInto("users").value("id", fromJson(bindMarker("id"))).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(:id));"); }
@Test(groups = "unit") public void quotingTest() { assertEquals(select().from("Metrics", "epochs").toString(), "SELECT * FROM Metrics.epochs;"); assertEquals( select().from("Metrics", quote("epochs")).toString(), "SELECT * FROM Metrics.\"epochs\";"); assertEquals( select().from(quote("Metrics"), "epochs").toString(), "SELECT * FROM \"Metrics\".epochs;"); assertEquals( select().from(quote("Metrics"), quote("epochs")).toString(), "SELECT * FROM \"Metrics\".\"epochs\";"); assertEquals( insertInto("Metrics", "epochs").toString(), "INSERT INTO Metrics.epochs () VALUES ();"); assertEquals( insertInto("Metrics", quote("epochs")).toString(), "INSERT INTO Metrics.\"epochs\" () VALUES ();"); assertEquals( insertInto(quote("Metrics"), "epochs").toString(), "INSERT INTO \"Metrics\".epochs () VALUES ();"); assertEquals( insertInto(quote("Metrics"), quote("epochs")).toString(), "INSERT INTO \"Metrics\".\"epochs\" () VALUES ();"); }
.value("i", 1) .value("f", 1.1) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 2) .value("f", 2.5) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 3) .value("f", 3.7) .toString(), insertInto(TABLE2) .value("k", "cast_t") .value("i", 4) .value("f", 5.0) .toString());
private Long nextValueInsert(IdSourceKeyMetadata metadata, String sequenceName, Long value) { Insert insert = QueryBuilder.insertInto( quote( metadata.getName() ) ) .value( quote( metadata.getKeyColumnName() ), QueryBuilder.bindMarker( "sequence_name" ) ) .value( quote( metadata.getValueColumnName() ), QueryBuilder.bindMarker( "sequence_value" ) ) .ifNotExists(); PreparedStatement preparedStatement = provider.getSession().prepare( insert.toString() ); BoundStatement boundStatement = preparedStatement.bind(); boundStatement.setString( "sequence_name", sequenceName ); boundStatement.setLong( "sequence_value", value ); try { provider.getSession().execute( boundStatement ); } catch (DriverException e) { throw e; } return nextValueSelect( metadata, sequenceName ); }