You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2011/06/01 17:52:57 UTC

svn commit: r1130197 - in /cassandra/branches/cassandra-0.8: CHANGES.txt drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java

Author: jbellis
Date: Wed Jun  1 15:52:56 2011
New Revision: 1130197

URL: http://svn.apache.org/viewvc?rev=1130197&view=rev
Log:
improve CQL JDBCspec compliance
patch by Rick Shaw; reviewed by Yuki Morishita and jbellis

Modified:
    cassandra/branches/cassandra-0.8/CHANGES.txt
    cassandra/branches/cassandra-0.8/drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java

Modified: cassandra/branches/cassandra-0.8/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/CHANGES.txt?rev=1130197&r1=1130196&r2=1130197&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/CHANGES.txt (original)
+++ cassandra/branches/cassandra-0.8/CHANGES.txt Wed Jun  1 15:52:56 2011
@@ -31,6 +31,7 @@
  * Added statusthrift to nodetool to report if thrift server is running (CASSANDRA-2722)
  * Fixed rows being cached if they do not exist (CASSANDRA-2723)
  * fix truncate/compaction race (CASSANDRA-2673)
+ * improve CQL JDBC spec compliance (CASSANDRA-2720)
 
 
 0.8.0-final

Modified: cassandra/branches/cassandra-0.8/drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java?rev=1130197&r1=1130196&r2=1130197&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java (original)
+++ cassandra/branches/cassandra-0.8/drivers/java/src/org/apache/cassandra/cql/jdbc/CassandraConnection.java Wed Jun  1 15:52:56 2011
@@ -28,8 +28,10 @@ import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.NClob;
 import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLClientInfoException;
 import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
 import java.sql.SQLWarning;
 import java.sql.SQLXML;
 import java.sql.Savepoint;
@@ -52,10 +54,13 @@ import org.apache.thrift.transport.TTran
  */
 class CassandraConnection implements Connection
 {
-    
-    /** The cassandra con. */
+    private Properties clientInfo = new Properties();
+
+    /**
+     * The cassandra con.
+     */
     private org.apache.cassandra.cql.jdbc.Connection cassandraCon;
-    
+
     /**
      * Instantiates a new cassandra connection.
      *
@@ -77,8 +82,7 @@ class CassandraConnection implements Con
             final int host_backwardIdx = host_port.indexOf('/');
             final String port = host_port.substring(host_colonIdx + 1, host_backwardIdx);
             final String keyspace = host_port.substring(host_backwardIdx + 1);
-            cassandraCon = new org.apache.cassandra.cql.jdbc.Connection(hostName, Integer.valueOf(port), userName,
-                                                                                                                             password);
+            cassandraCon = new org.apache.cassandra.cql.jdbc.Connection(hostName, Integer.valueOf(port), userName, password);
             final String useQ = "USE " + keyspace;
             cassandraCon.execute(useQ);
         }
@@ -120,7 +124,7 @@ class CassandraConnection implements Con
         }
 
     }
-    
+
     /**
      * @param arg0
      * @return
@@ -128,10 +132,10 @@ class CassandraConnection implements Con
      */
     public boolean isWrapperFor(Class<?> arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLException("no object is found that implements this interface");
     }
 
-    
+
     /**
      * @param <T>
      * @param arg0
@@ -140,17 +144,19 @@ class CassandraConnection implements Con
      */
     public <T> T unwrap(Class<T> arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLException("no object is found that implements this interface");
     }
 
 
-
     /**
      * @throws SQLException
      */
     public void clearWarnings() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        // This implementation does not support the collection of warnings so clearing is a no-op
+        // but it is still an exception to call this on a closed connection.
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
     }
 
     /**
@@ -172,7 +178,7 @@ class CassandraConnection implements Con
      */
     public void commit() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLException("The Cassandra Implementation is always in auto-commit mode.");
     }
 
 
