Browse Source

The binding callback can now return an error, which will in turn bubble up to the app

Ben Hood 11 years ago
parent
commit
f71c8bb773
3 changed files with 20 additions and 14 deletions
  1. 8 8
      cassandra_test.go
  2. 8 2
      conn.go
  3. 4 4
      session.go

+ 8 - 8
cassandra_test.go

@@ -564,10 +564,10 @@ func TestStaticQueryInfo(t *testing.T) {
 		t.Fatalf("insert into static_query_info failed, err '%v'", err)
 		t.Fatalf("insert into static_query_info failed, err '%v'", err)
 	}
 	}
 
 
-	autobinder := func(q *QueryInfo) []interface{} {
+	autobinder := func(q *QueryInfo) ([]interface{}, error) {
 		values := make([]interface{}, 1)
 		values := make([]interface{}, 1)
 		values[0] = 113
 		values[0] = 113
-		return values
+		return values, nil
 	}
 	}
 
 
 	qry := session.Bind("SELECT id, value FROM static_query_info WHERE id = ?", autobinder)
 	qry := session.Bind("SELECT id, value FROM static_query_info WHERE id = ?", autobinder)
@@ -599,7 +599,7 @@ type ClusteredKeyValue struct {
 	Value   string
 	Value   string
 }
 }
 
 
-func (kv *ClusteredKeyValue) Bind(q *QueryInfo) []interface{} {
+func (kv *ClusteredKeyValue) Bind(q *QueryInfo) ([]interface{}, error) {
 	values := make([]interface{}, len(q.args))
 	values := make([]interface{}, len(q.args))
 
 
 	for i, info := range q.args {
 	for i, info := range q.args {
@@ -609,7 +609,7 @@ func (kv *ClusteredKeyValue) Bind(q *QueryInfo) []interface{} {
 		values[i] = field.Addr().Interface()
 		values[i] = field.Addr().Interface()
 	}
 	}
 
 
-	return values
+	return values, nil
 }
 }
 
 
 func upcaseInitial(str string) string {
 func upcaseInitial(str string) string {
@@ -670,12 +670,12 @@ func TestBatchQueryInfo(t *testing.T) {
 		t.Fatalf("failed to create table with error '%v'", err)
 		t.Fatalf("failed to create table with error '%v'", err)
 	}
 	}
 
 
-	write := func(q *QueryInfo) []interface{} {
+	write := func(q *QueryInfo) ([]interface{}, error) {
 		values := make([]interface{}, 3)
 		values := make([]interface{}, 3)
 		values[0] = 4000
 		values[0] = 4000
 		values[1] = 5000
 		values[1] = 5000
 		values[2] = "bar"
 		values[2] = "bar"
-		return values
+		return values, nil
 	}
 	}
 
 
 	batch := session.NewBatch(LoggedBatch)
 	batch := session.NewBatch(LoggedBatch)
@@ -685,11 +685,11 @@ func TestBatchQueryInfo(t *testing.T) {
 		t.Fatalf("batch insert into batch_query_info failed, err '%v'", err)
 		t.Fatalf("batch insert into batch_query_info failed, err '%v'", err)
 	}
 	}
 
 
-	read := func(q *QueryInfo) []interface{} {
+	read := func(q *QueryInfo) ([]interface{}, error) {
 		values := make([]interface{}, 2)
 		values := make([]interface{}, 2)
 		values[0] = 4000
 		values[0] = 4000
 		values[1] = 5000
 		values[1] = 5000
-		return values
+		return values, nil
 	}
 	}
 
 
 	qry := session.Bind("SELECT id, cluster, value FROM batch_query_info WHERE id = ? and cluster = ?", read)
 	qry := session.Bind("SELECT id, cluster, value FROM batch_query_info WHERE id = ? and cluster = ?", read)

+ 8 - 2
conn.go

@@ -369,7 +369,10 @@ func (c *Conn) executeQuery(qry *Query) *Iter {
 		if qry.binding == nil {
 		if qry.binding == nil {
 			values = qry.values
 			values = qry.values
 		} else {
 		} else {
-			values = qry.binding(info)
+			values, err = qry.binding(info)
+			if err != nil {
+				return &Iter{err: err}
+			}
 		}
 		}
 
 
 		if len(values) != len(info.args) {
 		if len(values) != len(info.args) {
@@ -490,7 +493,10 @@ func (c *Conn) executeBatch(batch *Batch) error {
 			if entry.binding == nil {
 			if entry.binding == nil {
 				args = entry.Args
 				args = entry.Args
 			} else {
 			} else {
-				args = entry.binding(info)
+				args, err = entry.binding(info)
+				if err != nil {
+					return err
+				}
 			}
 			}
 
 
 			if len(args) != len(info.args) {
 			if len(args) != len(info.args) {

+ 4 - 4
session.go

@@ -90,7 +90,7 @@ func (s *Session) Query(stmt string, values ...interface{}) *Query {
 	return qry
 	return qry
 }
 }
 
 
-func (s *Session) Bind(stmt string, b func(q *QueryInfo) []interface{}) *Query {
+func (s *Session) Bind(stmt string, b func(q *QueryInfo) ([]interface{}, error)) *Query {
 	s.mu.RLock()
 	s.mu.RLock()
 	qry := &Query{stmt: stmt, binding: b, cons: s.cons,
 	qry := &Query{stmt: stmt, binding: b, cons: s.cons,
 		session: s, pageSize: s.pageSize, trace: s.trace,
 		session: s, pageSize: s.pageSize, trace: s.trace,
@@ -193,7 +193,7 @@ type Query struct {
 	trace     Tracer
 	trace     Tracer
 	session   *Session
 	session   *Session
 	rt        RetryPolicy
 	rt        RetryPolicy
-	binding   func(q *QueryInfo) []interface{}
+	binding   func(q *QueryInfo) ([]interface{}, error)
 }
 }
 
 
 // Consistency sets the consistency level for this query. If no consistency
 // Consistency sets the consistency level for this query. If no consistency
@@ -405,7 +405,7 @@ func (b *Batch) Query(stmt string, args ...interface{}) {
 	b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
 	b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
 }
 }
 
 
-func (b *Batch) Bind(stmt string, bind func(q *QueryInfo) []interface{}) {
+func (b *Batch) Bind(stmt string, bind func(q *QueryInfo) ([]interface{}, error)) {
 	b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, binding: bind})
 	b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, binding: bind})
 }
 }
 
 
@@ -431,7 +431,7 @@ const (
 type BatchEntry struct {
 type BatchEntry struct {
 	Stmt    string
 	Stmt    string
 	Args    []interface{}
 	Args    []interface{}
-	binding func(q *QueryInfo) []interface{}
+	binding func(q *QueryInfo) ([]interface{}, error)
 }
 }
 
 
 type Consistency int
 type Consistency int