From 6f2f2d68293374b10cb290393a481fc647fa41e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 19 Jul 2021 15:02:11 +0200 Subject: [PATCH 01/14] feat: support more options in dsn --- driver.go | 123 +++++++++++--- driver_test.go | 95 ++++++++++- driver_with_mockserver_test.go | 103 ++++++++++++ go.mod | 9 +- go.sum | 293 +++++++++++++++++++++++++++++++++ 5 files changed, 597 insertions(+), 26 deletions(-) create mode 100644 driver_with_mockserver_test.go diff --git a/driver.go b/driver.go index dee51857..e9b9f853 100644 --- a/driver.go +++ b/driver.go @@ -19,6 +19,7 @@ import ( "database/sql" "database/sql/driver" "errors" + "log" "os" "regexp" "time" @@ -33,22 +34,38 @@ import ( ) const userAgent = "go-sql-driver-spanner/0.1" +const dsnRegExpString = "((?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.*))?" +const paramsRegExpString = "(?is)(?P[^=]+?)=(?:.*?)" +const propertyRegExpString = "(?is)(?:;|\\?)%s=([^=]+?)(?:;|$)" + +var dsnRegExp *regexp.Regexp +var paramsRegExp *regexp.Regexp +var propertyRegExp *regexp.Regexp var _ driver.DriverContext = &Driver{} func init() { + var err error + dsnRegExp, err = regexp.Compile(dsnRegExpString) + if err != nil { + log.Fatalf("could not compile Spanner dsn regexp: %v", err) + return + } + paramsRegExp, err = regexp.Compile(paramsRegExpString) + if err != nil { + log.Fatalf("could not compile Spanner params regexp: %v", err) + return + } + propertyRegExp, err = regexp.Compile(propertyRegExpString) + if err != nil { + log.Fatalf("could not compile Spanner property regexp: %v", err) + return + } sql.Register("spanner", &Driver{}) } // 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,31 +73,95 @@ 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, + return newConnector(d, name) +} + +type connectorConfig struct { + host string + project string + instance string + database string + params map[string]string +} + +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 extractConnectorParams(params string) (map[string]string, error) { + match := paramsRegExp.FindStringSubmatch(params) + matches := make([]string, 0) + if match != nil { + for i, name := range paramsRegExp.SubexpNames() { + if i != 0 && name != "" { + matches = append(matches, match[i]) + } + } + } + return make(map[string]string), nil +} + type connector struct { driver *Driver - name string + connectorConfig connectorConfig + + // 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 +} + +func newConnector(d *Driver, dsn string) (*connector, error) { + config, err := extractConnectorConfig(dsn) + if err != nil { + return nil, err + } + return &connector{ + driver: d, + connectorConfig: config, + }, nil } func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { - return openDriverConn(ctx, c.driver, c.name) + return openDriverConn(ctx, c) } -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 openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { + if c.config.NumChannels == 0 { + c.config.NumChannels = 1 // TODO(jbd): Explain database/sql has a high-level management. } - opts := append(d.Options, option.WithUserAgent(userAgent)) - client, err := spanner.NewClientWithConfig(ctx, name, d.Config, opts...) + opts := append(c.options, option.WithUserAgent(userAgent)) + client, err := spanner.NewClientWithConfig(ctx, c.connectorConfig.database, c.config, opts...) if err != nil { return nil, err } @@ -89,7 +170,7 @@ func openDriverConn(ctx context.Context, d *Driver, name string) (driver.Conn, e if err != nil { return nil, err } - return &conn{client: client, adminClient: adminClient, name: name}, nil + return &conn{client: client, adminClient: adminClient, database: c.connectorConfig.database}, nil } func createAdminClient(ctx context.Context) (adminClient *adminapi.DatabaseAdminClient, err error) { @@ -123,7 +204,7 @@ type conn struct { adminClient *adminapi.DatabaseAdminClient roTx *spanner.ReadOnlyTransaction rwTx *rwTx - name string + database string } func (c *conn) Prepare(query string) (driver.Stmt, error) { @@ -149,7 +230,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name if isDdl { op, err := c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{ - Database: c.name, + Database: c.database, Statements: []string{query}, }) if err != nil { diff --git a/driver_test.go b/driver_test.go index 8bbf45e9..7455400a 100644 --- a/driver_test.go +++ b/driver_test.go @@ -20,6 +20,8 @@ import ( "os" "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) var ( @@ -42,12 +44,101 @@ func init() { databaseId = "gotest" } - // Derive data source name. + // Derive data source dsn. dsn = "projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId } -func TestQueryContext(t *testing.T) { +func TestExtractDnsParts(t *testing.T) { + tests := []struct { + input string + want connectorConfig + wantErr error + }{ + { + input: "projects/p/instances/i/databases/d", + want: connectorConfig{ + project: "p", + instance: "i", + database: "d", + }, + }, + { + input: "projects/DEFAULT_PROJECT_ID/instances/test-instance/databases/test-database", + want: connectorConfig{ + project: "DEFAULT_PROJECT_ID", + instance: "test-instance", + database: "test-database", + }, + }, + { + input: "localhost:9010/projects/p/instances/i/databases/d", + want: connectorConfig{ + host: "localhost:9010", + project: "p", + instance: "i", + database: "d", + }, + }, + { + input: "spanner.googleapis.com/projects/p/instances/i/databases/d", + want: connectorConfig{ + host: "spanner.googleapis.com", + project: "p", + instance: "i", + database: "d", + }, + }, + { + 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", + }, + }, + }, + { + 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", + }, + }, + }, + { + 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", + }, + }, + }, + } + for _, tc := range tests { + 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) + } + } + } +} +func TestQueryContext(t *testing.T) { // Open db. ctx := context.Background() db, err := sql.Open("spanner", dsn) diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go new file mode 100644 index 00000000..6e11d2d4 --- /dev/null +++ b/driver_with_mockserver_test.go @@ -0,0 +1,103 @@ +package spannerdriver + +import ( + "context" + "database/sql" + "fmt" + + "cloud.google.com/go/spanner" + "cloud.google.com/go/spanner/testutil" + "google.golang.org/api/iterator" + "google.golang.org/api/option" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "testing" +) + +func TestSimpleQuery(t *testing.T) { + t.Parallel() + + _, _, teardown := setupMockedTestServer(t) + defer teardown() + + db, err := sql.Open("spanner", "projects/p/instances/i/databases/d") + if err != nil { + t.Fatal(err) + } + defer db.Close() +} + +func TestClient_Single(t *testing.T) { + t.Parallel() + err := testSingleQuery(t, nil) + if err != nil { + t.Fatal(err) + } +} + +func testSingleQuery(t *testing.T, serverError error) error { + ctx := context.Background() + server, client, teardown := setupMockedTestServer(t) + defer teardown() + if serverError != nil { + server.TestSpanner.SetError(serverError) + } + return executeSingerQuery(ctx, client.Single()) +} + +func executeSingerQuery(ctx context.Context, tx *spanner.ReadOnlyTransaction) error { + return executeSingerQueryWithRowFunc(ctx, tx, nil) +} + +func executeSingerQueryWithRowFunc(ctx context.Context, tx *spanner.ReadOnlyTransaction, f func(rowCount int64) error) error { + iter := tx.Query(ctx, spanner.NewStatement(testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbums)) + defer iter.Stop() + rowCount := int64(0) + for { + row, err := iter.Next() + if err == iterator.Done { + break + } + if err != nil { + return err + } + var singerID, albumID int64 + var albumTitle string + if err := row.Columns(&singerID, &albumID, &albumTitle); err != nil { + return err + } + rowCount++ + if f != nil { + if err := f(rowCount); err != nil { + return err + } + } + } + if rowCount != testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount { + return status.Errorf(codes.Internal, "Row count mismatch, got %v, expected %v", rowCount, testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount) + } + return nil +} + +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() + } +} diff --git a/go.mod b/go.mod index c1ab166b..98b98e14 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,11 @@ go 1.14 require ( cloud.google.com/go/spanner v1.2.1 + github.com/google/go-cmp v0.5.6 // indirect 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 + google.golang.org/api v0.50.0 + google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22 + google.golang.org/grpc v1.39.0 ) + +replace cloud.google.com/go/spanner v1.2.1 => /home/loite/go/src/cloud.google.com/spanner diff --git a/go.sum b/go.sum index 031b23c2..9bbbef8e 100644 --- a/go.sum +++ b/go.sum @@ -8,37 +8,73 @@ cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg 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.86.0 h1:Lo1JDRwMOAxQxTQcbGXi4p60jyMoXNpkmzzzL2Agt5k= +cloud.google.com/go v0.86.0/go.mod h1:YG2MRW8zzPSZaztnTZtxbMPK2VYaHg4NTDYZMG+5ZqQ= 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/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 h1:Kt+gOPPp2LEPWp8CSfxhsM8ik9CcyE/gYu+0r+RnZvM= 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/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= 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/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-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/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= @@ -50,11 +86,32 @@ 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= @@ -62,18 +119,43 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw 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/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 h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= 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/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/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/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= @@ -90,20 +172,34 @@ 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= @@ -114,6 +210,7 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 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= @@ -126,13 +223,23 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl 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.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= 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,17 +250,48 @@ 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= @@ -161,6 +299,11 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ 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,6 +313,7 @@ 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= @@ -177,10 +321,43 @@ golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7w 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 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/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 +380,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= @@ -214,10 +392,35 @@ golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56 h1:DFtSed2q3HtNuVazwVDZ4nS 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 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs= +golang.org/x/tools v0.1.4/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= @@ -227,12 +430,32 @@ google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsb 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 h1:LX7NFCFYOHzr7WHaYiRUpeipZe9o5L8T+2F4Z798VDw= +google.golang.org/api v0.50.0/go.mod h1:4bNT5pAuq5ji4SRZm+5QIkjny9JAyVD/3gaSihNefaw= 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= @@ -250,22 +473,92 @@ google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvx 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-20210701133433-6b8dcf568a95/go.mod h1:yiaVoXHpRzHGyxV3o4DktVWY4mSUErTKaeEOq6C3t3U= +google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22 h1:jcklN/lATdu/CSsNEOLujIgfvY7IMQpRq6HNaPZuVIc= +google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= 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= From ce89c3a5173d0e851231713e029368f2bc0ebcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 20 Jul 2021 09:48:49 +0200 Subject: [PATCH 02/14] test: add tests with mock server --- driver.go | 74 +++++++++++++----------- driver_test.go | 4 ++ driver_with_mockserver_test.go | 102 ++++++++++++++++----------------- 3 files changed, 94 insertions(+), 86 deletions(-) diff --git a/driver.go b/driver.go index e9b9f853..9d556073 100644 --- a/driver.go +++ b/driver.go @@ -19,9 +19,12 @@ import ( "database/sql" "database/sql/driver" "errors" + "fmt" "log" "os" "regexp" + "strconv" + "strings" "time" "cloud.google.com/go/spanner" @@ -34,13 +37,9 @@ import ( ) const userAgent = "go-sql-driver-spanner/0.1" -const dsnRegExpString = "((?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.*))?" -const paramsRegExpString = "(?is)(?P[^=]+?)=(?:.*?)" -const propertyRegExpString = "(?is)(?:;|\\?)%s=([^=]+?)(?:;|$)" +const dsnRegExpString = "((?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 dsnRegExp *regexp.Regexp -var paramsRegExp *regexp.Regexp -var propertyRegExp *regexp.Regexp var _ driver.DriverContext = &Driver{} @@ -51,16 +50,6 @@ func init() { log.Fatalf("could not compile Spanner dsn regexp: %v", err) return } - paramsRegExp, err = regexp.Compile(paramsRegExpString) - if err != nil { - log.Fatalf("could not compile Spanner params regexp: %v", err) - return - } - propertyRegExp, err = regexp.Compile(propertyRegExpString) - if err != nil { - log.Fatalf("could not compile Spanner property regexp: %v", err) - return - } sql.Register("spanner", &Driver{}) } @@ -115,17 +104,20 @@ func extractConnectorConfig(dsn string) (connectorConfig, error) { }, nil } -func extractConnectorParams(params string) (map[string]string, error) { - match := paramsRegExp.FindStringSubmatch(params) - matches := make([]string, 0) - if match != nil { - for i, name := range paramsRegExp.SubexpNames() { - if i != 0 && name != "" { - matches = append(matches, match[i]) - } +func extractConnectorParams(paramsString string) (map[string]string, error) { + params := make(map[string]string) + if paramsString == "" { + return params, nil + } + 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 make(map[string]string), nil + return params, nil } type connector struct { @@ -142,13 +134,27 @@ type connector struct { } func newConnector(d *Driver, dsn string) (*connector, error) { - config, err := extractConnectorConfig(dsn) + connectorConfig, err := extractConnectorConfig(dsn) if err != nil { return nil, err } + 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: config, + driver: d, + connectorConfig: connectorConfig, + config: config, + options: opts, }, nil } @@ -158,23 +164,27 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { func openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { if c.config.NumChannels == 0 { - c.config.NumChannels = 1 // TODO(jbd): Explain database/sql has a high-level management. +// c.config.NumChannels = 1 // TODO(jbd): Explain database/sql has a high-level management. } opts := append(c.options, option.WithUserAgent(userAgent)) - client, err := spanner.NewClientWithConfig(ctx, c.connectorConfig.database, c.config, opts...) + 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.config, opts...) if err != nil { return nil, err } - adminClient, err := createAdminClient(ctx) + adminClient, err := adminapi.NewDatabaseAdminClient(ctx, opts...) if err != nil { return nil, err } - return &conn{client: client, adminClient: adminClient, database: c.connectorConfig.database}, nil + return &conn{client: client, adminClient: adminClient, database: databaseName}, nil } func createAdminClient(ctx context.Context) (adminClient *adminapi.DatabaseAdminClient, err error) { - // Admin client will connect to emulator if SPANNER_EMULATOR_HOST // is set in the environment. if spannerHost, ok := os.LookupEnv("SPANNER_EMULATOR_HOST"); ok { diff --git a/driver_test.go b/driver_test.go index 7455400a..cfbce00b 100644 --- a/driver_test.go +++ b/driver_test.go @@ -60,6 +60,7 @@ func TestExtractDnsParts(t *testing.T) { project: "p", instance: "i", database: "d", + params: map[string]string{}, }, }, { @@ -68,6 +69,7 @@ func TestExtractDnsParts(t *testing.T) { project: "DEFAULT_PROJECT_ID", instance: "test-instance", database: "test-database", + params: map[string]string{}, }, }, { @@ -77,6 +79,7 @@ func TestExtractDnsParts(t *testing.T) { project: "p", instance: "i", database: "d", + params: map[string]string{}, }, }, { @@ -86,6 +89,7 @@ func TestExtractDnsParts(t *testing.T) { project: "p", instance: "i", database: "d", + params: map[string]string{}, }, }, { diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index 6e11d2d4..85db5a56 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -1,82 +1,62 @@ package spannerdriver import ( + "cloud.google.com/go/spanner" + "cloud.google.com/go/spanner/testutil" "context" "database/sql" "fmt" - - "cloud.google.com/go/spanner" - "cloud.google.com/go/spanner/testutil" - "google.golang.org/api/iterator" + "github.com/google/go-cmp/cmp" "google.golang.org/api/option" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "testing" ) -func TestSimpleQuery(t *testing.T) { - t.Parallel() - - _, _, teardown := setupMockedTestServer(t) - defer teardown() - - db, err := sql.Open("spanner", "projects/p/instances/i/databases/d") +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) } - defer db.Close() + return db, server, func() { + db.Close() + serverTeardown() + } } -func TestClient_Single(t *testing.T) { +func TestSimpleQuery(t *testing.T) { t.Parallel() - err := testSingleQuery(t, nil) - if err != nil { - t.Fatal(err) - } -} -func testSingleQuery(t *testing.T, serverError error) error { - ctx := context.Background() - server, client, teardown := setupMockedTestServer(t) + db, server, teardown := setupTestDbConnection(t) defer teardown() - if serverError != nil { - server.TestSpanner.SetError(serverError) + rows, err := db.Query(testutil.SelectFooFromBar) + if err != nil { + t.Fatal(err) } - return executeSingerQuery(ctx, client.Single()) -} + defer rows.Close() -func executeSingerQuery(ctx context.Context, tx *spanner.ReadOnlyTransaction) error { - return executeSingerQueryWithRowFunc(ctx, tx, nil) -} - -func executeSingerQueryWithRowFunc(ctx context.Context, tx *spanner.ReadOnlyTransaction, f func(rowCount int64) error) error { - iter := tx.Query(ctx, spanner.NewStatement(testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbums)) - defer iter.Stop() - rowCount := int64(0) - for { - row, err := iter.Next() - if err == iterator.Done { - break - } + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() if err != nil { - return err + t.Fatal(err) } - var singerID, albumID int64 - var albumTitle string - if err := row.Columns(&singerID, &albumID, &albumTitle); err != nil { - return err + if !cmp.Equal(cols, []string{"FOO"}) { + t.Errorf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) } - rowCount++ - if f != nil { - if err := f(rowCount); err != nil { - return err - } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Errorf("value mismatch\nGot: %v\nWant: %v", got, want) } } - if rowCount != testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount { - return status.Errorf(codes.Internal, "Row count mismatch, got %v, expected %v", rowCount, testutil.SelectSingerIDAlbumIDAlbumTitleFromAlbumsRowCount) - } - return nil + requests := drainRequestsFromServer(server.TestSpanner) + + fmt.Printf("requests: %v\n", requests) } func setupMockedTestServer(t *testing.T) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) { @@ -101,3 +81,17 @@ func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanne serverTeardown() } } + +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 +} From a857ad6fd4d892bfa59e698affb91b5a313cf2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 20 Jul 2021 15:10:52 +0200 Subject: [PATCH 03/14] refactor: transactions --- driver.go | 121 +++-- driver_test.go | 528 +------------------- driver_with_mockserver_test.go | 197 +++++++- integration_test.go | 866 +++++++++++++++++++++++++++++++++ internal/rwtransaction.go | 94 ---- rows_test.go | 303 ------------ stmt.go | 11 +- transaction.go | 87 ++++ tx.go | 78 --- 9 files changed, 1248 insertions(+), 1037 deletions(-) create mode 100644 integration_test.go delete mode 100644 internal/rwtransaction.go create mode 100644 transaction.go delete mode 100644 tx.go diff --git a/driver.go b/driver.go index 9d556073..3e864461 100644 --- a/driver.go +++ b/driver.go @@ -15,21 +15,19 @@ package spannerdriver import ( + "cloud.google.com/go/spanner" "context" "database/sql" "database/sql/driver" "errors" "fmt" + "github.com/rakyll/go-sql-driver-spanner/internal" + "google.golang.org/api/option" "log" "os" "regexp" "strconv" "strings" - "time" - - "cloud.google.com/go/spanner" - "github.com/rakyll/go-sql-driver-spanner/internal" - "google.golang.org/api/option" adminapi "cloud.google.com/go/spanner/admin/database/apiv1" adminpb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" @@ -163,9 +161,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { } func openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { - if c.config.NumChannels == 0 { -// c.config.NumChannels = 1 // TODO(jbd): Explain database/sql has a high-level management. - } opts := append(c.options, option.WithUserAgent(userAgent)) databaseName := fmt.Sprintf( "projects/%s/instances/%s/databases/%s", @@ -210,15 +205,55 @@ func (c *connector) Driver() driver.Driver { } type conn struct { + closed bool client *spanner.Client adminClient *adminapi.DatabaseAdminClient - roTx *spanner.ReadOnlyTransaction - rwTx *rwTx + tx contextTransaction database string } +// Ping implements the 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 + } + stmt, err := c.PrepareContext(ctx, "SELECT 1") + if err != nil { + return err + } + rows, err := stmt.Query([]driver.Value{}) + if err != nil { + return driver.ErrBadConn + } + 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 +} + +func (c *conn) ResetSession(ctx context.Context) error { + if c.closed { + return driver.ErrBadConn + } + if c.inTransaction() { + if err := c.tx.Rollback(); err != nil { + return driver.ErrBadConn + } + } + return nil +} + +func (c *conn) IsValid() bool { + return !c.closed +} + func (c *conn) Prepare(query string) (driver.Stmt, error) { - panic("Using PrepareContext instead") + return nil, fmt.Errorf("use PrepareContext instead") } func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { @@ -230,8 +265,21 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e 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) if err != nil { @@ -252,19 +300,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 @@ -273,7 +318,6 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name } func isDdl(query string) (bool, error) { - matchddl, err := regexp.MatchString(`(?is)^\n*\s*(CREATE|DROP|ALTER)\s+.+$`, query) if err != nil { return false, err @@ -288,7 +332,7 @@ func (c *conn) Close() error { } func (c *conn) Begin() (driver.Tx, error) { - panic("Using BeginTx instead") + return nil, fmt.Errorf("use BeginTx instead") } func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { @@ -297,35 +341,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 cfbce00b..3b7b80c3 100644 --- a/driver_test.go +++ b/driver_test.go @@ -16,38 +16,12 @@ package spannerdriver import ( "context" - "database/sql" - "os" - "reflect" + "database/sql/driver" "testing" "github.com/google/go-cmp/cmp" ) -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 -} - func TestExtractDnsParts(t *testing.T) { tests := []struct { input string @@ -142,143 +116,29 @@ func TestExtractDnsParts(t *testing.T) { } } -func TestQueryContext(t *testing.T) { - // Open db. - ctx := context.Background() - db, err := sql.Open("spanner", dsn) - if err != nil { - t.Fatal(err) +func TestConnection_NoNestedTransactions(t *testing.T) { + c := conn{ + tx: &readOnlyTransaction{}, } - 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 := c.BeginTx(context.Background(), driver.TxOptions{}) + if err == nil { + t.Errorf("unexpected nil error for BeginTx call") } +} - _, 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) +func TestConn_ResetSession(t *testing.T) { + c := conn{ + tx: &readOnlyTransaction{close: func() {}}, } - - type testQueryContextRow struct { - A, B, C string + if err := c.ResetSession(context.Background()); err != nil { + t.Errorf("unexpected nil error for BeginTx call") } - - 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{}, - }, + _, err := c.BeginTx(context.Background(), driver.TxOptions{}) + if err == nil { + t.Errorf("unexpected error for ResetSession call: %v", err) } - - // 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) + if c.tx != nil { + t.Errorf("ResetSession did not clear transaction") } } @@ -404,355 +264,3 @@ func TestIsDdl(t *testing.T) { } } } - -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}, - }, - }, - }, - - { - 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) - } - -} diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index 85db5a56..63eac4ca 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -5,9 +5,14 @@ import ( "cloud.google.com/go/spanner/testutil" "context" "database/sql" + "database/sql/driver" "fmt" "github.com/google/go-cmp/cmp" "google.golang.org/api/option" + sppb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc/codes" + gstatus "google.golang.org/grpc/status" + "reflect" "testing" ) @@ -18,7 +23,7 @@ func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpa fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true", server.Address)) if err != nil { serverTeardown() - t.Fatal(err) + t.Error(err) } return db, server, func() { db.Close() @@ -26,6 +31,28 @@ func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpa } } +func TestPing(t *testing.T) { + t.Parallel() + + db, _, teardown := setupTestDbConnection(t) + defer teardown() + if err := db.Ping(); err != nil { + t.Errorf("unexpected error for ping: %v", err) + } +} + +func TestPing_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.Ping(), driver.ErrBadConn; g != w { + t.Errorf("ping error mismatch\nGot: %v\nWant: %v", g, w) + } +} + func TestSimpleQuery(t *testing.T) { t.Parallel() @@ -33,14 +60,14 @@ func TestSimpleQuery(t *testing.T) { defer teardown() rows, err := db.Query(testutil.SelectFooFromBar) if err != nil { - t.Fatal(err) + t.Error(err) } defer rows.Close() for want := int64(1); rows.Next(); want++ { cols, err := rows.Columns() if err != nil { - t.Fatal(err) + t.Error(err) } if !cmp.Equal(cols, []string{"FOO"}) { t.Errorf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) @@ -48,15 +75,151 @@ func TestSimpleQuery(t *testing.T) { var got int64 err = rows.Scan(&got) if err != nil { - t.Fatal(err) + t.Error(err) } if got != want { t.Errorf("value mismatch\nGot: %v\nWant: %v", got, want) } } requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Errorf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Errorf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetSingleUse() == nil { + t.Errorf("missing single use selector for ExecuteSqlRequest") + } + if req.Transaction.GetSingleUse().GetReadOnly() == nil { + t.Errorf("missing read-only option for ExecuteSqlRequest") + } + if !req.Transaction.GetSingleUse().GetReadOnly().GetStrong() { + t.Errorf("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.Error(err) + } + rows, err := tx.Query(testutil.SelectFooFromBar) + if err != nil { + t.Error(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() + if err != nil { + t.Error(err) + } + if !cmp.Equal(cols, []string{"FOO"}) { + t.Errorf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) + } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Error(err) + } + if got != want { + t.Errorf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + err = tx.Commit() + if err != nil { + t.Error(err) + } + + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Errorf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Errorf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetId() == nil { + t.Errorf("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.Errorf("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.Errorf("begin requests count mismatch\nGot: %v\nWant: %v", g, w) + } +} - fmt.Printf("requests: %v\n", requests) +func TestSimpleReadWriteTransaction(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + tx, err := db.Begin() + if err != nil { + t.Error(err) + } + rows, err := tx.Query(testutil.SelectFooFromBar) + if err != nil { + t.Error(err) + } + defer rows.Close() + + for want := int64(1); rows.Next(); want++ { + cols, err := rows.Columns() + if err != nil { + t.Error(err) + } + if !cmp.Equal(cols, []string{"FOO"}) { + t.Errorf("cols mismatch\nGot: %v\nWant: %v", cols, []string{"FOO"}) + } + var got int64 + err = rows.Scan(&got) + if err != nil { + t.Error(err) + } + if got != want { + t.Errorf("value mismatch\nGot: %v\nWant: %v", got, want) + } + } + err = tx.Commit() + if err != nil { + t.Error(err) + } + + requests := drainRequestsFromServer(server.TestSpanner) + sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) + if g, w := len(sqlRequests), 1; g != w { + t.Errorf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + } + req := sqlRequests[0].(*sppb.ExecuteSqlRequest) + if req.Transaction == nil { + t.Errorf("missing transaction for ExecuteSqlRequest") + } + if req.Transaction.GetId() == nil { + t.Errorf("missing id selector for ExecuteSqlRequest") + } + commitRequests := requestsOfType(requests, reflect.TypeOf(&sppb.CommitRequest{})) + if g, w := len(commitRequests), 1; g != w { + t.Errorf("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.Errorf("transaction id mismatch\nCommit: %c\nExecute: %v", c, e) + } } func setupMockedTestServer(t *testing.T) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) { @@ -74,7 +237,7 @@ func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanne 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) + t.Error(err) } return server, client, func() { client.Close() @@ -82,6 +245,28 @@ func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanne } } +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: diff --git a/integration_test.go b/integration_test.go new file mode 100644 index 00000000..b07e87e3 --- /dev/null +++ b/integration_test.go @@ -0,0 +1,866 @@ +// 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" + "flag" + "log" + "math" + "os" + "reflect" + "testing" +) + +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 +} + +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/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/rows_test.go b/rows_test.go index 16c0552e..70f47edb 100644 --- a/rows_test.go +++ b/rows_test.go @@ -14,306 +14,3 @@ 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..e1d982a3 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,10 +57,8 @@ 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) } diff --git a/transaction.go b/transaction.go new file mode 100644 index 00000000..6255f2cd --- /dev/null +++ b/transaction.go @@ -0,0 +1,87 @@ +// 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" + "context" + "fmt" +) + +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 { + if tx.roTx != nil { + tx.roTx.Close() + } + tx.close() + return nil +} + +func (tx *readOnlyTransaction) Rollback() error { + 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(ctx 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 -} From df4af35d5f8e54a2bab368c17771f094ba3a9649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 20 Jul 2021 21:52:24 +0200 Subject: [PATCH 04/14] feat: add comments parser --- .gitignore | 3 +- driver.go | 7 +- driver_test.go | 17 -- driver_with_mockserver_test.go | 177 ++++++++++----- internal/statement_parser.go | 116 ++++++++++ internal/statement_parser_test.go | 364 ++++++++++++++++++++++++++++++ 6 files changed, 609 insertions(+), 75 deletions(-) create mode 100644 internal/statement_parser.go create mode 100644 internal/statement_parser_test.go 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/driver.go b/driver.go index 3e864461..aa798ff4 100644 --- a/driver.go +++ b/driver.go @@ -218,14 +218,11 @@ func (c *conn) Ping(ctx context.Context) error { if c.closed { return driver.ErrBadConn } - stmt, err := c.PrepareContext(ctx, "SELECT 1") - if err != nil { - return err - } - rows, err := stmt.Query([]driver.Value{}) + 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 diff --git a/driver_test.go b/driver_test.go index 3b7b80c3..afb9f2e1 100644 --- a/driver_test.go +++ b/driver_test.go @@ -126,27 +126,10 @@ func TestConnection_NoNestedTransactions(t *testing.T) { } } -func TestConn_ResetSession(t *testing.T) { - c := conn{ - tx: &readOnlyTransaction{close: func() {}}, - } - if err := c.ResetSession(context.Background()); err != nil { - t.Errorf("unexpected nil error for BeginTx call") - } - _, err := c.BeginTx(context.Background(), driver.TxOptions{}) - if err == nil { - t.Errorf("unexpected error for ResetSession call: %v", err) - } - if c.tx != nil { - t.Errorf("ResetSession did not clear transaction") - } -} - // 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 diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index 63eac4ca..09a4c244 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -16,28 +16,13 @@ import ( "testing" ) -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.Error(err) - } - return db, server, func() { - db.Close() - serverTeardown() - } -} - func TestPing(t *testing.T) { t.Parallel() db, _, teardown := setupTestDbConnection(t) defer teardown() if err := db.Ping(); err != nil { - t.Errorf("unexpected error for ping: %v", err) + t.Fatalf("unexpected error for ping: %v", err) } } @@ -49,7 +34,7 @@ func TestPing_Fails(t *testing.T) { s := gstatus.Newf(codes.PermissionDenied, "Permission denied for database") server.TestSpanner.PutStatementResult("SELECT 1", &testutil.StatementResult{Err: s.Err()}) if g, w := db.Ping(), driver.ErrBadConn; g != w { - t.Errorf("ping error mismatch\nGot: %v\nWant: %v", g, w) + t.Fatalf("ping error mismatch\nGot: %v\nWant: %v", g, w) } } @@ -60,44 +45,47 @@ func TestSimpleQuery(t *testing.T) { defer teardown() rows, err := db.Query(testutil.SelectFooFromBar) if err != nil { - t.Error(err) + t.Fatal(err) } defer rows.Close() for want := int64(1); rows.Next(); want++ { cols, err := rows.Columns() if err != nil { - t.Error(err) + t.Fatal(err) } if !cmp.Equal(cols, []string{"FOO"}) { - t.Errorf("cols mismatch\nGot: %v\nWant: %v", 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.Error(err) + t.Fatal(err) } if got != want { - t.Errorf("value mismatch\nGot: %v\nWant: %v", 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.Errorf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) + t.Fatalf("sql requests count mismatch\nGot: %v\nWant: %v", g, w) } req := sqlRequests[0].(*sppb.ExecuteSqlRequest) if req.Transaction == nil { - t.Errorf("missing transaction for ExecuteSqlRequest") + t.Fatalf("missing transaction for ExecuteSqlRequest") } if req.Transaction.GetSingleUse() == nil { - t.Errorf("missing single use selector for ExecuteSqlRequest") + t.Fatalf("missing single use selector for ExecuteSqlRequest") } if req.Transaction.GetSingleUse().GetReadOnly() == nil { - t.Errorf("missing read-only option for ExecuteSqlRequest") + t.Fatalf("missing read-only option for ExecuteSqlRequest") } if !req.Transaction.GetSingleUse().GetReadOnly().GetStrong() { - t.Errorf("missing strong timestampbound for ExecuteSqlRequest") + t.Fatalf("missing strong timestampbound for ExecuteSqlRequest") } } @@ -109,57 +97,60 @@ func TestSimpleReadOnlyTransaction(t *testing.T) { defer teardown() tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) if err != nil { - t.Error(err) + t.Fatal(err) } rows, err := tx.Query(testutil.SelectFooFromBar) if err != nil { - t.Error(err) + t.Fatal(err) } defer rows.Close() for want := int64(1); rows.Next(); want++ { cols, err := rows.Columns() if err != nil { - t.Error(err) + t.Fatal(err) } if !cmp.Equal(cols, []string{"FOO"}) { - t.Errorf("cols mismatch\nGot: %v\nWant: %v", 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.Error(err) + t.Fatal(err) } if got != want { - t.Errorf("value mismatch\nGot: %v\nWant: %v", 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.Error(err) + t.Fatal(err) } requests := drainRequestsFromServer(server.TestSpanner) sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) if g, w := len(sqlRequests), 1; g != w { - t.Errorf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + t.Fatalf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) } req := sqlRequests[0].(*sppb.ExecuteSqlRequest) if req.Transaction == nil { - t.Errorf("missing transaction for ExecuteSqlRequest") + t.Fatalf("missing transaction for ExecuteSqlRequest") } if req.Transaction.GetId() == nil { - t.Errorf("missing id selector for ExecuteSqlRequest") + 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.Errorf("commit requests count mismatch\nGot: %v\nWant: %v", 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.Errorf("begin requests count mismatch\nGot: %v\nWant: %v", g, w) + t.Fatalf("begin requests count mismatch\nGot: %v\nWant: %v", g, w) } } @@ -170,55 +161,137 @@ func TestSimpleReadWriteTransaction(t *testing.T) { defer teardown() tx, err := db.Begin() if err != nil { - t.Error(err) + t.Fatal(err) } rows, err := tx.Query(testutil.SelectFooFromBar) if err != nil { - t.Error(err) + t.Fatal(err) } defer rows.Close() for want := int64(1); rows.Next(); want++ { cols, err := rows.Columns() if err != nil { - t.Error(err) + t.Fatal(err) } if !cmp.Equal(cols, []string{"FOO"}) { - t.Errorf("cols mismatch\nGot: %v\nWant: %v", 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.Error(err) + t.Fatal(err) } if got != want { - t.Errorf("value mismatch\nGot: %v\nWant: %v", 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.Error(err) + t.Fatal(err) } requests := drainRequestsFromServer(server.TestSpanner) sqlRequests := requestsOfType(requests, reflect.TypeOf(&sppb.ExecuteSqlRequest{})) if g, w := len(sqlRequests), 1; g != w { - t.Errorf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) + t.Fatalf("ExecuteSqlRequests count mismatch\nGot: %v\nWant: %v", g, w) } req := sqlRequests[0].(*sppb.ExecuteSqlRequest) if req.Transaction == nil { - t.Errorf("missing transaction for ExecuteSqlRequest") + t.Fatalf("missing transaction for ExecuteSqlRequest") } if req.Transaction.GetId() == nil { - t.Errorf("missing id selector for ExecuteSqlRequest") + t.Fatalf("missing id selector for ExecuteSqlRequest") } commitRequests := requestsOfType(requests, reflect.TypeOf(&sppb.CommitRequest{})) if g, w := len(commitRequests), 1; g != w { - t.Errorf("commit requests count mismatch\nGot: %v\nWant: %v", 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.Errorf("transaction id mismatch\nCommit: %c\nExecute: %v", 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{ + 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 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() } } @@ -237,7 +310,7 @@ func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanne formattedDatabase := fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]") client, err := spanner.NewClientWithConfig(ctx, formattedDatabase, config, opts...) if err != nil { - t.Error(err) + t.Fatal(err) } return server, client, func() { client.Close() diff --git a/internal/statement_parser.go b/internal/statement_parser.go new file mode 100644 index 00000000..fdb99a67 --- /dev/null +++ b/internal/statement_parser.go @@ -0,0 +1,116 @@ +// 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" +) + +// 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(query 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 uint8 + lastCharWasEscapeChar := false + isTripleQuoted := false + res := strings.Builder{} + res.Grow(len(query)) + index := 0 + for index < len(query) { + c := query[index] + if isInQuoted { + if (c == '\n' || c == '\r') && !isTripleQuoted { + return "", fmt.Errorf("query contains an unclosed literal: %s", query) + } else if c == startQuote { + if lastCharWasEscapeChar { + lastCharWasEscapeChar = false + } else if isTripleQuoted { + if len(query) > index + 2 && query[index+1] == startQuote && query[index+2] == startQuote { + isInQuoted = false + startQuote = 0 + isTripleQuoted = false + res.WriteByte(c) + res.WriteByte(c) + index += 2 + } + } else { + isInQuoted = false + startQuote = 0 + } + } else if c == '\\' { + lastCharWasEscapeChar = true + } else { + lastCharWasEscapeChar = false + } + res.WriteByte(c) + } else { + // We are not in a quoted string. + if isInSingleLineComment { + if c == '\n' { + isInSingleLineComment = false + // Include the line feed in the result. + res.WriteByte(c) + } + } else if isInMultiLineComment { + if len(query) > index + 1 && c == asterisk && query[index+1] == slash { + isInMultiLineComment = false + index++ + } + } else { + if c == dash || (len(query) > index + 1 && c == hyphen && query[index+1] == hyphen) { + // This is a single line comment. + isInSingleLineComment = true + } else if len(query) > index + 1 && c == slash && query[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(query) > index+2 && query[index+1] == startQuote && query[index+2] == startQuote { + isTripleQuoted = true + res.WriteByte(c) + res.WriteByte(c) + index += 2 + } + } + res.WriteByte(c) + } + } + } + index++ + } + if isInQuoted { + return "", fmt.Errorf("query contains an unclosed literal: %s", query) + } + trimmed := strings.TrimSpace(res.String()) + if len(trimmed) > 0 && trimmed[len(trimmed) - 1] == ';' { + return trimmed[:len(trimmed) - 1], nil + } + return trimmed, nil +} diff --git a/internal/statement_parser_test.go b/internal/statement_parser_test.go new file mode 100644 index 00000000..ab214d76 --- /dev/null +++ b/internal/statement_parser_test.go @@ -0,0 +1,364 @@ +// 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" + +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) + } + } +} From f702189bf91f050e6f8387e9079c8b2b7368acef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 21 Jul 2021 22:01:57 +0200 Subject: [PATCH 05/14] feat: parse parameters correctly --- driver.go | 3 +- driver_with_mockserver_test.go | 99 ++++++++++++++++- internal/namedargs.go | 34 ------ internal/statement_parser.go | 175 ++++++++++++++++++++++++++---- internal/statement_parser_test.go | 152 +++++++++++++++++++++++++- rows.go | 29 ++++- stmt.go | 9 +- 7 files changed, 434 insertions(+), 67 deletions(-) delete mode 100644 internal/namedargs.go diff --git a/driver.go b/driver.go index aa798ff4..5bc8da84 100644 --- a/driver.go +++ b/driver.go @@ -254,8 +254,7 @@ func (c *conn) Prepare(query string) (driver.Stmt, error) { } 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) + args, err := internal.FindParams(query) if err != nil { return nil, err } diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index 09a4c244..a7fdcef3 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -1,6 +1,21 @@ +// 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/civil" "cloud.google.com/go/spanner" "cloud.google.com/go/spanner/testutil" "context" @@ -12,8 +27,10 @@ import ( sppb "google.golang.org/genproto/googleapis/spanner/v1" "google.golang.org/grpc/codes" gstatus "google.golang.org/grpc/status" + "math/big" "reflect" "testing" + "time" ) func TestPing(t *testing.T) { @@ -224,7 +241,8 @@ func TestPreparedQuery(t *testing.T) { server.TestSpanner.PutStatementResult( "SELECT * FROM Test WHERE Id=@id", &testutil.StatementResult{ - ResultSet: testutil.CreateSelect1ResultSet(), + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateSelect1ResultSet(), }, ) @@ -280,6 +298,85 @@ func TestPreparedQuery(t *testing.T) { } } +func TestQueryWithAllTypes(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + sql := `SELECT * + FROM Test + WHERE ColBool=@bool + AND ColString=@string + AND ColBytes=@bytes + AND ColInt=@int + AND ColFloat=@float + AND ColNumeric=@numeric + AND ColDate=@date + AND ColTimestamp=@timestamp` + server.TestSpanner.PutStatementResult( + sql, + &testutil.StatementResult{ + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateResultSetWithAllTypes(), + }, + ) + + stmt, err := db.Prepare(sql) + if err != nil { + t.Fatal(err) + } + defer stmt.Close() + rows, err := stmt.QueryContext(context.Background(), true, "test", []byte("test"), int64(5), float64(3.14), big.NewRat(1, 1), civil.Date{Year: 2021, Month: 7, Day: 21}, time.Now()) + 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 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 pt, ok := req.ParamTypes["int"]; 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 @int") + } + if g, w := len(req.Params.Fields), 8; g != w { + t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) + } + if val, ok := req.Params.Fields["int"]; ok { + if g, w := val.GetStringValue(), "5"; g != w { + t.Fatalf("param value mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + t.Fatalf("no value found for param @int") + } +} + func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpannerInMemTestServer, teardown func()) { server, _, serverTeardown := setupMockedTestServer(t) db, err := sql.Open( 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/statement_parser.go b/internal/statement_parser.go index fdb99a67..c1e3be50 100644 --- a/internal/statement_parser.go +++ b/internal/statement_parser.go @@ -17,13 +17,40 @@ package internal import ( "fmt" "strings" + "unicode" ) -// RemoveCommentsAndTrim removes any comments in the query string and trims any +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 +} + +// FindParams returns the named parameters in the given sql string. +func FindParams(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(query string) (string, error) { +func removeCommentsAndTrim(sql string) (string, error) { const singleQuote = '\'' const doubleQuote = '"' const backtick = '`' @@ -34,27 +61,28 @@ func RemoveCommentsAndTrim(query string) (string, error) { isInQuoted := false isInSingleLineComment := false isInMultiLineComment := false - var startQuote uint8 + var startQuote rune lastCharWasEscapeChar := false isTripleQuoted := false res := strings.Builder{} - res.Grow(len(query)) + res.Grow(len(sql)) index := 0 - for index < len(query) { - c := query[index] + runes := []rune(sql) + for index < len(runes) { + c := runes[index] if isInQuoted { if (c == '\n' || c == '\r') && !isTripleQuoted { - return "", fmt.Errorf("query contains an unclosed literal: %s", query) + return "", fmt.Errorf("statement contains an unclosed literal: %s", sql) } else if c == startQuote { if lastCharWasEscapeChar { lastCharWasEscapeChar = false } else if isTripleQuoted { - if len(query) > index + 2 && query[index+1] == startQuote && query[index+2] == startQuote { + if len(runes) > index + 2 && runes[index+1] == startQuote && runes[index+2] == startQuote { isInQuoted = false startQuote = 0 isTripleQuoted = false - res.WriteByte(c) - res.WriteByte(c) + res.WriteRune(c) + res.WriteRune(c) index += 2 } } else { @@ -66,25 +94,25 @@ func RemoveCommentsAndTrim(query string) (string, error) { } else { lastCharWasEscapeChar = false } - res.WriteByte(c) + 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.WriteByte(c) + res.WriteRune(c) } } else if isInMultiLineComment { - if len(query) > index + 1 && c == asterisk && query[index+1] == slash { + if len(runes) > index + 1 && c == asterisk && runes[index+1] == slash { isInMultiLineComment = false index++ } } else { - if c == dash || (len(query) > index + 1 && c == hyphen && query[index+1] == hyphen) { + if c == dash || (len(runes) > index + 1 && c == hyphen && runes[index+1] == hyphen) { // This is a single line comment. isInSingleLineComment = true - } else if len(query) > index + 1 && c == slash && query[index+1] == asterisk { + } else if len(runes) > index + 1 && c == slash && runes[index+1] == asterisk { isInMultiLineComment = true index++ } else { @@ -92,21 +120,21 @@ func RemoveCommentsAndTrim(query string) (string, error) { isInQuoted = true startQuote = c // Check whether it is a triple-quote. - if len(query) > index+2 && query[index+1] == startQuote && query[index+2] == startQuote { + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { isTripleQuoted = true - res.WriteByte(c) - res.WriteByte(c) + res.WriteRune(c) + res.WriteRune(c) index += 2 } } - res.WriteByte(c) + res.WriteRune(c) } } } index++ } if isInQuoted { - return "", fmt.Errorf("query contains an unclosed literal: %s", query) + return "", fmt.Errorf("statement contains an unclosed literal: %s", sql) } trimmed := strings.TrimSpace(res.String()) if len(trimmed) > 0 && trimmed[len(trimmed) - 1] == ';' { @@ -114,3 +142,110 @@ func RemoveCommentsAndTrim(query string) (string, error) { } 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 +} \ No newline at end of file diff --git a/internal/statement_parser_test.go b/internal/statement_parser_test.go index ab214d76..6c529058 100644 --- a/internal/statement_parser_test.go +++ b/internal/statement_parser_test.go @@ -14,7 +14,10 @@ package internal -import "testing" +import ( + "github.com/google/go-cmp/cmp" + "testing" +) func TestRemoveCommentsAndTrim(t *testing.T) { tests := []struct { @@ -348,7 +351,7 @@ SELECT 1`, }, } for _, tc := range tests { - got, err := RemoveCommentsAndTrim(tc.input) + got, err := removeCommentsAndTrim(tc.input) if err != nil && !tc.wantErr { t.Error(err) continue @@ -358,7 +361,150 @@ SELECT 1`, continue } if got != tc.want { - t.Errorf("RemoveCommentsAndTrim result mismatch\nGot: %q\nWant: %q", 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 := FindParams(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("FindParams result mismatch\nGot: %s\nWant: %s", got, tc.want) } } } diff --git a/rows.go b/rows.go index 1e56b8f1..4da3d8a3 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,10 @@ 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 + return err } else { var err error row, err = r.it.Next() // returns io.EOF when there is no next @@ -106,6 +117,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 { diff --git a/stmt.go b/stmt.go index e1d982a3..5ed87c8b 100644 --- a/stmt.go +++ b/stmt.go @@ -65,11 +65,18 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv 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.FindParams(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 From 2172053c9815f40982b53184cb94ecc3ad28bf3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Thu, 22 Jul 2021 19:31:54 +0200 Subject: [PATCH 06/14] feat: support null params --- README.md | 26 +-- driver.go | 22 +-- driver_test.go | 46 ++--- driver_with_mockserver_test.go | 309 ++++++++++++++++++++++++++++-- go.mod | 4 +- go.sum | 45 +---- internal/statement_parser.go | 31 +-- internal/statement_parser_test.go | 26 +-- rows.go | 2 +- rows_test.go | 1 - stmt.go | 2 +- 11 files changed, 373 insertions(+), 141 deletions(-) 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 5bc8da84..3a187928 100644 --- a/driver.go +++ b/driver.go @@ -72,11 +72,11 @@ func (d *Driver) OpenConnector(name string) (driver.Connector, error) { } type connectorConfig struct { - host string - project string + host string + project string instance string database string - params map[string]string + params map[string]string } func extractConnectorConfig(dsn string) (connectorConfig, error) { @@ -94,11 +94,11 @@ func extractConnectorConfig(dsn string) (connectorConfig, error) { } return connectorConfig{ - host: matches["HOSTGROUP"], - project: matches["PROJECTGROUP"], + host: matches["HOSTGROUP"], + project: matches["PROJECTGROUP"], instance: matches["INSTANCEGROUP"], database: matches["DATABASEGROUP"], - params: params, + params: params, }, nil } @@ -119,7 +119,7 @@ func extractConnectorParams(paramsString string) (map[string]string, error) { } type connector struct { - driver *Driver + driver *Driver connectorConfig connectorConfig // config represents the optional advanced configuration to be used @@ -142,7 +142,7 @@ func newConnector(d *Driver, dsn string) (*connector, error) { } 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()) + opts = append(opts, option.WithGRPCDialOption(grpc.WithInsecure()), option.WithoutAuthentication()) } } config := spanner.ClientConfig{ @@ -254,7 +254,7 @@ func (c *conn) Prepare(query string) (driver.Stmt, error) { } func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { - args, err := internal.FindParams(query) + args, err := internal.ParseNamedParameters(query) if err != nil { return nil, err } @@ -352,8 +352,8 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e return nil, err } c.tx = &readWriteTransaction{ - ctx: ctx, - rwTx: tx, + ctx: ctx, + rwTx: tx, close: func() { c.tx = nil }, diff --git a/driver_test.go b/driver_test.go index afb9f2e1..95f4bb2b 100644 --- a/driver_test.go +++ b/driver_test.go @@ -31,75 +31,75 @@ func TestExtractDnsParts(t *testing.T) { { input: "projects/p/instances/i/databases/d", want: connectorConfig{ - project: "p", + project: "p", instance: "i", database: "d", - params: map[string]string{}, + params: map[string]string{}, }, }, { input: "projects/DEFAULT_PROJECT_ID/instances/test-instance/databases/test-database", want: connectorConfig{ - project: "DEFAULT_PROJECT_ID", + project: "DEFAULT_PROJECT_ID", instance: "test-instance", database: "test-database", - params: map[string]string{}, + params: map[string]string{}, }, }, { input: "localhost:9010/projects/p/instances/i/databases/d", want: connectorConfig{ - host: "localhost:9010", - project: "p", + host: "localhost:9010", + project: "p", instance: "i", database: "d", - params: map[string]string{}, + params: map[string]string{}, }, }, { input: "spanner.googleapis.com/projects/p/instances/i/databases/d", want: connectorConfig{ - host: "spanner.googleapis.com", - project: "p", + host: "spanner.googleapis.com", + project: "p", instance: "i", database: "d", - params: map[string]string{}, + params: map[string]string{}, }, }, { input: "spanner.googleapis.com/projects/p/instances/i/databases/d?usePlainText=true", want: connectorConfig{ - host: "spanner.googleapis.com", - project: "p", + host: "spanner.googleapis.com", + project: "p", instance: "i", database: "d", - params: map[string]string { - "usePlainText":"true", + params: map[string]string{ + "usePlainText": "true", }, }, }, { input: "spanner.googleapis.com/projects/p/instances/i/databases/d;credentials=/path/to/credentials.json", want: connectorConfig{ - host: "spanner.googleapis.com", - project: "p", + host: "spanner.googleapis.com", + project: "p", instance: "i", database: "d", - params: map[string]string { - "credentials":"/path/to/credentials.json", + params: map[string]string{ + "credentials": "/path/to/credentials.json", }, }, }, { 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", + host: "spanner.googleapis.com", + project: "p", instance: "i", database: "d", - params: map[string]string { - "credentials":"/path/to/credentials.json", - "readonly":"true", + params: map[string]string{ + "credentials": "/path/to/credentials.json", + "readonly": "true", }, }, }, diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index a7fdcef3..a952345c 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -21,9 +21,15 @@ import ( "context" "database/sql" "database/sql/driver" + "encoding/base64" "fmt" + "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" "google.golang.org/api/option" + longrunningpb "google.golang.org/genproto/googleapis/longrunning" sppb "google.golang.org/genproto/googleapis/spanner/v1" "google.golang.org/grpc/codes" gstatus "google.golang.org/grpc/status" @@ -241,7 +247,7 @@ func TestPreparedQuery(t *testing.T) { server.TestSpanner.PutStatementResult( "SELECT * FROM Test WHERE Id=@id", &testutil.StatementResult{ - Type: testutil.StatementResultResultSet, + Type: testutil.StatementResultResultSet, ResultSet: testutil.CreateSelect1ResultSet(), }, ) @@ -308,15 +314,15 @@ func TestQueryWithAllTypes(t *testing.T) { WHERE ColBool=@bool AND ColString=@string AND ColBytes=@bytes - AND ColInt=@int - AND ColFloat=@float + AND ColInt=@int64 + AND ColFloat=@float64 AND ColNumeric=@numeric AND ColDate=@date AND ColTimestamp=@timestamp` server.TestSpanner.PutStatementResult( sql, &testutil.StatementResult{ - Type: testutil.StatementResultResultSet, + Type: testutil.StatementResultResultSet, ResultSet: testutil.CreateResultSetWithAllTypes(), }, ) @@ -326,7 +332,17 @@ func TestQueryWithAllTypes(t *testing.T) { t.Fatal(err) } defer stmt.Close() - rows, err := stmt.QueryContext(context.Background(), true, "test", []byte("test"), int64(5), float64(3.14), big.NewRat(1, 1), civil.Date{Year: 2021, Month: 7, Day: 21}, time.Now()) + 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) } @@ -345,6 +361,32 @@ func TestQueryWithAllTypes(t *testing.T) { 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()) @@ -358,25 +400,262 @@ func TestQueryWithAllTypes(t *testing.T) { if g, w := len(req.ParamTypes), 8; g != w { t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w) } - if pt, ok := req.ParamTypes["int"]; ok { - if g, w := pt.Code, sppb.TypeCode_INT64; g != w { - t.Fatalf("param type 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) } - } else { - t.Fatalf("no param type found for @int") + } +} + +func TestQueryWithNullParameters(t *testing.T) { + t.Parallel() + + db, server, teardown := setupTestDbConnection(t) + defer teardown() + sql := `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( + sql, + &testutil.StatementResult{ + Type: testutil.StatementResultResultSet, + ResultSet: testutil.CreateResultSetWithAllTypes(), + }, + ) + + stmt, err := db.Prepare(sql) + 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) } - if val, ok := req.Params.Fields["int"]; ok { - if g, w := val.GetStringValue(), "5"; g != w { - t.Fatalf("param value 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{}) } - } else { - t.Fatalf("no value found for param @int") } } +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 = &emptypb.Empty{} + any, _ := ptypes.MarshalAny(expectedResponse) + server.TestDatabaseAdmin.SetResps([]proto.Message{ + &longrunningpb.Operation{ + Done: true, + Result: &longrunningpb.Operation_Response{Response: any}, + Name: "test-operation", + }, + }) + res, err := db.ExecContext(context.Background(), "CREATE TABLE Singers (SingerId INT64, FirstName STRING(100), LastName STRING(100)) PRIMARY KEY (SingerId)") + 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) + } +} + +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( diff --git a/go.mod b/go.mod index 98b98e14..ec9958d8 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,13 @@ module github.com/rakyll/go-sql-driver-spanner go 1.14 require ( + cloud.google.com/go v0.87.0 cloud.google.com/go/spanner v1.2.1 + github.com/golang/protobuf v1.5.2 github.com/google/go-cmp v0.5.6 // indirect github.com/jinzhu/gorm v1.9.12 google.golang.org/api v0.50.0 - google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22 + google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 google.golang.org/grpc v1.39.0 ) diff --git a/go.sum b/go.sum index 9bbbef8e..67e70d77 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,6 @@ 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= @@ -21,33 +20,26 @@ cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb 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.86.0 h1:Lo1JDRwMOAxQxTQcbGXi4p60jyMoXNpkmzzzL2Agt5k= -cloud.google.com/go v0.86.0/go.mod h1:YG2MRW8zzPSZaztnTZtxbMPK2VYaHg4NTDYZMG+5ZqQ= +cloud.google.com/go v0.87.0 h1:8ZtzmY4a2JIO2sljMbpqkDYxA8aJQveYr3AMa+X40oc= +cloud.google.com/go v0.87.0/go.mod h1:TpDYlFy7vuLzZMMZ+B6iRiELaY7z/gJPaqbMx6mlWcY= 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/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 h1:Kt+gOPPp2LEPWp8CSfxhsM8ik9CcyE/gYu+0r+RnZvM= 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/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= -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/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= @@ -77,7 +69,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/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/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= @@ -95,7 +86,6 @@ github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+Licev 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= @@ -117,7 +107,6 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ 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/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= @@ -128,7 +117,6 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ 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 h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= 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= @@ -156,9 +144,7 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ 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/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/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/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= @@ -186,7 +172,6 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 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= @@ -208,7 +193,6 @@ 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= @@ -221,7 +205,6 @@ 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= @@ -233,7 +216,6 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG 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.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= 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= @@ -254,7 +236,6 @@ golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLL 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= @@ -281,7 +262,6 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG 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= @@ -297,7 +277,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ 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= @@ -319,7 +298,6 @@ golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7w 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= @@ -351,7 +329,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn 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= @@ -388,10 +365,7 @@ 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= @@ -413,11 +387,11 @@ 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 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs= 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= @@ -428,7 +402,6 @@ 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= @@ -451,7 +424,6 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl 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= @@ -471,7 +443,6 @@ 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= @@ -502,9 +473,9 @@ google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxH 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-20210701133433-6b8dcf568a95/go.mod h1:yiaVoXHpRzHGyxV3o4DktVWY4mSUErTKaeEOq6C3t3U= -google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22 h1:jcklN/lATdu/CSsNEOLujIgfvY7IMQpRq6HNaPZuVIc= -google.golang.org/genproto v0.0.0-20210715145939-324b959e9c22/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= +google.golang.org/genproto v0.0.0-20210713002101-d411969a0d9a/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= +google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 h1:7yQQsvnwjfEahbNNEKcBHv3mR+HnB1ctGY/z1JXzx8M= +google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492/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= @@ -512,7 +483,6 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac 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= @@ -555,7 +525,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh 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= diff --git a/internal/statement_parser.go b/internal/statement_parser.go index c1e3be50..7b6a4d06 100644 --- a/internal/statement_parser.go +++ b/internal/statement_parser.go @@ -26,7 +26,7 @@ var dmlStatements = map[string]bool{"INSERT": true, "UPDATE": true, "DELETE": tr 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)) + res := make(map[string]bool, len(m1)+len(m2)) for k, v := range m1 { res[k] = v } @@ -36,8 +36,12 @@ func union(m1 map[string]bool, m2 map[string]bool) map[string]bool { return res } -// FindParams returns the named parameters in the given sql string. -func FindParams(sql string) ([]string, error) { +// 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 @@ -77,7 +81,7 @@ func removeCommentsAndTrim(sql string) (string, error) { if lastCharWasEscapeChar { lastCharWasEscapeChar = false } else if isTripleQuoted { - if len(runes) > index + 2 && runes[index+1] == startQuote && runes[index+2] == startQuote { + if len(runes) > index+2 && runes[index+1] == startQuote && runes[index+2] == startQuote { isInQuoted = false startQuote = 0 isTripleQuoted = false @@ -104,15 +108,15 @@ func removeCommentsAndTrim(sql string) (string, error) { res.WriteRune(c) } } else if isInMultiLineComment { - if len(runes) > index + 1 && c == asterisk && runes[index+1] == slash { + 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) { + 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 { + } else if len(runes) > index+1 && c == slash && runes[index+1] == asterisk { isInMultiLineComment = true index++ } else { @@ -137,13 +141,12 @@ func removeCommentsAndTrim(sql string) (string, error) { 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 + 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 { @@ -158,7 +161,7 @@ func removeStatementHint(sql string) string { // Statement hints are allowed for both queries and DML statements. startQueryIndex := -1 upperCaseSql := strings.ToUpper(sql) - for keyword, _ := range selectAndDmlStatements { + for keyword := range selectAndDmlStatements { if startQueryIndex = strings.Index(upperCaseSql, keyword); startQueryIndex > -1 { break } @@ -170,7 +173,7 @@ func removeStatementHint(sql string) string { // and let the caller handle the invalid query. return sql } - return strings.TrimSpace(sql[endStatementHintIndex + 1:]) + return strings.TrimSpace(sql[endStatementHintIndex+1:]) } // Seems invalid, just return the original statement. return sql @@ -224,7 +227,7 @@ func findParams(sql string) ([]string, error) { res = append(res, string(runes[startIndex:index])) break } - if index == len(runes) - 1 { + if index == len(runes)-1 { res = append(res, string(runes[startIndex:])) break } @@ -248,4 +251,4 @@ func findParams(sql string) ([]string, error) { return nil, fmt.Errorf("statement contains an unclosed literal: %s", sql) } return res, nil -} \ No newline at end of file +} diff --git a/internal/statement_parser_test.go b/internal/statement_parser_test.go index 6c529058..f54313b4 100644 --- a/internal/statement_parser_test.go +++ b/internal/statement_parser_test.go @@ -27,11 +27,11 @@ func TestRemoveCommentsAndTrim(t *testing.T) { }{ { input: ``, - want: ``, + want: ``, }, { input: `SELECT 1;`, - want: `SELECT 1`, + want: `SELECT 1`, }, { input: `-- This is a single line comment @@ -105,7 +105,7 @@ SELECT 1;`, input: `-- First comment SELECT--second comment 1`, - want: `SELECT + want: `SELECT 1`, }, { @@ -145,7 +145,7 @@ SELECT/* second comment */ }, { input: `SELECT "TEST -- This is not a comment"`, - want: `SELECT "TEST -- This is not a comment"`, + want: `SELECT "TEST -- This is not a comment"`, }, { input: `-- This is a comment @@ -159,7 +159,7 @@ SELECT "TEST -- This is not a comment" -- This is a comment`, }, { input: `SELECT "TEST # This is not a comment"`, - want: `SELECT "TEST # This is not a comment"`, + want: `SELECT "TEST # This is not a comment"`, }, { input: `# This is a comment @@ -173,7 +173,7 @@ SELECT "TEST # This is not a comment" # This is a comment`, }, { input: `SELECT "TEST /* This is not a comment */"`, - want: `SELECT "TEST /* This is not a comment */"`, + want: `SELECT "TEST /* This is not a comment */"`, }, { input: `/* This is a comment */ @@ -187,7 +187,7 @@ SELECT "TEST /* This is not a comment */" /* This is a comment */`, }, { input: `SELECT 'TEST -- This is not a comment'`, - want: `SELECT 'TEST -- This is not a comment'`, + want: `SELECT 'TEST -- This is not a comment'`, }, { input: `-- This is a comment @@ -201,7 +201,7 @@ SELECT 'TEST -- This is not a comment' -- This is a comment`, }, { input: `SELECT 'TEST # This is not a comment'`, - want: `SELECT 'TEST # This is not a comment'`, + want: `SELECT 'TEST # This is not a comment'`, }, { input: `# This is a comment @@ -215,7 +215,7 @@ SELECT 'TEST # This is not a comment' # This is a comment`, }, { input: `SELECT 'TEST /* This is not a comment */'`, - want: `SELECT 'TEST /* This is not a comment */'`, + want: `SELECT 'TEST /* This is not a comment */'`, }, { input: `/* This is a comment */ @@ -332,7 +332,7 @@ SELECT """TEST { input: `/* This is a comment /* this is still a comment */ SELECT 1`, - want: `SELECT 1`, + want: `SELECT 1`, }, { input: `/** This is a javadoc style comment /* this is still a comment */ @@ -482,7 +482,7 @@ func TestFindParams(t *testing.T) { input: `SELECT * FROM """strange @table """ WHERE Name like @name AND Email='test@test.com'`, - want: []string{"name"}, + want: []string{"name"}, }, { input: `@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable WHERE Name like @name AND Email='test@test.com'`, @@ -494,7 +494,7 @@ func TestFindParams(t *testing.T) { if err != nil { t.Fatal(err) } - got, err := FindParams(removeStatementHint(sql)) + got, err := ParseNamedParameters(removeStatementHint(sql)) if err != nil && !tc.wantErr { t.Error(err) continue @@ -504,7 +504,7 @@ func TestFindParams(t *testing.T) { continue } if !cmp.Equal(got, tc.want) { - t.Errorf("FindParams result mismatch\nGot: %s\nWant: %s", got, tc.want) + t.Errorf("ParseNamedParameters result mismatch\nGot: %s\nWant: %s", got, tc.want) } } } diff --git a/rows.go b/rows.go index 4da3d8a3..db975731 100644 --- a/rows.go +++ b/rows.go @@ -150,7 +150,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 70f47edb..4d2472fe 100644 --- a/rows_test.go +++ b/rows_test.go @@ -13,4 +13,3 @@ // limitations under the License. package spannerdriver - diff --git a/stmt.go b/stmt.go index 5ed87c8b..d3bdbf71 100644 --- a/stmt.go +++ b/stmt.go @@ -70,7 +70,7 @@ func (s *stmt) CheckNamedValue(value *driver.NamedValue) error { } func prepareSpannerStmt(q string, args []driver.NamedValue) (spanner.Statement, error) { - names, err := internal.FindParams(q) + names, err := internal.ParseNamedParameters(q) if err != nil { return spanner.Statement{}, err } From a3d0b913a261658665210b845b110841c890d5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Sat, 24 Jul 2021 08:14:31 +0200 Subject: [PATCH 07/14] test: add more tests --- driver.go | 22 ++-- driver_test.go | 122 -------------------- driver_with_mockserver_test.go | 182 ++++++++++++++++++++++++++---- internal/statement_parser.go | 18 ++- internal/statement_parser_test.go | 150 +++++++++++++++++++++++- transaction.go | 13 ++- 6 files changed, 342 insertions(+), 165 deletions(-) diff --git a/driver.go b/driver.go index 3a187928..7623f968 100644 --- a/driver.go +++ b/driver.go @@ -15,20 +15,21 @@ package spannerdriver import ( - "cloud.google.com/go/spanner" "context" "database/sql" "database/sql/driver" "errors" "fmt" - "github.com/rakyll/go-sql-driver-spanner/internal" - "google.golang.org/api/option" "log" "os" "regexp" "strconv" "strings" + "cloud.google.com/go/spanner" + "github.com/rakyll/go-sql-driver-spanner/internal" + "google.golang.org/api/option" + adminapi "cloud.google.com/go/spanner/admin/database/apiv1" adminpb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" "google.golang.org/grpc" @@ -277,12 +278,16 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam 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.database, Statements: []string{query}, @@ -313,15 +318,6 @@ 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 diff --git a/driver_test.go b/driver_test.go index 95f4bb2b..adb55f2a 100644 --- a/driver_test.go +++ b/driver_test.go @@ -125,125 +125,3 @@ func TestConnection_NoNestedTransactions(t *testing.T) { t.Errorf("unexpected nil error for BeginTx call") } } - -// 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) - } - } -} diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index a952345c..db8214ee 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -15,14 +15,19 @@ package spannerdriver import ( - "cloud.google.com/go/civil" - "cloud.google.com/go/spanner" - "cloud.google.com/go/spanner/testutil" "context" "database/sql" "database/sql/driver" "encoding/base64" "fmt" + "math/big" + "reflect" + "testing" + "time" + + "cloud.google.com/go/civil" + "cloud.google.com/go/spanner" + "cloud.google.com/go/spanner/testutil" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" emptypb "github.com/golang/protobuf/ptypes/empty" @@ -30,33 +35,30 @@ import ( "github.com/google/go-cmp/cmp" "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" - "math/big" - "reflect" - "testing" - "time" ) -func TestPing(t *testing.T) { +func TestPingContext(t *testing.T) { t.Parallel() db, _, teardown := setupTestDbConnection(t) defer teardown() - if err := db.Ping(); err != nil { + if err := db.PingContext(context.Background()); err != nil { t.Fatalf("unexpected error for ping: %v", err) } } -func TestPing_Fails(t *testing.T) { +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.Ping(), driver.ErrBadConn; g != w { + _ = 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) } } @@ -66,7 +68,7 @@ func TestSimpleQuery(t *testing.T) { db, server, teardown := setupTestDbConnection(t) defer teardown() - rows, err := db.Query(testutil.SelectFooFromBar) + rows, err := db.QueryContext(context.Background(), testutil.SelectFooFromBar) if err != nil { t.Fatal(err) } @@ -244,7 +246,7 @@ func TestPreparedQuery(t *testing.T) { db, server, teardown := setupTestDbConnection(t) defer teardown() - server.TestSpanner.PutStatementResult( + _ = server.TestSpanner.PutStatementResult( "SELECT * FROM Test WHERE Id=@id", &testutil.StatementResult{ Type: testutil.StatementResultResultSet, @@ -307,9 +309,10 @@ func TestPreparedQuery(t *testing.T) { func TestQueryWithAllTypes(t *testing.T) { t.Parallel() + // TODO: Add ARRAY types. db, server, teardown := setupTestDbConnection(t) defer teardown() - sql := `SELECT * + query := `SELECT * FROM Test WHERE ColBool=@bool AND ColString=@string @@ -319,15 +322,15 @@ func TestQueryWithAllTypes(t *testing.T) { AND ColNumeric=@numeric AND ColDate=@date AND ColTimestamp=@timestamp` - server.TestSpanner.PutStatementResult( - sql, + _ = server.TestSpanner.PutStatementResult( + query, &testutil.StatementResult{ Type: testutil.StatementResultResultSet, ResultSet: testutil.CreateResultSetWithAllTypes(), }, ) - stmt, err := db.Prepare(sql) + stmt, err := db.Prepare(query) if err != nil { t.Fatal(err) } @@ -479,9 +482,10 @@ func TestQueryWithAllTypes(t *testing.T) { func TestQueryWithNullParameters(t *testing.T) { t.Parallel() + // TODO: Add ARRAY types. db, server, teardown := setupTestDbConnection(t) defer teardown() - sql := `SELECT * + query := `SELECT * FROM Test WHERE ColBool=@bool AND ColString=@string @@ -491,15 +495,15 @@ func TestQueryWithNullParameters(t *testing.T) { AND ColNumeric=@numeric AND ColDate=@date AND ColTimestamp=@timestamp` - server.TestSpanner.PutStatementResult( - sql, + _ = server.TestSpanner.PutStatementResult( + query, &testutil.StatementResult{ Type: testutil.StatementResultResultSet, ResultSet: testutil.CreateResultSetWithAllTypes(), }, ) - stmt, err := db.Prepare(sql) + stmt, err := db.Prepare(query) if err != nil { t.Fatal(err) } @@ -629,7 +633,50 @@ func TestDdlInAutocommit(t *testing.T) { db, server, teardown := setupTestDbConnection(t) defer teardown() - var expectedResponse *emptypb.Empty = &emptypb.Empty{} + 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{ @@ -638,7 +685,12 @@ func TestDdlInAutocommit(t *testing.T) { Name: "test-operation", }, }) - res, err := db.ExecContext(context.Background(), "CREATE TABLE Singers (SingerId INT64, FirstName STRING(100), LastName STRING(100)) PRIMARY KEY (SingerId)") + 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) } @@ -649,6 +701,86 @@ func TestDdlInAutocommit(t *testing.T) { 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 { @@ -666,7 +798,7 @@ func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpa t.Fatal(err) } return db, server, func() { - db.Close() + _ = db.Close() serverTeardown() } } diff --git a/internal/statement_parser.go b/internal/statement_parser.go index 7b6a4d06..6843c715 100644 --- a/internal/statement_parser.go +++ b/internal/statement_parser.go @@ -50,7 +50,7 @@ func ParseNamedParameters(sql string) ([]string, error) { return findParams(sql) } -// removeCommentsAndTrim removes any comments in the query string and trims any +// 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. @@ -252,3 +252,19 @@ func findParams(sql string) ([]string, error) { } 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 index f54313b4..7d53ef7c 100644 --- a/internal/statement_parser_test.go +++ b/internal/statement_parser_test.go @@ -15,8 +15,9 @@ package internal import ( - "github.com/google/go-cmp/cmp" "testing" + + "github.com/google/go-cmp/cmp" ) func TestRemoveCommentsAndTrim(t *testing.T) { @@ -508,3 +509,150 @@ func TestFindParams(t *testing.T) { } } } + +// 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/transaction.go b/transaction.go index 6255f2cd..2f41413f 100644 --- a/transaction.go +++ b/transaction.go @@ -15,11 +15,14 @@ package spannerdriver import ( - "cloud.google.com/go/spanner" "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 @@ -33,6 +36,8 @@ type readOnlyTransaction struct { } 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() } @@ -41,6 +46,8 @@ func (tx *readOnlyTransaction) Commit() error { } 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() } @@ -52,8 +59,8 @@ func (tx *readOnlyTransaction) Query(ctx context.Context, stmt spanner.Statement return tx.roTx.Query(ctx, stmt) } -func (tx *readOnlyTransaction) ExecContext(ctx context.Context, stmt spanner.Statement) (int64, error) { - return 0, fmt.Errorf("Read-only transactions cannot write") +func (tx *readOnlyTransaction) ExecContext(_ context.Context, stmt spanner.Statement) (int64, error) { + return 0, fmt.Errorf("read-only transactions cannot write") } type readWriteTransaction struct { From ea44f34b0dbe27f11f3c0fcb4396def42dda46a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 27 Jul 2021 11:26:42 +0200 Subject: [PATCH 08/14] docs: add comments --- driver.go | 11 +++++++---- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/driver.go b/driver.go index 3a187928..ef11f6db 100644 --- a/driver.go +++ b/driver.go @@ -212,7 +212,7 @@ type conn struct { database string } -// Ping implements the Pinger interface. +// Ping implements the sql.Pinger interface. // returns ErrBadConn if the connection is no longer valid. func (c *conn) Ping(ctx context.Context) error { if c.closed { @@ -233,7 +233,9 @@ func (c *conn) Ping(ctx context.Context) error { return nil } -func (c *conn) ResetSession(ctx context.Context) error { +// ResetSession implements the sql.SessionResetter interface. +// returns ErrBadConn if the connection is no longer valid. +func (c *conn) ResetSession(_ context.Context) error { if c.closed { return driver.ErrBadConn } @@ -245,15 +247,16 @@ func (c *conn) ResetSession(ctx context.Context) error { return nil } +// IsValid implements the sql.Validator interface. func (c *conn) IsValid() bool { return !c.closed } func (c *conn) Prepare(query string) (driver.Stmt, error) { - return nil, fmt.Errorf("use PrepareContext instead") + return c.PrepareContext(context.Background(), query) } -func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { +func (c *conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) { args, err := internal.ParseNamedParameters(query) if err != nil { return nil, err diff --git a/go.mod b/go.mod index ec9958d8..dcf85f3b 100644 --- a/go.mod +++ b/go.mod @@ -13,4 +13,4 @@ require ( google.golang.org/grpc v1.39.0 ) -replace cloud.google.com/go/spanner v1.2.1 => /home/loite/go/src/cloud.google.com/spanner +replace cloud.google.com/go/spanner v1.2.1 => /Users/loite/go/src/google-cloud-go/spanner diff --git a/go.sum b/go.sum index 67e70d77..a0aa2507 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +<<<<<<< Updated upstream cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= @@ -474,6 +475,7 @@ google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08/go.mod h1:UODoCrxH 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-20210715145939-324b959e9c22/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 h1:7yQQsvnwjfEahbNNEKcBHv3mR+HnB1ctGY/z1JXzx8M= google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= From 48822dc35db0b47df7f0339e65abab98bcbbb5f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 27 Jul 2021 12:43:07 +0200 Subject: [PATCH 09/14] build: fix integration tests + add GitHub actions --- .github/workflows/unit-tests.yml | 18 +++++++++ driver.go | 6 +-- integration_test.go | 69 ++++++++++++++++++++++++++++++++ rows.go | 3 ++ 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/unit-tests.yml 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/driver.go b/driver.go index 6919903f..ecda92d4 100644 --- a/driver.go +++ b/driver.go @@ -213,7 +213,7 @@ type conn struct { database string } -// Ping implements the sql.Pinger interface. +// 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 { @@ -234,7 +234,7 @@ func (c *conn) Ping(ctx context.Context) error { return nil } -// ResetSession implements the sql.SessionResetter interface. +// 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 { @@ -248,7 +248,7 @@ func (c *conn) ResetSession(_ context.Context) error { return nil } -// IsValid implements the sql.Validator interface. +// IsValid implements the driver.Validator interface. func (c *conn) IsValid() bool { return !c.closed } diff --git a/integration_test.go b/integration_test.go index b07e87e3..fdcb76d0 100644 --- a/integration_test.go +++ b/integration_test.go @@ -15,14 +15,22 @@ 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 ( @@ -47,6 +55,67 @@ func init() { // 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()) { diff --git a/rows.go b/rows.go index db975731..fdfa0307 100644 --- a/rows.go +++ b/rows.go @@ -87,6 +87,9 @@ func (r *rows) Next(dest []driver.Value) error { } else if r.dirtyErr != nil { err := r.dirtyErr r.dirtyErr = nil + if err == iterator.Done { + return io.EOF + } return err } else { var err error From 92b8386872c7fd6ee5dd02fa8373e5cc8b9c20d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 27 Jul 2021 13:12:33 +0200 Subject: [PATCH 10/14] build: copy testutil to sql-driver and update Spanner version --- driver_with_mockserver_test.go | 2 +- go.mod | 13 +- go.sum | 31 +- testutil/inmem_database_admin_server.go | 91 ++ testutil/inmem_instance_admin_server.go | 86 ++ testutil/inmem_instance_admin_server_test.go | 95 ++ testutil/inmem_spanner_server.go | 1087 ++++++++++++++++++ testutil/inmem_spanner_server_test.go | 605 ++++++++++ testutil/mocked_inmem_server.go | 318 +++++ 9 files changed, 2301 insertions(+), 27 deletions(-) create mode 100644 testutil/inmem_database_admin_server.go create mode 100644 testutil/inmem_instance_admin_server.go create mode 100644 testutil/inmem_instance_admin_server_test.go create mode 100644 testutil/inmem_spanner_server.go create mode 100644 testutil/inmem_spanner_server_test.go create mode 100644 testutil/mocked_inmem_server.go diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index db8214ee..ccd4032f 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -27,12 +27,12 @@ import ( "cloud.google.com/go/civil" "cloud.google.com/go/spanner" - "cloud.google.com/go/spanner/testutil" "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" diff --git a/go.mod b/go.mod index dcf85f3b..2a039fe3 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,11 @@ module github.com/rakyll/go-sql-driver-spanner go 1.14 require ( - cloud.google.com/go v0.87.0 - cloud.google.com/go/spanner v1.2.1 + 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 // indirect - github.com/jinzhu/gorm v1.9.12 - google.golang.org/api v0.50.0 - google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 + 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 ) - -replace cloud.google.com/go/spanner v1.2.1 => /Users/loite/go/src/google-cloud-go/spanner diff --git a/go.sum b/go.sum index a0aa2507..a816f653 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -<<<<<<< Updated upstream cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= @@ -21,8 +20,9 @@ cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb 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 h1:8ZtzmY4a2JIO2sljMbpqkDYxA8aJQveYr3AMa+X40oc= 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/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc= @@ -35,6 +35,8 @@ cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2k cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw= cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA= 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/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= @@ -54,7 +56,6 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht 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= @@ -63,13 +64,10 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m 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-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/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/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= @@ -135,6 +133,7 @@ github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLe 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= @@ -145,9 +144,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ 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/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= -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/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= @@ -155,8 +151,6 @@ 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= @@ -180,11 +174,9 @@ 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= @@ -324,8 +316,9 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w 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 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio= 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= @@ -419,8 +412,9 @@ google.golang.org/api v0.41.0/go.mod h1:RkxM5lITDfTzmyKFPt+wGrCJbVfniCr2ool8kTBz 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 h1:LX7NFCFYOHzr7WHaYiRUpeipZe9o5L8T+2F4Z798VDw= 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= @@ -475,9 +469,10 @@ google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08/go.mod h1:UODoCrxH 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-20210715145939-324b959e9c22/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k= -google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492 h1:7yQQsvnwjfEahbNNEKcBHv3mR+HnB1ctGY/z1JXzx8M= -google.golang.org/genproto v0.0.0-20210719143636-1d5a45f8e492/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48= +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= 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..f02dce0a --- /dev/null +++ b/testutil/inmem_instance_admin_server.go @@ -0,0 +1,86 @@ +// Copyright 2020 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..f4e3b22b --- /dev/null +++ b/testutil/inmem_instance_admin_server_test.go @@ -0,0 +1,95 @@ +// Copyright 2020 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..ac6f9bdf --- /dev/null +++ b/testutil/inmem_spanner_server.go @@ -0,0 +1,1087 @@ +// Copyright 2019 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..7cc90ea7 --- /dev/null +++ b/testutil/inmem_spanner_server_test.go @@ -0,0 +1,605 @@ +// Copyright 2019 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..4de7def2 --- /dev/null +++ b/testutil/mocked_inmem_server.go @@ -0,0 +1,318 @@ +// Copyright 2019 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, + } +} From e641951c29c940cb4538076c325605fbf91de190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 27 Jul 2021 13:24:38 +0200 Subject: [PATCH 11/14] chore: update copyright year --- testutil/inmem_instance_admin_server.go | 2 +- testutil/inmem_instance_admin_server_test.go | 2 +- testutil/inmem_spanner_server.go | 2 +- testutil/inmem_spanner_server_test.go | 2 +- testutil/mocked_inmem_server.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/testutil/inmem_instance_admin_server.go b/testutil/inmem_instance_admin_server.go index f02dce0a..3d9542bf 100644 --- a/testutil/inmem_instance_admin_server.go +++ b/testutil/inmem_instance_admin_server.go @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. diff --git a/testutil/inmem_instance_admin_server_test.go b/testutil/inmem_instance_admin_server_test.go index f4e3b22b..ea49436d 100644 --- a/testutil/inmem_instance_admin_server_test.go +++ b/testutil/inmem_instance_admin_server_test.go @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. diff --git a/testutil/inmem_spanner_server.go b/testutil/inmem_spanner_server.go index ac6f9bdf..8d906ca8 100644 --- a/testutil/inmem_spanner_server.go +++ b/testutil/inmem_spanner_server.go @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// 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. diff --git a/testutil/inmem_spanner_server_test.go b/testutil/inmem_spanner_server_test.go index 7cc90ea7..9b570a16 100644 --- a/testutil/inmem_spanner_server_test.go +++ b/testutil/inmem_spanner_server_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// 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. diff --git a/testutil/mocked_inmem_server.go b/testutil/mocked_inmem_server.go index 4de7def2..47b09537 100644 --- a/testutil/mocked_inmem_server.go +++ b/testutil/mocked_inmem_server.go @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// 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. From 4906ec4446f8e264293ec4c1f98f2c0411df563b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 28 Jul 2021 22:11:32 +0200 Subject: [PATCH 12/14] feat: add ARRAY support --- driver.go | 2 +- driver_with_mockserver_test.go | 300 ++++++++++++++++++++++++------ go.mod | 1 + integration_test.go | 264 +++++++++++++++++++++++++- internal/statement_parser.go | 2 +- internal/statement_parser_test.go | 4 + rows.go | 90 +++++++-- testutil/mocked_inmem_server.go | 142 ++++++++++++-- 8 files changed, 716 insertions(+), 89 deletions(-) diff --git a/driver.go b/driver.go index ecda92d4..e2b4a82c 100644 --- a/driver.go +++ b/driver.go @@ -257,7 +257,7 @@ func (c *conn) Prepare(query string) (driver.Stmt, error) { return c.PrepareContext(context.Background(), query) } -func (c *conn) PrepareContext(_ context.Context, query string) (driver.Stmt, error) { +func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { args, err := internal.ParseNamedParameters(query) if err != nil { return nil, err diff --git a/driver_with_mockserver_test.go b/driver_with_mockserver_test.go index ccd4032f..24d84035 100644 --- a/driver_with_mockserver_test.go +++ b/driver_with_mockserver_test.go @@ -20,6 +20,8 @@ import ( "database/sql/driver" "encoding/base64" "fmt" + "github.com/google/go-cmp/cmp/cmpopts" + "google.golang.org/protobuf/types/known/structpb" "math/big" "reflect" "testing" @@ -309,7 +311,6 @@ func TestPreparedQuery(t *testing.T) { func TestQueryWithAllTypes(t *testing.T) { t.Parallel() - // TODO: Add ARRAY types. db, server, teardown := setupTestDbConnection(t) defer teardown() query := `SELECT * @@ -321,12 +322,20 @@ func TestQueryWithAllTypes(t *testing.T) { AND ColFloat=@float64 AND ColNumeric=@numeric AND ColDate=@date - AND ColTimestamp=@timestamp` + AND ColTimestamp=@timestamp + AND ColBoolArray=@boolArray + AND ColStringArray=@stringArray + AND ColBytesArray=@bytesArray + AND ColIntArray=@int64Array + AND ColFloatArray=@float64Array + AND ColNumericArray=@numericArray + AND ColDateArray=@dateArray + AND ColTimestampArray=@timestampArray` _ = server.TestSpanner.PutStatementResult( query, &testutil.StatementResult{ Type: testutil.StatementResultResultSet, - ResultSet: testutil.CreateResultSetWithAllTypes(), + ResultSet: testutil.CreateResultSetWithAllTypes(false), }, ) @@ -336,6 +345,8 @@ func TestQueryWithAllTypes(t *testing.T) { } defer stmt.Close() ts, _ := time.Parse(time.RFC3339Nano, "2021-07-22T10:26:17.123Z") + ts1, _ := time.Parse(time.RFC3339Nano, "2021-07-21T21:07:59.339911800Z") + ts2, _ := time.Parse(time.RFC3339Nano, "2021-07-27T21:07:59.339911800Z") rows, err := stmt.QueryContext( context.Background(), true, @@ -345,7 +356,16 @@ func TestQueryWithAllTypes(t *testing.T) { 3.14, numeric("6.626"), civil.Date{Year: 2021, Month: 7, Day: 21}, - ts) + ts, + []spanner.NullBool{{Valid: true, Bool: true}, {}, {Valid: true, Bool: false}}, + []spanner.NullString{{Valid: true, StringVal: "test1"}, {}, {Valid: true, StringVal: "test2"}}, + [][]byte{[]byte("testbytes1"), nil, []byte("testbytes2")}, + []spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}}, + []spanner.NullFloat64{{Valid: true, Float64: 6.626}, {}, {Valid: true, Float64: 10.01}}, + []spanner.NullNumeric{nullNumeric(true, "3.14"), {}, nullNumeric(true, "10.01")}, + []spanner.NullDate{{Valid: true, Date: civil.Date{Year: 2000, Month: 2, Day: 29}}, {}, {Valid: true, Date: civil.Date{Year: 2021, Month: 7, Day: 27}}}, + []spanner.NullTime{{Valid: true, Time: ts1}, {}, {Valid: true, Time: ts2}}, + ) if err != nil { t.Fatal(err) } @@ -357,10 +377,18 @@ func TestQueryWithAllTypes(t *testing.T) { var bt []byte var i int64 var f float64 - var r big.Rat - var d time.Time + var r spanner.NullNumeric + var d spanner.NullDate var ts time.Time - err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts) + var bArray []spanner.NullBool + var sArray []spanner.NullString + var btArray [][]byte + var iArray []spanner.NullInt64 + var fArray []spanner.NullFloat64 + var rArray []spanner.NullNumeric + var dArray []spanner.NullDate + var tsArray []spanner.NullTime + err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts, &bArray, &sArray, &btArray, &iArray, &fArray, &rArray, &dArray, &tsArray) if err != nil { t.Fatal(err) } @@ -379,17 +407,39 @@ func TestQueryWithAllTypes(t *testing.T) { 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 { + if g, w := r, numeric("6.626"); g.Numeric.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 { + if g, w := d, nullDate(true, "2021-07-21"); !cmp.Equal(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 g, w := bArray, []spanner.NullBool{{Valid: true, Bool: true}, {}, {Valid: true, Bool: false}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for bool array\nGot: %v\nWant: %v", g, w) + } + if g, w := sArray, []spanner.NullString{{Valid: true, StringVal: "test1"}, {}, {Valid: true, StringVal: "test2"}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for string array\nGot: %v\nWant: %v", g, w) + } + if g, w := btArray, [][]byte{[]byte("testbytes1"), nil, []byte("testbytes2")}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for bytes array\nGot: %v\nWant: %v", g, w) + } + if g, w := iArray, []spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for int array\nGot: %v\nWant: %v", g, w) + } + if g, w := fArray, []spanner.NullFloat64{{Valid: true, Float64: 6.626}, {}, {Valid: true, Float64: 10.01}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for float array\nGot: %v\nWant: %v", g, w) + } + if g, w := rArray, []spanner.NullNumeric{nullNumeric(true, "3.14"), {}, nullNumeric(true, "10.01")}; !cmp.Equal(g, w, cmp.AllowUnexported(big.Rat{}, big.Int{})) { + t.Errorf("row value mismatch for numeric array\nGot: %v\nWant: %v", g, w) + } + if g, w := dArray, []spanner.NullDate{{Valid: true, Date: civil.Date{Year: 2000, Month: 2, Day: 29}}, {}, {Valid: true, Date: civil.Date{Year: 2021, Month: 7, Day: 27}}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for date array\nGot: %v\nWant: %v", g, w) + } + if g, w := tsArray, []spanner.NullTime{{Valid: true, Time: ts1}, {}, {Valid: true, Time: ts2}}; !cmp.Equal(g, w) { + t.Errorf("row value mismatch for timestamp array\nGot: %v\nWant: %v", g, w) + } } if rows.Err() != nil { t.Fatal(rows.Err()) @@ -400,15 +450,16 @@ func TestQueryWithAllTypes(t *testing.T) { 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 { + if g, w := len(req.ParamTypes), 16; g != w { t.Fatalf("param types length mismatch\nGot: %v\nWant: %v", g, w) } - if g, w := len(req.Params.Fields), 8; g != w { + if g, w := len(req.Params.Fields), 16; g != w { t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) } wantParams := []struct { name string code sppb.TypeCode + array bool value interface{} }{ { @@ -451,27 +502,126 @@ func TestQueryWithAllTypes(t *testing.T) { code: sppb.TypeCode_TIMESTAMP, value: "2021-07-22T10:26:17.123Z", }, + { + name: "boolArray", + code: sppb.TypeCode_BOOL, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_BoolValue{BoolValue: true}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_BoolValue{BoolValue: false}}, + }}, + }, + { + name: "stringArray", + code: sppb.TypeCode_STRING, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "test1"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "test2"}}, + }}, + }, + { + name: "bytesArray", + code: sppb.TypeCode_BYTES, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString([]byte("testbytes1"))}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString([]byte("testbytes2"))}}, + }}, + }, + { + name: "int64Array", + code: sppb.TypeCode_INT64, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "1"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2"}}, + }}, + }, + { + name: "float64Array", + code: sppb.TypeCode_FLOAT64, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_NumberValue{NumberValue: 6.626}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_NumberValue{NumberValue: 10.01}}, + }}, + }, + { + name: "numericArray", + code: sppb.TypeCode_NUMERIC, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "3.140000000"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "10.010000000"}}, + }}, + }, + { + name: "dateArray", + code: sppb.TypeCode_DATE, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "2000-02-29"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-27"}}, + }}, + }, + { + name: "timestampArray", + code: sppb.TypeCode_TIMESTAMP, + array: true, + value: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-21T21:07:59.3399118Z"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-27T21:07:59.3399118Z"}}, + }}, + }, } 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) + if wantParam.array { + if g, w := pt.Code, sppb.TypeCode_ARRAY; g != w { + t.Errorf("param type mismatch\nGot: %v\nWant: %v", g, w) + } + if g, w := pt.ArrayElementType.Code, wantParam.code; g != w { + t.Errorf("param array element type mismatch\nGot: %v\nWant: %v", g, w) + } + } else { + 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 wantParam.array { + g = val.GetListValue() + } else { + 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) + if wantParam.array { + if !cmp.Equal(g, wantParam.value, cmpopts.IgnoreUnexported(structpb.ListValue{}, structpb.Value{})) { + t.Errorf("array param value mismatch\nGot: %v\nWant: %v", g, wantParam.value) + } + } else { + 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) @@ -482,7 +632,6 @@ func TestQueryWithAllTypes(t *testing.T) { func TestQueryWithNullParameters(t *testing.T) { t.Parallel() - // TODO: Add ARRAY types. db, server, teardown := setupTestDbConnection(t) defer teardown() query := `SELECT * @@ -494,12 +643,20 @@ func TestQueryWithNullParameters(t *testing.T) { AND ColFloat=@float64 AND ColNumeric=@numeric AND ColDate=@date - AND ColTimestamp=@timestamp` + AND ColTimestamp=@timestamp + AND ColBoolArray=@boolArray + AND ColStringArray=@stringArray + AND ColBytesArray=@bytesArray + AND ColIntArray=@int64Array + AND ColFloatArray=@float64Array + AND ColNumericArray=@numericArray + AND ColDateArray=@dateArray + AND ColTimestampArray=@timestampArray` _ = server.TestSpanner.PutStatementResult( query, &testutil.StatementResult{ Type: testutil.StatementResultResultSet, - ResultSet: testutil.CreateResultSetWithAllTypes(), + ResultSet: testutil.CreateResultSetWithAllTypes(true), }, ) @@ -518,6 +675,14 @@ func TestQueryWithNullParameters(t *testing.T) { nil, // numeric nil, // date nil, // timestamp + nil, // bool array + nil, // string array + nil, // bytes array + nil, // int64 array + nil, // float64 array + nil, // numeric array + nil, // date array + nil, // timestamp array ) if err != nil { t.Fatal(err) @@ -525,43 +690,49 @@ func TestQueryWithNullParameters(t *testing.T) { defer rows.Close() for rows.Next() { - var b bool - var s string + var b sql.NullBool + var s sql.NullString 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) + var i sql.NullInt64 + var f sql.NullFloat64 + var r spanner.NullNumeric // There's no equivalent sql type. + var d spanner.NullDate // There's no equivalent sql type. + var ts sql.NullTime + var bArray []spanner.NullBool + var sArray []spanner.NullString + var btArray [][]byte + var iArray []spanner.NullInt64 + var fArray []spanner.NullFloat64 + var rArray []spanner.NullNumeric + var dArray []spanner.NullDate + var tsArray []spanner.NullTime + err = rows.Scan(&b, &s, &bt, &i, &f, &r, &d, &ts, &bArray, &sArray, &btArray, &iArray, &fArray, &rArray, &dArray, &tsArray) 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 b.Valid { + t.Errorf("row value mismatch for bool\nGot: %v\nWant: %v", b, spanner.NullBool{}) } - if g, w := s, "test"; g != w { - t.Errorf("row value mismatch for string\nGot: %v\nWant: %v", g, w) + if s.Valid { + t.Errorf("row value mismatch for string\nGot: %v\nWant: %v", s, spanner.NullString{}) } - if g, w := bt, []byte("testbytes"); !cmp.Equal(g, w) { - t.Errorf("row value mismatch for bytes\nGot: %v\nWant: %v", g, w) + if bt != nil { + t.Errorf("row value mismatch for bytes\nGot: %v\nWant: %v", bt, nil) } - if g, w := i, int64(5); g != w { - t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", g, w) + if i.Valid { + t.Errorf("row value mismatch for int64\nGot: %v\nWant: %v", i, spanner.NullInt64{}) } - if g, w := f, 3.14; g != w { - t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", g, w) + if f.Valid { + t.Errorf("row value mismatch for float64\nGot: %v\nWant: %v", f, spanner.NullFloat64{}) } - if g, w := r, numeric("6.626"); g.Cmp(w) != 0 { - t.Errorf("row value mismatch for numeric\nGot: %v\nWant: %v", g, w) + if r.Valid { + t.Errorf("row value mismatch for numeric\nGot: %v\nWant: %v", r, spanner.NullNumeric{}) } - // 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) + if d.Valid { + t.Errorf("row value mismatch for date\nGot: %v\nWant: %v", d, spanner.NullDate{}) } - // 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 ts.Valid { + t.Errorf("row value mismatch for timestamp\nGot: %v\nWant: %v", ts, spanner.NullTime{}) } } if rows.Err() != nil { @@ -577,7 +748,7 @@ func TestQueryWithNullParameters(t *testing.T) { 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 { + if g, w := len(req.Params.Fields), 16; g != w { t.Fatalf("params length mismatch\nGot: %v\nWant: %v", g, w) } for _, param := range req.Params.Fields { @@ -783,11 +954,30 @@ func TestPing(t *testing.T) { } } -func numeric(v string) *big.Rat { +func numeric(v string) big.Rat { res, _ := big.NewRat(1, 1).SetString(v) + return *res +} + +func nullNumeric(valid bool, v string) spanner.NullNumeric { + if !valid { + return spanner.NullNumeric{} + } + return spanner.NullNumeric{Valid: true, Numeric: numeric(v)} +} + +func date(v string) civil.Date { + res, _ := civil.ParseDate(v) return res } +func nullDate(valid bool, v string) spanner.NullDate { + if !valid { + return spanner.NullDate{} + } + return spanner.NullDate{Valid: true, Date: date(v)} +} + func setupTestDbConnection(t *testing.T) (db *sql.DB, server *testutil.MockedSpannerInMemTestServer, teardown func()) { server, _, serverTeardown := setupMockedTestServer(t) db, err := sql.Open( diff --git a/go.mod b/go.mod index 2a039fe3..16aa55ae 100644 --- a/go.mod +++ b/go.mod @@ -10,4 +10,5 @@ require ( google.golang.org/api v0.51.0 google.golang.org/genproto v0.0.0-20210726143408-b02e89920bf0 google.golang.org/grpc v1.39.0 + google.golang.org/protobuf v1.27.1 // indirect ) diff --git a/integration_test.go b/integration_test.go index fdcb76d0..8b4f6cae 100644 --- a/integration_test.go +++ b/integration_test.go @@ -15,19 +15,23 @@ package spannerdriver import ( + "cloud.google.com/go/civil" "cloud.google.com/go/spanner" database "cloud.google.com/go/spanner/admin/database/apiv1" "context" "database/sql" "flag" "fmt" + "github.com/google/go-cmp/cmp" databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" "google.golang.org/grpc/codes" "log" "math" + "math/big" "os" "reflect" "testing" + "time" instance "cloud.google.com/go/spanner/admin/instance/apiv1" instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" @@ -64,6 +68,13 @@ func init() { } } +func runsOnEmulator() bool { + if _, ok := os.LookupEnv("SPANNER_EMULATOR_HOST"); ok { + return true + } + return false +} + func initEmulator(projectId, instanceId, databaseId string) error { ctx := context.Background() instanceAdmin, err := instance.NewInstanceAdminClient(ctx) @@ -99,8 +110,8 @@ func initEmulator(projectId, instanceId, databaseId string) error { } 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,), + 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. @@ -931,5 +942,254 @@ func TestRowsOverflowRead(t *testing.T) { if err != nil { t.Fatal(err) } +} + +func TestAllTypes(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() + + type AllTypesRow struct { + key int64 + boolCol sql.NullBool + stringCol sql.NullString + bytesCol []byte + int64Col sql.NullInt64 + float64Col sql.NullFloat64 + numericCol spanner.NullNumeric + dateCol spanner.NullDate + timestampCol sql.NullTime + boolArrayCol []spanner.NullBool + stringArrayCol []spanner.NullString + bytesArrayCol [][]byte + int64ArrayCol []spanner.NullInt64 + float64ArrayCol []spanner.NullFloat64 + numericArrayCol []spanner.NullNumeric + dateArrayCol []spanner.NullDate + timestampArrayCol []spanner.NullTime + } + + // Set up test table. + _, err = db.ExecContext(ctx, + `CREATE TABLE TestAllTypes ( + key INT64, + boolCol BOOL, + stringCol STRING(MAX), + bytesCol BYTES(MAX), + int64Col INT64, + float64Col FLOAT64, + numericCol NUMERIC, + dateCol DATE, + timestampCol TIMESTAMP, + boolArrayCol ARRAY, + stringArrayCol ARRAY, + bytesArrayCol ARRAY, + int64ArrayCol ARRAY, + float64ArrayCol ARRAY, + numericArrayCol ARRAY, + dateArrayCol ARRAY, + timestampArrayCol ARRAY, + ) PRIMARY KEY (key)`) + if err != nil { + t.Fatal(err) + } + defer db.ExecContext(ctx, `DROP TABLE TestAllTypes`) + + tests := []struct { + name string + key int64 + input []interface{} + want AllTypesRow + skipOnEmulator bool + }{ + { + name: "Non-null values", + key: 1, + input: []interface{}{ + 1, true, "test", []byte("testbytes"), int64(1), 3.14, numeric("6.626"), date("2021-07-28"), time.Date(2021, 7, 28, 15, 8, 30, 30294, time.UTC), + []spanner.NullBool{{Valid: true, Bool: true}, {}, {Valid: true, Bool: false}}, + []spanner.NullString{{Valid: true, StringVal: "test1"}, {}, {Valid: true, StringVal: "test2"}}, + [][]byte{[]byte("testbytes1"), nil, []byte("testbytes2")}, + []spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}}, + []spanner.NullFloat64{{Valid: true, Float64: 3.14}, {}, {Valid: true, Float64: 6.626}}, + []spanner.NullNumeric{{Valid: true, Numeric: numeric("3.14")}, {}, {Valid: true, Numeric: numeric("6.626")}}, + []spanner.NullDate{{Valid: true, Date: date("2021-07-28")}, {}, {Valid: true, Date: date("2000-02-29")}}, + []spanner.NullTime{{Valid: true, Time: time.Date(2021, 7, 28, 15, 16, 1, 999999999, time.UTC)}}, + }, + want: AllTypesRow{1, + sql.NullBool{Valid: true, Bool: true}, sql.NullString{Valid: true, String: "test"}, []byte("testbytes"), + sql.NullInt64{Valid: true, Int64: 1}, sql.NullFloat64{Valid: true, Float64: 3.14}, spanner.NullNumeric{Valid: true, Numeric: numeric("6.626")}, + spanner.NullDate{Valid: true, Date: date("2021-07-28")}, sql.NullTime{Valid: true, Time: time.Date(2021, 7, 28, 15, 8, 30, 30294, time.UTC)}, + []spanner.NullBool{{Valid: true, Bool: true}, {}, {Valid: true, Bool: false}}, + []spanner.NullString{{Valid: true, StringVal: "test1"}, {}, {Valid: true, StringVal: "test2"}}, + [][]byte{[]byte("testbytes1"), nil, []byte("testbytes2")}, + []spanner.NullInt64{{Valid: true, Int64: 1}, {}, {Valid: true, Int64: 2}}, + []spanner.NullFloat64{{Valid: true, Float64: 3.14}, {}, {Valid: true, Float64: 6.626}}, + []spanner.NullNumeric{{Valid: true, Numeric: numeric("3.14")}, {}, {Valid: true, Numeric: numeric("6.626")}}, + []spanner.NullDate{{Valid: true, Date: date("2021-07-28")}, {}, {Valid: true, Date: date("2000-02-29")}}, + []spanner.NullTime{{Valid: true, Time: time.Date(2021, 7, 28, 15, 16, 1, 999999999, time.UTC)}}, + }, + }, + { + name: "Untyped null values", + key: 2, + input: []interface{}{ + 2, nil, nil, nil, nil, nil, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, + }, + want: AllTypesRow{2, + sql.NullBool{}, sql.NullString{}, []byte(nil), + sql.NullInt64{}, sql.NullFloat64{}, spanner.NullNumeric{}, + spanner.NullDate{}, sql.NullTime{}, + []spanner.NullBool(nil), + []spanner.NullString(nil), + [][]byte(nil), + []spanner.NullInt64(nil), + []spanner.NullFloat64(nil), + []spanner.NullNumeric(nil), + []spanner.NullDate(nil), + []spanner.NullTime(nil), + }, + // The emulator does not support untyped null values. + skipOnEmulator: true, + }, + { + name: "Typed null values", + key: 3, + input: []interface{}{ + 3, nilBool(), nilString(), []byte(nil), nilInt64(), nilFloat64(), nilRat(), nilDate(), nilTime(), + []spanner.NullBool(nil), + []spanner.NullString(nil), + [][]byte(nil), + []spanner.NullInt64(nil), + []spanner.NullFloat64(nil), + []spanner.NullNumeric(nil), + []spanner.NullDate(nil), + []spanner.NullTime(nil), + }, + want: AllTypesRow{3, + sql.NullBool{}, sql.NullString{}, []byte(nil), + sql.NullInt64{}, sql.NullFloat64{}, spanner.NullNumeric{}, + spanner.NullDate{}, sql.NullTime{}, + []spanner.NullBool(nil), + []spanner.NullString(nil), + [][]byte(nil), + []spanner.NullInt64(nil), + []spanner.NullFloat64(nil), + []spanner.NullNumeric(nil), + []spanner.NullDate(nil), + []spanner.NullTime(nil), + }, + }, + { + name: "Null* struct values", + key: 4, + input: []interface{}{ + // TODO: Fix the requirement to use spanner.NullString here. + 4, sql.NullBool{}, spanner.NullString{}, []byte(nil), sql.NullInt64{}, sql.NullFloat64{}, + spanner.NullNumeric{}, spanner.NullDate{}, sql.NullTime{}, + []spanner.NullBool(nil), + []spanner.NullString(nil), + [][]byte(nil), + []spanner.NullInt64(nil), + []spanner.NullFloat64(nil), + []spanner.NullNumeric(nil), + []spanner.NullDate(nil), + []spanner.NullTime(nil), + }, + want: AllTypesRow{4, + sql.NullBool{}, sql.NullString{}, []byte(nil), + sql.NullInt64{}, sql.NullFloat64{}, spanner.NullNumeric{}, + spanner.NullDate{}, sql.NullTime{}, + []spanner.NullBool(nil), + []spanner.NullString(nil), + [][]byte(nil), + []spanner.NullInt64(nil), + []spanner.NullFloat64(nil), + []spanner.NullNumeric(nil), + []spanner.NullDate(nil), + []spanner.NullTime(nil), + }, + }, + } + stmt, err := db.PrepareContext(ctx, `INSERT INTO TestAllTypes (key, boolCol, stringCol, bytesCol, int64Col, + float64Col, numericCol, dateCol, timestampCol, boolArrayCol, + stringArrayCol, bytesArrayCol, int64ArrayCol, float64ArrayCol, + numericArrayCol, dateArrayCol, timestampArrayCol) VALUES (@key, @bool, + @string, @bytes, @int64, @float64, @numeric, @date, @timestamp, + @boolArray, @stringArray, @bytesArray, @int64Array, @float64Array, + @numericArray, @dateArray, @timestampArray)`) + for _, test := range tests { + if runsOnEmulator() && test.skipOnEmulator { + t.Logf("skipping test %q on emulator", test.name) + continue + } + res, err := stmt.ExecContext(ctx, test.input...) + if err != nil { + t.Fatalf("insert failed: %v", err) + } + affected, err := res.RowsAffected() + if err != nil { + t.Fatalf("could not get rows affected: %v", err) + } + if g, w := affected, int64(1); g != w { + t.Fatalf("affected rows mismatch\nGot: %v\nWant: %v", g, w) + } + // Read the row back. + row := db.QueryRowContext(ctx, "SELECT * FROM TestAllTypes WHERE key=@key", test.key) + var allTypesRow AllTypesRow + err = row.Scan( + &allTypesRow.key, &allTypesRow.boolCol, &allTypesRow.stringCol, &allTypesRow.bytesCol, &allTypesRow.int64Col, + &allTypesRow.float64Col, &allTypesRow.numericCol, &allTypesRow.dateCol, &allTypesRow.timestampCol, + &allTypesRow.boolArrayCol, &allTypesRow.stringArrayCol, &allTypesRow.bytesArrayCol, &allTypesRow.int64ArrayCol, + &allTypesRow.float64ArrayCol, &allTypesRow.numericArrayCol, &allTypesRow.dateArrayCol, &allTypesRow.timestampArrayCol, + ) + if err != nil { + t.Fatalf("could not query row: %v", err) + } + if !cmp.Equal(allTypesRow, test.want, cmp.AllowUnexported(AllTypesRow{}, big.Rat{}, big.Int{})) { + t.Fatalf("row mismatch\nGot: %v\nWant: %v", allTypesRow, test.want) + } + } +} + +func nilBool() *bool { + var b *bool + return b +} + +func nilString() *string { + var s *string + return s +} + +func nilInt64() *int64 { + var i *int64 + return i +} + +func nilFloat64() *float64 { + var f *float64 + return f +} + +func nilRat() *big.Rat { + var r *big.Rat + return r +} + +func nilDate() *civil.Date { + var d *civil.Date + return d +} +func nilTime() *time.Time { + var t *time.Time + return t } diff --git a/internal/statement_parser.go b/internal/statement_parser.go index 6843c715..81ba0882 100644 --- a/internal/statement_parser.go +++ b/internal/statement_parser.go @@ -223,7 +223,7 @@ func findParams(sql string) ([]string, error) { index++ startIndex := index for index < len(runes) { - if unicode.IsSpace(runes[index]) { + if !(unicode.IsLetter(runes[index]) || unicode.IsDigit(runes[index]) || runes[index] == '_') { res = append(res, string(runes[startIndex:index])) break } diff --git a/internal/statement_parser_test.go b/internal/statement_parser_test.go index 7d53ef7c..5c73cda5 100644 --- a/internal/statement_parser_test.go +++ b/internal/statement_parser_test.go @@ -489,6 +489,10 @@ func TestFindParams(t *testing.T) { input: `@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable WHERE Name like @name AND Email='test@test.com'`, want: []string{"name"}, }, + { + input: "INSERT INTO Foo (Col1, Col2, Col3) VALUES (@param1, @param2, @param3)", + want: []string{"param1", "param2", "param3"}, + }, } for _, tc := range tests { sql, err := removeCommentsAndTrim(tc.input) diff --git a/rows.go b/rows.go index fdfa0307..9bfb2058 100644 --- a/rows.go +++ b/rows.go @@ -15,14 +15,12 @@ package spannerdriver import ( - "database/sql/driver" - "io" - "sync" - "time" - "cloud.google.com/go/spanner" + "database/sql/driver" "google.golang.org/api/iterator" sppb "google.golang.org/genproto/googleapis/spanner/v1" + "io" + "sync" ) type rows struct { @@ -113,25 +111,31 @@ func (r *rows) Next(dest []driver.Value) error { if err := col.Decode(&v); err != nil { return err } - dest[i] = v.Int64 + if v.Valid { + dest[i] = v.Int64 + } case sppb.TypeCode_FLOAT64: var v spanner.NullFloat64 if err := col.Decode(&v); err != nil { return err } - dest[i] = v.Float64 + if v.Valid { + 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 + dest[i] = v case sppb.TypeCode_STRING: var v spanner.NullString if err := col.Decode(&v); err != nil { return err } - dest[i] = v.StringVal + if v.Valid { + dest[i] = v.StringVal + } case sppb.TypeCode_BYTES: // The column value is a base64 encoded string. var v []byte @@ -144,26 +148,76 @@ func (r *rows) Next(dest []driver.Value) error { if err := col.Decode(&v); err != nil { return err } - dest[i] = v.Bool + if v.Valid { + dest[i] = v.Bool + } case sppb.TypeCode_DATE: var v spanner.NullDate if err := col.Decode(&v); err != nil { return err } - if v.IsNull() { - dest[i] = v.Date // typed nil - } else { - dest[i] = v.Date.In(time.UTC) // TODO(jbd): Add note about this. - } + dest[i] = v case sppb.TypeCode_TIMESTAMP: var v spanner.NullTime if err := col.Decode(&v); err != nil { return err } - dest[i] = v.Time + if v.Valid { + dest[i] = v.Time + } + case sppb.TypeCode_ARRAY: + switch col.Type.ArrayElementType.Code { + case sppb.TypeCode_INT64: + var v []spanner.NullInt64 + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_FLOAT64: + var v []spanner.NullFloat64 + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_NUMERIC: + var v []spanner.NullNumeric + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_STRING: + var v []spanner.NullString + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_BYTES: + var v [][]byte + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_BOOL: + var v []spanner.NullBool + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_DATE: + var v []spanner.NullDate + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + case sppb.TypeCode_TIMESTAMP: + var v []spanner.NullTime + if err := col.Decode(&v); err != nil { + return err + } + dest[i] = v + } } - // TODO(jbd): Implement other types. - // How to handle array and struct? + // TODO: Implement struct } return nil } diff --git a/testutil/mocked_inmem_server.go b/testutil/mocked_inmem_server.go index 47b09537..339b60e6 100644 --- a/testutil/mocked_inmem_server.go +++ b/testutil/mocked_inmem_server.go @@ -15,13 +15,13 @@ package testutil import ( + "encoding/base64" "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" @@ -232,8 +232,8 @@ func createSingersRow(idx int64) *structpb.ListValue { } } -func CreateResultSetWithAllTypes() *spannerpb.ResultSet { - fields := make([]*spannerpb.StructType_Field, 8) +func CreateResultSetWithAllTypes(nullValues bool) *spannerpb.ResultSet { + fields := make([]*spannerpb.StructType_Field, 16) fields[0] = &spannerpb.StructType_Field{ Name: "ColBool", Type: &spannerpb.Type{Code: spannerpb.TypeCode_BOOL}, @@ -266,6 +266,62 @@ func CreateResultSetWithAllTypes() *spannerpb.ResultSet { Name: "ColTimestamp", Type: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, } + fields[8] = &spannerpb.StructType_Field{ + Name: "ColBoolArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_BOOL}, + }, + } + fields[9] = &spannerpb.StructType_Field{ + Name: "ColStringArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, + }, + } + fields[10] = &spannerpb.StructType_Field{ + Name: "ColBytesArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_BYTES}, + }, + } + fields[11] = &spannerpb.StructType_Field{ + Name: "ColIntArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, + }, + } + fields[12] = &spannerpb.StructType_Field{ + Name: "ColFloatArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_FLOAT64}, + }, + } + fields[13] = &spannerpb.StructType_Field{ + Name: "ColNumericArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_NUMERIC}, + }, + } + fields[14] = &spannerpb.StructType_Field{ + Name: "ColDateArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_DATE}, + }, + } + fields[15] = &spannerpb.StructType_Field{ + Name: "ColTimestampArray", + Type: &spannerpb.Type{ + Code: spannerpb.TypeCode_ARRAY, + ArrayElementType: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, + }, + } rowType := &spannerpb.StructType{ Fields: fields, } @@ -273,15 +329,77 @@ func CreateResultSetWithAllTypes() *spannerpb.ResultSet { 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"}} + rowValue := make([]*structpb.Value, len(fields)) + if nullValues { + for i := range fields { + rowValue[i] = &structpb.Value{Kind: &structpb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}} + } + } else { + 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"}} + rowValue[8] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_BoolValue{BoolValue: true}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_BoolValue{BoolValue: false}}, + }}, + }} + rowValue[9] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "test1"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "test2"}}, + }}, + }} + rowValue[10] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString([]byte("testbytes1"))}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: base64.StdEncoding.EncodeToString([]byte("testbytes2"))}}, + }}, + }} + rowValue[11] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "1"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2"}}, + }}, + }} + rowValue[12] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_NumberValue{NumberValue: 6.626}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_NumberValue{NumberValue: 10.01}}, + }}, + }} + rowValue[13] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "3.14"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "10.01"}}, + }}, + }} + rowValue[14] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "2000-02-29"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-27"}}, + }}, + }} + rowValue[15] = &structpb.Value{Kind: &structpb.Value_ListValue{ + ListValue: &structpb.ListValue{Values: []*structpb.Value{ + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-21T21:07:59.339911800Z"}}, + {Kind: &structpb.Value_NullValue{}}, + {Kind: &structpb.Value_StringValue{StringValue: "2021-07-27T21:07:59.339911800Z"}}, + }}, + }} + } rows[0] = &structpb.ListValue{ Values: rowValue, } From c6750fa88c8a477cd27b23f530d290bc3afd123f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 2 Aug 2021 06:53:38 +0200 Subject: [PATCH 13/14] fix: address review comments --- driver.go | 54 +++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/driver.go b/driver.go index ecda92d4..19bcb345 100644 --- a/driver.go +++ b/driver.go @@ -20,8 +20,6 @@ import ( "database/sql/driver" "errors" "fmt" - "log" - "os" "regexp" "strconv" "strings" @@ -36,19 +34,18 @@ import ( ) const userAgent = "go-sql-driver-spanner/0.1" -const dsnRegExpString = "((?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 dsnRegExp *regexp.Regexp +// 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() { - var err error - dsnRegExp, err = regexp.Compile(dsnRegExpString) - if err != nil { - log.Fatalf("could not compile Spanner dsn regexp: %v", err) - return - } sql.Register("spanner", &Driver{}) } @@ -123,9 +120,9 @@ type connector struct { driver *Driver connectorConfig connectorConfig - // config represents the optional advanced configuration to be used + // spannerClientConfig represents the optional advanced configuration to be used // by the Google Cloud Spanner client. - config spanner.ClientConfig + spannerClientConfig spanner.ClientConfig // options represent the optional Google Cloud client options // to be passed to the underlying client. @@ -150,10 +147,10 @@ func newConnector(d *Driver, dsn string) (*connector, error) { SessionPoolConfig: spanner.DefaultSessionPoolConfig, } return &connector{ - driver: d, - connectorConfig: connectorConfig, - config: config, - options: opts, + driver: d, + connectorConfig: connectorConfig, + spannerClientConfig: config, + options: opts, }, nil } @@ -168,7 +165,7 @@ func openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { c.connectorConfig.project, c.connectorConfig.instance, c.connectorConfig.database) - client, err := spanner.NewClientWithConfig(ctx, databaseName, c.config, opts...) + client, err := spanner.NewClientWithConfig(ctx, databaseName, c.spannerClientConfig, opts...) if err != nil { return nil, err } @@ -180,27 +177,6 @@ func openDriverConn(ctx context.Context, c *connector) (driver.Conn, error) { return &conn{client: client, adminClient: adminClient, database: databaseName}, nil } -func createAdminClient(ctx context.Context) (adminClient *adminapi.DatabaseAdminClient, err error) { - // 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 - } - } - return -} - func (c *connector) Driver() driver.Driver { return &Driver{} } @@ -327,7 +303,7 @@ func (c *conn) Close() error { } func (c *conn) Begin() (driver.Tx, error) { - return nil, fmt.Errorf("use BeginTx instead") + return c.BeginTx(context.Background(), driver.TxOptions{}) } func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { From 68e061ee61b8cb0edbee368c6a4be18655549b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 2 Aug 2021 07:46:08 +0200 Subject: [PATCH 14/14] docs: add comment on why we are assigning Null* value --- rows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rows.go b/rows.go index 9bfb2058..871f1795 100644 --- a/rows.go +++ b/rows.go @@ -127,6 +127,9 @@ func (r *rows) Next(dest []driver.Value) error { if err := col.Decode(&v); err != nil { return err } + // We always assign `v` to dest[i] here because there is no native type + // for numeric in the Go sql package. That means that instead of returning + // nil we should return a NullNumeric with valid=false. dest[i] = v case sppb.TypeCode_STRING: var v spanner.NullString @@ -156,6 +159,9 @@ func (r *rows) Next(dest []driver.Value) error { if err := col.Decode(&v); err != nil { return err } + // We always assign `v` to dest[i] here because there is no native type + // for date in the Go sql package. That means that instead of returning + // nil we should return a NullDate with valid=false. dest[i] = v case sppb.TypeCode_TIMESTAMP: var v spanner.NullTime