Revision: 2fbcd5bc3bb3
Author: Rick Shaw <wfs...@gmail.com>
Date: Mon Dec 19 09:03:13 2011
Log: Resolve Issue #10. Missing support for Boolean in getBoolean()
* Silly omission in getBoolean() method.
* Fixed same problem in getInteger().
* Tidied up other methods for consistency.
* Added a junit test for testing regressions.
Signed-off-by: Rick Shaw <wfs...@gmail.com>
http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/source/detail?r=2fbcd5bc3bb3
Added:
/src/test/java/org/apache/cassandra/cql/jdbc/JdbcRegressionTest.java
Modified:
/src/main/java/org/apache/cassandra/cql/jdbc/CResultSet.java
/src/test/java/org/apache/cassandra/cql/jdbc/DataSourceTest.java
=======================================
--- /dev/null
+++ /src/test/java/org/apache/cassandra/cql/jdbc/JdbcRegressionTest.java
Mon Dec 19 09:03:13 2011
@@ -0,0 +1,94 @@
+/*
+ *
+ * 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.cql.jdbc;
+
+import static org.junit.Assert.*;
+
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import org.apache.cassandra.cql.ConnectionDetails;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JdbcRegressionTest
+{
+ private static java.sql.Connection con = null;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception
+ {
+ Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
+ con =
DriverManager.getConnection(String.format("jdbc:cassandra://%s:%d/%s",
+ ConnectionDetails.getHost(),
+ ConnectionDetails.getPort(),
+ "JdbcTestKeyspace"));
+ Statement stmt = con.createStatement();
+
+ // Create KeySpace
+ String createKS = "CREATE KEYSPACE 'JdbcTestKeyspace' WITH "
+ + "strategy_class = SimpleStrategy AND
strategy_options:replication_factor = 1;";
+ stmt.execute(createKS);
+
+ // Create the target Column family
+ String createCF = "CREATE COLUMNFAMILY RegressionTest (KEY text
PRIMARY KEY,"
+ + "bValue boolean, "
+ + "iValue int "
+ + ") WITH comparator = ascii AND
default_validation = bigint;";
+
+
+ stmt.execute(createCF);
+ stmt.close();
+ con.close();
+
+ // open it up again to see the new CF
+ con =
DriverManager.getConnection(String.format("jdbc:cassandra://%s:%d/%s",
+ ConnectionDetails.getHost(),
+ ConnectionDetails.getPort(),
+ "JdbcTestKeyspace"));
+
+ }
+
+ @Test
+ public void testIssue10() throws Exception
+ {
+ String insert = "INSERT INTO RegressionTest (KEY,bValue,iValue)
VALUES( 'key0',true, 2000);";
+ Statement statement = con.createStatement();
+
+ statement.executeUpdate(insert);
+ statement.close();
+
+ Thread.sleep(3000);
+
+ statement = con.createStatement();
+ ResultSet result = statement.executeQuery("SELECT bValue,iValue
FROM RegressionTest WHERE KEY=key0;");
+ result.next();
+ boolean b = result.getBoolean(1);
+ System.out.println("b ="+ b);
+ assertTrue(b);
+ int i = result.getInt(2);
+ System.out.println("i ="+ i);
+ assertEquals(2000, i);
+ }
+
+}
=======================================
--- /src/main/java/org/apache/cassandra/cql/jdbc/CResultSet.java Fri Nov 4
20:25:07 2011
+++ /src/main/java/org/apache/cassandra/cql/jdbc/CResultSet.java Mon Dec 19
09:03:13 2011
@@ -36,6 +36,27 @@
import org.apache.cassandra.thrift.CqlRow;
import org.apache.cassandra.utils.ByteBufferUtil;
+/**
+ * <p>The Supported Data types in CQL are as follows:</p>
+ * <table>
+ * <tr><th>type</th><th>java type</th><th>description</th></tr>
+ * <tr><td>ascii</td><td>String</td><td>ASCII character string</td></tr>
+ * <tr><td>bigint</td><td>Long</td><td>64-bit signed long</td></tr>
+ * <tr><td>blob</td><td>ByteBuffer</td><td>Arbitrary bytes (no
validation)</td></tr>
+ * <tr><td>boolean</td><td>Boolean</td><td>true or false</td></tr>
+ * <tr><td>counter</td><td>Long</td><td>Counter column (64-bit
long)</td></tr>
+ * <tr><td>decimal</td><td>BigDecimal</td><td>Variable-precision
decimal</td></tr>
+ * <tr><td>double</td><td>Double</td><td>64-bit IEEE-754 floating
point</td></tr>
+ * <tr><td>float</td><td>Float</td><td>32-bit IEEE-754 floating
point</td></tr>
+ * <tr><td>int</td><td>Integer</td><td>32-bit signed int</td></tr>
+ * <tr><td>text</td><td>String</td><td>UTF8 encoded string</td></tr>
+ * <tr><td>timestamp</td><td>Date</td><td>A timestamp</td></tr>
+ * <tr><td>uuid</td><td>UUID</td><td>Type 1 or type 4 UUID</td></tr>
+ * <tr><td>varchar</td><td>String</td><td>UTF8 encoded string</td></tr>
+ * <tr><td>varint</td><td>BigInteger</td><td>Arbitrary-precision
integer</td></tr>
+ * </table>
+ *
+ */
class CResultSet extends AbstractResultSet implements CassandraResultSet
{
public static final int DEFAULT_TYPE = ResultSet.TYPE_FORWARD_ONLY;
@@ -127,7 +148,7 @@
{
// 1 <= index <= size()
if (index < 1 || index > values.size())
- throw new
SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE,
String.valueOf(index)));
+ throw new
SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE,
String.valueOf(index))+" "+values.size());
}
private final void checkName(String name) throws SQLException
@@ -239,10 +260,12 @@
if (wasNull) return BigInteger.ZERO;
- if (value instanceof Long) return BigInteger.valueOf((Long) value);
-
if (value instanceof BigInteger) return (BigInteger) value;
+ if (value instanceof Integer) return BigInteger.valueOf((Integer)
value);
+
+ if (value instanceof Long) return BigInteger.valueOf((Long) value);
+
try
{
if (value instanceof String) return (new BigInteger((String)
value));
@@ -275,6 +298,10 @@
if (wasNull) return false;
+ if (value instanceof Boolean) return (Boolean)value;
+
+ if (value instanceof Integer) return Boolean.valueOf(((Integer)
value) == 0 ? false : true);
+
if (value instanceof Long) return Boolean.valueOf(((Long) value)
== 0 ? false : true);
if (value instanceof BigInteger) return
Boolean.valueOf(((BigInteger) value).intValue() == 0 ? false : true);
@@ -311,6 +338,8 @@
if (wasNull) return 0;
+ if (value instanceof Integer) return ((Integer) value).byteValue();
+
if (value instanceof Long) return ((Long) value).byteValue();
if (value instanceof BigInteger) return ((BigInteger)
value).byteValue();
@@ -340,7 +369,7 @@
private byte[] getBytes(TypedColumn column) throws SQLException
{
checkNotClosed();
- ByteBuffer value = (ByteBuffer) column.getValue();
+ ByteBuffer value = (ByteBuffer) column.getRawColumn().value;
wasNull = value == null;
return value == null ? null : ByteBufferUtil.clone(value).array();
}
@@ -435,14 +464,16 @@
if (wasNull) return 0.0;
- if (value instanceof Long) return new Double((Long) value);
-
- if (value instanceof BigInteger) return new Double(((BigInteger)
value).doubleValue());
-
if (value instanceof Double) return ((Double) value);
if (value instanceof Float) return ((Float) value).doubleValue();
+ if (value instanceof Integer) return new Double((Integer) value);
+
+ if (value instanceof Long) return new Double((Long) value);
+
+ if (value instanceof BigInteger) return new Double(((BigInteger)
value).doubleValue());
+
try
{
if (value instanceof String) return new Double((String) value);
@@ -487,14 +518,16 @@
if (wasNull) return (float) 0.0;
- if (value instanceof Long) return new Float((Long) value);
-
- if (value instanceof BigInteger) return new Float(((BigInteger)
value).floatValue());
-
if (value instanceof Float) return ((Float) value);
if (value instanceof Double) return ((Double) value).floatValue();
+ if (value instanceof Integer) return new Float((Integer) value);
+
+ if (value instanceof Long) return new Float((Long) value);
+
+ if (value instanceof BigInteger) return new Float(((BigInteger)
value).floatValue());
+
try
{
if (value instanceof String) return new Float((String) value);
@@ -533,11 +566,12 @@
if (wasNull) return 0;
- // bit of a hack, this, but asking for getInt seems so common that
we should accommodate it
- if (value instanceof BigInteger) return ((BigInteger)
value).intValue();
-
+ if (value instanceof Integer) return ((Integer) value);
+
if (value instanceof Long) return ((Long) value).intValue();
+ if (value instanceof BigInteger) return ((BigInteger)
value).intValue();
+
try
{
if (value instanceof String) return (Integer.parseInt((String)
value));
@@ -575,6 +609,10 @@
if (wasNull) return 0L;
+ if (value instanceof Long) return (Long) value;
+
+ if (value instanceof Integer) return Long.valueOf((Integer) value);
+
if (value instanceof BigInteger) return
getBigInteger(column).longValue();
if (value instanceof Long) return (Long) value;
@@ -664,6 +702,8 @@
if (wasNull) return 0;
+ if (value instanceof Integer) return ((Integer)
value).shortValue();
+
if (value instanceof Long) return ((Long) value).shortValue();
if (value instanceof BigInteger) return ((BigInteger)
value).shortValue();
=======================================
--- /src/test/java/org/apache/cassandra/cql/jdbc/DataSourceTest.java Thu
Oct 13 01:56:33 2011
+++ /src/test/java/org/apache/cassandra/cql/jdbc/DataSourceTest.java Mon
Dec 19 09:03:13 2011
@@ -58,16 +58,12 @@
assertFalse(cnx.isClosed());
ds.setLoginTimeout(5);
assertEquals(5, ds.getLoginTimeout());
- ds.setLogWriter(pw);
- assertNotNull(ds.getLogWriter());
// no username and password
cnx = ds.getConnection();
assertFalse(cnx.isClosed());
ds.setLoginTimeout(5);
assertEquals(5, ds.getLoginTimeout());
- ds.setLogWriter(pw);
- assertNotNull(ds.getLogWriter());
}