瀏覽代碼

Merge pull request #853 from jesse-c/check-keyspacemetadata-query-result

Change KeypsaceMetadata() to return an error if the keyspace does not exist
Chris Bannister 8 年之前
父節點
當前提交
4d2d1ac719
共有 4 個文件被更改,包括 31 次插入11 次删除
  1. 1 0
      AUTHORS
  2. 13 1
      cassandra_test.go
  3. 6 0
      metadata.go
  4. 11 10
      session.go

+ 1 - 0
AUTHORS

@@ -84,3 +84,4 @@ Charles Law <charles.law@gmail.com>; <claw@conduce.com>
 Nathan Davies <nathanjamesdavies@gmail.com>
 Nathan Davies <nathanjamesdavies@gmail.com>
 Bo Blanton <bo.blanton@gmail.com>
 Bo Blanton <bo.blanton@gmail.com>
 Vincent Rischmann <me@vrischmann.me>
 Vincent Rischmann <me@vrischmann.me>
+Jesse Claven <jesse.claven@gmail.com>

+ 13 - 1
cassandra_test.go

@@ -1598,7 +1598,7 @@ func TestEmptyTimestamp(t *testing.T) {
 	}
 	}
 }
 }
 
 
-// Integration test of just querying for data from the system.schema_keyspace table
+// Integration test of just querying for data from the system.schema_keyspace table where the keyspace DOES exist.
 func TestGetKeyspaceMetadata(t *testing.T) {
 func TestGetKeyspaceMetadata(t *testing.T) {
 	session := createSession(t)
 	session := createSession(t)
 	defer session.Close()
 	defer session.Close()
@@ -1632,6 +1632,18 @@ func TestGetKeyspaceMetadata(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// Integration test of just querying for data from the system.schema_keyspace table where the keyspace DOES NOT exist.
+func TestGetKeyspaceMetadataFails(t *testing.T) {
+	session := createSession(t)
+	defer session.Close()
+
+	_, err := getKeyspaceMetadata(session, "gocql_keyspace_does_not_exist")
+
+	if err != ErrKeyspaceDoesNotExist || err == nil {
+		t.Fatalf("Expected error of type ErrKeySpaceDoesNotExist. Instead, error was %v", err)
+	}
+}
+
 // Integration test of just querying for data from the system.schema_columnfamilies table
 // Integration test of just querying for data from the system.schema_columnfamilies table
 func TestGetTableMetadata(t *testing.T) {
 func TestGetTableMetadata(t *testing.T) {
 	session := createSession(t)
 	session := createSession(t)

+ 6 - 0
metadata.go

@@ -416,6 +416,9 @@ func getKeyspaceMetadata(session *Session, keyspaceName string) (*KeyspaceMetada
 		var replication map[string]string
 		var replication map[string]string
 
 
 		iter := session.control.query(stmt, keyspaceName)
 		iter := session.control.query(stmt, keyspaceName)
+		if iter.NumRows() == 0 {
+			return nil, ErrKeyspaceDoesNotExist
+		}
 		iter.Scan(&keyspace.DurableWrites, &replication)
 		iter.Scan(&keyspace.DurableWrites, &replication)
 		err := iter.Close()
 		err := iter.Close()
 		if err != nil {
 		if err != nil {
@@ -438,6 +441,9 @@ func getKeyspaceMetadata(session *Session, keyspaceName string) (*KeyspaceMetada
 		var strategyOptionsJSON []byte
 		var strategyOptionsJSON []byte
 
 
 		iter := session.control.query(stmt, keyspaceName)
 		iter := session.control.query(stmt, keyspaceName)
+		if iter.NumRows() == 0 {
+			return nil, ErrKeyspaceDoesNotExist
+		}
 		iter.Scan(&keyspace.DurableWrites, &keyspace.StrategyClass, &strategyOptionsJSON)
 		iter.Scan(&keyspace.DurableWrites, &keyspace.StrategyClass, &strategyOptionsJSON)
 		err := iter.Close()
 		err := iter.Close()
 		if err != nil {
 		if err != nil {

+ 11 - 10
session.go

@@ -389,7 +389,7 @@ func (s *Session) executeQuery(qry *Query) *Iter {
 	return iter
 	return iter
 }
 }
 
 
-// KeyspaceMetadata returns the schema metadata for the keyspace specified.
+// KeyspaceMetadata returns the schema metadata for the keyspace specified. Returns an error if the keyspace does not exist.
 func (s *Session) KeyspaceMetadata(keyspace string) (*KeyspaceMetadata, error) {
 func (s *Session) KeyspaceMetadata(keyspace string) (*KeyspaceMetadata, error) {
 	// fail fast
 	// fail fast
 	if s.Closed() {
 	if s.Closed() {
@@ -1570,15 +1570,16 @@ func (e Error) Error() string {
 }
 }
 
 
 var (
 var (
-	ErrNotFound      = errors.New("not found")
-	ErrUnavailable   = errors.New("unavailable")
-	ErrUnsupported   = errors.New("feature not supported")
-	ErrTooManyStmts  = errors.New("too many statements")
-	ErrUseStmt       = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explaination.")
-	ErrSessionClosed = errors.New("session has been closed")
-	ErrNoConnections = errors.New("gocql: no hosts available in the pool")
-	ErrNoKeyspace    = errors.New("no keyspace provided")
-	ErrNoMetadata    = errors.New("no metadata available")
+	ErrNotFound             = errors.New("not found")
+	ErrUnavailable          = errors.New("unavailable")
+	ErrUnsupported          = errors.New("feature not supported")
+	ErrTooManyStmts         = errors.New("too many statements")
+	ErrUseStmt              = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explanation.")
+	ErrSessionClosed        = errors.New("session has been closed")
+	ErrNoConnections        = errors.New("gocql: no hosts available in the pool")
+	ErrNoKeyspace           = errors.New("no keyspace provided")
+	ErrKeyspaceDoesNotExist = errors.New("keyspace does not exist")
+	ErrNoMetadata           = errors.New("no metadata available")
 )
 )
 
 
 type ErrProtocol struct{ error }
 type ErrProtocol struct{ error }