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
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Select
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// UseStatement represents a SQL USE statement.
|
|
|
|
type UseStatement struct {
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
CK string // Cipherkey
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Select
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// SelectStatement represents a SQL SELECT statement.
|
|
|
|
type SelectStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Expr []*Field // Which fields
|
|
|
|
What []Expr // What to select
|
|
|
|
Cond []Expr // Select conditions
|
|
|
|
Group []*Group // Group by
|
|
|
|
Order []*Order // Order by
|
|
|
|
Limit Expr // Limit by
|
|
|
|
Start Expr // Start at
|
|
|
|
Version Expr // Version
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Items
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// CreateStatement represents a SQL CREATE statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// CREATE person SET column = 'value' RETURN ID
|
2016-02-26 17:27:07 +00:00
|
|
|
type CreateStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Expr // What to create
|
|
|
|
Data []Expr // Create data
|
|
|
|
Echo Token // What to return
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStatement represents a SQL UPDATE statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// UPDATE person SET column = 'value' WHERE age < 18 RETURN ID
|
2016-02-26 17:27:07 +00:00
|
|
|
type UpdateStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Expr // What to update
|
|
|
|
Data []Expr // Update data
|
|
|
|
Cond []Expr // Update conditions
|
|
|
|
Echo Token // What to return
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-27 09:44:49 +00:00
|
|
|
// ModifyStatement represents a SQL UPDATE statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// MODIFY @person:123 WITH {} RETURN ID
|
2016-02-27 09:44:49 +00:00
|
|
|
type ModifyStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Expr // What to modify
|
2016-07-04 10:37:37 +00:00
|
|
|
Diff []Expr // Diff object
|
|
|
|
Cond []Expr // Update conditions
|
2016-05-23 12:32:02 +00:00
|
|
|
Echo Token // What to return
|
2016-02-27 09:44:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// DeleteStatement represents a SQL DELETE statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// DELETE FROM person WHERE age < 18 RETURN ID
|
2016-02-26 17:27:07 +00:00
|
|
|
type DeleteStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
2016-07-04 10:37:37 +00:00
|
|
|
Hard bool // Expunge
|
2016-05-23 12:32:02 +00:00
|
|
|
What []Expr // What to delete
|
|
|
|
Cond []Expr // Delete conditions
|
|
|
|
Echo Token // What to return
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RelateStatement represents a SQL RELATE statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// RELATE friend FROM @person:123 TO @person:456 SET column = 'value' RETURN ID
|
2016-02-26 17:27:07 +00:00
|
|
|
type RelateStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Type []Expr
|
|
|
|
From []Expr
|
|
|
|
To []Expr
|
|
|
|
Data []Expr
|
|
|
|
Echo Token // What to return
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RecordStatement represents a SQL CREATE EVENT statement.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
2016-02-26 17:27:07 +00:00
|
|
|
// RECORD login ON @person:123 AT 2016-01-29T22:42:56.478Z SET column = true
|
|
|
|
type RecordStatement struct {
|
2016-05-23 12:32:02 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Type []Expr
|
|
|
|
On []Expr
|
2016-02-26 17:27:07 +00:00
|
|
|
At Expr
|
2016-05-23 12:32:02 +00:00
|
|
|
Data []Expr
|
|
|
|
Echo Token // What to return
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:50:16 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Rules
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// DefineRulesStatement represents an SQL DEFINE RULES statement.
|
|
|
|
//
|
|
|
|
// DEFINE RULES person
|
|
|
|
type DefineRulesStatement struct {
|
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Table // Table names
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRulesStatement represents an SQL REMOVE RULES statement.
|
|
|
|
//
|
|
|
|
// REMOVE RULES person
|
|
|
|
type RemoveRulesStatement struct {
|
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Table // Table names
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// --------------------------------------------------
|
2016-05-23 12:32:02 +00:00
|
|
|
// Table
|
2016-02-26 17:27:07 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// DefineTableStatement represents an SQL DEFINE TABLE statement.
|
|
|
|
//
|
|
|
|
// DEFINE TABLE person
|
|
|
|
type DefineTableStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Table // Table names
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// RemoveTableStatement represents an SQL REMOVE TABLE statement.
|
|
|
|
//
|
|
|
|
// REMOVE TABLE person
|
|
|
|
type RemoveTableStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Table // Table names
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Field
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// DefineFieldStatement represents an SQL DEFINE INDEX statement.
|
|
|
|
//
|
|
|
|
// DEFINE FIELD name ON person TYPE string CODE {}
|
2016-07-21 21:48:32 +00:00
|
|
|
// DEFINE FIELD name ON person TYPE number MIN 0 MAX 5 DEFAULT 0
|
|
|
|
// DEFINE FIELD name ON person TYPE custom ENUM [0,1,2,3,4,5] DEFAULT 0
|
2016-05-23 12:32:02 +00:00
|
|
|
type DefineFieldStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Name Ident // Field name
|
|
|
|
What []Table // Table names
|
2016-07-21 21:48:32 +00:00
|
|
|
Type Ident // Field type
|
2016-07-04 10:37:37 +00:00
|
|
|
Enum []interface{} // Custom options
|
|
|
|
Code string // Field code
|
|
|
|
Min float64 // Minimum value / length
|
|
|
|
Max float64 // Maximum value / length
|
2016-07-21 21:48:32 +00:00
|
|
|
Match string // Regex value
|
|
|
|
Default interface{} // Default value
|
|
|
|
Notnull bool // Notnull - can not be NULL?
|
|
|
|
Readonly bool // Readonly - can not be changed?
|
|
|
|
Mandatory bool // Mandatory - can not be VOID?
|
|
|
|
Validate bool // Validate - can not be INCORRECT?
|
2016-05-23 12:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFieldStatement represents an SQL REMOVE INDEX statement.
|
|
|
|
//
|
|
|
|
// REMOVE FIELD name ON person
|
|
|
|
type RemoveFieldStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Name Ident // Field name
|
|
|
|
What []Table // Table names
|
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.
|
|
|
|
//
|
|
|
|
// DEFINE INDEX name ON person COLUMNS (account, age) UNIQUE
|
|
|
|
type DefineIndexStatement struct {
|
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
2016-07-04 10:37:37 +00:00
|
|
|
Name Ident // Index name
|
|
|
|
What []Table // Table names
|
|
|
|
Code string // Index code
|
2016-05-23 12:32:02 +00:00
|
|
|
Cols []*Field // Index cols
|
|
|
|
Uniq bool // Unique index
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// RemoveIndexStatement represents an SQL REMOVE INDEX statement.
|
|
|
|
//
|
|
|
|
// REMOVE INDEX name ON person
|
|
|
|
type RemoveIndexStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
Name Ident // Index name
|
|
|
|
What []Table // Table names
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// ResyncIndexStatement represents an SQL RESYNC INDEX statement.
|
|
|
|
//
|
|
|
|
// RESYNC INDEX name ON person
|
|
|
|
type ResyncIndexStatement struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
EX bool // Explain
|
|
|
|
KV string // Bucket
|
|
|
|
NS string // Namespace
|
|
|
|
DB string // Database
|
|
|
|
What []Table // Table names
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Literals
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// Expr represents a sql expression
|
|
|
|
type Expr interface{}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
// All represents a wildcard expression.
|
|
|
|
type All struct{}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// Asc represents the ASC expression.
|
|
|
|
type Asc struct{}
|
|
|
|
|
|
|
|
// Desc represents the DESC expression.
|
|
|
|
type Desc struct{}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// Null represents a null expression.
|
|
|
|
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-02-26 17:27:07 +00:00
|
|
|
// Wildcard represents a wildcard expression.
|
|
|
|
type Wildcard struct{}
|
|
|
|
|
|
|
|
// ClosedExpression represents a parenthesized expression.
|
|
|
|
type ClosedExpression struct {
|
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// BinaryExpression represents a binary expression tree,
|
|
|
|
type BinaryExpression struct {
|
|
|
|
LHS Expr
|
|
|
|
Op string
|
|
|
|
RHS Expr
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
// DiffExpression represents a JSON DIFF PATCH
|
|
|
|
type DiffExpression struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
JSON interface{}
|
2016-05-23 12:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeExpression represents JSON to MERGE
|
|
|
|
type MergeExpression struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
JSON interface{}
|
2016-05-23 12:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContentExpression represents JSON to REPLACE
|
|
|
|
type ContentExpression struct {
|
2016-07-04 10:37:37 +00:00
|
|
|
JSON interface{}
|
2016-05-23 12:32:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Parts
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
type Table string
|
|
|
|
|
|
|
|
func (this Table) String() string {
|
|
|
|
return string(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Table) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(string(this)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Ident string
|
|
|
|
|
|
|
|
func (this Ident) String() string {
|
|
|
|
return string(this)
|
|
|
|
}
|
|
|
|
func (this Ident) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(string(this)), nil
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thing comment
|
|
|
|
type Thing struct {
|
2016-05-24 12:51:52 +00:00
|
|
|
TB string
|
|
|
|
ID interface{}
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Field comment
|
|
|
|
type Field struct {
|
|
|
|
Expr Expr
|
|
|
|
Alias Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group comment
|
|
|
|
type Group struct {
|
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order comment
|
|
|
|
type Order struct {
|
|
|
|
Expr Expr
|
|
|
|
Dir Expr
|
|
|
|
}
|