@Test(groups = "unit") public void should_quote_complex_column_names() { // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;"; Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1)); assertThat(statement.toString()).isEqualTo(query); }
@Test(groups = "unit") public void should_quote_column_names_with_escaped_quotes() { // A column name can include quotes as long as it is escaped with another set of quotes, so // "foo""bar" is a valid name. String query = "SELECT * FROM foo WHERE \"foo \"\" bar\"=1;"; Statement statement = select().from("foo").where(eq(quote("foo \" bar"), 1)); assertThat(statement.toString()).isEqualTo(query); }
@Test(groups = "unit") public void truncateTest() throws Exception { assertEquals(truncate("foo").toString(), "TRUNCATE foo;"); assertEquals(truncate("foo", quote("Bar")).toString(), "TRUNCATE foo.\"Bar\";"); }
@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 ();"); }
/** * @test_category queries:builder * @jira_ticket JAVA-1286 * @jira_ticket CASSANDRA-7423 */ @Test(groups = "unit") public void should_handle_setting_udt_fields() throws Exception { assertThat( update("tbl") .with(set(path("a", quote("B")), "foo")) .and(set(raw("c.\"D\""), "bar")) .where(eq("k", 0)) .getQueryString()) .isEqualTo("UPDATE tbl SET a.\"B\"=?,c.\"D\"=? WHERE k=0;"); }
.value("a", 123) .value("b", InetAddress.getByName("127.0.0.1")) .value(quote("C"), "foo'bar") .value( "d",
"SELECT a,b,\"C\" FROM foo WHERE a IN ('127.0.0.1','127.0.0.3') AND \"C\"='foo' ORDER BY a ASC,b DESC LIMIT 42;"; select = select("a", "b", quote("C")) .from("foo") .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3"))) .and(eq(quote("C"), "foo")) .orderBy(asc("a"), desc("b")) .limit(42);
@Test(groups = "unit") public void should_quote_column_names_with_escaped_quotes() { // A column name can include quotes as long as it is escaped with another set of quotes, so // "foo""bar" is a valid name. String query = "SELECT * FROM foo WHERE \"foo \"\" bar\"=1;"; Statement statement = select().from("foo").where(eq(quote("foo \" bar"), 1)); assertThat(statement.toString()).isEqualTo(query); }
@Test(groups = "unit") public void should_quote_complex_column_names() { // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;"; Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1)); assertThat(statement.toString()).isEqualTo(query); }
@Test(groups = "unit") public void truncateTest() throws Exception { assertEquals(truncate("foo").toString(), "TRUNCATE foo;"); assertEquals(truncate("foo", quote("Bar")).toString(), "TRUNCATE foo.\"Bar\";"); }
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 ); }
private boolean nextValueUpdate(IdSourceKeyMetadata metadata, String sequenceName, Long oldValue, Long newValue) { Statement update = QueryBuilder.update( quote( metadata.getName() ) ) .with( set( quote( metadata.getValueColumnName() ), QueryBuilder.bindMarker( "sequence_value_new" ) ) ) .where( eq( quote( metadata.getKeyColumnName() ), QueryBuilder.bindMarker( "sequence_name" ) ) ) .onlyIf( eq( quote( metadata.getValueColumnName() ), QueryBuilder.bindMarker( "sequence_value_old" ) ) ); PreparedStatement preparedStatement = provider.getSession().prepare( update.toString() ); BoundStatement boundStatement = preparedStatement.bind(); boundStatement.setString( "sequence_name", sequenceName ); boundStatement.setLong( "sequence_value_new", newValue ); boundStatement.setLong( "sequence_value_old", oldValue ); ResultSet resultSet; try { resultSet = provider.getSession().execute( boundStatement ); } catch (DriverException e) { throw e; } return resultSet.one().getBool( 0 ); }
@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 ();"); }
private Long nextValueSelect(IdSourceKeyMetadata metadata, String sequenceName) { Statement select = QueryBuilder.select().column( quote( metadata.getValueColumnName() ) ) .from( quote( metadata.getName() ) ) .where( eq( metadata.getKeyColumnName(), QueryBuilder.bindMarker() ) ); PreparedStatement preparedStatement = provider.getSession().prepare( select.toString() ); BoundStatement boundStatement = preparedStatement.bind( sequenceName ); ResultSet resultSet; try { resultSet = provider.getSession().execute( boundStatement ); } catch (DriverException e) { throw e; } if ( resultSet.isExhausted() ) { return null; } else { return resultSet.one().getLong( 0 ); } }
/** * @test_category queries:builder * @jira_ticket JAVA-1286 * @jira_ticket CASSANDRA-7423 */ @Test(groups = "unit") public void should_handle_setting_udt_fields() throws Exception { assertThat( update("tbl") .with(set(path("a", quote("B")), "foo")) .and(set(raw("c.\"D\""), "bar")) .where(eq("k", 0)) .getQueryString()) .isEqualTo("UPDATE tbl SET a.\"B\"=?,c.\"D\"=? WHERE k=0;"); }
.writeTime("age") .as("wage") .column(quote("~~vertex_exists")) .writeTime(quote("~~vertex_exists")) .as("wv") .from(cluster().getConfiguration().getGraphOptions().getGraphName(), "person_p") int age = row.getInt("age"); boolean vertexExists = row.getBool(quote("~~vertex_exists")); assertThat(age).isEqualTo(23); assertThat(row.getLong("wage")).isEqualTo(expectedTs); } else if (!row.isNull(quote("~~vertex_exists"))) { assertThat(vertexExists).isTrue(); assertThat(row.getLong("wv")).isEqualTo(expectedTs);
.value("a", 123) .value("b", InetAddress.getByName("127.0.0.1")) .value(quote("C"), "foo'bar") .value( "d",
"SELECT a,b,\"C\" FROM foo WHERE a IN ('127.0.0.1','127.0.0.3') AND \"C\"='foo' ORDER BY a ASC,b DESC LIMIT 42;"; select = select("a", "b", quote("C")) .from("foo") .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3"))) .and(eq(quote("C"), "foo")) .orderBy(asc("a"), desc("b")) .limit(42);