/** * Quotes a column name to make it case sensitive. * * @param columnName the column name to quote. * @return the quoted column name. * @see Metadata#quote(String) */ public static String quote(String columnName) { return Metadata.quote(columnName); }
/** * Quotes a CQL identifier if necessary. * * <p>This is similar to {@link #quote(String)}, except that it won't quote the input string if it * can safely be used as-is. For example: * * <ul> * <li>{@code quoteIfNecessary("foo").equals("foo")} (no need to quote). * <li>{@code quoteIfNecessary("Foo").equals("\"Foo\"")} (identifier is mixed case so case * sensitivity is required) * <li>{@code quoteIfNecessary("foo bar").equals("\"foo bar\"")} (identifier contains special * characters) * <li>{@code quoteIfNecessary("table").equals("\"table\"")} (identifier is a reserved CQL * keyword) * </ul> * * @param id the "internal" form of the identifier. That is, the identifier as it would appear in * Cassandra system tables (such as {@code system_schema.tables}, {@code * system_schema.columns}, etc.) * @return the identifier as it would appear in a CQL query string. This is also how you need to * pass it to public driver methods, such as {@link #getKeyspace(String)}. */ public static String quoteIfNecessary(String id) { return needsQuote(id) ? quote(id) : id; }
public TableMetadataAssert hasMaterializedView(MaterializedViewMetadata expected) { assertThat(actual.getView(Metadata.quote(expected.getName()))).isNotNull().isEqualTo(expected); return this; }
private static int[] computePkIndices(Metadata clusterMetadata, ColumnDefinitions boundColumns) { List<ColumnMetadata> partitionKeyColumns = null; int[] pkIndexes = null; KeyspaceMetadata km = clusterMetadata.getKeyspace(Metadata.quote(boundColumns.getKeyspace(0))); if (km != null) { TableMetadata tm = km.getTable(Metadata.quote(boundColumns.getTable(0))); if (tm != null) { partitionKeyColumns = tm.getPartitionKey(); pkIndexes = new int[partitionKeyColumns.size()]; for (int i = 0; i < pkIndexes.length; ++i) pkIndexes[i] = -1; } } // Note: we rely on the fact CQL queries cannot span multiple tables. If that change, we'll have // to get smarter. for (int i = 0; i < boundColumns.size(); i++) maybeGetIndex(boundColumns.getName(i), i, partitionKeyColumns, pkIndexes); return allSet(pkIndexes) ? pkIndexes : null; }
return childPolicy.newQueryPlan(keyspace, statement); final Set<Host> replicas = clusterMetadata.getReplicas(Metadata.quote(keyspace), partitionKey); if (replicas.isEmpty()) return childPolicy.newQueryPlan(loggedKeyspace, statement);
/** * @test_category queries:builder * @jira_ticket JAVA-1286 * @jira_ticket CASSANDRA-7423 */ @Test(groups = "unit") public void should_handle_retrieving_udt_fields() throws Exception { assertThat(select().path("a", Metadata.quote("B")).raw("c.\"D\"").from("tbl").getQueryString()) .isEqualTo("SELECT a.\"B\",c.\"D\" FROM tbl;"); }
@BeforeMethod(groups = "unit") public void initMocks() { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); Metadata metadata = mock(Metadata.class); childPolicy = mock(LoadBalancingPolicy.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getReplicas(Metadata.quote("keyspace"), routingKey)) .thenReturn(Sets.newLinkedHashSet(host1, host2)); when(childPolicy.newQueryPlan("keyspace", statement)) .thenReturn(Sets.newLinkedHashSet(host4, host3, host2, host1).iterator()); when(childPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); when(host1.isUp()).thenReturn(true); when(host2.isUp()).thenReturn(true); when(host3.isUp()).thenReturn(true); when(host4.isUp()).thenReturn(true); }
@Test(groups = "short") public void testCaseSensitiveKeyspace() throws Throwable { String ksName = "\"MyKeyspace2\""; session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); assertExists(ksName, "MyKeyspace2"); assertExists(Metadata.quote("MyKeyspace2"), "MyKeyspace2"); assertNotExists("mykeyspace2"); assertNotExists("MyKeyspace2"); assertNotExists("MYKEYSPACE2"); }
/** * Quotes a columnName to make it case sensitive. * * @param columnName the column name to quote. * @return the quoted column name. * @see Metadata#quote(String) */ public static String quote(String columnName) { return Metadata.quote(columnName); }
/** * Quotes a columnName to make it case sensitive. * * @param columnName the column name to quote. * @return the quoted column name. * @see Metadata#quote(String) */ public static String quote(String columnName) { return Metadata.quote(columnName); }
protected static String escapeId(String ident) { // we don't need to escape if it's lowercase and match non-quoted CQL3 ids. return lowercaseAlphanumeric.matcher(ident).matches() ? ident : Metadata.quote(ident); }
static String escapeId(String ident) { // we don't need to escape if it's lowercase and match non-quoted CQL3 ids. return lowercaseId.matcher(ident).matches() ? ident : quote(ident); }
@Test(groups = "short") public void should_store_and_retrieve_with_simple_statements() throws Exception { int userId = 1; UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); UDTValue addr = addrDef .newValue() .setString("street", "1600 Pennsylvania Ave NW") .setInt(quote("ZIP\""), 20500) .setSet("phones", ImmutableSet.of(phone1, phone2)); session().execute("INSERT INTO user(id, addr) VALUES (?, ?)", userId, addr); Row r = session().execute("SELECT * FROM user WHERE id=?", userId).one(); assertThat(r.getInt("id")).isEqualTo(userId); assertThat(r.getUDTValue("addr")).isEqualTo(addr); }
throws Exception { String c1 = Metadata.quote("quotes go \"\" here \"\" "); String c2 = Metadata.quote("\\x00\\x25"); String c3 = Metadata.quote("columnfamily"); String c4 = Metadata.quote("select"); String c5 = Metadata.quote("who''s there'? "); String c6 = Metadata.quote("faux )"); String c7 = Metadata.quote("COMPACT STORAGE");
@Test(groups = "short") public void should_store_and_retrieve_with_prepared_statements() throws Exception { int userId = 0; PreparedStatement ins = session().prepare("INSERT INTO user(id, addr) VALUES (?, ?)"); PreparedStatement sel = session().prepare("SELECT * FROM user WHERE id=?"); UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); UDTValue addr = addrDef .newValue() .setString("street", "1600 Pennsylvania Ave NW") .setInt(quote("ZIP\""), 20500) .setSet("phones", ImmutableSet.of(phone1, phone2)); session().execute(ins.bind(userId, addr)); Row r = session().execute(sel.bind(userId)).one(); assertThat(r.getInt("id")).isEqualTo(0); }
static String escapeId(String ident) { // we don't need to escape if it's lowercase and match non-quoted CQL3 ids, // and if it's not a CQL reserved keyword return lowercaseAlphanumeric.matcher(ident).matches() && !isReservedCqlKeyword(ident) ? ident : quote(ident); }
@Test(groups = "short") public void should_store_type_definitions_in_their_keyspace() throws Exception { KeyspaceMetadata thisKeyspace = cluster().getMetadata().getKeyspace(this.keyspace); // Types that don't exist don't have definitions assertThat(thisKeyspace.getUserType("address1")).isNull(); assertThat(thisKeyspace.getUserType("phone1")).isNull(); // Types created by this test have definitions assertThat(thisKeyspace.getUserType(quote("\"User Address\""))).isNotNull(); assertThat(thisKeyspace.getUserType("phone")).isNotNull(); // If we create another keyspace, it doesn't have the definitions of this keyspace String otherKeyspaceName = this.keyspace + "_nonEx"; session() .execute( "CREATE KEYSPACE " + otherKeyspaceName + " " + "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); KeyspaceMetadata otherKeyspace = cluster().getMetadata().getKeyspace(otherKeyspaceName); assertThat(otherKeyspace.getUserType(quote("\"User Address\""))).isNull(); assertThat(otherKeyspace.getUserType("phone")).isNull(); }
@Test(groups = "short") public void should_parse_nested_collection_types() { Metadata metadata = cluster().getMetadata(); KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(this.keyspace); assertThat(parse("list<list<int>>", cluster(), null, null, null, false, false)) .isEqualTo(list(list(cint()))); assertThat( parse( "set<list<frozen<map<bigint,varchar>>>>", cluster(), null, null, null, false, false)) .isEqualTo(set(list(map(bigint(), varchar(), true)))); UserType keyType = keyspaceMetadata.getUserType(quote("Incr,edibly\" EvilTy<>><<><p\"e")); UserType valueType = keyspaceMetadata.getUserType(quote("A")); assertThat( parse( "map<frozen<\"Incr,edibly\"\" EvilTy<>><<><p\"\"e\">,frozen<\"A\">>", cluster(), keyspace, keyspaceMetadata.userTypes, null, false, false)) .isEqualTo(map(keyType, valueType, false)); }
/** * @test_category queries:builder * @jira_ticket JAVA-1286 * @jira_ticket CASSANDRA-7423 */ @Test(groups = "unit") public void should_handle_retrieving_udt_fields() throws Exception { assertThat(select().path("a", Metadata.quote("B")).raw("c.\"D\"").from("tbl").getQueryString()) .isEqualTo("SELECT a.\"B\",c.\"D\" FROM tbl;"); }
@Test(groups = "short") public void testCaseSensitiveKeyspace() throws Throwable { String ksName = "\"MyKeyspace2\""; session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); assertExists(ksName, "MyKeyspace2"); assertExists(Metadata.quote("MyKeyspace2"), "MyKeyspace2"); assertNotExists("mykeyspace2"); assertNotExists("MyKeyspace2"); assertNotExists("MYKEYSPACE2"); }