You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ee...@apache.org on 2011/02/07 20:05:50 UTC
svn commit: r1068051 - in
/cassandra/trunk/src/java/org/apache/cassandra/cql: Cql.g Term.java
Author: eevans
Date: Mon Feb 7 19:05:49 2011
New Revision: 1068051
URL: http://svn.apache.org/viewvc?rev=1068051&view=rev
Log:
utf8 and integer term types
Patch by eevans for CASSANDRA-2027
Modified:
cassandra/trunk/src/java/org/apache/cassandra/cql/Cql.g
cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Cql.g
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Cql.g?rev=1068051&r1=1068050&r2=1068051&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Cql.g (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Cql.g Mon Feb 7 19:05:49 2011
@@ -178,7 +178,7 @@ createKeyspaceStatement returns [CreateK
// TODO: date/time, utf8
term returns [Term item]
- : ( t=STRING_LITERAL | t=LONG )
+ : ( t=STRING_LITERAL | t=LONG | t=INTEGER | t=UNICODE )
{ $item = new Term($t.text, $t.type); }
;
@@ -324,6 +324,10 @@ IDENT
COMPIDENT
: IDENT ( ':' IDENT)*
;
+
+UNICODE
+ : 'u' STRING_LITERAL
+ ;
WS
: (' ' | '\t' | '\n' | '\r')+ { $channel = HIDDEN; }
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java?rev=1068051&r1=1068050&r2=1068051&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java Mon Feb 7 19:05:49 2011
@@ -20,15 +20,13 @@
*/
package org.apache.cassandra.cql;
+import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.FBUtilities;
-/**
- * A term parsed from a CQL statement.
- *
- */
+/** A term parsed from a CQL statement. */
public class Term
{
private final String text;
@@ -67,15 +65,41 @@ public class Term
* Returns the typed value, serialized to a ByteBuffer.
*
* @return a ByteBuffer of the value.
+ * @throws InvalidRequestException if unable to coerce the string to its type.
*/
- public ByteBuffer getByteBuffer()
+ public ByteBuffer getByteBuffer() throws InvalidRequestException
{
switch (type)
{
case STRING:
return ByteBuffer.wrap(text.getBytes());
case LONG:
- return ByteBufferUtil.bytes(Long.parseLong(text));
+ try
+ {
+ return ByteBufferUtil.bytes(Long.parseLong(text));
+ }
+ catch (NumberFormatException e)
+ {
+ throw new InvalidRequestException(text + " is not valid for type long");
+ }
+ case INTEGER:
+ try
+ {
+ return ByteBufferUtil.bytes(Integer.parseInt(text));
+ }
+ catch (NumberFormatException e)
+ {
+ throw new InvalidRequestException(text + " is not valid for type int");
+ }
+ case UNICODE:
+ try
+ {
+ return ByteBuffer.wrap(text.getBytes("UTF-8"));
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new RuntimeException(e);
+ }
}
// FIXME: handle scenario that should never happen
@@ -101,7 +125,7 @@ public class Term
enum TermType
{
- STRING, LONG;
+ STRING, LONG, INTEGER, UNICODE;
static TermType forInt(int type)
{
@@ -109,6 +133,10 @@ enum TermType
return STRING;
else if (type == CqlParser.LONG)
return LONG;
+ else if (type == CqlParser.INTEGER)
+ return INTEGER;
+ else if (type == CqlParser.UNICODE)
+ return UNICODE;
// FIXME: handled scenario that should never occur.
return null;