-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
168 lines (151 loc) · 3.5 KB
/
utils.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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.
*/
package ydb
import (
"fmt"
"log"
"reflect"
"strings"
"unicode/utf8"
"github.com/qiniu/x/ctype"
)
// -----------------------------------------------------------------------------
func recoverErr(e any) error {
if v, ok := e.(error); ok {
return v
}
return fmt.Errorf("%v", e)
}
// -----------------------------------------------------------------------------
func isSlice(v any) bool {
return reflect.ValueOf(v).Kind() == reflect.Slice
}
func checkArgSlice(args []any) int {
iArgSlice := -1
for i, arg := range args {
if isSlice(arg) {
if iArgSlice >= 0 {
log.Panicf(
"query: multiple arguments (%dth, %dth) are slices (only one can be)\n",
iArgSlice+1, i+1,
)
}
iArgSlice = i
}
}
return iArgSlice
}
// -----------------------------------------------------------------------------
func (p *Class) tblFromNames(names []string) (tbl string) {
var v string
tbl, names[0] = tblFromName(names[0])
for i := 1; i < len(names); i++ {
if v, names[i] = tblFromName(names[i]); v != tbl {
log.Panicln("insert: multiple tables")
}
}
if tbl == "" {
tbl = p.tbl
}
return
}
func tblFromName(name string) (string, string) {
if pos := strings.IndexByte(name, '.'); pos > 0 {
return name[:pos], name[pos+1:]
}
return "", name
}
// -----------------------------------------------------------------------------
func (p *Class) exprTblname(cond string) string {
tbls := exprTblnames(cond)
tbl := ""
switch len(tbls) {
case 0:
case 1:
tbl = tbls[0]
default:
log.Panicln("query currently doesn't support multiple tables")
}
if tbl == "" {
tbl = p.tbl
}
return tbl
}
func exprTblnames(expr string) (tbls []string) {
for expr != "" {
pos := ctype.ScanCSymbol(expr)
if pos != 0 {
name := ""
if pos > 0 {
switch expr[pos] {
case '.':
name = expr[:pos]
expr = ctype.SkipCSymbol(expr[pos+1:])
case '(': // function call, eg. SUM(...)
expr = expr[pos+1:]
continue
default:
expr = expr[pos:]
}
} else {
expr = ""
}
switch name {
case "AND", "OR":
default:
tbls = addTblname(tbls, name)
}
continue
}
pos = ctype.ScanTypeEx(ctype.FLOAT_FIRST_CHAT, ctype.CSYMBOL_NEXT_CHAR, expr)
if pos == 0 {
c, size := utf8.DecodeRuneInString(expr)
switch c {
case '\'':
expr = skipStringConst(expr[1:], '\'')
default:
expr = expr[size:]
}
} else if pos < 0 {
break
} else {
expr = expr[pos:]
}
}
return
}
func skipStringConst(next string, quot rune) string {
skip := false
for i, c := range next {
if skip {
skip = false
} else if c == '\\' {
skip = true
} else if c == quot {
return next[i+1:]
}
}
return ""
}
func addTblname(tbls []string, tbl string) []string {
for _, v := range tbls {
if v == tbl {
return tbls
}
}
return append(tbls, tbl)
}
// -----------------------------------------------------------------------------