surrealpatch/sql/util.go

159 lines
2.5 KiB
Go
Raw Normal View History

2016-02-26 17:27:07 +00:00
// Copyright © 2016 Abcum Ltd
//
// 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 (
"encoding/json"
2016-05-23 12:32:02 +00:00
"fmt"
2016-07-04 10:37:37 +00:00
"regexp"
2016-02-26 17:27:07 +00:00
"strconv"
"time"
)
func (p *Parser) 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 (p *Parser) 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 (p *Parser) 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 STRING:
return lit, nil
case REGION:
return lit, nil
2016-02-26 17:27:07 +00:00
case NULL:
2016-05-23 12:32:02 +00:00
return &Null{}, nil
case VOID:
return &Void{}, nil
2016-09-06 13:30:59 +00:00
case MISSING:
return &Void{}, nil
2016-05-23 12:32:02 +00:00
case EMPTY:
return &Empty{}, nil
2016-02-26 17:27:07 +00:00
case ALL:
2016-07-04 10:37:37 +00:00
return &All{}, nil
2016-02-26 17:27:07 +00:00
case ASC:
2016-05-23 12:32:02 +00:00
return &Asc{}, nil
2016-02-26 17:27:07 +00:00
case DESC:
2016-05-23 12:32:02 +00:00
return &Desc{}, nil
case ID:
2016-09-06 13:30:59 +00:00
return &Ident{lit}, nil
2016-02-26 17:27:07 +00:00
case IDENT:
2016-09-06 13:30:59 +00:00
return &Ident{lit}, nil
2016-05-23 12:32:02 +00:00
case TABLE:
return &Table{lit}, nil
2016-05-23 12:32:02 +00:00
case NOW:
2016-07-04 10:37:37 +00:00
return time.Now(), nil
2016-02-26 17:27:07 +00:00
case DATE:
2016-07-04 10:37:37 +00:00
return time.Parse("2006-01-02", lit)
2016-02-26 17:27:07 +00:00
case TIME:
2016-07-04 10:37:37 +00:00
return time.Parse(time.RFC3339, lit)
2016-02-26 17:27:07 +00:00
2016-07-04 10:37:37 +00:00
case REGEX:
return regexp.Compile(lit)
2016-02-26 17:27:07 +00:00
case NUMBER:
2016-09-06 13:30:59 +00:00
return strconv.ParseFloat(lit, 64)
2016-02-26 17:27:07 +00:00
case DOUBLE:
2016-07-04 10:37:37 +00:00
return strconv.ParseFloat(lit, 64)
2016-02-26 17:27:07 +00:00
case DURATION:
2016-07-04 10:37:37 +00:00
return time.ParseDuration(lit)
2016-05-23 12:32:02 +00:00
case ARRAY:
2016-09-06 13:30:59 +00:00
var j []interface{}
2016-07-04 10:37:37 +00:00
json.Unmarshal([]byte(lit), &j)
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:
2016-09-06 13:30:59 +00:00
var j map[string]interface{}
2016-07-04 10:37:37 +00:00
json.Unmarshal([]byte(lit), &j)
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-09-07 15:44:23 +00:00
case PARAM:
if p, ok := p.v[lit]; ok {
return p, nil
}
return nil, fmt.Errorf("Param %s is not defined", lit)
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
}