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