diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 00000000..9842916a --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,18 @@ +on: [push, pull_request] +name: Unit Tests +jobs: + test: + strategy: + matrix: + go-version: [1.15.x, 1.16.x] + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v2 + - name: Run unit tests + run: go test -short diff --git a/.gitignore b/.gitignore index c67ebe51..ad2f2b24 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -gorm/ \ No newline at end of file +gorm/ +.idea diff --git a/README.md b/README.md index ece40a70..9e86fd9d 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,9 @@ $ export SPANNER_EMULATOR_HOST=localhost:9010 ## Troubleshooting -This driver shouldn't automatically retry the transactions but it does. -It causes unwanted results. Don't use this library in production yet. +The driver will propagate any Aborted error that is returned by Cloud Spanner +during a read/write transaction, and it will currently not automatically retry +the transaction. --- @@ -82,27 +83,6 @@ gorm cannot use the driver as it-is but @rakyll has been working on a dialect. She doesn't have bandwidth to ship a fully featured dialect right now but contact her if you would like to contribute. ---- - -`error = `: Use a typed nil, instead of just nil. - -The following query returns rows with NULL likes: - -``` go -var nilInt64 *int64 -db.QueryContext(ctx, "SELECT id, text FROM tweets WHERE likes = @likes LIMIT 10", nilInt64) -``` - ---- - -When querying and executing with emails, pass them as arguments and don't hardcode -them in the query: - -``` go -db.QueryContext(ctx, "SELECT id, name ... WHERE email = @email", "jbd@google.com") -``` - -The driver will relax this requirement in the future but it is a work-in-progress for now. --- diff --git a/driver.go b/driver.go index dee51857..19bcb345 100644 --- a/driver.go +++ b/driver.go @@ -19,9 +19,10 @@ import ( "database/sql" "database/sql/driver" "errors" - "os" + "fmt" "regexp" - "time" + "strconv" + "strings" "cloud.google.com/go/spanner" "github.com/rakyll/go-sql-driver-spanner/internal" @@ -34,6 +35,14 @@ import ( const userAgent = "go-sql-driver-spanner/0.1" +// dsnRegExpString describes the valid values for a dsn (connection name) for +// Google Cloud Spanner. The string consists of the following parts: +// 1. (Optional) Host: The host name and port number to connect to. +// 2. Database name: The database name to connect to in the format `projects/my-project/instances/my-instance/databases/my-database` +// 3. (Optional) Parameters: One or more parameters in the format `name=value`. Multiple entries are separated by `;`. +// Example: `localhost:9010/projects/test-project/instances/test-instance/databases/test-database;usePlainText=true` +var dsnRegExp = regexp.MustCompile("((?P[\\w.-]+(?:\\.[\\w\\.-]+)*[\\w\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=.]+)/)?projects/(?P(([a-z]|[-.:]|[0-9])+|(DEFAULT_PROJECT_ID)))(/instances/(?P([a-z]|[-]|[0-9])+)(/databases/(?P([a-z]|[-]|[_]|[0-9])+))?)?(([\\?|;])(?P.*))?") + var _ driver.DriverContext = &Driver{} func init() { @@ -42,13 +51,6 @@ func init() { // Driver represents a Google Cloud Spanner database/sql driver. type Driver struct { - // Config represents the optional advanced configuration to be used - // by the Google Cloud Spanner client. - Config spanner.ClientConfig - - // Options represent the optional Google Cloud client options - // to be passed to the underlying client. - Options []option.ClientOption } // Open opens a connection to a Google Cloud Spanner database. @@ -56,62 +58,123 @@ type Driver struct { // // Example: projects/$PROJECT/instances/$INSTANCE/databases/$DATABASE func (d *Driver) Open(name string) (driver.Conn, error) { - return openDriverConn(context.Background(), d, name) + c, err := newConnector(d, name) + if err != nil { + return nil, err + } + return openDriverConn(context.Background(), c) } func (d *Driver) OpenConnector(name string) (driver.Connector, error) { - return &connector{ - driver: d, - name: name, - }, nil + return newConnector(d, name) } -type connector struct { - driver *Driver - name string +type connectorConfig struct { + host string + project string + instance string + database string + params map[string]string } -func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { - return openDriverConn(ctx, c.driver, c.name) +func extractConnectorConfig(dsn string) (connectorConfig, error) { + match := dsnRegExp.FindStringSubmatch(dsn) + matches := make(map[string]string) + for i, name := range dsnRegExp.SubexpNames() { + if i != 0 && name != "" { + matches[name] = match[i] + } + } + paramsString := matches["PARAMSGROUP"] + params, err := extractConnectorParams(paramsString) + if err != nil { + return connectorConfig{}, err + } + + return connectorConfig{ + host: matches["HOSTGROUP"], + project: matches["PROJECTGROUP"], + instance: matches["INSTANCEGROUP"], + database: matches["DATABASEGROUP"], + params: params, + }, nil } -func openDriverConn(ctx context.Context, d *Driver, name string) (driver.Conn, error) { - if d.Config.NumChannels == 0 { - d.Config.NumChannels = 1 // TODO(jbd): Explain database/sql has a high-level management. +func extractConnectorParams(paramsString string) (map[string]string, error) { + params := make(map[string]string) + if paramsString == "" { + return params, nil } - opts := append(d.Options, option.WithUserAgent(userAgent)) - client, err := spanner.NewClientWithConfig(ctx, name, d.Config, opts...) - if err != nil { - return nil, err + keyValuePairs := strings.Split(paramsString, ";") + for _, keyValueString := range keyValuePairs { + keyValue := strings.SplitN(keyValueString, "=", 2) + if keyValue == nil || len(keyValue) != 2 { + return nil, fmt.Errorf("invalid connection property: %s", keyValueString) + } + params[keyValue[0]] = keyValue[1] } + return params, nil +} - adminClient, err := createAdminClient(ctx) +type connector struct { + driver *Driver + connectorConfig connectorConfig + + // spannerClientConfig represents the optional advanced configuration to be used + // by the Google Cloud Spanner client. + spannerClientConfig spanner.ClientConfig + + // options represent the optional Google Cloud client options + // to be passed to the underlying client. + options []option.ClientOption +} + +func newConnector(d *Driver, dsn string) (*connector, error) { + connectorConfig, err := extractConnectorConfig(dsn) if err != nil { return nil, err } - return &conn{client: client, adminClient: adminClient, name: name}, nil + opts := make([]option.ClientOption, 0) + if connectorConfig.host != "" { + opts = append(opts, option.WithEndpoint(connectorConfig.host)) + } + if strval, ok := connectorConfig.params["useplaintext"]; ok { + if val, err := strconv.ParseBool(strval); err == nil && val { + opts = append(opts, option.WithGRPCDialOption(grpc.WithInsecure()), option.WithoutAuthentication()) + } + } + config := spanner.ClientConfig{ + SessionPoolConfig: spanner.DefaultSessionPoolConfig, + } + return &connector{ + driver: d, + connectorConfig: connectorConfig, + spannerClientConfig: config, + options: opts, + }, nil } -func createAdminClient(ctx context.Context) (adminClient *adminapi.DatabaseAdminClient, err error) { +func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { + return openDriverConn(ctx, c) +} - // Admin client will connect to emulator if SPANNER_EMULATOR_HOST - // is set in the environment. - if spannerHost, ok := os.LookupEnv("SPANNER_EMULATOR_HOST"); ok { - adminClient, err = adminapi.NewDatabaseAdminClient( - ctx, - option.WithoutAuthentication(), - option.WithEndpoint(spannerHost), - option.WithGRPCDialOption(grpc.WithInsecure())) - if err != nil { - adminClient = nil - } - } else { - adminClient, err = adminapi.NewDatabaseAdminClient(ctx) - if err != nil { - adminClient = nil - } +func openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { + opts := append(c.options, option.WithUserAgent(userAgent)) + databaseName := fmt.Sprintf( + "projects/%s/instances/%s/databases/%s", + c.connectorConfig.project, + c.connectorConfig.instance, + c.connectorConfig.database) + client, err := spanner.NewClientWithConfig(ctx, databaseName, c.spannerClientConfig, opts...) + if err != nil { + return nil, err } - return + + adminClient, err := adminapi.NewDatabaseAdminClient(ctx, opts...) + if err != nil { + return nil, err + } + return &conn{client: client, adminClient: adminClient, database: databaseName}, nil } func (c *connector) Driver() driver.Driver { @@ -119,37 +182,93 @@ func (c *connector) Driver() driver.Driver { } type conn struct { + closed bool client *spanner.Client adminClient *adminapi.DatabaseAdminClient - roTx *spanner.ReadOnlyTransaction - rwTx *rwTx - name string + tx contextTransaction + database string +} + +// Ping implements the driver.Pinger interface. +// returns ErrBadConn if the connection is no longer valid. +func (c *conn) Ping(ctx context.Context) error { + if c.closed { + return driver.ErrBadConn + } + rows, err := c.QueryContext(ctx, "SELECT 1", []driver.NamedValue{}) + if err != nil { + return driver.ErrBadConn + } + defer rows.Close() + values := make([]driver.Value, 1) + if err := rows.Next(values); err != nil { + return driver.ErrBadConn + } + if values[0] != int64(1) { + return driver.ErrBadConn + } + return nil +} + +// ResetSession implements the driver.SessionResetter interface. +// returns ErrBadConn if the connection is no longer valid. +func (c *conn) ResetSession(_ context.Context) error { + if c.closed { + return driver.ErrBadConn + } + if c.inTransaction() { + if err := c.tx.Rollback(); err != nil { + return driver.ErrBadConn + } + } + return nil +} + +// IsValid implements the driver.Validator interface. +func (c *conn) IsValid() bool { + return !c.closed } func (c *conn) Prepare(query string) (driver.Stmt, error) { - panic("Using PrepareContext instead") + return c.PrepareContext(context.Background(), query) } -func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { - // TODO(jbd): Mention emails need to be escaped. - args, err := internal.NamedValueParamNames(query, -1) +func (c *conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) { + args, err := internal.ParseNamedParameters(query) if err != nil { return nil, err } return &stmt{conn: c, query: query, numArgs: len(args)}, nil } -func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { +func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { + stmt, err := prepareSpannerStmt(query, args) + if err != nil { + return nil, err + } + var iter *spanner.RowIterator + if c.tx == nil { + iter = c.client.Single().Query(ctx, stmt) + } else { + iter = c.tx.Query(ctx, stmt) + } + return &rows{it: iter}, nil +} +func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { // Use admin API if DDL statement is provided. - isDdl, err := isDdl(query) + isDdl, err := internal.IsDdl(query) if err != nil { return nil, err } if isDdl { + // TODO: Determine whether we want to return an error if a transaction + // is active. Cloud Spanner does not support DDL in transactions, but + // this makes it seem like the DDL statement is executed on the + // transaction on this connection if it has a transaction. op, err := c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{ - Database: c.name, + Database: c.database, Statements: []string{query}, }) if err != nil { @@ -161,19 +280,16 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name return &result{rowsAffected: 0}, nil } - if c.roTx != nil { - return nil, errors.New("cannot write in read-only transaction") - } ss, err := prepareSpannerStmt(query, args) if err != nil { return nil, err } var rowsAffected int64 - if c.rwTx == nil { + if c.tx == nil { rowsAffected, err = c.execContextInNewRWTransaction(ctx, ss) } else { - rowsAffected, err = c.rwTx.ExecContext(ctx, ss) + rowsAffected, err = c.tx.ExecContext(ctx, ss) } if err != nil { return nil, err @@ -181,23 +297,13 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name return &result{rowsAffected: rowsAffected}, nil } -func isDdl(query string) (bool, error) { - - matchddl, err := regexp.MatchString(`(?is)^\n*\s*(CREATE|DROP|ALTER)\s+.+$`, query) - if err != nil { - return false, err - } - - return matchddl, nil -} - func (c *conn) Close() error { c.client.Close() return nil } func (c *conn) Begin() (driver.Tx, error) { - panic("Using BeginTx instead") + return c.BeginTx(context.Background(), driver.TxOptions{}) } func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { @@ -206,35 +312,32 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e } if opts.ReadOnly { - c.roTx = c.client.ReadOnlyTransaction().WithTimestampBound(spanner.StrongRead()) - return &roTx{close: func() { - c.roTx.Close() - c.roTx = nil - }}, nil + ro := c.client.ReadOnlyTransaction().WithTimestampBound(spanner.StrongRead()) + c.tx = &readOnlyTransaction{ + roTx: ro, + close: func() { + c.tx = nil + }, + } + return c.tx, nil } - connector := internal.NewRWConnector(ctx, c.client) - c.rwTx = &rwTx{ - connector: connector, + tx, err := spanner.NewReadWriteStmtBasedTransaction(ctx, c.client) + if err != nil { + return nil, err + } + c.tx = &readWriteTransaction{ + ctx: ctx, + rwTx: tx, close: func() { - c.rwTx = nil + c.tx = nil }, } - - // TODO(jbd): Make sure we are not leaking - // a goroutine in connector if timeout happens. - select { - case <-connector.Ready: - return c.rwTx, nil - case err := <-connector.Errors: // If received before Ready, transaction failed to start. - return nil, err - case <-time.Tick(10 * time.Second): - return nil, errors.New("cannot begin transaction, timeout after 10 seconds") - } + return c.tx, nil } func (c *conn) inTransaction() bool { - return c.roTx != nil || c.rwTx != nil + return c.tx != nil } func (c *conn) execContextInNewRWTransaction(ctx context.Context, statement spanner.Statement) (int64, error) { diff --git a/driver_test.go b/driver_test.go index 8bbf45e9..adb55f2a 100644 --- a/driver_test.go +++ b/driver_test.go @@ -16,648 +16,112 @@ package spannerdriver import ( "context" - "database/sql" - "os" - "reflect" + "database/sql/driver" "testing" -) -var ( - dsn string + "github.com/google/go-cmp/cmp" ) -func init() { - - var projectId, instanceId, databaseId string - var ok bool - - // Get environment variables or set to default. - if instanceId, ok = os.LookupEnv("SPANNER_TEST_INSTANCE"); !ok { - instanceId = "test-instance" - } - if projectId, ok = os.LookupEnv("SPANNER_TEST_PROJECT"); !ok { - projectId = "test-project" - } - if databaseId, ok = os.LookupEnv("SPANNER_TEST_DBID"); !ok { - databaseId = "gotest" - } - - // Derive data source name. - dsn = "projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId -} - -func TestQueryContext(t *testing.T) { - - // Open db. - ctx := context.Background() - db, err := sql.Open("spanner", dsn) - if err != nil { - t.Fatal(err) - } - defer db.Close() - - // Set up test table. - _, err = db.ExecContext(ctx, - `CREATE TABLE TestQueryContext ( - A STRING(1024), - B STRING(1024), - C STRING(1024) - ) PRIMARY KEY (A)`) - if err != nil { - t.Fatal(err) - } - - _, err = db.ExecContext(ctx, `INSERT INTO TestQueryContext (A, B, C) - VALUES ("a1", "b1", "c1"), ("a2", "b2", "c2") , ("a3", "b3", "c3") `) - if err != nil { - t.Fatal(err) - } - - type testQueryContextRow struct { - A, B, C string - } - +func TestExtractDnsParts(t *testing.T) { tests := []struct { - name string - input string - want []testQueryContextRow - wantErrorQuery bool - wantErrorScan bool - wantErrorClose bool + input string + want connectorConfig + wantErr error }{ { - name: "empty query", - wantErrorClose: true, - input: "", - want: []testQueryContextRow{}, - }, - { - name: "syntax error", - wantErrorClose: true, - input: "SELECT SELECT * FROM TestQueryContext", - want: []testQueryContextRow{}, - }, - { - name: "return nothing", - input: "SELECT * FROM TestQueryContext WHERE A = \"hihihi\"", - want: []testQueryContextRow{}, - }, - { - name: "select one tuple", - input: "SELECT * FROM TestQueryContext WHERE A = \"a1\"", - want: []testQueryContextRow{ - {A: "a1", B: "b1", C: "c1"}, + input: "projects/p/instances/i/databases/d", + want: connectorConfig{ + project: "p", + instance: "i", + database: "d", + params: map[string]string{}, }, }, { - name: "select subset of tuples", - input: "SELECT * FROM TestQueryContext WHERE A = \"a1\" OR A = \"a2\"", - want: []testQueryContextRow{ - {A: "a1", B: "b1", C: "c1"}, - {A: "a2", B: "b2", C: "c2"}, + input: "projects/DEFAULT_PROJECT_ID/instances/test-instance/databases/test-database", + want: connectorConfig{ + project: "DEFAULT_PROJECT_ID", + instance: "test-instance", + database: "test-database", + params: map[string]string{}, }, }, { - name: "select subset of tuples with !=", - input: "SELECT * FROM TestQueryContext WHERE A != \"a3\"", - want: []testQueryContextRow{ - {A: "a1", B: "b1", C: "c1"}, - {A: "a2", B: "b2", C: "c2"}, + input: "localhost:9010/projects/p/instances/i/databases/d", + want: connectorConfig{ + host: "localhost:9010", + project: "p", + instance: "i", + database: "d", + params: map[string]string{}, }, }, { - name: "select entire table", - input: "SELECT * FROM TestQueryContext ORDER BY A", - want: []testQueryContextRow{ - {A: "a1", B: "b1", C: "c1"}, - {A: "a2", B: "b2", C: "c2"}, - {A: "a3", B: "b3", C: "c3"}, + input: "spanner.googleapis.com/projects/p/instances/i/databases/d", + want: connectorConfig{ + host: "spanner.googleapis.com", + project: "p", + instance: "i", + database: "d", + params: map[string]string{}, }, }, { - name: "query non existent table", - wantErrorClose: true, - input: "SELECT * FROM NonExistent", - want: []testQueryContextRow{}, - }, - } - - // Run tests - for _, tc := range tests { - - rows, err := db.QueryContext(ctx, tc.input) - if (err != nil) && (!tc.wantErrorQuery) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.wantErrorQuery) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - - got := []testQueryContextRow{} - for rows.Next() { - var curr testQueryContextRow - err := rows.Scan(&curr.A, &curr.B, &curr.C) - if (err != nil) && (!tc.wantErrorScan) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.wantErrorScan) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - - got = append(got, curr) - } - - rows.Close() - err = rows.Err() - if (err != nil) && (!tc.wantErrorClose) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.wantErrorClose) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - if !reflect.DeepEqual(tc.want, got) { - t.Errorf("Test failed: %s. want: %v, got: %v", tc.name, tc.want, got) - } - - } - - // Drop table. - if _, err = db.ExecContext(ctx, `DROP TABLE TestQueryContext`); err != nil { - t.Error(err) - } -} - -// note: isDdl function does not check validity of statement -// just that the statement begins with a DDL instruction. -// Other checking performed by database. -func TestIsDdl(t *testing.T) { - - tests := []struct { - name string - input string - want bool - }{ - { - name: "valid create", - input: `CREATE TABLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "leading spaces", - input: ` CREATE TABLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "leading newlines", - input: ` - - - CREATE TABLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "leading tabs", - input: ` CREATE TABLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "leading whitespace, miscellaneous", - input: ` - - CREATE TABLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "lower case", - input: `create table Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "mixed case, leading whitespace", - input: ` - cREAte taBLE Valid ( - A STRING(1024) - ) PRIMARY KEY (A)`, - want: true, - }, - { - name: "insert (not ddl)", - input: `INSERT INTO Valid`, - want: false, - }, - { - name: "delete (not ddl)", - input: `DELETE FROM Valid`, - want: false, - }, - { - name: "update (not ddl)", - input: `UPDATE Valid`, - want: false, - }, - { - name: "drop", - input: `DROP TABLE Valid`, - want: true, - }, - { - name: "alter", - input: `alter TABLE Valid`, - want: true, - }, - { - name: "typo (ccreate)", - input: `cCREATE TABLE Valid`, - want: false, - }, - { - name: "typo (reate)", - input: `REATE TABLE Valid`, - want: false, - }, - { - name: "typo (rx ceate)", - input: `x CREATE TABLE Valid`, - want: false, - }, - { - name: "leading int", - input: `0CREATE TABLE Valid`, - want: false, - }, - } - - for _, tc := range tests { - got, err := isDdl(tc.input) - if err != nil { - t.Error(err) - } - if got != tc.want { - t.Errorf("isDdl test failed, %s: wanted %t got %t.", tc.name, tc.want, got) - } - } -} - -func TestExecContextDml(t *testing.T) { - - // Open db. - ctx := context.Background() - db, err := sql.Open("spanner", dsn) - if err != nil { - t.Fatal(err) - } - defer db.Close() - - // Set up test table. - _, err = db.ExecContext(ctx, - `CREATE TABLE TestExecContextDml ( - key INT64, - testString STRING(1024), - testBytes BYTES(1024), - testInt INT64, - testFloat FLOAT64, - testBool BOOL - ) PRIMARY KEY (key)`) - if err != nil { - t.Fatal(err) - } - - type testExecContextDmlRow struct { - key int - testString string - testBytes []byte - testInt int - testFloat float64 - testBool bool - } - - type then struct { - query string - wantRows []testExecContextDmlRow - wantError bool - } - - tests := []struct { - name string - given string - when string - then then - }{ - { - name: "insert single tuple", - when: ` - INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ) `, - then: then{ - query: `SELECT * FROM TestExecContextDml WHERE key = 1`, - wantRows: []testExecContextDmlRow{ - {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - { - name: "insert multiple tuples", - when: ` - INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (2, "two", CAST("two" as bytes), 42, 42, true ), (3, "three", CAST("three" as bytes), 42, 42, true )`, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, - {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - { - name: "insert syntax error", - when: ` - INSERT INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (101, "one", CAST("one" as bytes), 42, 42, true ) `, - then: then{ - wantError: true, - }, - }, - { - name: "insert lower case dml", - when: ` - insert into TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (4, "four", CAST("four" as bytes), 42, 42, true ) `, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 4, testString: "four", testBytes: []byte("four"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - - { - name: "insert lower case table name", - when: ` - INSERT INTO testexeccontextdml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (5, "five", CAST("five" as bytes), 42, 42, true ) `, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 5, testString: "five", testBytes: []byte("five"), testInt: 42, testFloat: 42, testBool: true}, + input: "spanner.googleapis.com/projects/p/instances/i/databases/d?usePlainText=true", + want: connectorConfig{ + host: "spanner.googleapis.com", + project: "p", + instance: "i", + database: "d", + params: map[string]string{ + "usePlainText": "true", }, }, }, - - { - name: "wrong types", - when: ` - INSERT INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (102, 56, 56, 42, hello, "i should be a bool" ) `, - then: then{ - wantError: true, - }, - }, - - { - name: "insert primary key duplicate", - when: ` - INSERT INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (1, "one", CAST("one" as bytes), 42, 42, true ) `, - then: then{ - wantError: true, - }, - }, - - { - name: "insert null into primary key", - when: ` - INSERT INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (null, "one", CAST("one" as bytes), 42, 42, true ) `, - then: then{ - wantError: true, - }, - }, - - { - name: "insert too many values", - when: ` - INSERT INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (null, "one", CAST("one" as bytes), 42, 42, true, 42 ) `, - then: then{ - wantError: true, - }, - }, - - { - name: "insert too few values", - when: ` - INSERT INSERT INTO TestExecContextDml - (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (null, "one", CAST("one" as bytes), 42 ) `, - then: then{ - wantError: true, - }, - }, - { - name: "delete single tuple", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true )`, - when: `DELETE FROM TestExecContextDml WHERE key = 1`, - then: then{ - query: `SELECT * FROM TestExecContextDml`, - wantRows: []testExecContextDmlRow{ - {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + input: "spanner.googleapis.com/projects/p/instances/i/databases/d;credentials=/path/to/credentials.json", + want: connectorConfig{ + host: "spanner.googleapis.com", + project: "p", + instance: "i", + database: "d", + params: map[string]string{ + "credentials": "/path/to/credentials.json", }, }, }, - { - name: "delete multiple tuples with subthen", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true ), (4, "four", CAST("four" as bytes), 42, 42, true )`, - when: ` - DELETE FROM TestExecContextDml WHERE key - IN (SELECT key FROM TestExecContextDml WHERE key != 4)`, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 4, testString: "four", testBytes: []byte("four"), testInt: 42, testFloat: 42, testBool: true}, + input: "spanner.googleapis.com/projects/p/instances/i/databases/d?credentials=/path/to/credentials.json;readonly=true", + want: connectorConfig{ + host: "spanner.googleapis.com", + project: "p", + instance: "i", + database: "d", + params: map[string]string{ + "credentials": "/path/to/credentials.json", + "readonly": "true", }, }, }, - - { - name: "delete all tuples", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true ), (4, "four", CAST("four" as bytes), 42, 42, true )`, - when: `DELETE FROM TestExecContextDml WHERE true`, - then: then{ - query: `SELECT * FROM TestExecContextDml`, - wantRows: []testExecContextDmlRow{}, - }, - }, - { - name: "update one tuple", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true )`, - when: `UPDATE TestExecContextDml SET testSTring = "updated" WHERE key = 1`, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 1, testString: "updated", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, - {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, - {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - { - name: "update multiple tuples", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true )`, - when: `UPDATE TestExecContextDml SET testSTring = "updated" WHERE key = 2 OR key = 3`, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, - {key: 2, testString: "updated", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, - {key: 3, testString: "updated", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - - { - name: "update primary key", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true )`, - when: `UPDATE TestExecContextDml SET key = 100 WHERE key = 1`, - then: then{ - wantError: true, - }, - }, - - { - name: "update nothing", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true )`, - when: `UPDATE TestExecContextDml SET testSTring = "nothing" WHERE false`, - then: then{ - query: `SELECT * FROM TestExecContextDml ORDER BY key`, - wantRows: []testExecContextDmlRow{ - {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, - {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, - {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - - { - name: "update everything", - given: ` - INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) - VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), - (3, "three", CAST("three" as bytes), 42, 42, true )`, - when: `UPDATE TestExecContextDml SET testSTring = "everything" WHERE true `, - then: then{ - query: `SELECT * FROM TestExecContextDml WHERE testString = "everything"`, - wantRows: []testExecContextDmlRow{ - {key: 1, testString: "everything", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, - {key: 2, testString: "everything", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, - {key: 3, testString: "everything", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - }, - { - name: "update to wrong type", - when: `UPDATE TestExecContextDml SET testBool = 100 WHERE key = 1`, - then: then{ - wantError: true, - }, - }, } - - // Run tests. for _, tc := range tests { - - // Set up test table. - if tc.given != "" { - if _, err = db.ExecContext(ctx, tc.given); err != nil { - t.Errorf("%s: error setting up table: %v", tc.name, err) - } - } - - // Run DML. - _, err = db.ExecContext(ctx, tc.when) - if (err != nil) && (!tc.then.wantError) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.then.wantError) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - - // Check rows returned. - if tc.then.query != "" { - rows, err := db.QueryContext(ctx, tc.then.query) - if err != nil { - t.Errorf("%s: error from QueryContext : %v", tc.name, err) - } - - got := []testExecContextDmlRow{} - for rows.Next() { - var curr testExecContextDmlRow - err := rows.Scan( - &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) - if err != nil { - t.Errorf("%s: error while scanning rows: %v", tc.name, err) - } - got = append(got, curr) - } - - rows.Close() - if err = rows.Err(); err != nil { - t.Errorf("%s: error on rows.close: %v", tc.name, err) - } - if !reflect.DeepEqual(tc.then.wantRows, got) { - t.Errorf("Unexpecred rows: %s. want: %v, got: %v", tc.name, tc.then.wantRows, got) + config, err := extractConnectorConfig(tc.input) + if err != nil { + t.Errorf("extract failed for %q: %v", tc.input, err) + } else { + if !cmp.Equal(config, tc.want, cmp.AllowUnexported(connectorConfig{})) { + t.Errorf("connector config mismatch for %q\ngot: %v\nwant %v", tc.input, config, tc.want) } } - - // Clear table for next test. - _, err = db.ExecContext(ctx, `DELETE FROM TestExecContextDml WHERE true`) - if (err != nil) && (!tc.then.wantError) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - } +} - // Drop table. - if _, err = db.ExecContext(ctx, `DROP TABLE TestExecContextDml`); err != nil { - t.Error(err) +func TestConnection_NoNestedTransactions(t *testing.T) { + c := conn{ + tx: &readOnlyTransaction{}, + } + _, err := c.BeginTx(context.Background(), driver.TxOptions{}) + if err == nil { + t.Errorf("unexpected nil error for BeginTx call") } - } diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go new file mode 100644 index 00000000..ccd4032f --- /dev/null +++ b/driver_with_mockserver_test.go @@ -0,0 +1,863 @@ +// Copyright 2021 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package spannerdriver + +import ( + "context" + "database/sql" + "database/sql/driver" + "encoding/base64" + "fmt" + "math/big" + "reflect" + "testing" + "time" + + "cloud.google.com/go/civil" + "cloud.google.com/go/spanner" + "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/ptypes" + emptypb "github.com/golang/protobuf/ptypes/empty" + proto3 "github.com/golang/protobuf/ptypes/struct" + "github.com/google/go-cmp/cmp" + "github.com/rakyll/go-sql-driver-spanner/testutil" + "google.golang.org/api/option" + longrunningpb "google.golang.org/genproto/googleapis/longrunning" + databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" + sppb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc/codes" + gstatus "google.golang.org/grpc/status" +) + +func TestPingContext(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + if err := db.PingContext(context.Background()); err != nil { + t.Fatalf("unexpected error for ping: %v", err) + } +} + +func TestPingContext_Fails(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + s := gstatus.Newf(codes.PermissionDenied, "Permission denied for database") + _ = server.TestSpanner.PutStatementResult("SELECT 1", &testutil.StatementResult{Err: s.Err()}) + if g, w := db.PingContext(context.Background()), driver.ErrBadConn; g != w { + t.Fatalf("ping error mismatch\nGot: %v\nWant: %v", g, w) + } +} + +func TestSimpleQuery(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + rows, err := db.QueryContext(context.Background(), testutil.SelectFooFromBar) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() + if err != nil { + t.Fatal(err) + } + if !cmp.Equal(cols, []string{"FOO"}) { + t.Fatalf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) + } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Fatalf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetSingleUse() == nil { + t.Fatalf("missing single use selector for ExecuteSqlRequest") + } + if req.Transaction.GetSingleUse().GetReadOnly() == nil { + t.Fatalf("missing read-only option for ExecuteSqlRequest") + } + if !req.Transaction.GetSingleUse().GetReadOnly().GetStrong() { + t.Fatalf("missing strong timestampbound for ExecuteSqlRequest") + } +} + +func TestSimpleReadOnlyTransaction(t *testing.T) { + t.Parallel() + + ctx := context.Background() + db, server, teardown := setupTestDbConnection(t) + defer teardown() + tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) + if err != nil { + t.Fatal(err) + } + rows, err := tx.Query(testutil.SelectFooFromBar) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() + if err != nil { + t.Fatal(err) + } + if !cmp.Equal(cols, []string{"FOO"}) { + t.Fatalf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) + } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + err = tx.Commit() + if err != nil { + t.Fatal(err) + } + + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Fatalf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetId() == nil { + t.Fatalf("missing id selector for ExecuteSqlRequest") + } + // Read-only transactions are not really committed on Cloud Spanner, so + // there should be no commit request on the server. + commitRequests := requestsOfType(requests, reflect.TypeOf(&sppb.CommitRequest{})) + if g, w := len(commitRequests), 0; g != w { + t.Fatalf("commit requests count mismatch\nGot: %v\nWant: %v", g, w) + } + beginReadOnlyRequests := filterBeginReadOnlyRequests(requestsOfType(requests, reflect.TypeOf(&sppb.BeginTransactionRequest{}))) + if g, w := len(beginReadOnlyRequests), 1; g != w { + t.Fatalf("begin requests count mismatch\nGot: %v\nWant: %v", g, w) + } +} + +func TestSimpleReadWriteTransaction(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + tx, err := db.Begin() + if err != nil { + t.Fatal(err) + } + rows, err := tx.Query(testutil.SelectFooFromBar) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() + if err != nil { + t.Fatal(err) + } + if !cmp.Equal(cols, []string{"FOO"}) { + t.Fatalf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) + } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + err = tx.Commit() + if err != nil { + t.Fatal(err) + } + + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Fatalf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetId() == nil { + t.Fatalf("missing id selector for ExecuteSqlRequest") + } + commitRequests := requestsOfType(requests, reflect.TypeOf(&sppb.CommitRequest{})) + if g, w := len(commitRequests), 1; g != w { + t.Fatalf("commit requests count mismatch\nGot: %v\nWant: %v", g, w) + } + commitReq := commitRequests[0].(*sppb.CommitRequest) + if c, e := commitReq.GetTransactionId(), req.Transaction.GetId(); !cmp.Equal(c, e) { + t.Fatalf("transaction id mismatch\nCommit: %c\nExecute: %v", c, e) + } +} + +func TestPreparedQuery(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + _ = server.TestSpanner.PutStatementResult( + "SELECT * FROM Test WHERE Id=@id", + &testutil.StatementResult{ + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateSelect1ResultSet(), + }, + ) + + stmt, err := db.Prepare("SELECT * FROM Test WHERE Id=@id") + if err != nil { + t.Fatal(err) + } + defer stmt.Close() + rows, err := stmt.Query(1) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if g, w := len(req.ParamTypes), 1; g != w { + t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w) + } + if pt, ok := req.ParamTypes["id"]; ok { + if g, w := pt.Code, sppb.TypeCode_INT64; g != w { + t.Fatalf("param type mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Fatalf("no param type found for @id") + } + if g, w := len(req.Params.Fields), 1; g != w { + t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) + } + if val, ok := req.Params.Fields["id"]; ok { + if g, w := val.GetStringValue(), "1"; g != w { + t.Fatalf("param value mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Fatalf("no value found for param @id") + } +} + +func TestQueryWithAllTypes(t *testing.T) { + t.Parallel() + + // TODO: Add ARRAY types. + db, server, teardown := setupTestDbConnection(t) + defer teardown() + query := `SELECT * + FROM Test + WHERE ColBool=@bool + AND ColString=@string + AND ColBytes=@bytes + AND ColInt=@int64 + AND ColFloat=@float64 + AND ColNumeric=@numeric + AND ColDate=@date + AND ColTimestamp=@timestamp` + _ = server.TestSpanner.PutStatementResult( + query, + &testutil.StatementResult{ + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateResultSetWithAllTypes(), + }, + ) + + stmt, err := db.Prepare(query) + if err != nil { + t.Fatal(err) + } + defer stmt.Close() + ts, _ := time.Parse(time.RFC3339Nano, "2021-07-22T10:26:17.123Z") + rows, err := stmt.QueryContext( + context.Background(), + true, + "test", + []byte("testbytes"), + int64(5), + 3.14, + numeric("6.626"), + civil.Date{Year: 2021, Month: 7, Day: 21}, + ts) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var b bool + var s string + var bt []byte + var i int64 + var f float64 + var r big.Rat + var d time.Time + var ts time.Time + err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts) + if err != nil { + t.Fatal(err) + } + if g, w := b, true; g != w { + t.Errorf("row value mismatch for bool\nGot: %v\nWant: %v", g, w) + } + if g, w := s, "test"; g != w { + t.Errorf("row value mismatch for string\nGot: %v\nWant: %v", g, w) + } + if g, w := bt, []byte("testbytes"); !cmp.Equal(g, w) { + t.Errorf("row value mismatch for bytes\nGot: %v\nWant: %v", g, w) + } + if g, w := i, int64(5); g != w { + t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", g, w) + } + if g, w := f, 3.14; g != w { + t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", g, w) + } + if g, w := r, numeric("6.626"); g.Cmp(w) != 0 { + t.Errorf("row value mismatch for numeric\nGot: %v\nWant: %v", g, w) + } + // 2021-07-21 + if g, w := d, time.Date(2021, 7, 21, 0, 0, 0, 0, time.UTC); g != w { + t.Errorf("row value mismatch for date\nGot: %v\nWant: %v", g, w) + } + // 2021-07-21T21:07:59.339911800Z + if g, w := ts, time.Date(2021, 7, 21, 21, 7, 59, 339911800, time.UTC); g != w { + t.Errorf("row value mismatch for timestamp\nGot: %v\nWant: %v", g, w) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if g, w := len(req.ParamTypes), 8; g != w { + t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w) + } + if g, w := len(req.Params.Fields), 8; g != w { + t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) + } + wantParams := []struct { + name string + code sppb.TypeCode + value interface{} + }{ + { + name: "bool", + code: sppb.TypeCode_BOOL, + value: true, + }, + { + name: "string", + code: sppb.TypeCode_STRING, + value: "test", + }, + { + name: "bytes", + code: sppb.TypeCode_BYTES, + value: base64.StdEncoding.EncodeToString([]byte("testbytes")), + }, + { + name: "int64", + code: sppb.TypeCode_INT64, + value: "5", + }, + { + name: "float64", + code: sppb.TypeCode_FLOAT64, + value: 3.14, + }, + { + name: "numeric", + code: sppb.TypeCode_NUMERIC, + value: "6.626000000", + }, + { + name: "date", + code: sppb.TypeCode_DATE, + value: "2021-07-21", + }, + { + name: "timestamp", + code: sppb.TypeCode_TIMESTAMP, + value: "2021-07-22T10:26:17.123Z", + }, + } + for _, wantParam := range wantParams { + if pt, ok := req.ParamTypes[wantParam.name]; ok { + if g, w := pt.Code, wantParam.code; g != w { + t.Errorf("param type mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Errorf("no param type found for @%s", wantParam.name) + } + if val, ok := req.Params.Fields[wantParam.name]; ok { + var g interface{} + switch wantParam.code { + case sppb.TypeCode_BOOL: + g = val.GetBoolValue() + case sppb.TypeCode_FLOAT64: + g = val.GetNumberValue() + default: + g = val.GetStringValue() + } + if g != wantParam.value { + t.Errorf("param value mismatch\nGot: %v\nWant: %v", g, wantParam.value) + } + } else { + t.Errorf("no value found for param @%s", wantParam.name) + } + } +} + +func TestQueryWithNullParameters(t *testing.T) { + t.Parallel() + + // TODO: Add ARRAY types. + db, server, teardown := setupTestDbConnection(t) + defer teardown() + query := `SELECT * + FROM Test + WHERE ColBool=@bool + AND ColString=@string + AND ColBytes=@bytes + AND ColInt=@int64 + AND ColFloat=@float64 + AND ColNumeric=@numeric + AND ColDate=@date + AND ColTimestamp=@timestamp` + _ = server.TestSpanner.PutStatementResult( + query, + &testutil.StatementResult{ + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateResultSetWithAllTypes(), + }, + ) + + stmt, err := db.Prepare(query) + if err != nil { + t.Fatal(err) + } + defer stmt.Close() + rows, err := stmt.QueryContext( + context.Background(), + nil, // bool + nil, // string + nil, // bytes + nil, // int64 + nil, // float64 + nil, // numeric + nil, // date + nil, // timestamp + ) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var b bool + var s string + var bt []byte + var i int64 + var f float64 + var r big.Rat + var d time.Time + var ts time.Time + err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts) + if err != nil { + t.Fatal(err) + } + if g, w := b, true; g != w { + t.Errorf("row value mismatch for bool\nGot: %v\nWant: %v", g, w) + } + if g, w := s, "test"; g != w { + t.Errorf("row value mismatch for string\nGot: %v\nWant: %v", g, w) + } + if g, w := bt, []byte("testbytes"); !cmp.Equal(g, w) { + t.Errorf("row value mismatch for bytes\nGot: %v\nWant: %v", g, w) + } + if g, w := i, int64(5); g != w { + t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", g, w) + } + if g, w := f, 3.14; g != w { + t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", g, w) + } + if g, w := r, numeric("6.626"); g.Cmp(w) != 0 { + t.Errorf("row value mismatch for numeric\nGot: %v\nWant: %v", g, w) + } + // 2021-07-21 + if g, w := d, time.Date(2021, 7, 21, 0, 0, 0, 0, time.UTC); g != w { + t.Errorf("row value mismatch for date\nGot: %v\nWant: %v", g, w) + } + // 2021-07-21T21:07:59.339911800Z + if g, w := ts, time.Date(2021, 7, 21, 21, 7, 59, 339911800, time.UTC); g != w { + t.Errorf("row value mismatch for timestamp\nGot: %v\nWant: %v", g, w) + } + } + if rows.Err() != nil { + t.Fatal(rows.Err()) + } + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + // The param types map should be empty, as we are only sending nil params. + if g, w := len(req.ParamTypes), 0; g != w { + t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w) + } + if g, w := len(req.Params.Fields), 8; g != w { + t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) + } + for _, param := range req.Params.Fields { + if _, ok := param.GetKind().(*proto3.Value_NullValue); !ok { + t.Errorf("param value mismatch\nGot: %v\nWant: %v", param.GetKind(), proto3.Value_NullValue{}) + } + } +} + +func TestDmlInAutocommit(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + res, err := db.ExecContext(context.Background(), testutil.UpdateBarSetFoo) + if err != nil { + t.Fatal(err) + } + affected, err := res.RowsAffected() + if err != nil { + t.Fatal(err) + } + if g, w := affected, int64(testutil.UpdateBarSetFooRowCount); g != w { + t.Fatalf("row count mismatch\nGot: %v\nWant: %v", g, w) + } + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Fatalf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + } + // The DML statement should use a transaction even though no explicit + // transaction was created. + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Fatalf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetId() == nil { + t.Fatalf("missing id selector for ExecuteSqlRequest") + } + commitRequests := requestsOfType(requests, reflect.TypeOf(&sppb.CommitRequest{})) + if g, w := len(commitRequests), 1; g != w { + t.Fatalf("commit requests count mismatch\nGot: %v\nWant: %v", g, w) + } + commitReq := commitRequests[0].(*sppb.CommitRequest) + if c, e := commitReq.GetTransactionId(), req.Transaction.GetId(); !cmp.Equal(c, e) { + t.Fatalf("transaction id mismatch\nCommit: %c\nExecute: %v", c, e) + } +} + +func TestDdlInAutocommit(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + + var expectedResponse = &emptypb.Empty{} + any, _ := ptypes.MarshalAny(expectedResponse) + server.TestDatabaseAdmin.SetResps([]proto.Message{ + &longrunningpb.Operation{ + Done: true, + Result: &longrunningpb.Operation_Response{Response: any}, + Name: "test-operation", + }, + }) + query := "CREATE TABLE Singers (SingerId INT64, FirstName STRING(100), LastName STRING(100)) PRIMARY KEY (SingerId)" + res, err := db.ExecContext(context.Background(), query) + if err != nil { + t.Fatal(err) + } + affected, err := res.RowsAffected() + if err != nil { + t.Fatal(err) + } + if affected != 0 { + t.Fatalf("affected rows count mismatch\nGot: %v\nWant: %v", affected, 0) + } + requests := server.TestDatabaseAdmin.Reqs() + if g, w := len(requests), 1; g != w { + t.Fatalf("requests count mismatch\nGot: %v\nWant: %v", g, w) + } + if req, ok := requests[0].(*databasepb.UpdateDatabaseDdlRequest); ok { + if g, w := len(req.Statements), 1; g != w { + t.Fatalf("statement count mismatch\nGot: %v\nWant: %v", g, w) + } + if g, w := req.Statements[0], query; g != w { + t.Fatalf("statement mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Fatalf("request type mismatch, got %v", requests[0]) + } +} + +func TestDdlInTransaction(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + + var expectedResponse = &emptypb.Empty{} + any, _ := ptypes.MarshalAny(expectedResponse) + server.TestDatabaseAdmin.SetResps([]proto.Message{ + &longrunningpb.Operation{ + Done: true, + Result: &longrunningpb.Operation_Response{Response: any}, + Name: "test-operation", + }, + }) + query := "CREATE TABLE Singers (SingerId INT64, FirstName STRING(100), LastName STRING(100)) PRIMARY KEY (SingerId)" + tx, err := db.BeginTx(context.Background(), &sql.TxOptions{}) + if err != nil { + t.Fatal(err) + } + res, err := tx.ExecContext(context.Background(), query) + if err != nil { + t.Fatal(err) + } + affected, err := res.RowsAffected() + if err != nil { + t.Fatal(err) + } + if affected != 0 { + t.Fatalf("affected rows count mismatch\nGot: %v\nWant: %v", affected, 0) + } + requests := server.TestDatabaseAdmin.Reqs() + if g, w := len(requests), 1; g != w { + t.Fatalf("requests count mismatch\nGot: %v\nWant: %v", g, w) + } + if req, ok := requests[0].(*databasepb.UpdateDatabaseDdlRequest); ok { + if g, w := len(req.Statements), 1; g != w { + t.Fatalf("statement count mismatch\nGot: %v\nWant: %v", g, w) + } + if g, w := req.Statements[0], query; g != w { + t.Fatalf("statement mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Fatalf("request type mismatch, got %v", requests[0]) + } +} + +func TestBegin(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + + // Ensure that the old Begin method works. + _, err := db.Begin() + if err != nil { + t.Fatalf("Begin failed: %v", err) + } +} + +func TestQuery(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + + // Ensure that the old Query method works. + rows, err := db.Query(testutil.SelectFooFromBar) + if err != nil { + t.Fatalf("Query failed: %v", err) + } + defer rows.Close() +} + +func TestExec(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + + // Ensure that the old Exec method works. + _, err := db.Exec(testutil.UpdateBarSetFoo) + if err != nil { + t.Fatalf("Exec failed: %v", err) + } +} + +func TestPrepare(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + + // Ensure that the old Prepare method works. + _, err := db.Prepare(testutil.SelectFooFromBar) + if err != nil { + t.Fatalf("Prepare failed: %v", err) + } +} + +func TestPing(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + + // Ensure that the old Ping method works. + err := db.Ping() + if err != nil { + t.Fatalf("Ping failed: %v", err) + } +} + +func numeric(v string) *big.Rat { + res, _ := big.NewRat(1, 1).SetString(v) + return res +} + +func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpannerInMemTestServer, teardown func()) { + server, _, serverTeardown := setupMockedTestServer(t) + db, err := sql.Open( + "spanner", + fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true", server.Address)) + if err != nil { + serverTeardown() + t.Fatal(err) + } + return db, server, func() { + _ = db.Close() + serverTeardown() + } +} + +func setupMockedTestServer(t *testing.T) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) { + return setupMockedTestServerWithConfig(t, spanner.ClientConfig{}) +} + +func setupMockedTestServerWithConfig(t *testing.T, config spanner.ClientConfig) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) { + return setupMockedTestServerWithConfigAndClientOptions(t, config, []option.ClientOption{}) +} + +func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanner.ClientConfig, clientOptions []option.ClientOption) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) { + server, opts, serverTeardown := testutil.NewMockedSpannerInMemTestServer(t) + opts = append(opts, clientOptions...) + ctx := context.Background() + formattedDatabase := fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + client, err := spanner.NewClientWithConfig(ctx, formattedDatabase, config, opts...) + if err != nil { + t.Fatal(err) + } + return server, client, func() { + client.Close() + serverTeardown() + } +} + +func filterBeginReadOnlyRequests(requests []interface{}) []*sppb.BeginTransactionRequest { + res := make([]*sppb.BeginTransactionRequest, 0) + for _, r := range requests { + if req, ok := r.(*sppb.BeginTransactionRequest); ok { + if req.Options != nil && req.Options.GetReadOnly() != nil { + res = append(res, req) + } + } + } + return res +} + +func requestsOfType(requests []interface{}, t reflect.Type) []interface{} { + res := make([]interface{}, 0) + for _, req := range requests { + if reflect.TypeOf(req) == t { + res = append(res, req) + } + } + return res +} + +func drainRequestsFromServer(server testutil.InMemSpannerServer) []interface{} { + var reqs []interface{} +loop: + for { + select { + case req := <-server.ReceivedRequests(): + reqs = append(reqs, req) + default: + break loop + } + } + return reqs +} diff --git a/go.mod b/go.mod index c1ab166b..2a039fe3 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,11 @@ module github.com/rakyll/go-sql-driver-spanner go 1.14 require ( - cloud.google.com/go/spanner v1.2.1 - github.com/jinzhu/gorm v1.9.12 - golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd // indirect - google.golang.org/api v0.17.0 - google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce + cloud.google.com/go v0.88.0 + cloud.google.com/go/spanner v1.23.1-0.20210727075241-3d6c6c7873e1 + github.com/golang/protobuf v1.5.2 + github.com/google/go-cmp v0.5.6 + google.golang.org/api v0.51.0 + google.golang.org/genproto v0.0.0-20210726143408-b02e89920bf0 + google.golang.org/grpc v1.39.0 ) diff --git a/go.sum b/go.sum index 031b23c2..a816f653 100644 --- a/go.sum +++ b/go.sum @@ -6,42 +6,68 @@ cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxK cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To= -cloud.google.com/go v0.52.0 h1:GGslhk/BU052LPlnI1vpp3fcbUs+hQ3E+Doti/3/vF8= cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4= +cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M= +cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc= +cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk= +cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs= +cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc= +cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY= +cloud.google.com/go v0.72.0/go.mod h1:M+5Vjvlc2wnp6tjzE102Dw08nGShTscUx2nZMufOKPI= +cloud.google.com/go v0.74.0/go.mod h1:VV1xSbzvo+9QJOxLDaJfTjx5e+MePCpCWwvftOeQmWk= +cloud.google.com/go v0.78.0/go.mod h1:QjdrLG0uq+YwhjoVOLsS1t7TW8fs36kLs4XO5R5ECHg= +cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb8= +cloud.google.com/go v0.81.0/go.mod h1:mk/AM35KwGk/Nm2YSeZbxXdrNK3KZOYHmLkOqC2V6E0= +cloud.google.com/go v0.83.0/go.mod h1:Z7MJUsANfY0pYPdw0lbnivPx4/vhy/e2FEkSkF7vAVY= +cloud.google.com/go v0.84.0/go.mod h1:RazrYuxIK6Kb7YrzzhPoLmCVzl7Sup4NrbKPg8KHSUM= +cloud.google.com/go v0.87.0/go.mod h1:TpDYlFy7vuLzZMMZ+B6iRiELaY7z/gJPaqbMx6mlWcY= +cloud.google.com/go v0.88.0 h1:MZ2cf9Elnv1wqccq8ooKO2MqHQLc+ChCp/+QWObCpxg= +cloud.google.com/go v0.88.0/go.mod h1:dnKwfYbP9hQhefiUvpbcAyoGSHUrOxR20JVElLiUvEY= cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE= -cloud.google.com/go/bigquery v1.4.0 h1:xE3CPsOgttP4ACBePh79zTKALtXwn/Edhcr16R5hMWU= cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc= -cloud.google.com/go/datastore v1.0.0 h1:Kt+gOPPp2LEPWp8CSfxhsM8ik9CcyE/gYu+0r+RnZvM= +cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg= +cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc= +cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ= cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw= -cloud.google.com/go/pubsub v1.2.0 h1:Lpy6hKgdcl7a3WGSfJIFmxmcdjSpP6OmBEfcOv1Y680= cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA= -cloud.google.com/go/spanner v1.2.1 h1:cXy2h5AVCvYTZvXGAodZTIwSqxcV3p0dpwLkGAxlakU= -cloud.google.com/go/spanner v1.2.1/go.mod h1:qw1wef4Iy3ztA7dV9v0Jdos8nxIxZM4gyj3UhoLZfFQ= +cloud.google.com/go/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= +cloud.google.com/go/spanner v1.23.1-0.20210727075241-3d6c6c7873e1 h1:DOK5uvDxxzkTjLIb7xt15zWewNS66DnrOmOb2Hv7C9g= +cloud.google.com/go/spanner v1.23.1-0.20210727075241-3d6c6c7873e1/go.mod h1:EZI0yH1D/PrXK0XH9Ba5LGXTXWeqZv0ClOD/19a0Z58= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= -cloud.google.com/go/storage v1.5.0 h1:RPUcBvDeYgQFMfQu1eBMq6piD1SXmLH+vK3qjewZPus= cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= +cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= +cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= +cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -50,35 +76,74 @@ github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4er github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= +github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= +github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= +github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= +github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= +github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210715191844-86eeefc3e471/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= -github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= -github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= -github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= @@ -86,24 +151,33 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= +go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -112,8 +186,8 @@ golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20200207192155-f17229e696bd h1:zkO/Lhoka23X63N9OSzpSeROEUQ5ODw47tM3YWjygbs= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= +golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -124,15 +198,23 @@ golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= -golang.org/x/lint v0.0.0-20200130185559-910be7a94367 h1:0IiAsCRByjO2QjX7ZPkw5oU9x+n1YqRL802rjC0c3Aw= golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= +golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -143,24 +225,57 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 h1:a8jGStKg0XqKDlKqjLrXn0ioF5MH36pT7Z0BRTqLhbk= +golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 h1:3B43BWw0xEBsLZ/NO1VALz6fppU3481pik+2Ksv45z8= +golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -170,17 +285,50 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -203,6 +351,7 @@ golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200117161641-43d50277825c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= @@ -210,14 +359,36 @@ golang.org/x/tools v0.0.0-20200122220014-bf1340f18c4a/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56 h1:DFtSed2q3HtNuVazwVDZ4nSRS/JrZEig0gz2BY4VNrg= golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd h1:hHkvGJK23seRCflePJnVa9IMv8fsuavSCWKd11kDQFs= -golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= +golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= +golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= +golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= @@ -225,14 +396,33 @@ google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEn google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= google.golang.org/api v0.15.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.17.0 h1:0q95w+VuFtv4PAx4PZVQdBMmYbaCHbnfKaEiDIcVyag= google.golang.org/api v0.17.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/api v0.18.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/api v0.19.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/api v0.22.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= +google.golang.org/api v0.28.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= +google.golang.org/api v0.29.0/go.mod h1:Lcubydp8VUV7KeIHD9z2Bys/sm/vGKnG1UHuDBSrHWM= +google.golang.org/api v0.30.0/go.mod h1:QGmEvQ87FHZNiUVJkT14jQNYJ4ZJjdRF23ZXz5138Fc= +google.golang.org/api v0.35.0/go.mod h1:/XrVsuzM0rZmrsbjJutiuftIzeuTQcEeaYcSk/mQ1dg= +google.golang.org/api v0.36.0/go.mod h1:+z5ficQTmoYpPn8LCUNVpK5I7hwkpjbcgqA7I34qYtE= +google.golang.org/api v0.40.0/go.mod h1:fYKFpnQN0DsDSKRVRcQSDQNtqWPfM9i+zNPxepjRCQ8= +google.golang.org/api v0.41.0/go.mod h1:RkxM5lITDfTzmyKFPt+wGrCJbVfniCr2ool8kTBzRTU= +google.golang.org/api v0.43.0/go.mod h1:nQsDGjRXMo4lvh5hP0TKqF244gqhGcr/YSIykhUk/94= +google.golang.org/api v0.47.0/go.mod h1:Wbvgpq1HddcWVtzsVLyfLp8lDg6AA241LmgIL59tHXo= +google.golang.org/api v0.48.0/go.mod h1:71Pr1vy+TAZRPkPs/xlCf5SsU8WjuAWv1Pfjbtukyy4= +google.golang.org/api v0.50.0/go.mod h1:4bNT5pAuq5ji4SRZm+5QIkjny9JAyVD/3gaSihNefaw= +google.golang.org/api v0.51.0 h1:SQaA2Cx57B+iPw2MBgyjEkoeMkRK2IenSGoia0U3lCk= +google.golang.org/api v0.51.0/go.mod h1:t4HdrdoNgyN5cbEfm7Lum0lcLDLiise1F8qDKX00sOU= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= -google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= @@ -248,24 +438,93 @@ google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb/go.mod h1:n3cpQtvx google.golang.org/genproto v0.0.0-20200115191322-ca5a22157cba/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= -google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce h1:1mbrb1tUU+Zmt5C94IGKADBTJZjZXAd+BubWi7r9EiI= google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= +google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20201201144952-b05cb90ed32e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A= +google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384/go.mod h1:P3QM42oQyzQSnHPnZ/vqoCdDmzH28fzWByN9asMeM8A= +google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= +google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= +google.golang.org/genproto v0.0.0-20210608205507-b6d2f5bf0d7d/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= +google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24= +google.golang.org/genproto v0.0.0-20210713002101-d411969a0d9a/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= +google.golang.org/genproto v0.0.0-20210716133855-ce7ef5c701ea/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= +google.golang.org/genproto v0.0.0-20210721163202-f1cecdd8b78a/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48= +google.golang.org/genproto v0.0.0-20210726143408-b02e89920bf0 h1:tcs4DyF9LYv8cynRAbX8JeBpuezJLaK6RfiATAsGwnY= +google.golang.org/genproto v0.0.0-20210726143408-b02e89920bf0/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= +google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= +google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= +google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= +google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= +google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI= +google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= +google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= +rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= +rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/integration_test.go b/integration_test.go new file mode 100644 index 00000000..fdcb76d0 --- /dev/null +++ b/integration_test.go @@ -0,0 +1,935 @@ +// Copyright 2021 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package spannerdriver + +import ( + "cloud.google.com/go/spanner" + database "cloud.google.com/go/spanner/admin/database/apiv1" + "context" + "database/sql" + "flag" + "fmt" + databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" + "google.golang.org/grpc/codes" + "log" + "math" + "os" + "reflect" + "testing" + + instance "cloud.google.com/go/spanner/admin/instance/apiv1" + instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" +) + +var ( + dsn string +) + +func init() { + + var projectId, instanceId, databaseId string + var ok bool + + // Get environment variables or set to default. + if instanceId, ok = os.LookupEnv("SPANNER_TEST_INSTANCE"); !ok { + instanceId = "test-instance" + } + if projectId, ok = os.LookupEnv("SPANNER_TEST_PROJECT"); !ok { + projectId = "test-project" + } + if databaseId, ok = os.LookupEnv("SPANNER_TEST_DBID"); !ok { + databaseId = "gotest" + } + + // Derive data source dsn. + dsn = "projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId + + // Automatically create test instance and database on the emulator if necessary. + if _, ok = os.LookupEnv("SPANNER_EMULATOR_HOST"); ok { + if err := initEmulator(projectId, instanceId, databaseId); err != nil { + log.Fatal(err) + } + } +} + +func initEmulator(projectId, instanceId, databaseId string) error { + ctx := context.Background() + instanceAdmin, err := instance.NewInstanceAdminClient(ctx) + if err != nil { + return err + } + defer instanceAdmin.Close() + op, err := instanceAdmin.CreateInstance(ctx, &instancepb.CreateInstanceRequest{ + Parent: fmt.Sprintf("projects/%s", projectId), + InstanceId: instanceId, + Instance: &instancepb.Instance{ + Config: fmt.Sprintf("projects/%s/instanceConfigs/%s", projectId, "emulator-config"), + DisplayName: instanceId, + NodeCount: 1, + }, + }) + if err != nil { + // Check if the instance has already been created by another (parallel) test. + code := spanner.ErrCode(err) + if code != codes.AlreadyExists { + return fmt.Errorf("could not create instance %s: %v", fmt.Sprintf("projects/%s/instances/%s", projectId, instanceId), err) + } + } else { + // Wait for the instance creation to finish. + _, err := op.Wait(ctx) + if err != nil { + return fmt.Errorf("waiting for instance creation to finish failed: %v", err) + } + } + databaseAdminClient, err := database.NewDatabaseAdminClient(ctx) + if err != nil { + return err + } + defer databaseAdminClient.Close() + opDb, err := databaseAdminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{ + Parent: fmt.Sprintf("projects/%s/instances/%s", projectId, instanceId), + CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", databaseId,), + }) + if err != nil { + // Check if the database has already been created by another (parallel) test. + code := spanner.ErrCode(err) + if code != codes.AlreadyExists { + return fmt.Errorf("could not create database %s: %v", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId), err) + } + } else { + // Wait for the database creation to finish. + _, err := opDb.Wait(ctx) + if err != nil { + return fmt.Errorf("waiting for database creation to finish failed: %v", err) + } + } + return nil +} + +func initIntegrationTests() (cleanup func()) { + flag.Parse() // Needed for testing.Short(). + noop := func() {} + + if testing.Short() { + log.Println("Integration tests skipped in -short mode.") + return noop + } + return noop +} + +func TestMain(m *testing.M) { + cleanup := initIntegrationTests() + res := m.Run() + cleanup() + os.Exit(res) +} + +func skipIfShort(t *testing.T) { + if testing.Short() { + t.Skip("Integration tests skipped in -short mode.") + } +} + +func TestQueryContext(t *testing.T) { + skipIfShort(t) + + // Open db. + ctx := context.Background() + db, err := sql.Open("spanner", dsn) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Set up test table. + _, err = db.ExecContext(ctx, + `CREATE TABLE TestQueryContext ( + A STRING(1024), + B STRING(1024), + C STRING(1024) + ) PRIMARY KEY (A)`) + if err != nil { + t.Fatal(err) + } + + _, err = db.ExecContext(ctx, `INSERT INTO TestQueryContext (A, B, C) + VALUES ("a1", "b1", "c1"), ("a2", "b2", "c2") , ("a3", "b3", "c3") `) + if err != nil { + t.Fatal(err) + } + + type testQueryContextRow struct { + A, B, C string + } + + tests := []struct { + name string + input string + want []testQueryContextRow + wantErrorQuery bool + wantErrorScan bool + wantErrorClose bool + }{ + { + name: "empty query", + wantErrorClose: true, + input: "", + want: []testQueryContextRow{}, + }, + { + name: "syntax error", + wantErrorClose: true, + input: "SELECT SELECT * FROM TestQueryContext", + want: []testQueryContextRow{}, + }, + { + name: "return nothing", + input: "SELECT * FROM TestQueryContext WHERE A = \"hihihi\"", + want: []testQueryContextRow{}, + }, + { + name: "select one tuple", + input: "SELECT * FROM TestQueryContext WHERE A = \"a1\"", + want: []testQueryContextRow{ + {A: "a1", B: "b1", C: "c1"}, + }, + }, + { + name: "select subset of tuples", + input: "SELECT * FROM TestQueryContext WHERE A = \"a1\" OR A = \"a2\"", + want: []testQueryContextRow{ + {A: "a1", B: "b1", C: "c1"}, + {A: "a2", B: "b2", C: "c2"}, + }, + }, + { + name: "select subset of tuples with !=", + input: "SELECT * FROM TestQueryContext WHERE A != \"a3\"", + want: []testQueryContextRow{ + {A: "a1", B: "b1", C: "c1"}, + {A: "a2", B: "b2", C: "c2"}, + }, + }, + { + name: "select entire table", + input: "SELECT * FROM TestQueryContext ORDER BY A", + want: []testQueryContextRow{ + {A: "a1", B: "b1", C: "c1"}, + {A: "a2", B: "b2", C: "c2"}, + {A: "a3", B: "b3", C: "c3"}, + }, + }, + { + name: "query non existent table", + wantErrorClose: true, + input: "SELECT * FROM NonExistent", + want: []testQueryContextRow{}, + }, + } + + // Run tests + for _, tc := range tests { + + rows, err := db.QueryContext(ctx, tc.input) + if (err != nil) && (!tc.wantErrorQuery) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.wantErrorQuery) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + + got := []testQueryContextRow{} + for rows.Next() { + var curr testQueryContextRow + err := rows.Scan(&curr.A, &curr.B, &curr.C) + if (err != nil) && (!tc.wantErrorScan) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.wantErrorScan) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + + got = append(got, curr) + } + + rows.Close() + err = rows.Err() + if (err != nil) && (!tc.wantErrorClose) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.wantErrorClose) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + if !reflect.DeepEqual(tc.want, got) { + t.Errorf("Test failed: %s. want: %v, got: %v", tc.name, tc.want, got) + } + + } + + // Drop table. + if _, err = db.ExecContext(ctx, `DROP TABLE TestQueryContext`); err != nil { + t.Error(err) + } +} + +func TestExecContextDml(t *testing.T) { + skipIfShort(t) + + // Open db. + ctx := context.Background() + db, err := sql.Open("spanner", dsn) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Set up test table. + _, err = db.ExecContext(ctx, + `CREATE TABLE TestExecContextDml ( + key INT64, + testString STRING(1024), + testBytes BYTES(1024), + testInt INT64, + testFloat FLOAT64, + testBool BOOL + ) PRIMARY KEY (key)`) + if err != nil { + t.Fatal(err) + } + + type testExecContextDmlRow struct { + key int + testString string + testBytes []byte + testInt int + testFloat float64 + testBool bool + } + + type then struct { + query string + wantRows []testExecContextDmlRow + wantError bool + } + + tests := []struct { + name string + given string + when string + then then + }{ + { + name: "insert single tuple", + when: ` + INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ) `, + then: then{ + query: `SELECT * FROM TestExecContextDml WHERE key = 1`, + wantRows: []testExecContextDmlRow{ + {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + { + name: "insert multiple tuples", + when: ` + INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (2, "two", CAST("two" as bytes), 42, 42, true ), (3, "three", CAST("three" as bytes), 42, 42, true )`, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + { + name: "insert syntax error", + when: ` + INSERT INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (101, "one", CAST("one" as bytes), 42, 42, true ) `, + then: then{ + wantError: true, + }, + }, + { + name: "insert lower case dml", + when: ` + insert into TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (4, "four", CAST("four" as bytes), 42, 42, true ) `, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 4, testString: "four", testBytes: []byte("four"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "insert lower case table name", + when: ` + INSERT INTO testexeccontextdml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (5, "five", CAST("five" as bytes), 42, 42, true ) `, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 5, testString: "five", testBytes: []byte("five"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "wrong types", + when: ` + INSERT INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (102, 56, 56, 42, hello, "i should be a bool" ) `, + then: then{ + wantError: true, + }, + }, + + { + name: "insert primary key duplicate", + when: ` + INSERT INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (1, "one", CAST("one" as bytes), 42, 42, true ) `, + then: then{ + wantError: true, + }, + }, + + { + name: "insert null into primary key", + when: ` + INSERT INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (null, "one", CAST("one" as bytes), 42, 42, true ) `, + then: then{ + wantError: true, + }, + }, + + { + name: "insert too many values", + when: ` + INSERT INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (null, "one", CAST("one" as bytes), 42, 42, true, 42 ) `, + then: then{ + wantError: true, + }, + }, + + { + name: "insert too few values", + when: ` + INSERT INSERT INTO TestExecContextDml + (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (null, "one", CAST("one" as bytes), 42 ) `, + then: then{ + wantError: true, + }, + }, + + { + name: "delete single tuple", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true )`, + when: `DELETE FROM TestExecContextDml WHERE key = 1`, + then: then{ + query: `SELECT * FROM TestExecContextDml`, + wantRows: []testExecContextDmlRow{ + {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "delete multiple tuples with subthen", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true ), (4, "four", CAST("four" as bytes), 42, 42, true )`, + when: ` + DELETE FROM TestExecContextDml WHERE key + IN (SELECT key FROM TestExecContextDml WHERE key != 4)`, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 4, testString: "four", testBytes: []byte("four"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "delete all tuples", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true ), (4, "four", CAST("four" as bytes), 42, 42, true )`, + when: `DELETE FROM TestExecContextDml WHERE true`, + then: then{ + query: `SELECT * FROM TestExecContextDml`, + wantRows: []testExecContextDmlRow{}, + }, + }, + { + name: "update one tuple", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true )`, + when: `UPDATE TestExecContextDml SET testSTring = "updated" WHERE key = 1`, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 1, testString: "updated", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, + {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + { + name: "update multiple tuples", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true )`, + when: `UPDATE TestExecContextDml SET testSTring = "updated" WHERE key = 2 OR key = 3`, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, + {key: 2, testString: "updated", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + {key: 3, testString: "updated", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "update primary key", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true )`, + when: `UPDATE TestExecContextDml SET key = 100 WHERE key = 1`, + then: then{ + wantError: true, + }, + }, + + { + name: "update nothing", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true )`, + when: `UPDATE TestExecContextDml SET testSTring = "nothing" WHERE false`, + then: then{ + query: `SELECT * FROM TestExecContextDml ORDER BY key`, + wantRows: []testExecContextDmlRow{ + {key: 1, testString: "one", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, + {key: 2, testString: "two", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + {key: 3, testString: "three", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + + { + name: "update everything", + given: ` + INSERT INTO TestExecContextDml (key, testString, testBytes, testInt, testFloat, testBool) + VALUES (1, "one", CAST("one" as bytes), 42, 42, true ), (2, "two", CAST("two" as bytes), 42, 42, true ), + (3, "three", CAST("three" as bytes), 42, 42, true )`, + when: `UPDATE TestExecContextDml SET testSTring = "everything" WHERE true `, + then: then{ + query: `SELECT * FROM TestExecContextDml WHERE testString = "everything"`, + wantRows: []testExecContextDmlRow{ + {key: 1, testString: "everything", testBytes: []byte("one"), testInt: 42, testFloat: 42, testBool: true}, + {key: 2, testString: "everything", testBytes: []byte("two"), testInt: 42, testFloat: 42, testBool: true}, + {key: 3, testString: "everything", testBytes: []byte("three"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + }, + { + name: "update to wrong type", + when: `UPDATE TestExecContextDml SET testBool = 100 WHERE key = 1`, + then: then{ + wantError: true, + }, + }, + } + + // Run tests. + for _, tc := range tests { + + // Set up test table. + if tc.given != "" { + if _, err = db.ExecContext(ctx, tc.given); err != nil { + t.Errorf("%s: error setting up table: %v", tc.name, err) + } + } + + // Run DML. + _, err = db.ExecContext(ctx, tc.when) + if (err != nil) && (!tc.then.wantError) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.then.wantError) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + + // Check rows returned. + if tc.then.query != "" { + rows, err := db.QueryContext(ctx, tc.then.query) + if err != nil { + t.Errorf("%s: error from QueryContext : %v", tc.name, err) + } + + got := []testExecContextDmlRow{} + for rows.Next() { + var curr testExecContextDmlRow + err := rows.Scan( + &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) + if err != nil { + t.Errorf("%s: error while scanning rows: %v", tc.name, err) + } + got = append(got, curr) + } + + rows.Close() + if err = rows.Err(); err != nil { + t.Errorf("%s: error on rows.close: %v", tc.name, err) + } + if !reflect.DeepEqual(tc.then.wantRows, got) { + t.Errorf("Unexpecred rows: %s. want: %v, got: %v", tc.name, tc.then.wantRows, got) + } + } + + // Clear table for next test. + _, err = db.ExecContext(ctx, `DELETE FROM TestExecContextDml WHERE true`) + if (err != nil) && (!tc.then.wantError) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + + } + + // Drop table. + if _, err = db.ExecContext(ctx, `DROP TABLE TestExecContextDml`); err != nil { + t.Error(err) + } + +} +func TestRowsAtomicTypes(t *testing.T) { + skipIfShort(t) + + // Open db. + ctx := context.Background() + db, err := sql.Open("spanner", dsn) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Set up test table. + _, err = db.ExecContext(ctx, + `CREATE TABLE TestAtomicTypes ( + key STRING(1024), + testString STRING(1024), + testBytes BYTES(1024), + testInt INT64, + testFloat FLOAT64, + testBool BOOL + ) PRIMARY KEY (key)`) + if err != nil { + t.Fatal(err) + } + + _, err = db.ExecContext(ctx, + `INSERT INTO TestAtomicTypes (key, testString, testBytes, testInt, testFloat, testBool) VALUES + ("general", "hello", CAST ("hello" as bytes), 42, 42, TRUE), + ("negative", "hello", CAST ("hello" as bytes), -42, -42, TRUE), + ("maxint", "hello", CAST ("hello" as bytes), 9223372036854775807 , 42, TRUE), + ("minint", "hello", CAST ("hello" as bytes), -9223372036854775808 , 42, TRUE), + ("nan", "hello" ,CAST("hello" as bytes), 42, CAST("nan" AS FLOAT64), TRUE), + ("pinf", "hello" ,CAST("hello" as bytes), 42, CAST("inf" AS FLOAT64), TRUE), + ("ninf", "hello" ,CAST("hello" as bytes), 42, CAST("-inf" AS FLOAT64), TRUE), + ("nullstring", null, CAST ("hello" as bytes), 42, 42, TRUE), + ("nullbytes", "hello", null, 42, 42, TRUE), + ("nullint", "hello", CAST ("hello" as bytes), null, 42, TRUE), + ("nullfloat", "hello", CAST ("hello" as bytes), 42, null, TRUE), + ("nullbool", "hello", CAST ("hello" as bytes), 42, 42, null) + `) + if err != nil { + t.Fatal(err) + } + + type testAtomicTypesRow struct { + key string + testString string + testBytes []byte + testInt int + testFloat float64 + testBool bool + } + + type wantError struct { + scan bool + close bool + query bool + } + + tests := []struct { + name string + input string + want []testAtomicTypesRow + wantError wantError + }{ + + { + name: "general read", + input: `SELECT * FROM TestAtomicTypes WHERE key = "general"`, + want: []testAtomicTypesRow{ + {key: "general", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + { + name: "negative int and float", + input: `SELECT * FROM TestAtomicTypes WHERE key = "negative"`, + want: []testAtomicTypesRow{ + {key: "negative", testString: "hello", testBytes: []byte("hello"), testInt: -42, testFloat: -42, testBool: true}, + }, + }, + { + name: "max int", + input: `SELECT * FROM TestAtomicTypes WHERE key = "maxint"`, + want: []testAtomicTypesRow{ + {key: "maxint", testString: "hello", testBytes: []byte("hello"), testInt: 9223372036854775807, testFloat: 42, testBool: true}, + }, + }, + { + name: "min int", + input: `SELECT * FROM TestAtomicTypes WHERE key = "minint"`, + want: []testAtomicTypesRow{ + {key: "minint", testString: "hello", testBytes: []byte("hello"), testInt: -9223372036854775808, testFloat: 42, testBool: true}, + }, + }, + { + name: "special float positive infinity", + input: `SELECT * FROM TestAtomicTypes WHERE key = "pinf"`, + want: []testAtomicTypesRow{ + {key: "pinf", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: math.Inf(1), testBool: true}, + }, + }, + { + name: "special float negative infinity", + input: `SELECT * FROM TestAtomicTypes WHERE key = "ninf"`, + want: []testAtomicTypesRow{ + {key: "ninf", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: math.Inf(-1), testBool: true}, + }, + }, + } + + // Run tests. + for _, tc := range tests { + + rows, err := db.QueryContext(ctx, tc.input) + if (err != nil) && (!tc.wantError.query) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.query) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + + got := []testAtomicTypesRow{} + for rows.Next() { + var curr testAtomicTypesRow + err := rows.Scan( + &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) + if (err != nil) && (!tc.wantError.scan) { + t.Errorf("%s: unexpected scan error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.scan) { + t.Errorf("%s: expected scan error but error was %v", tc.name, err) + } + + got = append(got, curr) + } + + rows.Close() + err = rows.Err() + if (err != nil) && (!tc.wantError.close) { + t.Errorf("%s: unexpected rows.Err error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.close) { + t.Errorf("%s: expected rows.Err error but error was %v", tc.name, err) + } + + if !reflect.DeepEqual(tc.want, got) { + t.Errorf("Unexpected rows: %s. want: %v, got: %v", tc.name, tc.want, got) + } + + } + + // Drop table. + _, err = db.ExecContext(ctx, `DROP TABLE TestAtomicTypes`) + if err != nil { + t.Fatal(err) + } + +} + +func TestRowsOverflowRead(t *testing.T) { + skipIfShort(t) + + // Open db. + ctx := context.Background() + db, err := sql.Open("spanner", dsn) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Set up test table. + _, err = db.ExecContext(ctx, + `CREATE TABLE TestOverflowRead ( + key STRING(1024), + testString STRING(1024), + testBytes BYTES(1024), + testInt INT64, + testFloat FLOAT64, + testBool BOOL + ) PRIMARY KEY (key)`) + if err != nil { + t.Fatal(err) + } + + _, err = db.ExecContext(ctx, + `INSERT INTO TestOverflowRead (key, testString, testBytes, testInt, testFloat, testBool) VALUES + ("general", "hello", CAST ("hello" as bytes), 42, 42, TRUE), + ("maxint", "hello", CAST ("hello" as bytes), 9223372036854775807 , 42, TRUE), + ("minint", "hello", CAST ("hello" as bytes), -9223372036854775808 , 42, TRUE), + ("max int8", "hello", CAST ("hello" as bytes), 127 , 42, TRUE), + ("max uint8", "hello", CAST ("hello" as bytes), 255 , 42, TRUE) + `) + if err != nil { + t.Fatal(err) + } + + type testAtomicTypesRowSmallInt struct { + key string + testString string + testBytes []byte + testInt int8 + testFloat float64 + testBool bool + } + + type wantError struct { + scan bool + close bool + query bool + } + + tests := []struct { + name string + input string + want []testAtomicTypesRowSmallInt + wantError wantError + }{ + { + name: "read int64 into int8 ok", + input: `SELECT * FROM TestOverflowRead WHERE key = "general"`, + want: []testAtomicTypesRowSmallInt{ + {key: "general", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: 42, testBool: true}, + }, + }, + { + name: "read int64 into int8 overflow", + input: `SELECT * FROM TestOverflowRead WHERE key = "maxint"`, + wantError: wantError{scan: true}, + want: []testAtomicTypesRowSmallInt{ + {key: "maxint", testString: "hello", testBytes: []byte("hello"), testInt: 0, testFloat: 0, testBool: false}, + }, + }, + { + name: "read max int8 ", + input: `SELECT * FROM TestOverflowRead WHERE key = "max int8"`, + want: []testAtomicTypesRowSmallInt{ + {key: "max int8", testString: "hello", testBytes: []byte("hello"), testInt: 127, testFloat: 42, testBool: true}, + }, + }, + { + name: "read max uint8 into int8", + input: `SELECT * FROM TestOverflowRead WHERE key = "max uint8"`, + wantError: wantError{scan: true}, + want: []testAtomicTypesRowSmallInt{ + {key: "max uint8", testString: "hello", testBytes: []byte("hello"), testInt: 0, testFloat: 0, testBool: false}, + }, + }, + } + + // Run tests. + for _, tc := range tests { + + rows, err := db.QueryContext(ctx, tc.input) + if (err != nil) && (!tc.wantError.query) { + t.Errorf("%s: unexpected query error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.query) { + t.Errorf("%s: expected query error but error was %v", tc.name, err) + } + + got := []testAtomicTypesRowSmallInt{} + for rows.Next() { + var curr testAtomicTypesRowSmallInt + err := rows.Scan( + &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) + if (err != nil) && (!tc.wantError.scan) { + t.Errorf("%s: unexpected scan error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.scan) { + t.Errorf("%s: expected scan error but error was %v", tc.name, err) + } + + got = append(got, curr) + } + + rows.Close() + err = rows.Err() + if (err != nil) && (!tc.wantError.close) { + t.Errorf("%s: unexpected rows.Err error: %v", tc.name, err) + } + if (err == nil) && (tc.wantError.close) { + t.Errorf("%s: expected rows.Err error but error was %v", tc.name, err) + } + + if !reflect.DeepEqual(tc.want, got) { + t.Errorf("%s: unexpected rows. want: %v, got: %v", tc.name, tc.want, got) + } + + } + + // Drop table. + _, err = db.ExecContext(ctx, `DROP TABLE TestOverflowRead`) + if err != nil { + t.Fatal(err) + } + +} diff --git a/internal/namedargs.go b/internal/namedargs.go deleted file mode 100644 index 24091606..00000000 --- a/internal/namedargs.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2020 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal - -import ( - "fmt" - "regexp" -) - -var namedValueParamNameRegex = regexp.MustCompile("@(\\w+)") - -func NamedValueParamNames(q string, n int) ([]string, error) { - var names []string - matches := namedValueParamNameRegex.FindAllStringSubmatch(q, n) - if m := len(matches); n != -1 && m < n { - return nil, fmt.Errorf("query has %d placeholders but %d arguments are provided", m, n) - } - for _, m := range matches { - names = append(names, m[1]) - } - return names, nil -} diff --git a/internal/rwtransaction.go b/internal/rwtransaction.go deleted file mode 100644 index 5fbfb6ac..00000000 --- a/internal/rwtransaction.go +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2020 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal - -import ( - "context" - "errors" - - "cloud.google.com/go/spanner" -) - -// RWConnector starts a Cloud Spanner read-write -// transaction and provides blocking APIs to -// query, exec, rollback and commit. -type RWConnector struct { - QueryIn chan *RWQueryMessage - QueryOut chan *RWQueryMessage - - ExecIn chan *RWExecMessage - ExecOut chan *RWExecMessage - - RollbackIn chan struct{} - CommitIn chan struct{} - Errors chan error // only for starting, commit and rollback - - Ready chan struct{} -} - -func NewRWConnector(ctx context.Context, c *spanner.Client) *RWConnector { - connector := &RWConnector{ - QueryIn: make(chan *RWQueryMessage), - QueryOut: make(chan *RWQueryMessage), - ExecIn: make(chan *RWExecMessage), - ExecOut: make(chan *RWExecMessage), - RollbackIn: make(chan struct{}), - CommitIn: make(chan struct{}), - Errors: make(chan error), - Ready: make(chan struct{}), - } - - fn := func(ctx context.Context, tx *spanner.ReadWriteTransaction) error { - connector.Ready <- struct{}{} - for { - select { - case <-ctx.Done(): - return ctx.Err() - case msg := <-connector.QueryIn: - msg.It = tx.Query(msg.Ctx, msg.Stmt) - connector.QueryOut <- msg - case msg := <-connector.ExecIn: - msg.Rows, msg.Error = tx.Update(msg.Ctx, msg.Stmt) - connector.ExecOut <- msg - case <-connector.RollbackIn: - return ErrAborted - case <-connector.CommitIn: - return nil - } - } - } - go func() { - _, err := c.ReadWriteTransaction(ctx, fn) - connector.Errors <- err - }() - return connector -} - -type RWQueryMessage struct { - Ctx context.Context // in - Stmt spanner.Statement // in - - It *spanner.RowIterator // out -} - -type RWExecMessage struct { - Ctx context.Context // in - Stmt spanner.Statement // in - - Rows int64 // out - Error error // out -} - -var ErrAborted = errors.New("aborted") diff --git a/internal/statement_parser.go b/internal/statement_parser.go new file mode 100644 index 00000000..6843c715 --- /dev/null +++ b/internal/statement_parser.go @@ -0,0 +1,270 @@ +// Copyright 2021 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal + +import ( + "fmt" + "strings" + "unicode" +) + +var ddlStatements = map[string]bool{"CREATE": true, "DROP": true, "ALTER": true} +var selectStatements = map[string]bool{"SELECT": true, "WITH": true} +var dmlStatements = map[string]bool{"INSERT": true, "UPDATE": true, "DELETE": true} +var selectAndDmlStatements = union(selectStatements, dmlStatements) + +func union(m1 map[string]bool, m2 map[string]bool) map[string]bool { + res := make(map[string]bool, len(m1)+len(m2)) + for k, v := range m1 { + res[k] = v + } + for k, v := range m2 { + res[k] = v + } + return res +} + +// ParseNamedParameters returns the named parameters in the given sql string. +// The sql string must be a valid Cloud Spanner sql statement. It may contain +// comments and (string) literals without any restrictions. That is, string +// literals containing for example an email address ('test@test.com') will be +// recognized as a string literal and not returned as a named parameter. +func ParseNamedParameters(sql string) ([]string, error) { + sql, err := removeCommentsAndTrim(sql) + if err != nil { + return nil, err + } + sql = removeStatementHint(sql) + return findParams(sql) +} + +// RemoveCommentsAndTrim removes any comments in the query string and trims any +// spaces at the beginning and end of the query. This makes checking what type +// of query a string is a lot easier, as only the first word(s) need to be +// checked after this has been removed. +func removeCommentsAndTrim(sql string) (string, error) { + const singleQuote = '\'' + const doubleQuote = '"' + const backtick = '`' + const hyphen = '-' + const dash = '#' + const slash = '/' + const asterisk = '*' + isInQuoted := false + isInSingleLineComment := false + isInMultiLineComment := false + var startQuote rune + lastCharWasEscapeChar := false + isTripleQuoted := false + res := strings.Builder{} + res.Grow(len(sql)) + index := 0 + runes := []rune(sql) + for index < len(runes) { + c := runes[index] + if isInQuoted { + if (c == '\n' || c == '\r') && !isTripleQuoted { + return "", fmt.Errorf("statement contains an unclosed literal: %s", sql) + } else if c == startQuote { + if lastCharWasEscapeChar { + lastCharWasEscapeChar = false + } else if isTripleQuoted { + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { + isInQuoted = false + startQuote = 0 + isTripleQuoted = false + res.WriteRune(c) + res.WriteRune(c) + index += 2 + } + } else { + isInQuoted = false + startQuote = 0 + } + } else if c == '\\' { + lastCharWasEscapeChar = true + } else { + lastCharWasEscapeChar = false + } + res.WriteRune(c) + } else { + // We are not in a quoted string. + if isInSingleLineComment { + if c == '\n' { + isInSingleLineComment = false + // Include the line feed in the result. + res.WriteRune(c) + } + } else if isInMultiLineComment { + if len(runes) > index+1 && c == asterisk && runes[index+1] == slash { + isInMultiLineComment = false + index++ + } + } else { + if c == dash || (len(runes) > index+1 && c == hyphen && runes[index+1] == hyphen) { + // This is a single line comment. + isInSingleLineComment = true + } else if len(runes) > index+1 && c == slash && runes[index+1] == asterisk { + isInMultiLineComment = true + index++ + } else { + if c == singleQuote || c == doubleQuote || c == backtick { + isInQuoted = true + startQuote = c + // Check whether it is a triple-quote. + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { + isTripleQuoted = true + res.WriteRune(c) + res.WriteRune(c) + index += 2 + } + } + res.WriteRune(c) + } + } + } + index++ + } + if isInQuoted { + return "", fmt.Errorf("statement contains an unclosed literal: %s", sql) + } + trimmed := strings.TrimSpace(res.String()) + if len(trimmed) > 0 && trimmed[len(trimmed)-1] == ';' { + return trimmed[:len(trimmed)-1], nil + } + return trimmed, nil +} + +// Removes any statement hints at the beginning of the statement. +// It assumes that any comments have already been removed. +func removeStatementHint(sql string) string { + // Valid statement hints at the beginning of a query statement can only contain a fixed set of + // possible values. Although it is possible to add a @{FORCE_INDEX=...} as a statement hint, the + // only allowed value is _BASE_TABLE. This means that we can safely assume that the statement + // hint will not contain any special characters, for example a closing curly brace or one of the + // keywords SELECT, UPDATE, DELETE, WITH, and that we can keep the check simple by just + // searching for the first occurrence of a keyword that should be preceded by a closing curly + // brace at the end of the statement hint. + startStatementHintIndex := strings.Index(sql, "{") + // Statement hints are allowed for both queries and DML statements. + startQueryIndex := -1 + upperCaseSql := strings.ToUpper(sql) + for keyword := range selectAndDmlStatements { + if startQueryIndex = strings.Index(upperCaseSql, keyword); startQueryIndex > -1 { + break + } + } + if startQueryIndex > -1 { + endStatementHintIndex := strings.LastIndex(sql[:startQueryIndex], "}") + if startStatementHintIndex == -1 || startStatementHintIndex > endStatementHintIndex { + // Looks like an invalid statement hint. Just ignore at this point + // and let the caller handle the invalid query. + return sql + } + return strings.TrimSpace(sql[endStatementHintIndex+1:]) + } + // Seems invalid, just return the original statement. + return sql +} + +// This function assumes that all comments and statement hints have already +// been removed from the statement. +func findParams(sql string) ([]string, error) { + const paramPrefix = '@' + const singleQuote = '\'' + const doubleQuote = '"' + const backtick = '`' + isInQuoted := false + var startQuote rune + lastCharWasEscapeChar := false + isTripleQuoted := false + res := make([]string, 0) + index := 0 + runes := []rune(sql) + for index < len(runes) { + c := runes[index] + if isInQuoted { + if (c == '\n' || c == '\r') && !isTripleQuoted { + return nil, fmt.Errorf("statement contains an unclosed literal: %s", sql) + } else if c == startQuote { + if lastCharWasEscapeChar { + lastCharWasEscapeChar = false + } else if isTripleQuoted { + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { + isInQuoted = false + startQuote = 0 + isTripleQuoted = false + index += 2 + } + } else { + isInQuoted = false + startQuote = 0 + } + } else if c == '\\' { + lastCharWasEscapeChar = true + } else { + lastCharWasEscapeChar = false + } + } else { + // We are not in a quoted string. + if c == paramPrefix && len(runes) > index+1 && !unicode.IsSpace(runes[index+1]) { + index++ + startIndex := index + for index < len(runes) { + if unicode.IsSpace(runes[index]) { + res = append(res, string(runes[startIndex:index])) + break + } + if index == len(runes)-1 { + res = append(res, string(runes[startIndex:])) + break + } + index++ + } + } else { + if c == singleQuote || c == doubleQuote || c == backtick { + isInQuoted = true + startQuote = c + // Check whether it is a triple-quote. + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { + isTripleQuoted = true + index += 2 + } + } + } + } + index++ + } + if isInQuoted { + return nil, fmt.Errorf("statement contains an unclosed literal: %s", sql) + } + return res, nil +} + +func IsDdl(query string) (bool, error) { + query, err := removeCommentsAndTrim(query) + if err != nil { + return false, err + } + // We can safely check if the string starts with a specific string, as we + // have already removed all leading spaces, and there are no keywords that + // start with the same substring as one of the DDL keywords. + for ddl := range ddlStatements { + if strings.EqualFold(query[:len(ddl)], ddl) { + return true, nil + } + } + return false, nil +} diff --git a/internal/statement_parser_test.go b/internal/statement_parser_test.go new file mode 100644 index 00000000..7d53ef7c --- /dev/null +++ b/internal/statement_parser_test.go @@ -0,0 +1,658 @@ +// Copyright 2021 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestRemoveCommentsAndTrim(t *testing.T) { + tests := []struct { + input string + want string + wantErr bool + }{ + { + input: ``, + want: ``, + }, + { + input: `SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `-- This is a single line comment +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `# This is a single line comment +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/* This is a multi line comment on one line */ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/* This +is +a +multiline +comment +*/ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/* This +* is +* a +* multiline +* comment +*/ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/** This is a javadoc style comment on one line */ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/** This +is +a +javadoc +style +comment +on +multiple +lines +*/ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `/** This +* is +* a +* javadoc +* style +* comment +* on +* multiple +* lines +*/ +SELECT 1;`, + want: `SELECT 1`, + }, + { + input: `-- First comment +SELECT--second comment +1`, + want: `SELECT +1`, + }, + { + input: `# First comment +SELECT#second comment +1`, + want: `SELECT +1`, + }, + { + input: `-- First comment +SELECT--second comment +1--third comment`, + want: `SELECT +1`, + }, + { + input: `# First comment +SELECT#second comment +1#third comment`, + want: `SELECT +1`, + }, + { + input: `/* First comment */ +SELECT/* second comment */ +1`, + want: `SELECT +1`, + }, + { + input: `/* First comment */ +SELECT/* second comment */ +1/* third comment */`, + want: `SELECT +1`, + }, + { + input: `SELECT "TEST -- This is not a comment"`, + want: `SELECT "TEST -- This is not a comment"`, + }, + { + input: `-- This is a comment +SELECT "TEST -- This is not a comment"`, + want: `SELECT "TEST -- This is not a comment"`, + }, + { + input: `-- This is a comment +SELECT "TEST -- This is not a comment" -- This is a comment`, + want: `SELECT "TEST -- This is not a comment"`, + }, + { + input: `SELECT "TEST # This is not a comment"`, + want: `SELECT "TEST # This is not a comment"`, + }, + { + input: `# This is a comment +SELECT "TEST # This is not a comment"`, + want: `SELECT "TEST # This is not a comment"`, + }, + { + input: `# This is a comment +SELECT "TEST # This is not a comment" # This is a comment`, + want: `SELECT "TEST # This is not a comment"`, + }, + { + input: `SELECT "TEST /* This is not a comment */"`, + want: `SELECT "TEST /* This is not a comment */"`, + }, + { + input: `/* This is a comment */ +SELECT "TEST /* This is not a comment */"`, + want: `SELECT "TEST /* This is not a comment */"`, + }, + { + input: `/* This is a comment */ +SELECT "TEST /* This is not a comment */" /* This is a comment */`, + want: `SELECT "TEST /* This is not a comment */"`, + }, + { + input: `SELECT 'TEST -- This is not a comment'`, + want: `SELECT 'TEST -- This is not a comment'`, + }, + { + input: `-- This is a comment +SELECT 'TEST -- This is not a comment'`, + want: `SELECT 'TEST -- This is not a comment'`, + }, + { + input: `-- This is a comment +SELECT 'TEST -- This is not a comment' -- This is a comment`, + want: `SELECT 'TEST -- This is not a comment'`, + }, + { + input: `SELECT 'TEST # This is not a comment'`, + want: `SELECT 'TEST # This is not a comment'`, + }, + { + input: `# This is a comment +SELECT 'TEST # This is not a comment'`, + want: `SELECT 'TEST # This is not a comment'`, + }, + { + input: `# This is a comment +SELECT 'TEST # This is not a comment' # This is a comment`, + want: `SELECT 'TEST # This is not a comment'`, + }, + { + input: `SELECT 'TEST /* This is not a comment */'`, + want: `SELECT 'TEST /* This is not a comment */'`, + }, + { + input: `/* This is a comment */ +SELECT 'TEST /* This is not a comment */'`, + want: `SELECT 'TEST /* This is not a comment */'`, + }, + { + input: `/* This is a comment */ +SELECT 'TEST /* This is not a comment */' /* This is a comment */`, + want: `SELECT 'TEST /* This is not a comment */'`, + }, + { + input: `SELECT '''TEST +-- This is not a comment +'''`, + want: `SELECT '''TEST +-- This is not a comment +'''`, + }, + { + input: ` -- This is a comment +SELECT '''TEST +-- This is not a comment +''' -- This is a comment`, + want: `SELECT '''TEST +-- This is not a comment +'''`, + }, + { + input: `SELECT '''TEST +# This is not a comment +'''`, + want: `SELECT '''TEST +# This is not a comment +'''`, + }, + { + input: ` # This is a comment +SELECT '''TEST +# This is not a comment +''' # This is a comment`, + want: `SELECT '''TEST +# This is not a comment +'''`, + }, + { + input: `SELECT '''TEST +/* This is not a comment */ +'''`, + want: `SELECT '''TEST +/* This is not a comment */ +'''`, + }, + { + input: ` /* This is a comment */ +SELECT '''TEST +/* This is not a comment */ +''' /* This is a comment */`, + want: `SELECT '''TEST +/* This is not a comment */ +'''`, + }, + { + input: `SELECT """TEST +-- This is not a comment +"""`, + want: `SELECT """TEST +-- This is not a comment +"""`, + }, + { + input: ` -- This is a comment +SELECT """TEST +-- This is not a comment +""" -- This is a comment`, + want: `SELECT """TEST +-- This is not a comment +"""`, + }, + { + input: `SELECT """TEST +# This is not a comment +"""`, + want: `SELECT """TEST +# This is not a comment +"""`, + }, + { + input: ` # This is a comment +SELECT """TEST +# This is not a comment +""" # This is a comment`, + want: `SELECT """TEST +# This is not a comment +"""`, + }, + { + input: `SELECT """TEST +/* This is not a comment */ +"""`, + want: `SELECT """TEST +/* This is not a comment */ +"""`, + }, + { + input: ` /* This is a comment */ +SELECT """TEST +/* This is not a comment */ +""" /* This is a comment */`, + want: `SELECT """TEST +/* This is not a comment */ +"""`, + }, + { + input: `/* This is a comment /* this is still a comment */ +SELECT 1`, + want: `SELECT 1`, + }, + { + input: `/** This is a javadoc style comment /* this is still a comment */ +SELECT 1`, + want: `SELECT 1`, + }, + { + input: `/** This is a javadoc style comment /** this is still a comment */ +SELECT 1`, + want: `SELECT 1`, + }, + { + input: `/** This is a javadoc style comment /** this is still a comment **/ +SELECT 1`, + want: `SELECT 1`, + }, + } + for _, tc := range tests { + got, err := removeCommentsAndTrim(tc.input) + if err != nil && !tc.wantErr { + t.Error(err) + continue + } + if tc.wantErr { + t.Errorf("missing expected error for %q", tc.input) + continue + } + if got != tc.want { + t.Errorf("removeCommentsAndTrim result mismatch\nGot: %q\nWant: %q", got, tc.want) + } + } +} + +func TestRemoveStatementHint(t *testing.T) { + tests := []struct { + input string + want string + }{ + { + input: `@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@ {JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{ JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN } SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN} +SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{ +JOIN_METHOD = HASH_JOIN } + SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN} +-- Single line comment +SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN} +/* Multi line comment +with more comments +*/SELECT * FROM PersonsTable`, + want: `SELECT * FROM PersonsTable`, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN} WITH subQ1 AS (SELECT SchoolID FROM Roster), + subQ2 AS (SELECT OpponentID FROM PlayerStats) +SELECT * FROM subQ1 +UNION ALL +SELECT * FROM subQ2`, + want: `WITH subQ1 AS (SELECT SchoolID FROM Roster), + subQ2 AS (SELECT OpponentID FROM PlayerStats) +SELECT * FROM subQ1 +UNION ALL +SELECT * FROM subQ2`, + }, + // Multiple query hints. + { + input: `@{FORCE_INDEX=index_name} @{JOIN_METHOD=HASH_JOIN} SELECT * FROM tbl`, + want: `SELECT * FROM tbl`, + }, + { + input: `@{FORCE_INDEX=index_name} +@{JOIN_METHOD=HASH_JOIN} +SELECT SchoolID FROM Roster`, + want: `SELECT SchoolID FROM Roster`, + }, + // Invalid query hints. + { + input: `@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable`, + want: `@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable`, + }, + { + input: `@JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable`, + want: `@JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable`, + }, + { + input: `@JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable`, + want: `@JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable`, + }, + } + for _, tc := range tests { + sql, err := removeCommentsAndTrim(tc.input) + if err != nil { + t.Fatal(err) + } + got := removeStatementHint(sql) + if got != tc.want { + t.Errorf("removeStatementHint result mismatch\nGot: %q\nWant: %q", got, tc.want) + } + } +} + +func TestFindParams(t *testing.T) { + tests := []struct { + input string + want []string + wantErr bool + }{ + { + input: `SELECT * FROM PersonsTable WHERE id=@id`, + want: []string{"id"}, + }, + { + input: `SELECT * FROM PersonsTable WHERE id=@id AND name=@name`, + want: []string{"id", "name"}, + }, + { + input: `SELECT * FROM PersonsTable WHERE Name like @name AND Email='test@test.com'`, + want: []string{"name"}, + }, + { + input: `SELECT * FROM """strange + @table +""" WHERE Name like @name AND Email='test@test.com'`, + want: []string{"name"}, + }, + { + input: `@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable WHERE Name like @name AND Email='test@test.com'`, + want: []string{"name"}, + }, + } + for _, tc := range tests { + sql, err := removeCommentsAndTrim(tc.input) + if err != nil { + t.Fatal(err) + } + got, err := ParseNamedParameters(removeStatementHint(sql)) + if err != nil && !tc.wantErr { + t.Error(err) + continue + } + if tc.wantErr { + t.Errorf("missing expected error for %q", tc.input) + continue + } + if !cmp.Equal(got, tc.want) { + t.Errorf("ParseNamedParameters result mismatch\nGot: %s\nWant: %s", got, tc.want) + } + } +} + +// note: isDdl function does not check validity of statement +// just that the statement begins with a DDL instruction. +// Other checking performed by database. +func TestIsDdl(t *testing.T) { + tests := []struct { + name string + input string + want bool + }{ + { + name: "valid create", + input: `CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading spaces", + input: ` CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading newlines", + input: ` + + + CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading tabs", + input: ` CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading whitespace, miscellaneous", + input: ` + + CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "lower case", + input: `create table Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "mixed case, leading whitespace", + input: ` + cREAte taBLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "insert (not ddl)", + input: `INSERT INTO Valid`, + want: false, + }, + { + name: "delete (not ddl)", + input: `DELETE FROM Valid`, + want: false, + }, + { + name: "update (not ddl)", + input: `UPDATE Valid`, + want: false, + }, + { + name: "drop", + input: `DROP TABLE Valid`, + want: true, + }, + { + name: "alter", + input: `alter TABLE Valid`, + want: true, + }, + { + name: "typo (ccreate)", + input: `cCREATE TABLE Valid`, + want: false, + }, + { + name: "typo (reate)", + input: `REATE TABLE Valid`, + want: false, + }, + { + name: "typo (rx ceate)", + input: `x CREATE TABLE Valid`, + want: false, + }, + { + name: "leading int", + input: `0CREATE TABLE Valid`, + want: false, + }, + { + name: "leading single line comment", + input: `-- Create the Valid table + CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading single line comment using hash", + input: `# Create the Valid table + CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + { + name: "leading multi line comment", + input: `/* Create the Valid table. + This comment will be stripped before the statement is parsed. */ + CREATE TABLE Valid ( + A STRING(1024) + ) PRIMARY KEY (A)`, + want: true, + }, + } + + for _, tc := range tests { + got, err := IsDdl(tc.input) + if err != nil { + t.Error(err) + } + if got != tc.want { + t.Errorf("isDdl test failed, %s: wanted %t got %t.", tc.name, tc.want, got) + } + } +} diff --git a/rows.go b/rows.go index 1e56b8f1..fdfa0307 100644 --- a/rows.go +++ b/rows.go @@ -17,7 +17,6 @@ package spannerdriver import ( "database/sql/driver" "io" - "log" "sync" "time" @@ -30,6 +29,7 @@ type rows struct { it *spanner.RowIterator colsOnce sync.Once + dirtyErr error cols []string dirtyRow *spanner.Row @@ -53,12 +53,19 @@ func (r *rows) Close() error { func (r *rows) getColumns() { r.colsOnce.Do(func() { row, err := r.it.Next() - if err != nil { - log.Println(err) - return + if err == nil { + r.dirtyRow = row + } else { + r.dirtyErr = err + if err != iterator.Done { + return + } + } + rowType := r.it.Metadata.RowType + r.cols = make([]string, len(rowType.Fields)) + for i, c := range rowType.Fields { + r.cols[i] = c.Name } - r.dirtyRow = row - r.cols = row.ColumnNames() }) } @@ -77,6 +84,13 @@ func (r *rows) Next(dest []driver.Value) error { if r.dirtyRow != nil { row = r.dirtyRow r.dirtyRow = nil + } else if r.dirtyErr != nil { + err := r.dirtyErr + r.dirtyErr = nil + if err == iterator.Done { + return io.EOF + } + return err } else { var err error row, err = r.it.Next() // returns io.EOF when there is no next @@ -106,6 +120,12 @@ func (r *rows) Next(dest []driver.Value) error { return err } dest[i] = v.Float64 + case sppb.TypeCode_NUMERIC: + var v spanner.NullNumeric + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v.Numeric case sppb.TypeCode_STRING: var v spanner.NullString if err := col.Decode(&v); err != nil { @@ -133,7 +153,7 @@ func (r *rows) Next(dest []driver.Value) error { if v.IsNull() { dest[i] = v.Date // typed nil } else { - dest[i] = v.Date.In(time.Local) // TODO(jbd): Add note about this. + dest[i] = v.Date.In(time.UTC) // TODO(jbd): Add note about this. } case sppb.TypeCode_TIMESTAMP: var v spanner.NullTime diff --git a/rows_test.go b/rows_test.go index 16c0552e..4d2472fe 100644 --- a/rows_test.go +++ b/rows_test.go @@ -13,307 +13,3 @@ // limitations under the License. package spannerdriver - -import ( - "context" - "database/sql" - "math" - "reflect" - "testing" -) - -func TestRowsAtomicTypes(t *testing.T) { - - // Open db. - ctx := context.Background() - db, err := sql.Open("spanner", dsn) - if err != nil { - t.Fatal(err) - } - defer db.Close() - - // Set up test table. - _, err = db.ExecContext(ctx, - `CREATE TABLE TestAtomicTypes ( - key STRING(1024), - testString STRING(1024), - testBytes BYTES(1024), - testInt INT64, - testFloat FLOAT64, - testBool BOOL - ) PRIMARY KEY (key)`) - if err != nil { - t.Fatal(err) - } - - _, err = db.ExecContext(ctx, - `INSERT INTO TestAtomicTypes (key, testString, testBytes, testInt, testFloat, testBool) VALUES - ("general", "hello", CAST ("hello" as bytes), 42, 42, TRUE), - ("negative", "hello", CAST ("hello" as bytes), -42, -42, TRUE), - ("maxint", "hello", CAST ("hello" as bytes), 9223372036854775807 , 42, TRUE), - ("minint", "hello", CAST ("hello" as bytes), -9223372036854775808 , 42, TRUE), - ("nan", "hello" ,CAST("hello" as bytes), 42, CAST("nan" AS FLOAT64), TRUE), - ("pinf", "hello" ,CAST("hello" as bytes), 42, CAST("inf" AS FLOAT64), TRUE), - ("ninf", "hello" ,CAST("hello" as bytes), 42, CAST("-inf" AS FLOAT64), TRUE), - ("nullstring", null, CAST ("hello" as bytes), 42, 42, TRUE), - ("nullbytes", "hello", null, 42, 42, TRUE), - ("nullint", "hello", CAST ("hello" as bytes), null, 42, TRUE), - ("nullfloat", "hello", CAST ("hello" as bytes), 42, null, TRUE), - ("nullbool", "hello", CAST ("hello" as bytes), 42, 42, null) - `) - if err != nil { - t.Fatal(err) - } - - type testAtomicTypesRow struct { - key string - testString string - testBytes []byte - testInt int - testFloat float64 - testBool bool - } - - type wantError struct { - scan bool - close bool - query bool - } - - tests := []struct { - name string - input string - want []testAtomicTypesRow - wantError wantError - }{ - - { - name: "general read", - input: `SELECT * FROM TestAtomicTypes WHERE key = "general"`, - want: []testAtomicTypesRow{ - {key: "general", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - { - name: "negative int and float", - input: `SELECT * FROM TestAtomicTypes WHERE key = "negative"`, - want: []testAtomicTypesRow{ - {key: "negative", testString: "hello", testBytes: []byte("hello"), testInt: -42, testFloat: -42, testBool: true}, - }, - }, - { - name: "max int", - input: `SELECT * FROM TestAtomicTypes WHERE key = "maxint"`, - want: []testAtomicTypesRow{ - {key: "maxint", testString: "hello", testBytes: []byte("hello"), testInt: 9223372036854775807, testFloat: 42, testBool: true}, - }, - }, - { - name: "min int", - input: `SELECT * FROM TestAtomicTypes WHERE key = "minint"`, - want: []testAtomicTypesRow{ - {key: "minint", testString: "hello", testBytes: []byte("hello"), testInt: -9223372036854775808, testFloat: 42, testBool: true}, - }, - }, - { - name: "special float positive infinity", - input: `SELECT * FROM TestAtomicTypes WHERE key = "pinf"`, - want: []testAtomicTypesRow{ - {key: "pinf", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: math.Inf(1), testBool: true}, - }, - }, - { - name: "special float negative infinity", - input: `SELECT * FROM TestAtomicTypes WHERE key = "ninf"`, - want: []testAtomicTypesRow{ - {key: "ninf", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: math.Inf(-1), testBool: true}, - }, - }, - } - - // Run tests. - for _, tc := range tests { - - rows, err := db.QueryContext(ctx, tc.input) - if (err != nil) && (!tc.wantError.query) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.query) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - - got := []testAtomicTypesRow{} - for rows.Next() { - var curr testAtomicTypesRow - err := rows.Scan( - &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) - if (err != nil) && (!tc.wantError.scan) { - t.Errorf("%s: unexpected scan error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.scan) { - t.Errorf("%s: expected scan error but error was %v", tc.name, err) - } - - got = append(got, curr) - } - - rows.Close() - err = rows.Err() - if (err != nil) && (!tc.wantError.close) { - t.Errorf("%s: unexpected rows.Err error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.close) { - t.Errorf("%s: expected rows.Err error but error was %v", tc.name, err) - } - - if !reflect.DeepEqual(tc.want, got) { - t.Errorf("Unexpected rows: %s. want: %v, got: %v", tc.name, tc.want, got) - } - - } - - // Drop table. - _, err = db.ExecContext(ctx, `DROP TABLE TestAtomicTypes`) - if err != nil { - t.Fatal(err) - } - -} - -func TestRowsOverflowRead(t *testing.T) { - - // Open db. - ctx := context.Background() - db, err := sql.Open("spanner", dsn) - if err != nil { - t.Fatal(err) - } - defer db.Close() - - // Set up test table. - _, err = db.ExecContext(ctx, - `CREATE TABLE TestOverflowRead ( - key STRING(1024), - testString STRING(1024), - testBytes BYTES(1024), - testInt INT64, - testFloat FLOAT64, - testBool BOOL - ) PRIMARY KEY (key)`) - if err != nil { - t.Fatal(err) - } - - _, err = db.ExecContext(ctx, - `INSERT INTO TestOverflowRead (key, testString, testBytes, testInt, testFloat, testBool) VALUES - ("general", "hello", CAST ("hello" as bytes), 42, 42, TRUE), - ("maxint", "hello", CAST ("hello" as bytes), 9223372036854775807 , 42, TRUE), - ("minint", "hello", CAST ("hello" as bytes), -9223372036854775808 , 42, TRUE), - ("max int8", "hello", CAST ("hello" as bytes), 127 , 42, TRUE), - ("max uint8", "hello", CAST ("hello" as bytes), 255 , 42, TRUE) - `) - if err != nil { - t.Fatal(err) - } - - type testAtomicTypesRowSmallInt struct { - key string - testString string - testBytes []byte - testInt int8 - testFloat float64 - testBool bool - } - - type wantError struct { - scan bool - close bool - query bool - } - - tests := []struct { - name string - input string - want []testAtomicTypesRowSmallInt - wantError wantError - }{ - { - name: "read int64 into int8 ok", - input: `SELECT * FROM TestOverflowRead WHERE key = "general"`, - want: []testAtomicTypesRowSmallInt{ - {key: "general", testString: "hello", testBytes: []byte("hello"), testInt: 42, testFloat: 42, testBool: true}, - }, - }, - { - name: "read int64 into int8 overflow", - input: `SELECT * FROM TestOverflowRead WHERE key = "maxint"`, - wantError: wantError{scan: true}, - want: []testAtomicTypesRowSmallInt{ - {key: "maxint", testString: "hello", testBytes: []byte("hello"), testInt: 0, testFloat: 0, testBool: false}, - }, - }, - { - name: "read max int8 ", - input: `SELECT * FROM TestOverflowRead WHERE key = "max int8"`, - want: []testAtomicTypesRowSmallInt{ - {key: "max int8", testString: "hello", testBytes: []byte("hello"), testInt: 127, testFloat: 42, testBool: true}, - }, - }, - { - name: "read max uint8 into int8", - input: `SELECT * FROM TestOverflowRead WHERE key = "max uint8"`, - wantError: wantError{scan: true}, - want: []testAtomicTypesRowSmallInt{ - {key: "max uint8", testString: "hello", testBytes: []byte("hello"), testInt: 0, testFloat: 0, testBool: false}, - }, - }, - } - - // Run tests. - for _, tc := range tests { - - rows, err := db.QueryContext(ctx, tc.input) - if (err != nil) && (!tc.wantError.query) { - t.Errorf("%s: unexpected query error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.query) { - t.Errorf("%s: expected query error but error was %v", tc.name, err) - } - - got := []testAtomicTypesRowSmallInt{} - for rows.Next() { - var curr testAtomicTypesRowSmallInt - err := rows.Scan( - &curr.key, &curr.testString, &curr.testBytes, &curr.testInt, &curr.testFloat, &curr.testBool) - if (err != nil) && (!tc.wantError.scan) { - t.Errorf("%s: unexpected scan error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.scan) { - t.Errorf("%s: expected scan error but error was %v", tc.name, err) - } - - got = append(got, curr) - } - - rows.Close() - err = rows.Err() - if (err != nil) && (!tc.wantError.close) { - t.Errorf("%s: unexpected rows.Err error: %v", tc.name, err) - } - if (err == nil) && (tc.wantError.close) { - t.Errorf("%s: expected rows.Err error but error was %v", tc.name, err) - } - - if !reflect.DeepEqual(tc.want, got) { - t.Errorf("%s: unexpected rows. want: %v, got: %v", tc.name, tc.want, got) - } - - } - - // Drop table. - _, err = db.ExecContext(ctx, `DROP TABLE TestOverflowRead`) - if err != nil { - t.Fatal(err) - } - -} diff --git a/stmt.go b/stmt.go index 1b747230..d3bdbf71 100644 --- a/stmt.go +++ b/stmt.go @@ -18,6 +18,7 @@ import ( "context" "database/sql/driver" "errors" + "fmt" "cloud.google.com/go/spanner" "github.com/rakyll/go-sql-driver-spanner/internal" @@ -38,7 +39,7 @@ func (s *stmt) NumInput() int { } func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { - panic("Using ExecContext instead") + return nil, fmt.Errorf("use ExecContext instead") } func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { @@ -46,7 +47,7 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive } func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { - panic("Using QueryContext instead") + return nil, fmt.Errorf("use QueryContext instead") } func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { @@ -56,21 +57,26 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv } var it *spanner.RowIterator - if s.conn.roTx != nil { - it = s.conn.roTx.Query(ctx, ss) - } else if s.conn.rwTx != nil { - it = s.conn.rwTx.Query(ctx, ss) + if s.conn.tx != nil { + it = s.conn.tx.Query(ctx, ss) } else { it = s.conn.client.Single().Query(ctx, ss) } return &rows{it: it}, nil } +func (s *stmt) CheckNamedValue(value *driver.NamedValue) error { + return nil +} + func prepareSpannerStmt(q string, args []driver.NamedValue) (spanner.Statement, error) { - names, err := internal.NamedValueParamNames(q, len(args)) + names, err := internal.ParseNamedParameters(q) if err != nil { return spanner.Statement{}, err } + if len(names) != len(args) { + return spanner.Statement{}, fmt.Errorf("got %v argument values, but found %v parameters in the sql string", len(args), len(names)) + } ss := spanner.NewStatement(q) for i, v := range args { name := args[i].Name diff --git a/testutil/inmem_database_admin_server.go b/testutil/inmem_database_admin_server.go new file mode 100644 index 00000000..1d20cfa4 --- /dev/null +++ b/testutil/inmem_database_admin_server.go @@ -0,0 +1,91 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "context" + "fmt" + "github.com/golang/protobuf/proto" + longrunningpb "google.golang.org/genproto/googleapis/longrunning" + databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" + "google.golang.org/grpc/metadata" + "strings" +) + +// InMemDatabaseAdminServer contains the DatabaseAdminServer interface plus a couple +// of specific methods for setting mocked results. +type InMemDatabaseAdminServer interface { + databasepb.DatabaseAdminServer + Stop() + Resps() []proto.Message + SetResps([]proto.Message) + Reqs() []proto.Message + SetReqs([]proto.Message) + SetErr(error) +} + +// inMemDatabaseAdminServer implements InMemDatabaseAdminServer interface. Note that +// there is no mutex protecting the data structures, so it is not safe for +// concurrent use. +type inMemDatabaseAdminServer struct { + databasepb.DatabaseAdminServer + reqs []proto.Message + // If set, all calls return this error + err error + // responses to return if err == nil + resps []proto.Message +} + +// NewInMemDatabaseAdminServer creates a new in-mem test server. +func NewInMemDatabaseAdminServer() InMemDatabaseAdminServer { + res := &inMemDatabaseAdminServer{} + return res +} + +func (s *inMemDatabaseAdminServer) UpdateDatabaseDdl(ctx context.Context, req *databasepb.UpdateDatabaseDdlRequest) (*longrunningpb.Operation, error) { + md, _ := metadata.FromIncomingContext(ctx) + if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") { + return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg) + } + s.reqs = append(s.reqs, req) + if s.err != nil { + return nil, s.err + } + return s.resps[0].(*longrunningpb.Operation), nil +} + +func (s *inMemDatabaseAdminServer) Stop() { + // do nothing +} + +func (s *inMemDatabaseAdminServer) Resps() []proto.Message { + return s.resps +} + +func (s *inMemDatabaseAdminServer) SetResps(resps []proto.Message) { + s.resps = resps +} + +func (s *inMemDatabaseAdminServer) Reqs() []proto.Message { + return s.reqs +} + +func (s *inMemDatabaseAdminServer) SetReqs(reqs []proto.Message) { + s.reqs = reqs +} + +func (s *inMemDatabaseAdminServer) SetErr(err error) { + s.err = err +} diff --git a/testutil/inmem_instance_admin_server.go b/testutil/inmem_instance_admin_server.go new file mode 100644 index 00000000..3d9542bf --- /dev/null +++ b/testutil/inmem_instance_admin_server.go @@ -0,0 +1,86 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "context" + + "github.com/golang/protobuf/proto" + instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" +) + +// InMemInstanceAdminServer contains the InstanceAdminServer interface plus a couple +// of specific methods for setting mocked results. +type InMemInstanceAdminServer interface { + instancepb.InstanceAdminServer + Stop() + Resps() []proto.Message + SetResps([]proto.Message) + Reqs() []proto.Message + SetReqs([]proto.Message) + SetErr(error) +} + +// inMemInstanceAdminServer implements InMemInstanceAdminServer interface. Note that +// there is no mutex protecting the data structures, so it is not safe for +// concurrent use. +type inMemInstanceAdminServer struct { + instancepb.InstanceAdminServer + reqs []proto.Message + // If set, all calls return this error + err error + // responses to return if err == nil + resps []proto.Message +} + +// NewInMemInstanceAdminServer creates a new in-mem test server. +func NewInMemInstanceAdminServer() InMemInstanceAdminServer { + res := &inMemInstanceAdminServer{} + return res +} + +// GetInstance returns the metadata of a spanner instance. +func (s *inMemInstanceAdminServer) GetInstance(ctx context.Context, req *instancepb.GetInstanceRequest) (*instancepb.Instance, error) { + s.reqs = append(s.reqs, req) + if s.err != nil { + defer func() { s.err = nil }() + return nil, s.err + } + return s.resps[0].(*instancepb.Instance), nil +} + +func (s *inMemInstanceAdminServer) Stop() { + // do nothing +} + +func (s *inMemInstanceAdminServer) Resps() []proto.Message { + return s.resps +} + +func (s *inMemInstanceAdminServer) SetResps(resps []proto.Message) { + s.resps = resps +} + +func (s *inMemInstanceAdminServer) Reqs() []proto.Message { + return s.reqs +} + +func (s *inMemInstanceAdminServer) SetReqs(reqs []proto.Message) { + s.reqs = reqs +} + +func (s *inMemInstanceAdminServer) SetErr(err error) { + s.err = err +} diff --git a/testutil/inmem_instance_admin_server_test.go b/testutil/inmem_instance_admin_server_test.go new file mode 100644 index 00000000..ea49436d --- /dev/null +++ b/testutil/inmem_instance_admin_server_test.go @@ -0,0 +1,95 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil_test + +import ( + "context" + "flag" + "fmt" + "log" + "net" + "testing" + + instance "cloud.google.com/go/spanner/admin/instance/apiv1" + "cloud.google.com/go/spanner/internal/testutil" + "github.com/golang/protobuf/proto" + "google.golang.org/api/option" + instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" + "google.golang.org/grpc" +) + +var instanceClientOpt option.ClientOption + +var ( + mockInstanceAdmin = testutil.NewInMemInstanceAdminServer() +) + +func setupInstanceAdminServer() { + flag.Parse() + + serv := grpc.NewServer() + instancepb.RegisterInstanceAdminServer(serv, mockInstanceAdmin) + + lis, err := net.Listen("tcp", "localhost:0") + if err != nil { + log.Fatal(err) + } + go serv.Serve(lis) + + conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure()) + if err != nil { + log.Fatal(err) + } + instanceClientOpt = option.WithGRPCConn(conn) +} + +func TestInstanceAdminGetInstance(t *testing.T) { + setupInstanceAdminServer() + var expectedResponse = &instancepb.Instance{ + Name: "name2-1052831874", + Config: "config-1354792126", + DisplayName: "displayName1615086568", + NodeCount: 1539922066, + } + + mockInstanceAdmin.SetErr(nil) + mockInstanceAdmin.SetReqs(nil) + + mockInstanceAdmin.SetResps(append(mockInstanceAdmin.Resps()[:0], expectedResponse)) + + var formattedName string = fmt.Sprintf("projects/%s/instances/%s", "[PROJECT]", "[INSTANCE]") + var request = &instancepb.GetInstanceRequest{ + Name: formattedName, + } + + c, err := instance.NewInstanceAdminClient(context.Background(), instanceClientOpt) + if err != nil { + t.Fatal(err) + } + + resp, err := c.GetInstance(context.Background(), request) + + if err != nil { + t.Fatal(err) + } + + if want, got := request, mockInstanceAdmin.Reqs()[0]; !proto.Equal(want, got) { + t.Errorf("wrong request %q, want %q", got, want) + } + + if want, got := expectedResponse, resp; !proto.Equal(want, got) { + t.Errorf("wrong response %q, want %q)", got, want) + } +} diff --git a/testutil/inmem_spanner_server.go b/testutil/inmem_spanner_server.go new file mode 100644 index 00000000..8d906ca8 --- /dev/null +++ b/testutil/inmem_spanner_server.go @@ -0,0 +1,1087 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "bytes" + "context" + "encoding/binary" + "fmt" + "math/rand" + "sort" + "strings" + "sync" + "time" + + "github.com/golang/protobuf/ptypes" + emptypb "github.com/golang/protobuf/ptypes/empty" + structpb "github.com/golang/protobuf/ptypes/struct" + "github.com/golang/protobuf/ptypes/timestamp" + "google.golang.org/genproto/googleapis/rpc/errdetails" + "google.golang.org/genproto/googleapis/rpc/status" + spannerpb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc/codes" + gstatus "google.golang.org/grpc/status" +) + +var ( + // KvMeta is the Metadata for mocked KV table. + KvMeta = spannerpb.ResultSetMetadata{ + RowType: &spannerpb.StructType{ + Fields: []*spannerpb.StructType_Field{ + { + Name: "Key", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, + }, + { + Name: "Value", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, + }, + }, + }, + } +) + +// StatementResultType indicates the type of result returned by a SQL +// statement. +type StatementResultType int + +const ( + // StatementResultError indicates that the sql statement returns an error. + StatementResultError StatementResultType = 0 + // StatementResultResultSet indicates that the sql statement returns a + // result set. + StatementResultResultSet StatementResultType = 1 + // StatementResultUpdateCount indicates that the sql statement returns an + // update count. + StatementResultUpdateCount StatementResultType = 2 + // MaxRowsPerPartialResultSet is the maximum number of rows returned in + // each PartialResultSet. This number is deliberately set to a low value to + // ensure that most queries return more than one PartialResultSet. + MaxRowsPerPartialResultSet = 1 +) + +// The method names that can be used to register execution times and errors. +const ( + MethodBeginTransaction string = "BEGIN_TRANSACTION" + MethodCommitTransaction string = "COMMIT_TRANSACTION" + MethodBatchCreateSession string = "BATCH_CREATE_SESSION" + MethodCreateSession string = "CREATE_SESSION" + MethodDeleteSession string = "DELETE_SESSION" + MethodGetSession string = "GET_SESSION" + MethodExecuteSql string = "EXECUTE_SQL" + MethodExecuteStreamingSql string = "EXECUTE_STREAMING_SQL" + MethodExecuteBatchDml string = "EXECUTE_BATCH_DML" + MethodStreamingRead string = "EXECUTE_STREAMING_READ" +) + +// StatementResult represents a mocked result on the test server. The result is +// either of: a ResultSet, an update count or an error. +type StatementResult struct { + Type StatementResultType + Err error + ResultSet *spannerpb.ResultSet + UpdateCount int64 + ResumeTokens [][]byte +} + +// PartialResultSetExecutionTime represents execution times and errors that +// should be used when a PartialResult at the specified resume token is to +// be returned. +type PartialResultSetExecutionTime struct { + ResumeToken []byte + ExecutionTime time.Duration + Err error +} + +// ToPartialResultSets converts a ResultSet to a PartialResultSet. This method +// is used to convert a mocked result to a PartialResultSet when one of the +// streaming methods are called. +func (s *StatementResult) ToPartialResultSets(resumeToken []byte) (result []*spannerpb.PartialResultSet, err error) { + var startIndex uint64 + if len(resumeToken) > 0 { + if startIndex, err = DecodeResumeToken(resumeToken); err != nil { + return nil, err + } + } + + totalRows := uint64(len(s.ResultSet.Rows)) + if totalRows > 0 { + for { + rowCount := min(totalRows-startIndex, uint64(MaxRowsPerPartialResultSet)) + rows := s.ResultSet.Rows[startIndex : startIndex+rowCount] + values := make([]*structpb.Value, + len(rows)*len(s.ResultSet.Metadata.RowType.Fields)) + var idx int + for _, row := range rows { + for colIdx := range s.ResultSet.Metadata.RowType.Fields { + values[idx] = row.Values[colIdx] + idx++ + } + } + var rt []byte + if len(s.ResumeTokens) == 0 { + rt = EncodeResumeToken(startIndex + rowCount) + } else { + rt = s.ResumeTokens[startIndex] + } + result = append(result, &spannerpb.PartialResultSet{ + Metadata: s.ResultSet.Metadata, + Values: values, + ResumeToken: rt, + }) + + startIndex += rowCount + if startIndex == totalRows { + break + } + } + } else { + result = append(result, &spannerpb.PartialResultSet{ + Metadata: s.ResultSet.Metadata, + }) + } + return result, nil +} + +func min(x, y uint64) uint64 { + if x > y { + return y + } + return x +} + +func (s *StatementResult) updateCountToPartialResultSet(exact bool) *spannerpb.PartialResultSet { + return &spannerpb.PartialResultSet{ + Stats: s.convertUpdateCountToResultSet(exact).Stats, + } +} + +// Converts an update count to a ResultSet, as DML statements also return the +// update count as the statistics of a ResultSet. +func (s *StatementResult) convertUpdateCountToResultSet(exact bool) *spannerpb.ResultSet { + if exact { + return &spannerpb.ResultSet{ + Stats: &spannerpb.ResultSetStats{ + RowCount: &spannerpb.ResultSetStats_RowCountExact{ + RowCountExact: s.UpdateCount, + }, + }, + } + } + return &spannerpb.ResultSet{ + Stats: &spannerpb.ResultSetStats{ + RowCount: &spannerpb.ResultSetStats_RowCountLowerBound{ + RowCountLowerBound: s.UpdateCount, + }, + }, + } +} + +// SimulatedExecutionTime represents the time the execution of a method +// should take, and any errors that should be returned by the method. +type SimulatedExecutionTime struct { + MinimumExecutionTime time.Duration + RandomExecutionTime time.Duration + Errors []error + // Keep error after execution. The error will continue to be returned until + // it is cleared. + KeepError bool +} + +// InMemSpannerServer contains the SpannerServer interface plus a couple +// of specific methods for adding mocked results and resetting the server. +type InMemSpannerServer interface { + spannerpb.SpannerServer + + // Stops this server. + Stop() + + // Resets the in-mem server to its default state, deleting all sessions and + // transactions that have been created on the server. Mocked results are + // not deleted. + Reset() + + // Sets an error that will be returned by the next server call. The server + // call will also automatically clear the error. + SetError(err error) + + // Puts a mocked result on the server for a specific sql statement. The + // server does not parse the SQL string in any way, it is merely used as + // a key to the mocked result. The result will be used for all methods that + // expect a SQL statement, including (batch) DML methods. + PutStatementResult(sql string, result *StatementResult) error + + // Puts a mocked result on the server for a specific partition token. The + // result will only be used for query requests that specify a partition + // token. + PutPartitionResult(partitionToken []byte, result *StatementResult) error + + // Adds a PartialResultSetExecutionTime to the server that should be returned + // for the specified SQL string. + AddPartialResultSetError(sql string, err PartialResultSetExecutionTime) + + // Removes a mocked result on the server for a specific sql statement. + RemoveStatementResult(sql string) + + // Aborts the specified transaction . This method can be used to test + // transaction retry logic. + AbortTransaction(id []byte) + + // Puts a simulated execution time for one of the Spanner methods. + PutExecutionTime(method string, executionTime SimulatedExecutionTime) + // Freeze stalls all requests. + Freeze() + // Unfreeze restores processing requests. + Unfreeze() + + TotalSessionsCreated() uint + TotalSessionsDeleted() uint + SetMaxSessionsReturnedByServerPerBatchRequest(sessionCount int32) + SetMaxSessionsReturnedByServerInTotal(sessionCount int32) + + ReceivedRequests() chan interface{} + DumpSessions() map[string]bool + ClearPings() + DumpPings() []string +} + +type inMemSpannerServer struct { + // Embed for forward compatibility. + // Tests will keep working if more methods are added + // in the future. + spannerpb.SpannerServer + + mu sync.Mutex + // Set to true when this server been stopped. This is the end state of a + // server, a stopped server cannot be restarted. + stopped bool + // If set, all calls return this error. + err error + // The mock server creates session IDs using this counter. + sessionCounter uint64 + // The sessions that have been created on this mock server. + sessions map[string]*spannerpb.Session + // Last use times per session. + sessionLastUseTime map[string]time.Time + // The mock server creates transaction IDs per session using these + // counters. + transactionCounters map[string]*uint64 + // The transactions that have been created on this mock server. + transactions map[string]*spannerpb.Transaction + // The transactions that have been (manually) aborted on the server. + abortedTransactions map[string]bool + // The transactions that are marked as PartitionedDMLTransaction + partitionedDmlTransactions map[string]bool + // The mocked results for this server. + statementResults map[string]*StatementResult + partitionResults map[string]*StatementResult + // The simulated execution times per method. + executionTimes map[string]*SimulatedExecutionTime + // The simulated errors for partial result sets + partialResultSetErrors map[string][]*PartialResultSetExecutionTime + + totalSessionsCreated uint + totalSessionsDeleted uint + // The maximum number of sessions that will be created per batch request. + maxSessionsReturnedByServerPerBatchRequest int32 + maxSessionsReturnedByServerInTotal int32 + receivedRequests chan interface{} + // Session ping history. + pings []string + + // Server will stall on any requests. + freezed chan struct{} +} + +// NewInMemSpannerServer creates a new in-mem test server. +func NewInMemSpannerServer() InMemSpannerServer { + res := &inMemSpannerServer{} + res.initDefaults() + res.statementResults = make(map[string]*StatementResult) + res.partitionResults = make(map[string]*StatementResult) + res.executionTimes = make(map[string]*SimulatedExecutionTime) + res.partialResultSetErrors = make(map[string][]*PartialResultSetExecutionTime) + res.receivedRequests = make(chan interface{}, 1000000) + // Produce a closed channel, so the default action of ready is to not block. + res.Freeze() + res.Unfreeze() + return res +} + +func (s *inMemSpannerServer) Stop() { + s.mu.Lock() + defer s.mu.Unlock() + s.stopped = true + close(s.receivedRequests) +} + +// Resets the test server to its initial state, deleting all sessions and +// transactions that have been created on the server. This method will not +// remove mocked results. +func (s *inMemSpannerServer) Reset() { + s.mu.Lock() + defer s.mu.Unlock() + close(s.receivedRequests) + s.receivedRequests = make(chan interface{}, 1000000) + s.initDefaults() +} + +func (s *inMemSpannerServer) SetError(err error) { + s.mu.Lock() + defer s.mu.Unlock() + s.err = err +} + +// Registers a mocked result for a SQL statement on the server. +func (s *inMemSpannerServer) PutStatementResult(sql string, result *StatementResult) error { + s.mu.Lock() + defer s.mu.Unlock() + s.statementResults[sql] = result + return nil +} + +func (s *inMemSpannerServer) RemoveStatementResult(sql string) { + s.mu.Lock() + defer s.mu.Unlock() + delete(s.statementResults, sql) +} + +// Registers a mocked result for a partition token on the server. +func (s *inMemSpannerServer) PutPartitionResult(partitionToken []byte, result *StatementResult) error { + tokenString := string(partitionToken) + s.mu.Lock() + defer s.mu.Unlock() + s.partitionResults[tokenString] = result + return nil +} + +func (s *inMemSpannerServer) AbortTransaction(id []byte) { + s.mu.Lock() + defer s.mu.Unlock() + s.abortedTransactions[string(id)] = true +} + +func (s *inMemSpannerServer) PutExecutionTime(method string, executionTime SimulatedExecutionTime) { + s.mu.Lock() + defer s.mu.Unlock() + s.executionTimes[method] = &executionTime +} + +func (s *inMemSpannerServer) AddPartialResultSetError(sql string, partialResultSetError PartialResultSetExecutionTime) { + s.mu.Lock() + defer s.mu.Unlock() + s.partialResultSetErrors[sql] = append(s.partialResultSetErrors[sql], &partialResultSetError) +} + +// Freeze stalls all requests. +func (s *inMemSpannerServer) Freeze() { + s.mu.Lock() + defer s.mu.Unlock() + s.freezed = make(chan struct{}) +} + +// Unfreeze restores processing requests. +func (s *inMemSpannerServer) Unfreeze() { + s.mu.Lock() + defer s.mu.Unlock() + close(s.freezed) +} + +// ready checks conditions before executing requests +func (s *inMemSpannerServer) ready() { + s.mu.Lock() + freezed := s.freezed + s.mu.Unlock() + // check if server should be freezed + <-freezed +} + +func (s *inMemSpannerServer) TotalSessionsCreated() uint { + s.mu.Lock() + defer s.mu.Unlock() + return s.totalSessionsCreated +} + +func (s *inMemSpannerServer) TotalSessionsDeleted() uint { + s.mu.Lock() + defer s.mu.Unlock() + return s.totalSessionsDeleted +} + +func (s *inMemSpannerServer) SetMaxSessionsReturnedByServerPerBatchRequest(sessionCount int32) { + s.mu.Lock() + defer s.mu.Unlock() + s.maxSessionsReturnedByServerPerBatchRequest = sessionCount +} + +func (s *inMemSpannerServer) SetMaxSessionsReturnedByServerInTotal(sessionCount int32) { + s.mu.Lock() + defer s.mu.Unlock() + s.maxSessionsReturnedByServerInTotal = sessionCount +} + +func (s *inMemSpannerServer) ReceivedRequests() chan interface{} { + return s.receivedRequests +} + +// ClearPings clears the ping history from the server. +func (s *inMemSpannerServer) ClearPings() { + s.mu.Lock() + defer s.mu.Unlock() + s.pings = nil +} + +// DumpPings dumps the ping history. +func (s *inMemSpannerServer) DumpPings() []string { + s.mu.Lock() + defer s.mu.Unlock() + return append([]string(nil), s.pings...) +} + +// DumpSessions dumps the internal session table. +func (s *inMemSpannerServer) DumpSessions() map[string]bool { + s.mu.Lock() + defer s.mu.Unlock() + st := map[string]bool{} + for s := range s.sessions { + st[s] = true + } + return st +} + +func (s *inMemSpannerServer) initDefaults() { + s.sessionCounter = 0 + s.maxSessionsReturnedByServerPerBatchRequest = 100 + s.sessions = make(map[string]*spannerpb.Session) + s.sessionLastUseTime = make(map[string]time.Time) + s.transactions = make(map[string]*spannerpb.Transaction) + s.abortedTransactions = make(map[string]bool) + s.partitionedDmlTransactions = make(map[string]bool) + s.transactionCounters = make(map[string]*uint64) +} + +func (s *inMemSpannerServer) generateSessionNameLocked(database string) string { + s.sessionCounter++ + return fmt.Sprintf("%s/sessions/%d", database, s.sessionCounter) +} + +func (s *inMemSpannerServer) findSession(name string) (*spannerpb.Session, error) { + s.mu.Lock() + defer s.mu.Unlock() + session := s.sessions[name] + if session == nil { + return nil, newSessionNotFoundError(name) + } + return session, nil +} + +// sessionResourceType is the type name of Spanner sessions. +const sessionResourceType = "type.googleapis.com/google.spanner.v1.Session" + +func newSessionNotFoundError(name string) error { + s := gstatus.Newf(codes.NotFound, "Session not found: Session with id %s not found", name) + s, _ = s.WithDetails(&errdetails.ResourceInfo{ResourceType: sessionResourceType, ResourceName: name}) + return s.Err() +} + +func (s *inMemSpannerServer) updateSessionLastUseTime(session string) { + s.mu.Lock() + defer s.mu.Unlock() + s.sessionLastUseTime[session] = time.Now() +} + +func getCurrentTimestamp() *timestamp.Timestamp { + t := time.Now() + return ×tamp.Timestamp{Seconds: t.Unix(), Nanos: int32(t.Nanosecond())} +} + +// Gets the transaction id from the transaction selector. If the selector +// specifies that a new transaction should be started, this method will start +// a new transaction and return the id of that transaction. +func (s *inMemSpannerServer) getTransactionID(session *spannerpb.Session, txSelector *spannerpb.TransactionSelector) []byte { + var res []byte + if txSelector.GetBegin() != nil { + // Start a new transaction. + res = s.beginTransaction(session, txSelector.GetBegin()).Id + } else if txSelector.GetId() != nil { + res = txSelector.GetId() + } + return res +} + +func (s *inMemSpannerServer) generateTransactionName(session string) string { + s.mu.Lock() + defer s.mu.Unlock() + counter, ok := s.transactionCounters[session] + if !ok { + counter = new(uint64) + s.transactionCounters[session] = counter + } + *counter++ + return fmt.Sprintf("%s/transactions/%d", session, *counter) +} + +func (s *inMemSpannerServer) beginTransaction(session *spannerpb.Session, options *spannerpb.TransactionOptions) *spannerpb.Transaction { + id := s.generateTransactionName(session.Name) + res := &spannerpb.Transaction{ + Id: []byte(id), + ReadTimestamp: getCurrentTimestamp(), + } + s.mu.Lock() + s.transactions[id] = res + s.partitionedDmlTransactions[id] = options.GetPartitionedDml() != nil + s.mu.Unlock() + return res +} + +func (s *inMemSpannerServer) getTransactionByID(id []byte) (*spannerpb.Transaction, error) { + s.mu.Lock() + defer s.mu.Unlock() + tx, ok := s.transactions[string(id)] + if !ok { + return nil, gstatus.Error(codes.NotFound, "Transaction not found") + } + aborted, ok := s.abortedTransactions[string(id)] + if ok && aborted { + return nil, newAbortedErrorWithMinimalRetryDelay() + } + return tx, nil +} + +func newAbortedErrorWithMinimalRetryDelay() error { + st := gstatus.New(codes.Aborted, "Transaction has been aborted") + retry := &errdetails.RetryInfo{ + RetryDelay: ptypes.DurationProto(time.Nanosecond), + } + st, _ = st.WithDetails(retry) + return st.Err() +} + +func (s *inMemSpannerServer) removeTransaction(tx *spannerpb.Transaction) { + s.mu.Lock() + defer s.mu.Unlock() + delete(s.transactions, string(tx.Id)) + delete(s.partitionedDmlTransactions, string(tx.Id)) +} + +func (s *inMemSpannerServer) getPartitionResult(partitionToken []byte) (*StatementResult, error) { + tokenString := string(partitionToken) + s.mu.Lock() + defer s.mu.Unlock() + result, ok := s.partitionResults[tokenString] + if !ok { + return nil, gstatus.Error(codes.Internal, fmt.Sprintf("No result found for partition token %v", tokenString)) + } + return result, nil +} + +func (s *inMemSpannerServer) getStatementResult(sql string) (*StatementResult, error) { + s.mu.Lock() + defer s.mu.Unlock() + result, ok := s.statementResults[sql] + if !ok { + return nil, gstatus.Error(codes.Internal, fmt.Sprintf("No result found for statement %v", sql)) + } + return result, nil +} + +func (s *inMemSpannerServer) simulateExecutionTime(method string, req interface{}) error { + s.mu.Lock() + if s.stopped { + s.mu.Unlock() + return gstatus.Error(codes.Unavailable, "server has been stopped") + } + s.receivedRequests <- req + s.mu.Unlock() + s.ready() + s.mu.Lock() + if s.err != nil { + err := s.err + s.err = nil + s.mu.Unlock() + return err + } + executionTime, ok := s.executionTimes[method] + s.mu.Unlock() + if ok { + var randTime int64 + if executionTime.RandomExecutionTime > 0 { + randTime = rand.Int63n(int64(executionTime.RandomExecutionTime)) + } + totalExecutionTime := time.Duration(int64(executionTime.MinimumExecutionTime) + randTime) + <-time.After(totalExecutionTime) + s.mu.Lock() + if executionTime.Errors != nil && len(executionTime.Errors) > 0 { + err := executionTime.Errors[0] + if !executionTime.KeepError { + executionTime.Errors = executionTime.Errors[1:] + } + s.mu.Unlock() + return err + } + s.mu.Unlock() + } + return nil +} + +func (s *inMemSpannerServer) CreateSession(ctx context.Context, req *spannerpb.CreateSessionRequest) (*spannerpb.Session, error) { + if err := s.simulateExecutionTime(MethodCreateSession, req); err != nil { + return nil, err + } + if req.Database == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing database") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.maxSessionsReturnedByServerInTotal > int32(0) && int32(len(s.sessions)) == s.maxSessionsReturnedByServerInTotal { + return nil, gstatus.Error(codes.ResourceExhausted, "No more sessions available") + } + sessionName := s.generateSessionNameLocked(req.Database) + ts := getCurrentTimestamp() + session := &spannerpb.Session{Name: sessionName, CreateTime: ts, ApproximateLastUseTime: ts} + s.totalSessionsCreated++ + s.sessions[sessionName] = session + return session, nil +} + +func (s *inMemSpannerServer) BatchCreateSessions(ctx context.Context, req *spannerpb.BatchCreateSessionsRequest) (*spannerpb.BatchCreateSessionsResponse, error) { + if err := s.simulateExecutionTime(MethodBatchCreateSession, req); err != nil { + return nil, err + } + if req.Database == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing database") + } + if req.SessionCount <= 0 { + return nil, gstatus.Error(codes.InvalidArgument, "Session count must be >= 0") + } + sessionsToCreate := req.SessionCount + s.mu.Lock() + defer s.mu.Unlock() + if s.maxSessionsReturnedByServerInTotal > int32(0) && int32(len(s.sessions)) >= s.maxSessionsReturnedByServerInTotal { + return nil, gstatus.Error(codes.ResourceExhausted, "No more sessions available") + } + if sessionsToCreate > s.maxSessionsReturnedByServerPerBatchRequest { + sessionsToCreate = s.maxSessionsReturnedByServerPerBatchRequest + } + if s.maxSessionsReturnedByServerInTotal > int32(0) && (sessionsToCreate+int32(len(s.sessions))) > s.maxSessionsReturnedByServerInTotal { + sessionsToCreate = s.maxSessionsReturnedByServerInTotal - int32(len(s.sessions)) + } + sessions := make([]*spannerpb.Session, sessionsToCreate) + for i := int32(0); i < sessionsToCreate; i++ { + sessionName := s.generateSessionNameLocked(req.Database) + ts := getCurrentTimestamp() + sessions[i] = &spannerpb.Session{Name: sessionName, CreateTime: ts, ApproximateLastUseTime: ts} + s.totalSessionsCreated++ + s.sessions[sessionName] = sessions[i] + } + return &spannerpb.BatchCreateSessionsResponse{Session: sessions}, nil +} + +func (s *inMemSpannerServer) GetSession(ctx context.Context, req *spannerpb.GetSessionRequest) (*spannerpb.Session, error) { + if err := s.simulateExecutionTime(MethodGetSession, req); err != nil { + return nil, err + } + if req.Name == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Name) + if err != nil { + return nil, err + } + return session, nil +} + +func (s *inMemSpannerServer) ListSessions(ctx context.Context, req *spannerpb.ListSessionsRequest) (*spannerpb.ListSessionsResponse, error) { + s.mu.Lock() + if s.stopped { + s.mu.Unlock() + return nil, gstatus.Error(codes.Unavailable, "server has been stopped") + } + s.receivedRequests <- req + s.mu.Unlock() + if req.Database == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing database") + } + expectedSessionName := req.Database + "/sessions/" + var sessions []*spannerpb.Session + s.mu.Lock() + for _, session := range s.sessions { + if strings.Index(session.Name, expectedSessionName) == 0 { + sessions = append(sessions, session) + } + } + s.mu.Unlock() + sort.Slice(sessions[:], func(i, j int) bool { + return sessions[i].Name < sessions[j].Name + }) + res := &spannerpb.ListSessionsResponse{Sessions: sessions} + return res, nil +} + +func (s *inMemSpannerServer) DeleteSession(ctx context.Context, req *spannerpb.DeleteSessionRequest) (*emptypb.Empty, error) { + if err := s.simulateExecutionTime(MethodDeleteSession, req); err != nil { + return nil, err + } + if req.Name == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + if _, err := s.findSession(req.Name); err != nil { + return nil, err + } + s.mu.Lock() + defer s.mu.Unlock() + s.totalSessionsDeleted++ + delete(s.sessions, req.Name) + return &emptypb.Empty{}, nil +} + +func (s *inMemSpannerServer) ExecuteSql(ctx context.Context, req *spannerpb.ExecuteSqlRequest) (*spannerpb.ResultSet, error) { + if err := s.simulateExecutionTime(MethodExecuteSql, req); err != nil { + return nil, err + } + if req.Sql == "SELECT 1" { + s.mu.Lock() + s.pings = append(s.pings, req.Session) + s.mu.Unlock() + } + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + var id []byte + s.updateSessionLastUseTime(session.Name) + if id = s.getTransactionID(session, req.Transaction); id != nil { + _, err = s.getTransactionByID(id) + if err != nil { + return nil, err + } + } + var statementResult *StatementResult + if req.PartitionToken != nil { + statementResult, err = s.getPartitionResult(req.PartitionToken) + } else { + statementResult, err = s.getStatementResult(req.Sql) + } + if err != nil { + return nil, err + } + s.mu.Lock() + isPartitionedDml := s.partitionedDmlTransactions[string(id)] + s.mu.Unlock() + switch statementResult.Type { + case StatementResultError: + return nil, statementResult.Err + case StatementResultResultSet: + return statementResult.ResultSet, nil + case StatementResultUpdateCount: + return statementResult.convertUpdateCountToResultSet(!isPartitionedDml), nil + } + return nil, gstatus.Error(codes.Internal, "Unknown result type") +} + +func (s *inMemSpannerServer) ExecuteStreamingSql(req *spannerpb.ExecuteSqlRequest, stream spannerpb.Spanner_ExecuteStreamingSqlServer) error { + if err := s.simulateExecutionTime(MethodExecuteStreamingSql, req); err != nil { + return err + } + return s.executeStreamingSQL(req, stream) +} + +func (s *inMemSpannerServer) executeStreamingSQL(req *spannerpb.ExecuteSqlRequest, stream spannerpb.Spanner_ExecuteStreamingSqlServer) error { + if req.Session == "" { + return gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return err + } + s.updateSessionLastUseTime(session.Name) + var id []byte + if id = s.getTransactionID(session, req.Transaction); id != nil { + _, err = s.getTransactionByID(id) + if err != nil { + return err + } + } + var statementResult *StatementResult + if req.PartitionToken != nil { + statementResult, err = s.getPartitionResult(req.PartitionToken) + } else { + statementResult, err = s.getStatementResult(req.Sql) + } + if err != nil { + return err + } + s.mu.Lock() + isPartitionedDml := s.partitionedDmlTransactions[string(id)] + s.mu.Unlock() + switch statementResult.Type { + case StatementResultError: + return statementResult.Err + case StatementResultResultSet: + parts, err := statementResult.ToPartialResultSets(req.ResumeToken) + if err != nil { + return err + } + var nextPartialResultSetError *PartialResultSetExecutionTime + s.mu.Lock() + pErrors := s.partialResultSetErrors[req.Sql] + if len(pErrors) > 0 { + nextPartialResultSetError = pErrors[0] + s.partialResultSetErrors[req.Sql] = pErrors[1:] + } + s.mu.Unlock() + for _, part := range parts { + if nextPartialResultSetError != nil && bytes.Equal(part.ResumeToken, nextPartialResultSetError.ResumeToken) { + if nextPartialResultSetError.ExecutionTime > 0 { + <-time.After(nextPartialResultSetError.ExecutionTime) + } + if nextPartialResultSetError.Err != nil { + return nextPartialResultSetError.Err + } + } + if err := stream.Send(part); err != nil { + return err + } + } + return nil + case StatementResultUpdateCount: + part := statementResult.updateCountToPartialResultSet(!isPartitionedDml) + if err := stream.Send(part); err != nil { + return err + } + return nil + } + return gstatus.Error(codes.Internal, "Unknown result type") +} + +func (s *inMemSpannerServer) ExecuteBatchDml(ctx context.Context, req *spannerpb.ExecuteBatchDmlRequest) (*spannerpb.ExecuteBatchDmlResponse, error) { + if err := s.simulateExecutionTime(MethodExecuteBatchDml, req); err != nil { + return nil, err + } + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + s.updateSessionLastUseTime(session.Name) + var id []byte + if id = s.getTransactionID(session, req.Transaction); id != nil { + _, err = s.getTransactionByID(id) + if err != nil { + return nil, err + } + } + s.mu.Lock() + isPartitionedDml := s.partitionedDmlTransactions[string(id)] + s.mu.Unlock() + resp := &spannerpb.ExecuteBatchDmlResponse{} + resp.ResultSets = make([]*spannerpb.ResultSet, len(req.Statements)) + resp.Status = &status.Status{Code: int32(codes.OK)} + for idx, batchStatement := range req.Statements { + statementResult, err := s.getStatementResult(batchStatement.Sql) + if err != nil { + return nil, err + } + switch statementResult.Type { + case StatementResultError: + resp.Status = &status.Status{Code: int32(gstatus.Code(statementResult.Err)), Message: statementResult.Err.Error()} + resp.ResultSets = resp.ResultSets[:idx] + return resp, nil + case StatementResultResultSet: + return nil, gstatus.Error(codes.InvalidArgument, fmt.Sprintf("Not an update statement: %v", batchStatement.Sql)) + case StatementResultUpdateCount: + resp.ResultSets[idx] = statementResult.convertUpdateCountToResultSet(!isPartitionedDml) + } + } + return resp, nil +} + +func (s *inMemSpannerServer) Read(ctx context.Context, req *spannerpb.ReadRequest) (*spannerpb.ResultSet, error) { + s.mu.Lock() + if s.stopped { + s.mu.Unlock() + return nil, gstatus.Error(codes.Unavailable, "server has been stopped") + } + s.receivedRequests <- req + s.mu.Unlock() + return nil, gstatus.Error(codes.Unimplemented, "Method not yet implemented") +} + +func (s *inMemSpannerServer) StreamingRead(req *spannerpb.ReadRequest, stream spannerpb.Spanner_StreamingReadServer) error { + if err := s.simulateExecutionTime(MethodStreamingRead, req); err != nil { + return err + } + sqlReq := &spannerpb.ExecuteSqlRequest{ + Session: req.Session, + Transaction: req.Transaction, + PartitionToken: req.PartitionToken, + ResumeToken: req.ResumeToken, + // KeySet is currently ignored. + Sql: fmt.Sprintf( + "SELECT %s FROM %s", + strings.Join(req.Columns, ", "), + req.Table, + ), + } + return s.executeStreamingSQL(sqlReq, stream) +} + +func (s *inMemSpannerServer) BeginTransaction(ctx context.Context, req *spannerpb.BeginTransactionRequest) (*spannerpb.Transaction, error) { + if err := s.simulateExecutionTime(MethodBeginTransaction, req); err != nil { + return nil, err + } + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + s.updateSessionLastUseTime(session.Name) + tx := s.beginTransaction(session, req.Options) + return tx, nil +} + +func (s *inMemSpannerServer) Commit(ctx context.Context, req *spannerpb.CommitRequest) (*spannerpb.CommitResponse, error) { + if err := s.simulateExecutionTime(MethodCommitTransaction, req); err != nil { + return nil, err + } + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + s.updateSessionLastUseTime(session.Name) + var tx *spannerpb.Transaction + if req.GetSingleUseTransaction() != nil { + tx = s.beginTransaction(session, req.GetSingleUseTransaction()) + } else if req.GetTransactionId() != nil { + tx, err = s.getTransactionByID(req.GetTransactionId()) + if err != nil { + return nil, err + } + } else { + return nil, gstatus.Error(codes.InvalidArgument, "Missing transaction in commit request") + } + s.removeTransaction(tx) + resp := &spannerpb.CommitResponse{CommitTimestamp: getCurrentTimestamp()} + if req.ReturnCommitStats { + resp.CommitStats = &spannerpb.CommitResponse_CommitStats{ + MutationCount: int64(1), + } + } + return resp, nil +} + +func (s *inMemSpannerServer) Rollback(ctx context.Context, req *spannerpb.RollbackRequest) (*emptypb.Empty, error) { + s.mu.Lock() + if s.stopped { + s.mu.Unlock() + return nil, gstatus.Error(codes.Unavailable, "server has been stopped") + } + s.receivedRequests <- req + s.mu.Unlock() + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + s.updateSessionLastUseTime(session.Name) + tx, err := s.getTransactionByID(req.TransactionId) + if err != nil { + return nil, err + } + s.removeTransaction(tx) + return &emptypb.Empty{}, nil +} + +func (s *inMemSpannerServer) PartitionQuery(ctx context.Context, req *spannerpb.PartitionQueryRequest) (*spannerpb.PartitionResponse, error) { + s.mu.Lock() + if s.stopped { + s.mu.Unlock() + return nil, gstatus.Error(codes.Unavailable, "server has been stopped") + } + s.receivedRequests <- req + s.mu.Unlock() + if req.Session == "" { + return nil, gstatus.Error(codes.InvalidArgument, "Missing session name") + } + session, err := s.findSession(req.Session) + if err != nil { + return nil, err + } + var id []byte + var tx *spannerpb.Transaction + s.updateSessionLastUseTime(session.Name) + if id = s.getTransactionID(session, req.Transaction); id != nil { + tx, err = s.getTransactionByID(id) + if err != nil { + return nil, err + } + } + var partitions []*spannerpb.Partition + for i := int64(0); i < req.PartitionOptions.MaxPartitions; i++ { + token := make([]byte, 10) + _, err := rand.Read(token) + if err != nil { + return nil, gstatus.Error(codes.Internal, "failed to generate random partition token") + } + partitions = append(partitions, &spannerpb.Partition{PartitionToken: token}) + } + return &spannerpb.PartitionResponse{ + Partitions: partitions, + Transaction: tx, + }, nil +} + +func (s *inMemSpannerServer) PartitionRead(ctx context.Context, req *spannerpb.PartitionReadRequest) (*spannerpb.PartitionResponse, error) { + return s.PartitionQuery(ctx, &spannerpb.PartitionQueryRequest{ + Session: req.Session, + Transaction: req.Transaction, + PartitionOptions: req.PartitionOptions, + // KeySet is currently ignored. + Sql: fmt.Sprintf( + "SELECT %s FROM %s", + strings.Join(req.Columns, ", "), + req.Table, + ), + }) +} + +// EncodeResumeToken return mock resume token encoding for an uint64 integer. +func EncodeResumeToken(t uint64) []byte { + rt := make([]byte, 16) + binary.PutUvarint(rt, t) + return rt +} + +// DecodeResumeToken decodes a mock resume token into an uint64 integer. +func DecodeResumeToken(t []byte) (uint64, error) { + s, n := binary.Uvarint(t) + if n <= 0 { + return 0, fmt.Errorf("invalid resume token: %v", t) + } + return s, nil +} diff --git a/testutil/inmem_spanner_server_test.go b/testutil/inmem_spanner_server_test.go new file mode 100644 index 00000000..9b570a16 --- /dev/null +++ b/testutil/inmem_spanner_server_test.go @@ -0,0 +1,605 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil_test + +import ( + "strconv" + + . "cloud.google.com/go/spanner/internal/testutil" + + "context" + "flag" + "fmt" + "log" + "net" + "os" + "strings" + "testing" + + structpb "github.com/golang/protobuf/ptypes/struct" + spannerpb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc/codes" + + apiv1 "cloud.google.com/go/spanner/apiv1" + "google.golang.org/api/iterator" + "google.golang.org/api/option" + "google.golang.org/grpc" + + gstatus "google.golang.org/grpc/status" +) + +// clientOpt is the option tests should use to connect to the test server. +// It is initialized by TestMain. +var serverAddress string +var clientOpt option.ClientOption +var testSpanner InMemSpannerServer + +// Mocked selectSQL statement. +const selectSQL = "SELECT FOO FROM BAR" +const selectRowCount int64 = 2 +const selectColCount int = 1 + +var selectValues = [...]int64{1, 2} + +// Mocked DML statement. +const updateSQL = "UPDATE FOO SET BAR=1 WHERE ID=ID" +const updateRowCount int64 = 2 + +func TestMain(m *testing.M) { + flag.Parse() + + testSpanner = NewInMemSpannerServer() + serv := grpc.NewServer() + spannerpb.RegisterSpannerServer(serv, testSpanner) + + lis, err := net.Listen("tcp", "localhost:0") + if err != nil { + log.Fatal(err) + } + go serv.Serve(lis) + + serverAddress = lis.Addr().String() + conn, err := grpc.Dial(serverAddress, grpc.WithInsecure()) + if err != nil { + log.Fatal(err) + } + clientOpt = option.WithGRPCConn(conn) + + os.Exit(m.Run()) +} + +// Resets the mock server to its default values and registers a mocked result +// for the statements "SELECT FOO FROM BAR" and +// "UPDATE FOO SET BAR=1 WHERE ID=ID". +func setup() { + testSpanner.Reset() + fields := make([]*spannerpb.StructType_Field, selectColCount) + fields[0] = &spannerpb.StructType_Field{ + Name: "FOO", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + rowType := &spannerpb.StructType{ + Fields: fields, + } + metadata := &spannerpb.ResultSetMetadata{ + RowType: rowType, + } + rows := make([]*structpb.ListValue, selectRowCount) + for idx, value := range selectValues { + rowValue := make([]*structpb.Value, selectColCount) + rowValue[0] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: strconv.FormatInt(value, 10)}, + } + rows[idx] = &structpb.ListValue{ + Values: rowValue, + } + } + resultSet := &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } + result := &StatementResult{Type: StatementResultResultSet, ResultSet: resultSet} + testSpanner.PutStatementResult(selectSQL, result) + + updateResult := &StatementResult{Type: StatementResultUpdateCount, UpdateCount: updateRowCount} + testSpanner.PutStatementResult(updateSQL, updateResult) +} + +func TestSpannerCreateSession(t *testing.T) { + testSpanner.Reset() + var expectedName = fmt.Sprintf("projects/%s/instances/%s/databases/%s/sessions/", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var request = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + resp, err := c.CreateSession(context.Background(), request) + if err != nil { + t.Fatal(err) + } + if strings.Index(resp.Name, expectedName) != 0 { + t.Errorf("Session name mismatch\nGot: %s\nWant: Name should start with %s)", resp.Name, expectedName) + } +} + +func TestSpannerCreateSession_Unavailable(t *testing.T) { + testSpanner.Reset() + var expectedName = fmt.Sprintf("projects/%s/instances/%s/databases/%s/sessions/", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var request = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + testSpanner.SetError(gstatus.Error(codes.Unavailable, "Temporary unavailable")) + resp, err := c.CreateSession(context.Background(), request) + if err != nil { + t.Fatal(err) + } + if strings.Index(resp.Name, expectedName) != 0 { + t.Errorf("Session name mismatch\nGot: %s\nWant: Name should start with %s)", resp.Name, expectedName) + } +} + +func TestSpannerGetSession(t *testing.T) { + testSpanner.Reset() + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + createResp, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + var getRequest = &spannerpb.GetSessionRequest{ + Name: createResp.Name, + } + getResp, err := c.GetSession(context.Background(), getRequest) + if err != nil { + t.Fatal(err) + } + if getResp.Name != getRequest.Name { + t.Errorf("Session name mismatch\nGot: %s\nWant: Name should start with %s)", getResp.Name, getRequest.Name) + } +} + +func TestSpannerListSessions(t *testing.T) { + testSpanner.Reset() + const expectedNumberOfSessions = 5 + var expectedName = fmt.Sprintf("projects/%s/instances/%s/databases/%s/sessions/", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + for i := 0; i < expectedNumberOfSessions; i++ { + _, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + } + var listRequest = &spannerpb.ListSessionsRequest{ + Database: formattedDatabase, + } + var sessionCount int + listResp := c.ListSessions(context.Background(), listRequest) + for { + session, err := listResp.Next() + if err == iterator.Done { + break + } + if err != nil { + t.Fatal(err) + } + if strings.Index(session.Name, expectedName) != 0 { + t.Errorf("Session name mismatch\nGot: %s\nWant: Name should start with %s)", session.Name, expectedName) + } + sessionCount++ + } + if sessionCount != expectedNumberOfSessions { + t.Errorf("Session count mismatch\nGot: %d\nWant: %d", sessionCount, expectedNumberOfSessions) + } +} + +func TestSpannerDeleteSession(t *testing.T) { + testSpanner.Reset() + const expectedNumberOfSessions = 5 + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + for i := 0; i < expectedNumberOfSessions; i++ { + _, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + } + var listRequest = &spannerpb.ListSessionsRequest{ + Database: formattedDatabase, + } + var sessionCount int + listResp := c.ListSessions(context.Background(), listRequest) + for { + session, err := listResp.Next() + if err == iterator.Done { + break + } + if err != nil { + t.Fatal(err) + } + var deleteRequest = &spannerpb.DeleteSessionRequest{ + Name: session.Name, + } + c.DeleteSession(context.Background(), deleteRequest) + sessionCount++ + } + if sessionCount != expectedNumberOfSessions { + t.Errorf("Session count mismatch\nGot: %d\nWant: %d", sessionCount, expectedNumberOfSessions) + } + // Re-list all sessions. This should now be empty. + listResp = c.ListSessions(context.Background(), listRequest) + _, err = listResp.Next() + if err != iterator.Done { + t.Errorf("expected empty session iterator") + } +} + +func TestSpannerExecuteSql(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + request := &spannerpb.ExecuteSqlRequest{ + Session: session.Name, + Sql: selectSQL, + Transaction: &spannerpb.TransactionSelector{ + Selector: &spannerpb.TransactionSelector_SingleUse{ + SingleUse: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadOnly_{ + ReadOnly: &spannerpb.TransactionOptions_ReadOnly{ + ReturnReadTimestamp: false, + TimestampBound: &spannerpb.TransactionOptions_ReadOnly_Strong{ + Strong: true, + }, + }, + }, + }, + }, + }, + Seqno: 1, + QueryMode: spannerpb.ExecuteSqlRequest_NORMAL, + } + response, err := c.ExecuteSql(context.Background(), request) + if err != nil { + t.Fatal(err) + } + var rowCount int64 + for _, row := range response.Rows { + if len(row.Values) != selectColCount { + t.Fatalf("Column count mismatch\nGot: %d\nWant: %d", len(row.Values), selectColCount) + } + rowCount++ + } + if rowCount != selectRowCount { + t.Fatalf("Row count mismatch\nGot: %d\nWant: %d", rowCount, selectRowCount) + } +} + +func TestSpannerExecuteSqlDml(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + request := &spannerpb.ExecuteSqlRequest{ + Session: session.Name, + Sql: updateSQL, + Transaction: &spannerpb.TransactionSelector{ + Selector: &spannerpb.TransactionSelector_Begin{ + Begin: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadWrite_{ + ReadWrite: &spannerpb.TransactionOptions_ReadWrite{}, + }, + }, + }, + }, + Seqno: 1, + QueryMode: spannerpb.ExecuteSqlRequest_NORMAL, + } + response, err := c.ExecuteSql(context.Background(), request) + if err != nil { + t.Fatal(err) + } + var rowCount int64 = response.Stats.GetRowCountExact() + if rowCount != updateRowCount { + t.Fatalf("Update count mismatch\nGot: %d\nWant: %d", rowCount, updateRowCount) + } +} + +func TestSpannerExecuteStreamingSql(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + request := &spannerpb.ExecuteSqlRequest{ + Session: session.Name, + Sql: selectSQL, + Transaction: &spannerpb.TransactionSelector{ + Selector: &spannerpb.TransactionSelector_SingleUse{ + SingleUse: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadOnly_{ + ReadOnly: &spannerpb.TransactionOptions_ReadOnly{ + ReturnReadTimestamp: false, + TimestampBound: &spannerpb.TransactionOptions_ReadOnly_Strong{ + Strong: true, + }, + }, + }, + }, + }, + }, + Seqno: 1, + QueryMode: spannerpb.ExecuteSqlRequest_NORMAL, + } + response, err := c.ExecuteStreamingSql(context.Background(), request) + if err != nil { + t.Fatal(err) + } + var rowIndex int64 + var colCount int + for { + for rowIndexInPartial := int64(0); rowIndexInPartial < MaxRowsPerPartialResultSet; rowIndexInPartial++ { + partial, err := response.Recv() + if err != nil { + t.Fatal(err) + } + if rowIndex == 0 { + colCount = len(partial.Metadata.RowType.Fields) + if colCount != selectColCount { + t.Fatalf("Column count mismatch\nGot: %d\nWant: %d", colCount, selectColCount) + } + } + for col := 0; col < colCount; col++ { + pIndex := rowIndexInPartial*int64(colCount) + int64(col) + val, err := strconv.ParseInt(partial.Values[pIndex].GetStringValue(), 10, 64) + if err != nil { + t.Fatalf("Error parsing integer at #%d: %v", pIndex, err) + } + if val != selectValues[rowIndex] { + t.Fatalf("Value mismatch at index %d\nGot: %d\nWant: %d", rowIndex, val, selectValues[rowIndex]) + } + } + rowIndex++ + } + if rowIndex == selectRowCount { + break + } + } + if rowIndex != selectRowCount { + t.Fatalf("Row count mismatch\nGot: %d\nWant: %d", rowIndex, selectRowCount) + } +} + +func TestSpannerExecuteBatchDml(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + statements := make([]*spannerpb.ExecuteBatchDmlRequest_Statement, 3) + for idx := 0; idx < len(statements); idx++ { + statements[idx] = &spannerpb.ExecuteBatchDmlRequest_Statement{Sql: updateSQL} + } + executeBatchDmlRequest := &spannerpb.ExecuteBatchDmlRequest{ + Session: session.Name, + Statements: statements, + Transaction: &spannerpb.TransactionSelector{ + Selector: &spannerpb.TransactionSelector_Begin{ + Begin: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadWrite_{ + ReadWrite: &spannerpb.TransactionOptions_ReadWrite{}, + }, + }, + }, + }, + Seqno: 1, + } + response, err := c.ExecuteBatchDml(context.Background(), executeBatchDmlRequest) + if err != nil { + t.Fatal(err) + } + var totalRowCount int64 + for _, res := range response.ResultSets { + var rowCount int64 = res.Stats.GetRowCountExact() + if rowCount != updateRowCount { + t.Fatalf("Update count mismatch\nGot: %d\nWant: %d", rowCount, updateRowCount) + } + totalRowCount += rowCount + } + if totalRowCount != updateRowCount*int64(len(statements)) { + t.Fatalf("Total update count mismatch\nGot: %d\nWant: %d", totalRowCount, updateRowCount*int64(len(statements))) + } +} + +func TestBeginTransaction(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + beginRequest := &spannerpb.BeginTransactionRequest{ + Session: session.Name, + Options: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadWrite_{ + ReadWrite: &spannerpb.TransactionOptions_ReadWrite{}, + }, + }, + } + tx, err := c.BeginTransaction(context.Background(), beginRequest) + if err != nil { + t.Fatal(err) + } + expectedName := fmt.Sprintf("%s/transactions/", session.Name) + if strings.Index(string(tx.Id), expectedName) != 0 { + t.Errorf("Transaction name mismatch\nGot: %s\nWant: Name should start with %s)", string(tx.Id), expectedName) + } +} + +func TestCommitTransaction(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + beginRequest := &spannerpb.BeginTransactionRequest{ + Session: session.Name, + Options: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadWrite_{ + ReadWrite: &spannerpb.TransactionOptions_ReadWrite{}, + }, + }, + } + tx, err := c.BeginTransaction(context.Background(), beginRequest) + if err != nil { + t.Fatal(err) + } + commitRequest := &spannerpb.CommitRequest{ + Session: session.Name, + Transaction: &spannerpb.CommitRequest_TransactionId{ + TransactionId: tx.Id, + }, + } + resp, err := c.Commit(context.Background(), commitRequest) + if err != nil { + t.Fatal(err) + } + if resp.CommitTimestamp == nil { + t.Fatalf("No commit timestamp returned") + } +} + +func TestRollbackTransaction(t *testing.T) { + setup() + c, err := apiv1.NewClient(context.Background(), clientOpt) + if err != nil { + t.Fatal(err) + } + + var formattedDatabase = fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") + var createRequest = &spannerpb.CreateSessionRequest{ + Database: formattedDatabase, + } + session, err := c.CreateSession(context.Background(), createRequest) + if err != nil { + t.Fatal(err) + } + beginRequest := &spannerpb.BeginTransactionRequest{ + Session: session.Name, + Options: &spannerpb.TransactionOptions{ + Mode: &spannerpb.TransactionOptions_ReadWrite_{ + ReadWrite: &spannerpb.TransactionOptions_ReadWrite{}, + }, + }, + } + tx, err := c.BeginTransaction(context.Background(), beginRequest) + if err != nil { + t.Fatal(err) + } + rollbackRequest := &spannerpb.RollbackRequest{ + Session: session.Name, + TransactionId: tx.Id, + } + err = c.Rollback(context.Background(), rollbackRequest) + if err != nil { + t.Fatal(err) + } +} diff --git a/testutil/mocked_inmem_server.go b/testutil/mocked_inmem_server.go new file mode 100644 index 00000000..47b09537 --- /dev/null +++ b/testutil/mocked_inmem_server.go @@ -0,0 +1,318 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "fmt" + databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" + "net" + "strconv" + "testing" + + "encoding/base64" + structpb "github.com/golang/protobuf/ptypes/struct" + "google.golang.org/api/option" + instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" + spannerpb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc" +) + +// SelectFooFromBar is a SELECT statement that is added to the mocked test +// server and will return a one-col-two-rows result set containing the INT64 +// values 1 and 2. +const SelectFooFromBar = "SELECT FOO FROM BAR" +const selectFooFromBarRowCount int64 = 2 +const selectFooFromBarColCount int = 1 + +var selectFooFromBarResults = [...]int64{1, 2} + +// SelectSingerIDAlbumIDAlbumTitleFromAlbums i a SELECT statement that is added +// to the mocked test server and will return a 3-cols-3-rows result set. +const SelectSingerIDAlbumIDAlbumTitleFromAlbums = "SELECT SingerId, AlbumId, AlbumTitle FROM Albums" + +// SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount is the number of rows +// returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement. +const SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount int64 = 3 + +// SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount is the number of cols +// returned by the SelectSingerIDAlbumIDAlbumTitleFromAlbums statement. +const SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount int = 3 + +// UpdateBarSetFoo is an UPDATE statement that is added to the mocked test +// server that will return an update count of 5. +const UpdateBarSetFoo = "UPDATE FOO SET BAR=1 WHERE BAZ=2" + +// UpdateBarSetFooRowCount is the constant update count value returned by the +// statement defined in UpdateBarSetFoo. +const UpdateBarSetFooRowCount = 5 + +// MockedSpannerInMemTestServer is an InMemSpannerServer with results for a +// number of SQL statements readily mocked. +type MockedSpannerInMemTestServer struct { + TestSpanner InMemSpannerServer + TestInstanceAdmin InMemInstanceAdminServer + TestDatabaseAdmin InMemDatabaseAdminServer + server *grpc.Server + Address string +} + +// NewMockedSpannerInMemTestServer creates a MockedSpannerInMemTestServer at +// localhost with a random port and returns client options that can be used +// to connect to it. +func NewMockedSpannerInMemTestServer(t *testing.T) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func()) { + return NewMockedSpannerInMemTestServerWithAddr(t, "localhost:0") +} + +// NewMockedSpannerInMemTestServerWithAddr creates a MockedSpannerInMemTestServer +// at a given listening address and returns client options that can be used +// to connect to it. +func NewMockedSpannerInMemTestServerWithAddr(t *testing.T, addr string) (mockedServer *MockedSpannerInMemTestServer, opts []option.ClientOption, teardown func()) { + mockedServer = &MockedSpannerInMemTestServer{} + opts = mockedServer.setupMockedServerWithAddr(t, addr) + return mockedServer, opts, func() { + mockedServer.TestSpanner.Stop() + mockedServer.TestInstanceAdmin.Stop() + mockedServer.TestDatabaseAdmin.Stop() + mockedServer.server.Stop() + } +} + +func (s *MockedSpannerInMemTestServer) setupMockedServerWithAddr(t *testing.T, addr string) []option.ClientOption { + s.TestSpanner = NewInMemSpannerServer() + s.TestInstanceAdmin = NewInMemInstanceAdminServer() + s.TestDatabaseAdmin = NewInMemDatabaseAdminServer() + s.setupSelect1Result() + s.setupFooResults() + s.setupSingersResults() + s.server = grpc.NewServer() + spannerpb.RegisterSpannerServer(s.server, s.TestSpanner) + instancepb.RegisterInstanceAdminServer(s.server, s.TestInstanceAdmin) + databasepb.RegisterDatabaseAdminServer(s.server, s.TestDatabaseAdmin) + + lis, err := net.Listen("tcp", addr) + if err != nil { + t.Fatal(err) + } + go s.server.Serve(lis) + + s.Address = lis.Addr().String() + opts := []option.ClientOption{ + option.WithEndpoint(s.Address), + option.WithGRPCDialOption(grpc.WithInsecure()), + option.WithoutAuthentication(), + } + return opts +} + +func (s *MockedSpannerInMemTestServer) setupSelect1Result() { + result := &StatementResult{Type: StatementResultResultSet, ResultSet: CreateSelect1ResultSet()} + s.TestSpanner.PutStatementResult("SELECT 1", result) +} + +func (s *MockedSpannerInMemTestServer) setupFooResults() { + fields := make([]*spannerpb.StructType_Field, selectFooFromBarColCount) + fields[0] = &spannerpb.StructType_Field{ + Name: "FOO", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + rowType := &spannerpb.StructType{ + Fields: fields, + } + metadata := &spannerpb.ResultSetMetadata{ + RowType: rowType, + } + rows := make([]*structpb.ListValue, selectFooFromBarRowCount) + for idx, value := range selectFooFromBarResults { + rowValue := make([]*structpb.Value, selectFooFromBarColCount) + rowValue[0] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: strconv.FormatInt(value, 10)}, + } + rows[idx] = &structpb.ListValue{ + Values: rowValue, + } + } + resultSet := &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } + result := &StatementResult{Type: StatementResultResultSet, ResultSet: resultSet} + s.TestSpanner.PutStatementResult(SelectFooFromBar, result) + s.TestSpanner.PutStatementResult(UpdateBarSetFoo, &StatementResult{ + Type: StatementResultUpdateCount, + UpdateCount: UpdateBarSetFooRowCount, + }) +} + +func (s *MockedSpannerInMemTestServer) setupSingersResults() { + metadata := createSingersMetadata() + rows := make([]*structpb.ListValue, SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount) + var idx int64 + for idx = 0; idx < SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount; idx++ { + rows[idx] = createSingersRow(idx) + } + resultSet := &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } + result := &StatementResult{Type: StatementResultResultSet, ResultSet: resultSet} + s.TestSpanner.PutStatementResult(SelectSingerIDAlbumIDAlbumTitleFromAlbums, result) +} + +// CreateSingleRowSingersResult creates a result set containing a single row of +// the SelectSingerIDAlbumIDAlbumTitleFromAlbums result set, or zero rows if +// the given rowNum is greater than the number of rows in the result set. This +// method can be used to mock results for different partitions of a +// BatchReadOnlyTransaction. +func (s *MockedSpannerInMemTestServer) CreateSingleRowSingersResult(rowNum int64) *StatementResult { + metadata := createSingersMetadata() + var returnedRows int + if rowNum < SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount { + returnedRows = 1 + } else { + returnedRows = 0 + } + rows := make([]*structpb.ListValue, returnedRows) + if returnedRows > 0 { + rows[0] = createSingersRow(rowNum) + } + resultSet := &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } + return &StatementResult{Type: StatementResultResultSet, ResultSet: resultSet} +} + +func createSingersMetadata() *spannerpb.ResultSetMetadata { + fields := make([]*spannerpb.StructType_Field, SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount) + fields[0] = &spannerpb.StructType_Field{ + Name: "SingerId", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + fields[1] = &spannerpb.StructType_Field{ + Name: "AlbumId", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + fields[2] = &spannerpb.StructType_Field{ + Name: "AlbumTitle", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, + } + rowType := &spannerpb.StructType{ + Fields: fields, + } + return &spannerpb.ResultSetMetadata{ + RowType: rowType, + } +} + +func createSingersRow(idx int64) *structpb.ListValue { + rowValue := make([]*structpb.Value, SelectSingerIDAlbumIDAlbumTitleFromAlbumsColCount) + rowValue[0] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: strconv.FormatInt(idx+1, 10)}, + } + rowValue[1] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: strconv.FormatInt(idx*10+idx, 10)}, + } + rowValue[2] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: fmt.Sprintf("Album title %d", idx)}, + } + return &structpb.ListValue{ + Values: rowValue, + } +} + +func CreateResultSetWithAllTypes() *spannerpb.ResultSet { + fields := make([]*spannerpb.StructType_Field, 8) + fields[0] = &spannerpb.StructType_Field{ + Name: "ColBool", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_BOOL}, + } + fields[1] = &spannerpb.StructType_Field{ + Name: "ColString", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, + } + fields[2] = &spannerpb.StructType_Field{ + Name: "ColBytes", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_BYTES}, + } + fields[3] = &spannerpb.StructType_Field{ + Name: "ColInt", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + fields[4] = &spannerpb.StructType_Field{ + Name: "ColFloat", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_FLOAT64}, + } + fields[5] = &spannerpb.StructType_Field{ + Name: "ColNumeric", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_NUMERIC}, + } + fields[6] = &spannerpb.StructType_Field{ + Name: "ColDate", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_DATE}, + } + fields[7] = &spannerpb.StructType_Field{ + Name: "ColTimestamp", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, + } + rowType := &spannerpb.StructType{ + Fields: fields, + } + metadata := &spannerpb.ResultSetMetadata{ + RowType: rowType, + } + rows := make([]*structpb.ListValue, 1) + rowValue := make([]*structpb.Value, 8) + rowValue[0] = &structpb.Value{Kind: &structpb.Value_BoolValue{BoolValue: true}} + rowValue[1] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "test"}} + rowValue[2] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString([]byte("testbytes"))}} + rowValue[3] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "5"}} + rowValue[4] = &structpb.Value{Kind: &structpb.Value_NumberValue{NumberValue: 3.14}} + rowValue[5] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "6.626"}} + rowValue[6] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "2021-07-21"}} + rowValue[7] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "2021-07-21T21:07:59.339911800Z"}} + rows[0] = &structpb.ListValue{ + Values: rowValue, + } + return &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } +} + +func CreateSelect1ResultSet() *spannerpb.ResultSet { + fields := make([]*spannerpb.StructType_Field, 1) + fields[0] = &spannerpb.StructType_Field{ + Name: "", + Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + } + rowType := &spannerpb.StructType{ + Fields: fields, + } + metadata := &spannerpb.ResultSetMetadata{ + RowType: rowType, + } + rows := make([]*structpb.ListValue, 1) + rowValue := make([]*structpb.Value, 1) + rowValue[0] = &structpb.Value{ + Kind: &structpb.Value_StringValue{StringValue: "1"}, + } + rows[0] = &structpb.ListValue{ + Values: rowValue, + } + return &spannerpb.ResultSet{ + Metadata: metadata, + Rows: rows, + } +} diff --git a/transaction.go b/transaction.go new file mode 100644 index 00000000..2f41413f --- /dev/null +++ b/transaction.go @@ -0,0 +1,94 @@ +// Copyright 2021 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package spannerdriver + +import ( + "context" + "fmt" + + "cloud.google.com/go/spanner" +) + +// contextTransaction is the combination of both read/write and read-only +// transactions. +type contextTransaction interface { + Commit() error + Rollback() error + Query(ctx context.Context, stmt spanner.Statement) *spanner.RowIterator + ExecContext(ctx context.Context, stmt spanner.Statement) (int64, error) +} + +type readOnlyTransaction struct { + roTx *spanner.ReadOnlyTransaction + close func() +} + +func (tx *readOnlyTransaction) Commit() error { + // Read-only transactions don't really commit, but closing the transaction + // will return the session to the pool. + if tx.roTx != nil { + tx.roTx.Close() + } + tx.close() + return nil +} + +func (tx *readOnlyTransaction) Rollback() error { + // Read-only transactions don't really rollback, but closing the transaction + // will return the session to the pool. + if tx.roTx != nil { + tx.roTx.Close() + } + tx.close() + return nil +} + +func (tx *readOnlyTransaction) Query(ctx context.Context, stmt spanner.Statement) *spanner.RowIterator { + return tx.roTx.Query(ctx, stmt) +} + +func (tx *readOnlyTransaction) ExecContext(_ context.Context, stmt spanner.Statement) (int64, error) { + return 0, fmt.Errorf("read-only transactions cannot write") +} + +type readWriteTransaction struct { + ctx context.Context + rwTx *spanner.ReadWriteStmtBasedTransaction + close func() +} + +func (tx *readWriteTransaction) Commit() (err error) { + if tx.rwTx != nil { + _, err = tx.rwTx.Commit(tx.ctx) + } + tx.close() + return err +} + +func (tx *readWriteTransaction) Rollback() error { + if tx.rwTx != nil { + tx.rwTx.Rollback(tx.ctx) + } + tx.close() + return nil +} + +func (tx *readWriteTransaction) Query(ctx context.Context, stmt spanner.Statement) *spanner.RowIterator { + return tx.rwTx.Query(ctx, stmt) +} + +func (tx *readWriteTransaction) ExecContext(ctx context.Context, stmt spanner.Statement) (int64, error) { + return tx.rwTx.Update(ctx, stmt) +} diff --git a/tx.go b/tx.go deleted file mode 100644 index 7c88f6ca..00000000 --- a/tx.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2020 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package spannerdriver - -import ( - "context" - - "cloud.google.com/go/spanner" - "github.com/rakyll/go-sql-driver-spanner/internal" -) - -type roTx struct { - close func() -} - -func (tx *roTx) Commit() error { - tx.close() - return nil -} - -func (tx *roTx) Rollback() error { - tx.close() - return nil -} - -type rwTx struct { - connector *internal.RWConnector - close func() -} - -func (tx *rwTx) Query(ctx context.Context, stmt spanner.Statement) *spanner.RowIterator { - tx.connector.QueryIn <- &internal.RWQueryMessage{ - Ctx: ctx, - Stmt: stmt, - } - msg := <-tx.connector.QueryOut - return msg.It -} - -func (tx *rwTx) ExecContext(ctx context.Context, stmt spanner.Statement) (int64, error) { - tx.connector.ExecIn <- &internal.RWExecMessage{ - Ctx: ctx, - Stmt: stmt, - } - msg := <-tx.connector.ExecOut - return msg.Rows, msg.Error -} - -func (tx *rwTx) Commit() error { - tx.connector.CommitIn <- struct{}{} - err := <-tx.connector.Errors - if err == nil { - tx.close() - } - return err -} - -func (tx *rwTx) Rollback() error { - tx.connector.RollbackIn <- struct{}{} - err := <-tx.connector.Errors - if err == internal.ErrAborted { - tx.close() - return nil - } - return err -}