You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

102 lines
3.0 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "errors"
  16. "fmt"
  17. proto3 "github.com/golang/protobuf/ptypes/struct"
  18. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  19. "google.golang.org/grpc/codes"
  20. )
  21. // A Statement is a SQL query with named parameters.
  22. //
  23. // A parameter placeholder consists of '@' followed by the parameter name.
  24. // Parameter names consist of any combination of letters, numbers, and
  25. // underscores. Names may be entirely numeric (e.g., "WHERE m.id = @5").
  26. // Parameters may appear anywhere that a literal value is expected. The same
  27. // parameter name may be used more than once. It is an error to execute a
  28. // statement with unbound parameters. On the other hand, it is allowable to
  29. // bind parameter names that are not used.
  30. //
  31. // See the documentation of the Row type for how Go types are mapped to Cloud
  32. // Spanner types.
  33. type Statement struct {
  34. SQL string
  35. Params map[string]interface{}
  36. }
  37. // NewStatement returns a Statement with the given SQL and an empty Params map.
  38. func NewStatement(sql string) Statement {
  39. return Statement{SQL: sql, Params: map[string]interface{}{}}
  40. }
  41. // errBindParam returns error for not being able to bind parameter to query request.
  42. func errBindParam(k string, v interface{}, err error) error {
  43. if err == nil {
  44. return nil
  45. }
  46. se, ok := toSpannerError(err).(*Error)
  47. if !ok {
  48. return spannerErrorf(codes.InvalidArgument, "failed to bind query parameter(name: %q, value: %v), error = <%v>", k, v, err)
  49. }
  50. se.decorate(fmt.Sprintf("failed to bind query parameter(name: %q, value: %v)", k, v))
  51. return se
  52. }
  53. var (
  54. errNilParam = errors.New("use T(nil), not nil")
  55. errNoType = errors.New("no type information")
  56. )
  57. // bindParams binds parameters in a Statement to a sppb.ExecuteSqlRequest or sppb.PartitionQueryRequest.
  58. func (s *Statement) bindParams(i interface{}) error {
  59. params := &proto3.Struct{
  60. Fields: map[string]*proto3.Value{},
  61. }
  62. paramTypes := map[string]*sppb.Type{}
  63. for k, v := range s.Params {
  64. if v == nil {
  65. return errBindParam(k, v, errNilParam)
  66. }
  67. val, t, err := encodeValue(v)
  68. if err != nil {
  69. return errBindParam(k, v, err)
  70. }
  71. if t == nil { // should not happen, because of nil check above
  72. return errBindParam(k, v, errNoType)
  73. }
  74. params.Fields[k] = val
  75. paramTypes[k] = t
  76. }
  77. switch r := i.(type) {
  78. default:
  79. return fmt.Errorf("failed to bind query parameter, unexpected request type: %v", r)
  80. case *sppb.ExecuteSqlRequest:
  81. r.Params = params
  82. r.ParamTypes = paramTypes
  83. case *sppb.PartitionQueryRequest:
  84. r.Params = params
  85. r.ParamTypes = paramTypes
  86. }
  87. return nil
  88. }