diff --git a/CHANGES.txt b/CHANGES.txt index b8d2ff661a..28dd6b40dd 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -18,6 +18,8 @@ * Improve JBOD disk utilization (CASSANDRA-7386) * Log failed host when preparing incremental repair (CASSANDRA-8228) Merged from 2.0: + * Throw correct exception when trying to bind a keyspace or table + name (CASSANDRA-6952) * Make HHOM.compact synchronized (CASSANDRA-8416) * cancel latency-sampling task when CF is dropped (CASSANDRA-8401) * don't block SocketThread for MessagingService (CASSANDRA-8188) diff --git a/src/java/org/apache/cassandra/cql3/Cql.g b/src/java/org/apache/cassandra/cql3/Cql.g index 06ba81c2f3..034813cba6 100644 --- a/src/java/org/apache/cassandra/cql3/Cql.g +++ b/src/java/org/apache/cassandra/cql3/Cql.g @@ -859,6 +859,7 @@ cfOrKsName[CFName name, boolean isKs] : t=IDENT { if (isKs) $name.setKeyspace($t.text, false); else $name.setColumnFamily($t.text, false); } | t=QUOTED_NAME { if (isKs) $name.setKeyspace($t.text, true); else $name.setColumnFamily($t.text, true); } | k=unreserved_keyword { if (isKs) $name.setKeyspace(k, false); else $name.setColumnFamily(k, false); } + | QMARK {addRecognitionError("Bind variables cannot be used for keyspace or table names");} ; constant returns [Constants.Literal constant] diff --git a/test/unit/org/apache/cassandra/cql3/CQLTester.java b/test/unit/org/apache/cassandra/cql3/CQLTester.java index dd2289647a..22261533dc 100644 --- a/test/unit/org/apache/cassandra/cql3/CQLTester.java +++ b/test/unit/org/apache/cassandra/cql3/CQLTester.java @@ -384,13 +384,17 @@ public abstract class CQLTester { if (errorMessage != null) { - Assert.assertTrue("Expected error message to contain '" + errorMessage + "', but got '" + e.getMessage() + "'", - e.getMessage().contains(errorMessage)); + assertMessageContains(errorMessage, e); } } } protected void assertInvalidSyntax(String query, Object... values) throws Throwable + { + assertInvalidSyntaxMessage(null, query, values); + } + + protected void assertInvalidSyntaxMessage(String errorMessage, String query, Object... values) throws Throwable { try { @@ -402,10 +406,25 @@ public abstract class CQLTester } catch (SyntaxException e) { - // This is what we expect + if (errorMessage != null) + { + assertMessageContains(errorMessage, e); + } } } + /** + * Asserts that the message of the specified exception contains the specified text. + * + * @param text the text that the exception message must contains + * @param e the exception to check + */ + private static void assertMessageContains(String text, Exception e) + { + Assert.assertTrue("Expected error message to contain '" + text + "', but got '" + e.getMessage() + "'", + e.getMessage().contains(text)); + } + private static String replaceValues(String query, Object[] values) { StringBuilder sb = new StringBuilder(); diff --git a/test/unit/org/apache/cassandra/cql3/UseStatementTest.java b/test/unit/org/apache/cassandra/cql3/UseStatementTest.java new file mode 100644 index 0000000000..77ac8a76ae --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/UseStatementTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.cql3; + +import org.junit.Test; + +public class UseStatementTest extends CQLTester +{ + @Test + public void testUseStatementWithBindVariable() throws Throwable + { + assertInvalidSyntaxMessage("Bind variables cannot be used for keyspace or table names", "USE ?"); + } +}