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.
 
 
 

93 lines
2.9 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. structpb "github.com/golang/protobuf/ptypes/struct"
  19. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  20. "google.golang.org/grpc/codes"
  21. )
  22. // A Statement is a SQL query with named parameters.
  23. //
  24. // A parameter placeholder consists of '@' followed by the parameter name.
  25. // Parameter names consist of any combination of letters, numbers, and
  26. // underscores. Names may be entirely numeric (e.g., "WHERE m.id = @5").
  27. // Parameters may appear anywhere that a literal value is expected. The same
  28. // parameter name may be used more than once. It is an error to execute a
  29. // statement with unbound parameters. On the other hand, it is allowable to
  30. // bind parameter names that are not used.
  31. //
  32. // See the documentation of the Row type for how Go types are mapped to Cloud
  33. // Spanner types.
  34. type Statement struct {
  35. SQL string
  36. Params map[string]interface{}
  37. }
  38. // NewStatement returns a Statement with the given SQL and an empty Params map.
  39. func NewStatement(sql string) Statement {
  40. return Statement{SQL: sql, Params: map[string]interface{}{}}
  41. }
  42. var (
  43. errNilParam = errors.New("use T(nil), not nil")
  44. errNoType = errors.New("no type information")
  45. )
  46. // convertParams converts a statement's parameters into proto Param and
  47. // ParamTypes.
  48. func (s *Statement) convertParams() (*structpb.Struct, map[string]*sppb.Type, error) {
  49. params := &proto3.Struct{
  50. Fields: map[string]*proto3.Value{},
  51. }
  52. paramTypes := map[string]*sppb.Type{}
  53. for k, v := range s.Params {
  54. if v == nil {
  55. return nil, nil, errBindParam(k, v, errNilParam)
  56. }
  57. val, t, err := encodeValue(v)
  58. if err != nil {
  59. return nil, nil, errBindParam(k, v, err)
  60. }
  61. if t == nil { // should not happen, because of nil check above
  62. return nil, nil, errBindParam(k, v, errNoType)
  63. }
  64. params.Fields[k] = val
  65. paramTypes[k] = t
  66. }
  67. return params, paramTypes, nil
  68. }
  69. // errBindParam returns error for not being able to bind parameter to query request.
  70. func errBindParam(k string, v interface{}, err error) error {
  71. if err == nil {
  72. return nil
  73. }
  74. se, ok := toSpannerError(err).(*Error)
  75. if !ok {
  76. return spannerErrorf(codes.InvalidArgument, "failed to bind query parameter(name: %q, value: %v), error = <%v>", k, v, err)
  77. }
  78. se.decorate(fmt.Sprintf("failed to bind query parameter(name: %q, value: %v)", k, v))
  79. return se
  80. }