surrealpatch/sql/util.go

172 lines
2.9 KiB
Go
Raw Normal View History

2021-12-14 08:12:26 +00:00
// Copyright © 2016 SurrealDB Ltd.
2016-02-26 17:27:07 +00:00
//
// 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 sql
import (
2016-05-23 12:32:02 +00:00
"fmt"
2016-02-26 17:27:07 +00:00
"strconv"
2017-11-16 20:53:13 +00:00
"strings"
2016-02-26 17:27:07 +00:00
"time"
2017-02-03 19:08:57 +00:00
"github.com/hjson/hjson-go"
2016-02-26 17:27:07 +00:00
)
func in(token Token, tokens []Token) bool {
2016-02-26 17:27:07 +00:00
for _, t := range tokens {
if token == t {
return true
}
}
return false
}
func is(token Token, tokens ...Token) bool {
2016-02-26 17:27:07 +00:00
for _, t := range tokens {
if token == t {
return true
}
}
return false
}
func contains(search string, strings []string) bool {
2016-05-23 12:32:02 +00:00
for _, str := range strings {
if str == search {
return true
}
}
return false
}
func (p *parser) declare(tok Token, lit string) (interface{}, error) {
2016-02-26 17:27:07 +00:00
if val := p.hold(tok); val != nil {
return val, nil
}
2016-02-26 17:27:07 +00:00
switch tok {
2016-07-04 10:37:37 +00:00
case TRUE:
return true, nil
case FALSE:
return false, nil
case MUL:
return new(All), nil
case QMARK:
return new(Any), nil
2017-12-12 01:09:08 +00:00
case NULL:
return new(Null), nil
2017-12-12 01:09:08 +00:00
2016-05-23 12:32:02 +00:00
case VOID:
return new(Void), nil
2016-05-23 12:32:02 +00:00
2016-09-06 13:30:59 +00:00
case MISSING:
return new(Void), nil
2016-09-06 13:30:59 +00:00
2016-05-23 12:32:02 +00:00
case EMPTY:
return new(Empty), nil
2017-03-02 10:47:10 +00:00
2017-11-16 20:53:13 +00:00
case EXPR:
return NewIdent(lit), nil
2016-02-26 17:27:07 +00:00
case IDENT:
return NewIdent(lit), nil
2016-05-23 12:32:02 +00:00
case TABLE:
return NewTable(lit), nil
2017-11-16 20:53:13 +00:00
case PARAM:
return NewParam(lit), nil
2016-02-26 17:27:07 +00:00
case REGEX:
return NewRegex(lit), nil
case STRING:
return NewValue(lit), nil
case REGION:
return NewValue(lit), nil
2016-02-26 17:27:07 +00:00
case DATE:
2017-11-16 20:53:13 +00:00
return time.Parse(RFCDate, lit)
2016-02-26 17:27:07 +00:00
case TIME:
2017-11-16 20:53:13 +00:00
return time.Parse(RFCTime, lit)
2016-02-26 17:27:07 +00:00
case NUMBER:
2017-11-16 20:53:13 +00:00
val, err := strconv.ParseFloat(lit, 64)
if err != nil {
return val, fmt.Errorf("Invalid number: %s", lit)
}
return val, nil
2016-02-26 17:27:07 +00:00
case DOUBLE:
2017-11-16 20:53:13 +00:00
val, err := strconv.ParseFloat(lit, 64)
if err != nil {
return val, fmt.Errorf("Invalid number: %s", lit)
}
return val, nil
2016-02-26 17:27:07 +00:00
case DURATION:
2017-11-16 20:53:13 +00:00
var mul time.Duration
switch {
default:
mul = 1
case strings.HasSuffix(lit, "d"):
mul, lit = 24, strings.Replace(lit, "d", "h", -1)
case strings.HasSuffix(lit, "w"):
mul, lit = 168, strings.Replace(lit, "w", "h", -1)
}
2017-11-16 20:53:13 +00:00
val, err := time.ParseDuration(lit)
if err != nil {
return val, fmt.Errorf("Invalid duration: %s", lit)
}
return val * mul, nil
2016-05-23 12:32:02 +00:00
case ARRAY:
2017-11-16 20:53:13 +00:00
var j []interface{}
hjson.Unmarshal([]byte(lit), &j)
2016-07-04 10:37:37 +00:00
if j == nil {
return j, fmt.Errorf("Invalid JSON: %s", lit)
2016-05-23 12:32:02 +00:00
}
2016-07-04 10:37:37 +00:00
return j, nil
2016-02-26 17:27:07 +00:00
case JSON:
2017-11-16 20:53:13 +00:00
var j map[string]interface{}
hjson.Unmarshal([]byte(lit), &j)
2016-07-04 10:37:37 +00:00
if j == nil {
return j, fmt.Errorf("Invalid JSON: %s", lit)
2016-05-23 12:32:02 +00:00
}
2016-07-04 10:37:37 +00:00
return j, nil
2016-02-26 17:27:07 +00:00
}
2016-05-23 12:32:02 +00:00
return lit, nil
2016-02-26 17:27:07 +00:00
}