Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ func (c *conn) setReadOnlyStaleness(staleness spanner.TimestampBound) (driver.Re
return driver.ResultNoRows, nil
}

func (c *conn) readOnlyStalenessPointer() *spanner.TimestampBound {
val := propertyReadOnlyStaleness.GetConnectionPropertyValue(c.state)
if val == nil || !val.HasValue() {
return nil
}
staleness, _ := val.GetValue()
timestampBound := staleness.(spanner.TimestampBound)
return &timestampBound
}

func (c *conn) IsolationLevel() sql.IsolationLevel {
return propertyIsolationLevel.GetValueOrDefault(c.state)
}
Expand Down Expand Up @@ -987,6 +997,7 @@ func (c *conn) options(reset bool) *ExecOptions {
},
},
PartitionedQueryOptions: PartitionedQueryOptions{},
TimestampBound: c.readOnlyStalenessPointer(),
}
if c.tempExecOptions != nil {
effectiveOptions.merge(c.tempExecOptions)
Expand Down Expand Up @@ -1274,6 +1285,9 @@ func (c *conn) rollback(ctx context.Context) error {
}

func queryInSingleUse(ctx context.Context, c *spanner.Client, statement spanner.Statement, tb spanner.TimestampBound, options *ExecOptions) *spanner.RowIterator {
if options.TimestampBound != nil {
tb = *options.TimestampBound
}
return c.Single().WithTimestampBound(tb).QueryWithOptions(ctx, statement, options.QueryOptions)
}

Expand Down
29 changes: 29 additions & 0 deletions conn_with_mockserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"reflect"
"testing"
"time"

"cloud.google.com/go/longrunning/autogen/longrunningpb"
"cloud.google.com/go/spanner"
Expand Down Expand Up @@ -587,6 +588,34 @@ func TestSetLocalReadLockMode(t *testing.T) {
}
}

func TestTimestampBound(t *testing.T) {
t.Parallel()

db, server, teardown := setupTestDBConnection(t)
defer teardown()
ctx := context.Background()

staleness := spanner.MaxStaleness(10 * time.Second)
row := db.QueryRowContext(ctx, testutil.SelectFooFromBar, ExecOptions{TimestampBound: &staleness})
if row.Err() != nil {
t.Fatal(row.Err())
}
var val int64
if err := row.Scan(&val); err != nil {
t.Fatal(err)
}

requests := server.TestSpanner.DrainRequestsFromServer()
executeRequests := testutil.RequestsOfType(requests, reflect.TypeOf(&spannerpb.ExecuteSqlRequest{}))
if g, w := len(executeRequests), 1; g != w {
t.Fatalf("execute requests count mismatch\n Got: %v\nWant: %v", g, w)
}
request := executeRequests[0].(*spannerpb.ExecuteSqlRequest)
if g, w := request.Transaction.GetSingleUse().GetReadOnly().GetMaxStaleness().GetSeconds(), int64(10); g != w {
t.Fatalf("read staleness mismatch\n Got: %v\nWant: %v", g, w)
}
}

func TestCreateDatabase(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ type ExecOptions struct {
TransactionOptions spanner.TransactionOptions
// QueryOptions are the query options that will be used for the statement.
QueryOptions spanner.QueryOptions
// TimestampBound is the timestamp bound that will be used for the statement
// if it is a query outside a transaction. Setting this option will override
// the default TimestampBound that is set on the connection.
TimestampBound *spanner.TimestampBound

// PartitionedQueryOptions are used for partitioned queries, and ignored
// for all other statements.
Expand Down Expand Up @@ -234,6 +238,9 @@ func (dest *ExecOptions) merge(src *ExecOptions) {
if src.AutocommitDMLMode != Unspecified {
dest.AutocommitDMLMode = src.AutocommitDMLMode
}
if src.TimestampBound != nil {
dest.TimestampBound = src.TimestampBound
}
(&dest.PartitionedQueryOptions).merge(&src.PartitionedQueryOptions)
mergeQueryOptions(&dest.QueryOptions, &src.QueryOptions)
mergeTransactionOptions(&dest.TransactionOptions, &src.TransactionOptions)
Expand Down
Loading