Prechádzať zdrojové kódy

Merge pull request #210 from relops/rebind

Query rebinding
Phillip Couto 11 rokov pred
rodič
commit
3f302ce5c5
2 zmenil súbory, kde vykonal 46 pridanie a 0 odobranie
  1. 39 0
      cassandra_test.go
  2. 7 0
      session.go

+ 39 - 0
cassandra_test.go

@@ -552,6 +552,45 @@ func TestScanCASWithNilArguments(t *testing.T) {
 	}
 }
 
+func TestRebindQueryInfo(t *testing.T) {
+	session := createSession(t)
+	defer session.Close()
+
+	if err := session.Query("CREATE TABLE rebind_query (id int, value text, PRIMARY KEY (id))").Exec(); err != nil {
+		t.Fatalf("failed to create table with error '%v'", err)
+	}
+
+	if err := session.Query("INSERT INTO rebind_query (id, value) VALUES (?, ?)", 23, "quux").Exec(); err != nil {
+		t.Fatalf("insert into rebind_query failed, err '%v'", err)
+	}
+
+	if err := session.Query("INSERT INTO rebind_query (id, value) VALUES (?, ?)", 24, "w00t").Exec(); err != nil {
+		t.Fatalf("insert into rebind_query failed, err '%v'", err)
+	}
+
+	q := session.Query("SELECT value FROM rebind_query WHERE ID = ?")
+	q.Bind(23)
+
+	iter := q.Iter()
+	var value string
+	for iter.Scan(&value) {
+	}
+
+	if value != "quux" {
+		t.Fatalf("expected %v but got %v", "quux", value)
+	}
+
+	q.Bind(24)
+	iter = q.Iter()
+
+	for iter.Scan(&value) {
+	}
+
+	if value != "w00t" {
+		t.Fatalf("expected %v but got %v", "quux", value)
+	}
+}
+
 //TestStaticQueryInfo makes sure that the application can manually bind query parameters using the simplest possible static binding strategy
 func TestStaticQueryInfo(t *testing.T) {
 	session := createSession(t)

+ 7 - 0
session.go

@@ -262,6 +262,13 @@ func (q *Query) RetryPolicy(r RetryPolicy) *Query {
 	return q
 }
 
+// Bind sets query arguments of query. This can also be used to rebind new query arguments
+// to an existing query instance.
+func (q *Query) Bind(v ...interface{}) *Query {
+	q.values = v
+	return q
+}
+
 // Exec executes the query without returning any rows.
 func (q *Query) Exec() error {
 	iter := q.Iter()