surrealpatch/sql/util.go

144 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-02-26 17:27:07 +00:00
"strconv"
"time"
)
func in(token Token, tokens []Token) bool {
for _, t := range tokens {
if token == t {
return true
}
}
return false
}
func is(token Token, tokens ...Token) bool {
for _, t := range tokens {
if token == t {
return true
}
}
return false
}
2016-05-23 12:32:02 +00:00
func contains(search string, strings []string) bool {
for _, str := range strings {
if str == search {
return true
}
}
return false
}
func declare(tok Token, lit string) (Expr, error) {
2016-02-26 17:27:07 +00:00
switch tok {
case NULL:
2016-05-23 12:32:02 +00:00
return &Null{}, nil
case VOID:
return &Void{}, nil
case EMPTY:
return &Empty{}, nil
2016-02-26 17:27:07 +00:00
case ALL:
2016-05-23 12:32:02 +00:00
return &Wildcard{}, 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:
return &IdentLiteral{Val: lit}, nil
2016-02-26 17:27:07 +00:00
case IDENT:
2016-05-23 12:32:02 +00:00
return &IdentLiteral{Val: lit}, nil
case NOW:
return &DatetimeLiteral{Val: time.Now()}, nil
2016-02-26 17:27:07 +00:00
case DATE:
2016-05-23 12:32:02 +00:00
t, err := time.Parse("2006-01-02", lit)
return &DatetimeLiteral{Val: t}, err
2016-02-26 17:27:07 +00:00
case TIME:
2016-05-23 12:32:02 +00:00
t, err := time.Parse(time.RFC3339, lit)
return &DatetimeLiteral{Val: t}, err
2016-02-26 17:27:07 +00:00
case TRUE:
2016-05-23 12:32:02 +00:00
return &BooleanLiteral{Val: true}, nil
2016-02-26 17:27:07 +00:00
case FALSE:
2016-05-23 12:32:02 +00:00
return &BooleanLiteral{Val: false}, nil
2016-02-26 17:27:07 +00:00
case STRING:
2016-05-23 12:32:02 +00:00
return &StringLiteral{Val: lit}, nil
2016-02-26 17:27:07 +00:00
case REGION:
2016-05-23 12:32:02 +00:00
return &StringLiteral{Val: lit}, nil
2016-02-26 17:27:07 +00:00
case NUMBER:
2016-05-23 12:32:02 +00:00
i, err := strconv.ParseInt(lit, 10, 64)
return &NumberLiteral{Val: i}, err
2016-02-26 17:27:07 +00:00
case DOUBLE:
2016-05-23 12:32:02 +00:00
f, err := strconv.ParseFloat(lit, 64)
return &DoubleLiteral{Val: f}, err
2016-02-26 17:27:07 +00:00
case DURATION:
2016-05-23 12:32:02 +00:00
t, err := time.ParseDuration(lit)
return &DurationLiteral{Val: t}, err
case ARRAY:
var j ArrayLiteral
json.Unmarshal([]byte(lit), &j.Val)
if j.Val == nil {
return &j, fmt.Errorf("Invalid JSON: %s", lit)
}
return &j, nil
2016-02-26 17:27:07 +00:00
case JSON:
2016-05-23 12:32:02 +00:00
var j JSONLiteral
json.Unmarshal([]byte(lit), &j.Val)
if j.Val == nil {
return &j, fmt.Errorf("Invalid JSON: %s", lit)
}
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
}