Kaynağa Gözat

(cleanup) move shouldPrepare func to being a Query type method

Harpreet Sawhney 11 yıl önce
ebeveyn
işleme
6ed2c7d73f
3 değiştirilmiş dosya ile 26 ekleme ve 24 silme
  1. 2 1
      cass1batch_test.go
  2. 1 23
      conn.go
  3. 23 0
      session.go

+ 2 - 1
cass1batch_test.go

@@ -49,7 +49,8 @@ func TestShouldPrepareFunction(t *testing.T) {
 	}
 
 	for _, test := range shouldPrepareTests {
-		if got := shouldPrepare(test.Stmt); got != test.Result {
+		q := &Query{stmt: test.Stmt}
+		if got := q.ShouldPrepare(); got != test.Result {
 			t.Fatalf("%q: got %v, expected %v\n", test.Stmt, got, test.Result)
 		}
 	}

+ 1 - 23
conn.go

@@ -8,11 +8,9 @@ import (
 	"bufio"
 	"fmt"
 	"net"
-	"strings"
 	"sync"
 	"sync/atomic"
 	"time"
-	"unicode"
 )
 
 const defaultFrameSize = 4096
@@ -359,26 +357,6 @@ func (c *Conn) prepareStatement(stmt string, trace Tracer) (*queryInfo, error) {
 	return flight.info, flight.err
 }
 
-func shouldPrepare(stmt string) bool {
-	stmt = strings.TrimLeftFunc(strings.TrimRightFunc(stmt, func(r rune) bool {
-		return unicode.IsSpace(r) || r == ';'
-	}), unicode.IsSpace)
-
-	var stmtType string
-	if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
-		stmtType = strings.ToLower(stmt[:n])
-	}
-	if stmtType == "begin" {
-		if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
-			stmtType = strings.ToLower(stmt[n+1:])
-		}
-	}
-	switch stmtType {
-	case "select", "insert", "update", "delete", "batch":
-		return true
-	}
-	return false
-}
 
 func (c *Conn) executeQuery(qry *Query) *Iter {
 	op := &queryFrame{
@@ -387,7 +365,7 @@ func (c *Conn) executeQuery(qry *Query) *Iter {
 		PageSize:  qry.pageSize,
 		PageState: qry.pageState,
 	}
-	if shouldPrepare(op.Stmt) {
+	if qry.ShouldPrepare() {
 		// Prepare all DML queries. Other queries can not be prepared.
 		info, err := c.prepareStatement(qry.stmt, qry.trace)
 		if err != nil {

+ 23 - 0
session.go

@@ -11,6 +11,7 @@ import (
 	"strings"
 	"sync"
 	"time"
+	"unicode"
 )
 
 // Session is the interface used by users to interact with the database.
@@ -175,6 +176,28 @@ func (q *Query) PageSize(n int) *Query {
 	return q
 }
 
+func (q *Query) ShouldPrepare() bool {
+
+	stmt := strings.TrimLeftFunc(strings.TrimRightFunc(q.stmt, func(r rune) bool {
+		return unicode.IsSpace(r) || r == ';'
+	}), unicode.IsSpace)
+
+	var stmtType string
+	if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
+		stmtType = strings.ToLower(stmt[:n])
+	}
+	if stmtType == "begin" {
+		if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
+			stmtType = strings.ToLower(stmt[n+1:])
+		}
+	}
+	switch stmtType {
+	case "select", "insert", "update", "delete", "batch":
+		return true
+	}
+	return false
+}
+
 // SetPrefetch sets the default threshold for pre-fetching new pages. If
 // there are only p*pageSize rows remaining, the next page will be requested
 // automatically.