forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
147 lines (131 loc) · 4.13 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 The Cockroach Authors.
//
// 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"fmt"
"strings"
"time"
"gopkg.in/inf.v0"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
)
// Set sets session variables.
// Privileges: None.
// Notes: postgres/mysql do not require privileges for session variables (some exceptions).
func (p *planner) Set(n *parser.Set) (planNode, *roachpb.Error) {
// By using QualifiedName.String() here any variables that are keywords will
// be double quoted.
name := strings.ToUpper(n.Name.String())
switch name {
case `DATABASE`:
dbName, err := p.getStringVal(name, n.Values)
if err != nil {
return nil, roachpb.NewError(err)
}
if len(dbName) != 0 {
// Verify database descriptor exists.
if _, pErr := p.getDatabaseDesc(dbName); pErr != nil {
return nil, pErr
}
}
p.session.Database = dbName
case `SYNTAX`:
s, err := p.getStringVal(name, n.Values)
if err != nil {
return nil, roachpb.NewError(err)
}
switch NormalizeName(s) {
case NormalizeName(parser.Modern.String()):
p.session.Syntax = int32(parser.Modern)
case NormalizeName(parser.Traditional.String()):
p.session.Syntax = int32(parser.Traditional)
default:
return nil, roachpb.NewUErrorf("%s: \"%s\" is not in (%q, %q)", name, s, parser.Modern, parser.Traditional)
}
case `EXTRA_FLOAT_DIGITS`:
// These settings are sent by the JDBC driver but we silently ignore them.
default:
return nil, roachpb.NewUErrorf("unknown variable: %q", name)
}
return &emptyNode{}, nil
}
func (p *planner) getStringVal(name string, values parser.Exprs) (string, error) {
if len(values) != 1 {
return "", fmt.Errorf("%s: requires a single string value", name)
}
val, err := values[0].Eval(p.evalCtx)
if err != nil {
return "", err
}
s, ok := val.(parser.DString)
if !ok {
return "", fmt.Errorf("%s: requires a single string value: %s is a %s",
name, values[0], val.Type())
}
return string(s), nil
}
func (p *planner) SetDefaultIsolation(n *parser.SetDefaultIsolation) (planNode, error) {
switch n.Isolation {
case parser.SerializableIsolation:
p.session.DefaultIsolationLevel = roachpb.SERIALIZABLE
case parser.SnapshotIsolation:
p.session.DefaultIsolationLevel = roachpb.SNAPSHOT
default:
return nil, fmt.Errorf("unsupported default isolation level: %s", n.Isolation)
}
return &emptyNode{}, nil
}
func (p *planner) SetTimeZone(n *parser.SetTimeZone) (planNode, error) {
d, err := n.Value.Eval(p.evalCtx)
if err != nil {
return nil, err
}
var offset int64
switch v := d.(type) {
case parser.DString:
location := string(v)
if location == "DEFAULT" || location == "LOCAL" {
location = "UTC"
}
if _, err := time.LoadLocation(location); err != nil {
return nil, fmt.Errorf("cannot find time zone %q: %v", location, err)
}
p.session.Timezone = &SessionLocation{Location: location}
case parser.DInterval:
offset, _, _, err = v.Duration.Div(time.Second.Nanoseconds()).Encode()
if err != nil {
return nil, err
}
case parser.DInt:
offset = int64(v) * 60 * 60
case parser.DFloat:
offset = int64(float64(v) * 60.0 * 60.0)
case *parser.DDecimal:
sixty := inf.NewDec(60, 0)
sixty.Mul(sixty, sixty).Mul(sixty, &v.Dec)
var ok bool
if offset, ok = sixty.Unscaled(); !ok {
return nil, fmt.Errorf("time zone value %s would overflow an int64", sixty)
}
default:
return nil, fmt.Errorf("bad time zone value: %v", n.Value)
}
if offset != 0 {
p.session.Timezone = &SessionOffset{Offset: offset}
}
p.evalCtx.GetLocation = p.session.getLocation
return &emptyNode{}, nil
}