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 (
|
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"
|
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
|
|
|
|
|
|
|
json "github.com/hjson/hjson-go"
|
2016-02-26 17:27:07 +00:00
|
|
|
)
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) declare(tok Token, lit string) (interface{}, error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-09-07 15:53:04 +00:00
|
|
|
if val := p.hold(tok); val != nil {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
switch tok {
|
|
|
|
|
2017-11-16 20:53:13 +00:00
|
|
|
case NULL:
|
|
|
|
return nil, nil
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
case TRUE:
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
case FALSE:
|
|
|
|
return false, nil
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
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
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
case MUL:
|
2016-07-04 10:37:37 +00:00
|
|
|
return &All{}, nil
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
case QMARK:
|
|
|
|
return &Any{}, nil
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
case STRING:
|
|
|
|
return &Value{lit}, nil
|
|
|
|
|
|
|
|
case REGION:
|
|
|
|
return &Value{lit}, nil
|
|
|
|
|
2017-11-16 20:53:13 +00:00
|
|
|
case EXPR:
|
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
|
|
|
|
2016-09-07 16:01:32 +00:00
|
|
|
case TABLE:
|
|
|
|
return &Table{lit}, nil
|
|
|
|
|
2017-11-16 20:53:13 +00:00
|
|
|
case PARAM:
|
|
|
|
return &Param{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
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
case REGEX:
|
|
|
|
return regexp.Compile(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)
|
2016-10-29 11:28:20 +00:00
|
|
|
}
|
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-10-29 11:28:20 +00:00
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
case ARRAY:
|
2017-11-16 20:53:13 +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:
|
2017-11-16 20:53:13 +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-05-23 12:32:02 +00:00
|
|
|
return lit, nil
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|