@@ -184,7 +190,7 @@ class CassandraConnection implements Con
      */
     public Array createArrayOf(String arg0, Object[] arg1) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -194,7 +200,7 @@ class CassandraConnection implements Con
      */
     public Blob createBlob() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -204,7 +210,7 @@ class CassandraConnection implements Con
      */
     public Clob createClob() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -214,7 +220,7 @@ class CassandraConnection implements Con
      */
     public NClob createNClob() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -224,10 +230,10 @@ class CassandraConnection implements Con
      */
     public SQLXML createSQLXML() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
-    
+
     /**
      * @return
      * @throws SQLException
@@ -246,7 +252,7 @@ class CassandraConnection implements Con
      */
     public Statement createStatement(int arg0, int arg1) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -259,7 +265,7 @@ class CassandraConnection implements Con
      */
     public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -271,7 +277,7 @@ class CassandraConnection implements Con
      */
     public Struct createStruct(String arg0, Object[] arg1) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -281,7 +287,7 @@ class CassandraConnection implements Con
      */
     public boolean getAutoCommit() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        return true;
     }
 
 
@@ -291,7 +297,12 @@ class CassandraConnection implements Con
      */
     public String getCatalog() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        // This implementation does not support the catalog names so null is always returned if the connection is open.
+        // but it is still an exception to call this on a closed connection.
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        return null;
     }
 
 
@@ -301,18 +312,24 @@ class CassandraConnection implements Con
      */
     public Properties getClientInfo() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        return clientInfo;
     }
 
 
     /**
-     * @param arg0
+     * @param label
      * @return
      * @throws SQLException
      */
-    public String getClientInfo(String arg0) throws SQLException
+    public String getClientInfo(String label) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        return clientInfo.getProperty(label);
     }
 
 
@@ -322,7 +339,11 @@ class CassandraConnection implements Con
      */
     public int getHoldability() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there are really no commits in Cassandra so no boundary...
+        return ResultSet.HOLD_CURSORS_OVER_COMMIT;
     }
 
 
@@ -332,7 +353,11 @@ class CassandraConnection implements Con
      */
     public DatabaseMetaData getMetaData() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there is no DatabaseMetaData to return but if there was we would return it...
+        return null;
     }
 
 
@@ -342,7 +367,10 @@ class CassandraConnection implements Con
      */
     public int getTransactionIsolation() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        return Connection.TRANSACTION_NONE;
     }
 
 
@@ -352,7 +380,7 @@ class CassandraConnection implements Con
      */
     public Map<String, Class<?>> getTypeMap() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -362,7 +390,11 @@ class CassandraConnection implements Con
      */
     public SQLWarning getWarnings() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there are no warnings to return in this implementation...
+        return null;
     }
 
 
@@ -372,7 +404,10 @@ class CassandraConnection implements Con
      */
     public boolean isClosed() throws SQLException
     {
-        return false;
+        if (cassandraCon == null)
+            return true;
+
+        return !cassandraCon.isOpen();
     }
 
 
@@ -387,24 +422,34 @@ class CassandraConnection implements Con
 
 
     /**
-     * @param arg0
+     * @param timeout
      * @return
      * @throws SQLException
      */
-    public boolean isValid(int arg0) throws SQLException
+    public boolean isValid(int timeout) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+
+        if (timeout < 0)
+            throw new SQLException("the timeout value was less than zero");
+
+        // this needs to be more robust. Some query needs to be made to verify connection is really up.
+        return !isClosed();
     }
 
 
     /**
-     * @param arg0
+     * @param sql
      * @return
      * @throws SQLException
      */
-    public String nativeSQL(String arg0) throws SQLException
+    public String nativeSQL(String sql) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there are no distinction between grammars in this implementation...
+        // so we are just return the input argument
+        return sql;
     }
 
 
