surrealpatch/sql/ast.go

846 lines
16 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
2016-09-14 21:23:02 +00:00
import (
"strconv"
2017-11-16 20:53:13 +00:00
"strings"
2016-09-14 21:23:02 +00:00
"time"
2017-11-16 20:53:13 +00:00
"golang.org/x/text/language"
2016-09-14 21:23:02 +00:00
)
2016-02-26 17:27:07 +00:00
// --------------------------------------------------
// Queries
// --------------------------------------------------
// Query represents a multi statement SQL query
type Query struct {
Statements Statements
}
// Statement represents a single SQL AST
type Statement interface{}
// Statements represents multiple SQL ASTs
type Statements []Statement
// --------------------------------------------------
// Other
// --------------------------------------------------
type AuthableStatement interface {
Auth() (string, string)
}
type KillableStatement interface {
Duration() time.Duration
}
2017-11-16 20:53:13 +00:00
type WriteableStatement interface {
Writeable() bool
}
2016-05-23 12:32:02 +00:00
// --------------------------------------------------
2016-09-06 13:30:59 +00:00
// Trans
2016-05-23 12:32:02 +00:00
// --------------------------------------------------
2016-09-14 21:23:02 +00:00
// UseStatement represents a SQL BEGIN TRANSACTION statement.
2016-09-06 13:30:59 +00:00
type BeginStatement struct{}
2016-09-14 21:23:02 +00:00
// UseStatement represents a SQL CANCEL TRANSACTION statement.
2016-09-06 13:30:59 +00:00
type CancelStatement struct{}
2016-09-14 21:23:02 +00:00
// UseStatement represents a SQL COMMIT TRANSACTION statement.
2016-09-06 13:30:59 +00:00
type CommitStatement struct{}
2016-09-19 10:08:44 +00:00
// --------------------------------------------------
// Use
// --------------------------------------------------
// UseStatement represents a SQL USE statement.
type UseStatement struct {
NS string
DB string
2016-09-19 10:08:44 +00:00
}
// --------------------------------------------------
// Info
// --------------------------------------------------
// InfoStatement represents an SQL INFO statement.
type InfoStatement struct {
KV string
NS string
DB string
Kind Token
2018-09-13 14:23:17 +00:00
What *Ident
2016-09-19 10:08:44 +00:00
}
2017-11-16 20:53:13 +00:00
// --------------------------------------------------
// If
// --------------------------------------------------
// IfStatement represents an if else clause.
type IfStatement struct {
RW bool
Cond Exprs
Then Exprs
Else Expr
2017-11-16 20:53:13 +00:00
}
2018-04-14 16:55:05 +00:00
// --------------------------------------------------
// Run
// --------------------------------------------------
// RunStatement represents a run clause.
type RunStatement struct {
RW bool
Expr Expr
2018-04-14 16:55:05 +00:00
}
// --------------------------------------------------
// LET
// --------------------------------------------------
// LetStatement represents a SQL LET statement.
type LetStatement struct {
RW bool
KV string
NS string
DB string
Name *Ident
What Expr
}
// ReturnStatement represents a SQL RETURN statement.
type ReturnStatement struct {
RW bool
KV string
NS string
DB string
What Exprs
}
2016-09-06 13:30:59 +00:00
// --------------------------------------------------
// Normal
// --------------------------------------------------
2016-10-14 21:21:17 +00:00
// LiveStatement represents a SQL LIVE statement.
type LiveStatement struct {
ID string
FB string
KV string
NS string
DB string
Diff bool
Expr Fields
What Exprs
Cond Expr
Fetch Fetchs
2017-11-16 20:53:13 +00:00
}
// KillStatement represents a SQL KILL statement.
type KillStatement struct {
FB string
KV string
NS string
DB string
What Exprs
2016-10-14 21:21:17 +00:00
}
2016-02-26 17:27:07 +00:00
// SelectStatement represents a SQL SELECT statement.
type SelectStatement struct {
RW bool
KV string
NS string
DB string
Expr Fields
What Exprs
Cond Expr
Group Groups
Order Orders
Limit Expr
Start Expr
Fetch Fetchs
Version Expr
Timeout time.Duration
Parallel int
2016-02-26 17:27:07 +00:00
}
// CreateStatement represents a SQL CREATE statement.
type CreateStatement struct {
KV string
NS string
DB string
What Exprs
Data Expr
Echo Token
Timeout time.Duration
Parallel int
2016-02-26 17:27:07 +00:00
}
// UpdateStatement represents a SQL UPDATE statement.
type UpdateStatement struct {
KV string
NS string
DB string
What Exprs
Data Expr
Cond Expr
Echo Token
Timeout time.Duration
Parallel int
2016-02-26 17:27:07 +00:00
}
// DeleteStatement represents a SQL DELETE statement.
type DeleteStatement struct {
KV string
NS string
DB string
What Exprs
Cond Expr
Echo Token
Timeout time.Duration
Parallel int
2016-02-26 17:27:07 +00:00
}
// RelateStatement represents a SQL RELATE statement.
type RelateStatement struct {
KV string
NS string
DB string
Type Expr
From Exprs
With Exprs
Data Expr
Uniq bool
Echo Token
Timeout time.Duration
Parallel int
2017-11-16 20:53:13 +00:00
}
// InsertStatement represents a SQL INSERT statement.
type InsertStatement struct {
KV string
NS string
DB string
Data Expr
Into *Table
Echo Token
Timeout time.Duration
Parallel int
2017-11-16 20:53:13 +00:00
}
// UpsertStatement represents a SQL UPSERT statement.
type UpsertStatement struct {
KV string
NS string
DB string
Data Expr
Into *Table
Echo Token
Timeout time.Duration
Parallel int
2016-02-26 17:27:07 +00:00
}
// --------------------------------------------------
// Namespace
// --------------------------------------------------
type DefineNamespaceStatement struct {
KV string
NS string
DB string
Name *Ident
}
type RemoveNamespaceStatement struct {
KV string
NS string
DB string
Name *Ident
}
// --------------------------------------------------
// Database
// --------------------------------------------------
type DefineDatabaseStatement struct {
KV string
NS string
DB string
Name *Ident
}
type RemoveDatabaseStatement struct {
KV string
NS string
DB string
Name *Ident
}
2016-11-16 22:43:36 +00:00
// --------------------------------------------------
// Login
// --------------------------------------------------
// DefineLoginStatement represents an SQL DEFINE LOGIN statement.
type DefineLoginStatement struct {
KV string
NS string
DB string
Kind Token
User *Ident
Pass []byte
Hash []byte
Code []byte
2016-11-16 22:43:36 +00:00
}
// RemoveLoginStatement represents an SQL REMOVE LOGIN statement.
type RemoveLoginStatement struct {
KV string
NS string
DB string
Kind Token
User *Ident
2016-11-16 22:43:36 +00:00
}
2016-11-16 22:47:23 +00:00
// --------------------------------------------------
// Token
// --------------------------------------------------
// DefineTokenStatement represents an SQL DEFINE TOKEN statement.
type DefineTokenStatement struct {
KV string
NS string
DB string
Kind Token
Name *Ident
What *Ident
Type string
Code []byte
2016-11-16 22:47:23 +00:00
}
// RemoveTokenStatement represents an SQL REMOVE TOKEN statement.
type RemoveTokenStatement struct {
KV string
NS string
DB string
Kind Token
Name *Ident
What *Ident
2016-11-16 22:47:23 +00:00
}
2016-10-14 21:21:25 +00:00
// --------------------------------------------------
// Scope
// --------------------------------------------------
// DefineScopeStatement represents an SQL DEFINE SCOPE statement.
type DefineScopeStatement struct {
KV string
NS string
DB string
Name *Ident
Time time.Duration
Code []byte
Signup Expr
Signin Expr
Connect Expr
OnSignup Expr
OnSignin Expr
2016-10-14 21:21:25 +00:00
}
// RemoveScopeStatement represents an SQL REMOVE SCOPE statement.
type RemoveScopeStatement struct {
KV string
NS string
DB string
Name *Ident
2016-10-14 21:21:25 +00:00
}
2016-07-21 21:50:16 +00:00
// --------------------------------------------------
2016-09-20 23:25:39 +00:00
// Table
2016-07-21 21:50:16 +00:00
// --------------------------------------------------
2016-09-20 23:25:39 +00:00
// DefineTableStatement represents an SQL DEFINE TABLE statement.
type DefineTableStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
Full bool
Vers bool
Drop bool
Lock bool
Expr Fields
From Tables
Cond Expr
Group Groups
Perms Expr
2016-07-21 21:50:16 +00:00
}
2016-09-20 23:25:39 +00:00
// RemoveTableStatement represents an SQL REMOVE TABLE statement.
type RemoveTableStatement struct {
KV string
NS string
DB string
What Tables
2016-07-21 21:50:16 +00:00
}
2017-11-16 20:53:13 +00:00
// --------------------------------------------------
// Event
// --------------------------------------------------
// DefineEventStatement represents an SQL DEFINE EVENT statement.
type DefineEventStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
When Expr
Then Expr
2017-11-16 20:53:13 +00:00
}
// RemoveEventStatement represents an SQL REMOVE EVENT statement.
type RemoveEventStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
2017-11-16 20:53:13 +00:00
}
2016-05-23 12:32:02 +00:00
// --------------------------------------------------
// Field
// --------------------------------------------------
2016-09-20 23:25:39 +00:00
// DefineFieldStatement represents an SQL DEFINE FIELD statement.
2016-05-23 12:32:02 +00:00
type DefineFieldStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
Perms Expr
Type string
Kind string
Value Expr
Assert Expr
Priority float64
2016-05-23 12:32:02 +00:00
}
2016-09-20 23:25:39 +00:00
// RemoveFieldStatement represents an SQL REMOVE FIELD statement.
2016-05-23 12:32:02 +00:00
type RemoveFieldStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
2016-02-26 17:27:07 +00:00
}
// --------------------------------------------------
2016-05-23 12:32:02 +00:00
// Index
2016-02-26 17:27:07 +00:00
// --------------------------------------------------
2016-05-23 12:32:02 +00:00
// DefineIndexStatement represents an SQL DEFINE INDEX statement.
type DefineIndexStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
Cols Idents
Uniq bool
2016-02-26 17:27:07 +00:00
}
2016-05-23 12:32:02 +00:00
// RemoveIndexStatement represents an SQL REMOVE INDEX statement.
type RemoveIndexStatement struct {
KV string
NS string
DB string
Name *Ident
What Tables
2016-02-26 17:27:07 +00:00
}
// --------------------------------------------------
// Literals
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Expr represents a sql expression.
2016-02-26 17:27:07 +00:00
type Expr interface{}
2017-03-02 10:47:10 +00:00
// Exprs represents multiple sql expressions.
type Exprs []Expr
// All represents a * expression.
2016-07-04 10:37:37 +00:00
type All struct{}
// Any represents a ? expression.
type Any struct{}
2017-12-12 01:09:08 +00:00
// Null represents an expression which is null.
type Null struct{}
2016-05-23 12:32:02 +00:00
// Void represents an expression which is not set.
type Void struct{}
// Empty represents an expression which is null or "".
type Empty struct{}
2016-09-14 21:23:02 +00:00
// Field represents a SELECT AS clause.
type Field struct {
Expr Expr
2017-11-16 20:53:13 +00:00
Field string
Alias string
2016-09-14 21:23:02 +00:00
}
2017-03-02 10:47:10 +00:00
// Fields represents multiple SELECT AS clauses.
type Fields []*Field
2016-09-14 21:23:02 +00:00
// Group represents a GROUP BY clause.
type Group struct {
Expr Expr
}
2017-03-02 10:47:10 +00:00
// Groups represents multiple GROUP BY clauses.
type Groups []*Group
2016-09-14 21:23:02 +00:00
// Order represents a ORDER BY clause.
type Order struct {
Expr Expr
2017-11-16 20:53:13 +00:00
Dir bool
Tag language.Tag
2016-09-14 21:23:02 +00:00
}
2017-03-02 10:47:10 +00:00
// Orders represents multiple ORDER BY clauses.
type Orders []*Order
// Fetch represents a FETCH AS clause.
type Fetch struct {
Expr Expr
}
// Fetchs represents multiple FETCH AS clauses.
type Fetchs []*Fetch
// --------------------------------------------------
// Expressions
// --------------------------------------------------
// SubExpression represents a subquery.
type SubExpression struct {
2016-02-26 17:27:07 +00:00
Expr Expr
}
2018-03-18 21:01:29 +00:00
// MultExpression represents multiple queries.
type MultExpression struct {
Expr []Expr
}
2017-11-16 20:53:13 +00:00
// IfelExpression represents an if else clause.
type IfelExpression struct {
Cond Exprs
Then Exprs
Else Expr
}
// FuncExpression represents a function call.
type FuncExpression struct {
Name string
2017-03-02 10:47:10 +00:00
Args Exprs
2017-11-16 20:53:13 +00:00
Aggr bool
}
2017-03-02 10:47:10 +00:00
// ItemExpression represents a part of a SET expression.
type ItemExpression struct {
LHS Expr
Op Token
RHS Expr
}
// BinaryExpression represents a WHERE expression.
2016-02-26 17:27:07 +00:00
type BinaryExpression struct {
LHS Expr
2016-09-06 13:30:59 +00:00
Op Token
2016-02-26 17:27:07 +00:00
RHS Expr
}
// PathExpression represents a path expression.
type PathExpression struct {
2017-03-02 10:47:10 +00:00
Expr Exprs
}
// PartExpression represents a path part expression.
type PartExpression struct {
Part Expr
}
2017-03-02 10:47:10 +00:00
// JoinExpression represents a path join expression.
type JoinExpression struct {
Join Token
}
// SubpExpression represents a sub path expression.
type SubpExpression struct {
2017-03-02 10:47:10 +00:00
What Exprs
Name *Ident
Cond Expr
}
// PermExpression represents a permissions expression.
type PermExpression struct {
Select Expr
Create Expr
Update Expr
Delete Expr
2017-03-02 10:47:10 +00:00
}
// DataExpression represents a SET expression.
type DataExpression struct {
Data []*ItemExpression
}
// DiffExpression represents a JSON to DIFF
2016-05-23 12:32:02 +00:00
type DiffExpression struct {
2017-03-02 10:47:10 +00:00
Data Expr
2016-05-23 12:32:02 +00:00
}
// MergeExpression represents JSON to MERGE
type MergeExpression struct {
2017-03-02 10:47:10 +00:00
Data Expr
2016-05-23 12:32:02 +00:00
}
// ContentExpression represents JSON to REPLACE
type ContentExpression struct {
2017-03-02 10:47:10 +00:00
Data Expr
2016-05-23 12:32:02 +00:00
}
2016-02-26 17:27:07 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Param
2016-02-26 17:27:07 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Params represents multiple Param clauses.
type Params []*Param
// Param comment
type Param struct {
VA string
2016-07-04 10:37:37 +00:00
}
func NewParam(VA string) *Param {
return &Param{VA}
2016-09-14 21:23:02 +00:00
}
// --------------------------------------------------
// Ident
// --------------------------------------------------
// Idents represents multiple Ident clauses.
type Idents []*Ident
// Ident comment
type Ident struct {
VA string
}
func NewIdent(VA string) *Ident {
return &Ident{VA}
}
2016-09-14 21:23:02 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Value
2016-09-14 21:23:02 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Values represents multiple Value clauses.
type Values []*Value
// Value comment
type Value struct {
VA string
}
func NewValue(VA string) *Value {
return &Value{VA}
2017-03-02 10:47:10 +00:00
}
// --------------------------------------------------
// Regex
2017-03-02 10:47:10 +00:00
// --------------------------------------------------
// Regexs represents multiple Regex clauses.
type Regexs []*Regex
2017-03-02 10:47:10 +00:00
// Regex comment
type Regex struct {
VA string
2017-03-02 10:47:10 +00:00
}
func NewRegex(VA string) *Regex {
return &Regex{VA}
}
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Table
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Tables represents multiple Table clauses.
type Tables []*Table
2016-09-06 13:30:59 +00:00
// Table comment
type Table struct {
TB string
2016-02-26 17:27:07 +00:00
}
2016-09-14 21:23:02 +00:00
func NewTable(TB string) *Table {
return &Table{TB}
}
2017-11-16 20:53:13 +00:00
// --------------------------------------------------
// Batch
// --------------------------------------------------
// Batchs represents multiple Batch clauses.
type Batchs []*Batch
// Batch comment
type Batch struct {
TB string
BA []*Thing
}
func NewBatch(TB string, BA []interface{}) *Batch {
var b = &Batch{TB: TB}
for _, ID := range BA {
b.BA = append(b.BA, NewThing(TB, ID))
}
return b
}
// --------------------------------------------------
// Model
// --------------------------------------------------
// Models represents multiple Model clauses.
type Models []*Model
// Model comment
type Model struct {
TB string
MIN float64
INC float64
MAX float64
}
func NewModel(TB string, MIN, INC, MAX float64) *Model {
return &Model{TB: TB, MIN: MIN, INC: INC, MAX: MAX}
}
2016-09-14 21:23:02 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Thing
2016-09-14 21:23:02 +00:00
// --------------------------------------------------
2017-03-02 10:47:10 +00:00
// Things represents multiple Thing clauses.
type Things []*Thing
2016-02-26 17:27:07 +00:00
// Thing comment
type Thing struct {
2017-03-02 10:47:10 +00:00
TB string
2016-05-24 12:51:52 +00:00
ID interface{}
2016-02-26 17:27:07 +00:00
}
func ParseThing(val string) *Thing {
r := strings.NewReader(val)
s := newScanner(r)
if t, _, v := s.scanIdiom(); t == THING {
return v.(*Thing)
}
return nil
}
2017-03-02 10:47:10 +00:00
func NewThing(TB string, ID interface{}) *Thing {
2017-11-16 20:53:13 +00:00
switch val := ID.(type) {
default:
return &Thing{TB: TB, ID: ID}
case int:
return &Thing{TB: TB, ID: float64(val)}
case int64:
return &Thing{TB: TB, ID: float64(val)}
case string:
val = strings.Replace(val, TB+":", "", -1)
if cnv, err := strconv.ParseFloat(val, 64); err == nil {
2016-09-14 21:23:02 +00:00
return &Thing{TB: TB, ID: cnv}
2017-11-16 20:53:13 +00:00
} else if cnv, err := strconv.ParseBool(val); err == nil {
return &Thing{TB: TB, ID: cnv}
2017-11-16 20:53:13 +00:00
} else if cnv, err := time.Parse(RFCDate, val); err == nil {
2016-09-14 21:23:02 +00:00
return &Thing{TB: TB, ID: cnv.UTC()}
2017-11-16 20:53:13 +00:00
} else if cnv, err := time.Parse(RFCTime, val); err == nil {
2016-09-14 21:23:02 +00:00
return &Thing{TB: TB, ID: cnv.UTC()}
}
2017-11-16 20:53:13 +00:00
return &Thing{TB: TB, ID: val}
2016-09-14 21:23:02 +00:00
}
2017-11-16 20:53:13 +00:00
}
// --------------------------------------------------
// Point
// --------------------------------------------------
// Points represents multiple Point clauses.
type Points []*Point
// Point comment
type Point struct {
LA float64
LO float64
}
func NewPoint(LA, LO float64) *Point {
return &Point{LA: LA, LO: LO}
}
// --------------------------------------------------
// Circle
// --------------------------------------------------
// Circles represents multiple Circle clauses.
type Circles []*Circle
// Circle comment
type Circle struct {
CE *Point
RA float64
}
func NewCircle(CE *Point, RA float64) *Circle {
return &Circle{CE: CE, RA: RA}
}
// --------------------------------------------------
// Polygon
// --------------------------------------------------
// Polygons represents multiple Polygon clauses.
type Polygons []*Polygon
// Polygon comment
type Polygon struct {
PS []*Point
2017-11-16 20:53:13 +00:00
}
func NewPolygon(PS ...*Point) *Polygon {
return &Polygon{PS: PS}
2016-02-26 17:27:07 +00:00
}