Sfoglia il codice sorgente

Merge pull request #95 from skoikovs/master

Allow variable number of arguments for iter.Scan()
Ben Hood 11 anni fa
parent
commit
3fb03da796
3 ha cambiato i file con 87 aggiunte e 3 eliminazioni
  1. 1 0
      AUTHORS
  2. 80 0
      cassandra_test.go
  3. 6 3
      session.go

+ 1 - 0
AUTHORS

@@ -18,3 +18,4 @@ Alex Zorin <git@zor.io>
 Kasper Middelboe Petersen <me@phant.dk>
 Harpreet Sawhney <harpreet.sawhney@gmail.com>
 Charlie Andrews <charlieandrews.cwa@gmail.com>
+Stanislavs Koikovs <stanislavs.koikovs@gmail.com>

+ 80 - 0
cassandra_test.go

@@ -413,3 +413,83 @@ func TestSliceMap(t *testing.T) {
 	}
 
 }
+
+func TestScanWithNilArguments(t *testing.T) {
+	session := createSession(t)
+	defer session.Close()
+
+	if err := session.Query(`CREATE TABLE scan_with_nil_arguments (
+			foo   varchar,
+			bar   int,
+			PRIMARY KEY (foo, bar)
+	)`).Exec(); err != nil {
+		t.Fatal("create:", err)
+	}
+	for i := 1; i <= 20; i++ {
+		if err := session.Query("INSERT INTO scan_with_nil_arguments (foo, bar) VALUES (?, ?)",
+			"squares", i*i).Exec(); err != nil {
+			t.Fatal("insert:", err)
+		}
+	}
+
+	iter := session.Query("SELECT * FROM scan_with_nil_arguments WHERE foo = ?", "squares").Iter()
+	var n int
+	count := 0
+	for iter.Scan(nil, &n) {
+		count += n
+	}
+	if err := iter.Close(); err != nil {
+		t.Fatal("close:", err)
+	}
+	if count != 2870 {
+		t.Fatalf("expected %d, got %d", 2870, count)
+	}
+}
+
+func TestScanCASWithNilArguments(t *testing.T) {
+	if *flagProto == 1 {
+		t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
+	}
+
+	session := createSession(t)
+	defer session.Close()
+
+	if err := session.Query(`CREATE TABLE scan_cas_with_nil_arguments (
+		foo   varchar,
+		bar   varchar,
+		PRIMARY KEY (foo, bar)
+	)`).Exec(); err != nil {
+		t.Fatal("create:", err)
+	}
+
+	foo := "baz"
+	var cas string
+
+	if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
+		VALUES (?, ?) IF NOT EXISTS`,
+		foo, foo).ScanCAS(nil, nil); err != nil {
+		t.Fatal("insert:", err)
+	} else if !applied {
+		t.Fatal("insert should have been applied")
+	}
+
+	if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
+		VALUES (?, ?) IF NOT EXISTS`,
+		foo, foo).ScanCAS(&cas, nil); err != nil {
+		t.Fatal("insert:", err)
+	} else if applied {
+		t.Fatal("insert should not have been applied")
+	} else if foo != cas {
+		t.Fatalf("expected %v but got %v", foo, cas)
+	}
+
+	if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
+		VALUES (?, ?) IF NOT EXISTS`,
+		foo, foo).ScanCAS(nil, &cas); err != nil {
+		t.Fatal("insert:", err)
+	} else if applied {
+		t.Fatal("insert should not have been applied")
+	} else if foo != cas {
+		t.Fatalf("expected %v but got %v", foo, cas)
+	}
+}

+ 6 - 3
session.go

@@ -253,9 +253,9 @@ func (iter *Iter) Columns() []ColumnInfo {
 }
 
 // Scan consumes the next row of the iterator and copies the columns of the
-// current row into the values pointed at by dest. Scan might send additional
-// queries to the database to retrieve the next set of rows if paging was
-// enabled.
+// current row into the values pointed at by dest. Use nil as a dest value
+// to skip the corresponding column. Scan might send additional queries
+// to the database to retrieve the next set of rows if paging was enabled.
 //
 // Scan returns true if the row was successfully unmarshaled or false if the
 // end of the result set was reached or if an error occurred. Close should
@@ -279,6 +279,9 @@ func (iter *Iter) Scan(dest ...interface{}) bool {
 		return false
 	}
 	for i := 0; i < len(iter.columns); i++ {
+		if dest[i] == nil {
+			continue
+		}
 		err := Unmarshal(iter.columns[i].TypeInfo, iter.rows[iter.pos][i], dest[i])
 		if err != nil {
 			iter.err = err