forked from cockroachdb/cockroach
/
set.go
119 lines (107 loc) · 3.21 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
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"fmt"
"strings"
"time"
"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, 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, err
}
if len(dbName) != 0 {
// Verify database descriptor exists.
if _, err := p.getDatabaseDesc(dbName); err != nil {
return nil, err
}
}
p.session.Database = dbName
case `SYNTAX`:
s, err := p.getStringVal(name, n.Values)
if err != nil {
return nil, err
}
switch normalizeName(string(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, fmt.Errorf("%s: \"%s\" is not in (%q, %q)", name, s, parser.Modern, parser.Traditional)
}
default:
return nil, fmt.Errorf("unknown variable: %q", name)
}
return &valuesNode{}, 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 := p.evalCtx.EvalExpr(values[0])
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) SetTimeZone(n *parser.SetTimeZone) (planNode, error) {
d, err := p.evalCtx.EvalExpr(n.Value)
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, err
}
p.session.Timezone = &Session_Location{Location: location}
case parser.DInterval:
offset = int64(v.Duration / time.Second)
case parser.DInt:
offset = int64(v) * 60 * 60
case parser.DFloat:
offset = int64(float64(v) * 60.0 * 60.0)
default:
return nil, fmt.Errorf("bad time zone value: %v", n.Value)
}
if offset != 0 {
p.session.Timezone = &Session_Offset{Offset: offset}
}
p.evalCtx.GetLocation = p.session.getLocation
return &valuesNode{}, nil
}