@@ -415,7 +460,7 @@ class CassandraConnection implements Con
      */
     public CallableStatement prepareCall(String arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -428,7 +473,7 @@ class CassandraConnection implements Con
      */
     public CallableStatement prepareCall(String arg0, int arg1, int arg2) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -442,7 +487,7 @@ class CassandraConnection implements Con
      */
     public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -465,7 +510,7 @@ class CassandraConnection implements Con
      */
     public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException
     {
-        return null;
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -477,7 +522,7 @@ class CassandraConnection implements Con
      */
     public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -489,7 +534,7 @@ class CassandraConnection implements Con
      */
     public PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -502,7 +547,7 @@ class CassandraConnection implements Con
      */
     public PreparedStatement prepareStatement(String arg0, int arg1, int arg2) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -516,7 +561,7 @@ class CassandraConnection implements Con
      */
     public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
@@ -526,18 +571,16 @@ class CassandraConnection implements Con
      */
     public void releaseSavepoint(Savepoint arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
-
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
     /**
      * @throws SQLException
-     */ 
+     */
     public void rollback() throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
-
+        throw new SQLException("the Cassandra Implementation is always in auto-commit mode");
     }
 
 
@@ -547,19 +590,18 @@ class CassandraConnection implements Con
      */
     public void rollback(Savepoint arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
-
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 
     /**
-     * @param arg0
+     * @param autoCommit
      * @throws SQLException
      */
-    public void setAutoCommit(boolean arg0) throws SQLException
+    public void setAutoCommit(boolean autoCommit) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
-
+        if (!autoCommit)
+            throw new SQLException("the Cassandra Implementation is always in auto-commit mode");
     }
 
 
@@ -569,29 +611,35 @@ class CassandraConnection implements Con
      */
     public void setCatalog(String arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there are no catalog name to set in this implementation...
+        // so we are "silently ignoring" the request
     }
 
 
     /**
-     * @param arg0
+     * @param props
      * @throws SQLClientInfoException
      */
-    public void setClientInfo(Properties arg0) throws SQLClientInfoException
+    public void setClientInfo(Properties props) throws SQLClientInfoException
     {
-        throw new UnsupportedOperationException("method not supported");
+        // this needs to be revisited when and if we actually start to use the clientInfo properties
+        if (props != null)
+            clientInfo = props;
     }
 
 
     /**
-     * @param arg0
-     * @param arg1
+     * @param key
+     * @param value
      * @throws SQLClientInfoException
      */
-    public void setClientInfo(String arg0, String arg1) throws SQLClientInfoException
+    public void setClientInfo(String key, String value) throws SQLClientInfoException
     {
-        throw new UnsupportedOperationException("method not supported");
-
+        // this needs to be revisited when and if we actually start to use the clientInfo properties
+        clientInfo.setProperty(key, value);
     }
 
 
@@ -601,9 +649,13 @@ class CassandraConnection implements Con
      */
     public void setHoldability(int arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is there are no holdability to set in this implementation...
+        // so we are "silently ignoring" the request
     }
-    
+
 
     /**
      * @param arg0
@@ -611,9 +663,13 @@ class CassandraConnection implements Con
      */
     public void setReadOnly(boolean arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        // the rationale is all connections are read/write in the Cassandra implementation...
+        // so we are "silently ignoring" the request
     }
-    
+
 
     /**
      * @return
@@ -623,7 +679,7 @@ class CassandraConnection implements Con
     {
         throw new UnsupportedOperationException("method not supported");
     }
-    
+
 
     /**
      * @param arg0
@@ -632,19 +688,23 @@ class CassandraConnection implements Con
      */
     public Savepoint setSavepoint(String arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
-    
+
 
     /**
-     * @param arg0
+     * @param level
      * @throws SQLException
      */
-    public void setTransactionIsolation(int arg0) throws SQLException
+    public void setTransactionIsolation(int level) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        if (isClosed())
+            throw new SQLException("this method was called on a closed Connection");
+
+        if (level != Connection.TRANSACTION_NONE)
+            throw new SQLException("the Cassandra Inplementation does not support transactions");
     }
-    
+
 
     /**
      * @param arg0
@@ -652,7 +712,7 @@ class CassandraConnection implements Con
      */
     public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException
     {
-        throw new UnsupportedOperationException("method not supported");
+        throw new SQLFeatureNotSupportedException("the Cassandra Implementation does not support this method");
     }
 
 }