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 (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
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 `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Info
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// InfoStatement represents an SQL INFO statement.
|
|
|
|
type InfoStatement struct {
|
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
What string `codec:"-"`
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:30:59 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Normal
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// SelectStatement represents a SQL SELECT statement.
|
|
|
|
type SelectStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
Expr []*Field `codec:"expr"`
|
|
|
|
What []Expr `codec:"what"`
|
|
|
|
Cond []Expr `codec:"cond"`
|
|
|
|
Group []*Group `codec:"group"`
|
|
|
|
Order []*Order `codec:"order"`
|
|
|
|
Limit Expr `codec:"limit"`
|
|
|
|
Start Expr `codec:"start"`
|
|
|
|
Version Expr `codec:"version"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateStatement represents a SQL CREATE statement.
|
|
|
|
type CreateStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
What []Expr `codec:"what"`
|
|
|
|
Data []Expr `codec:"data"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStatement represents a SQL UPDATE statement.
|
|
|
|
type UpdateStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
What []Expr `codec:"what"`
|
|
|
|
Data []Expr `codec:"data"`
|
|
|
|
Cond []Expr `codec:"cond"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 21:23:02 +00:00
|
|
|
// ModifyStatement represents a SQL MODIFY statement.
|
2016-02-27 09:44:49 +00:00
|
|
|
type ModifyStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
What []Expr `codec:"what"`
|
|
|
|
Diff []Expr `codec:"diff"`
|
|
|
|
Cond []Expr `codec:"cond"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-27 09:44:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// DeleteStatement represents a SQL DELETE statement.
|
|
|
|
type DeleteStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
Hard bool `codec:"hard"`
|
|
|
|
What []Expr `codec:"what"`
|
|
|
|
Cond []Expr `codec:"cond"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RelateStatement represents a SQL RELATE statement.
|
|
|
|
type RelateStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
Type []Expr `codec:"type"`
|
|
|
|
From []Expr `codec:"from"`
|
|
|
|
To []Expr `codec:"to"`
|
|
|
|
Data []Expr `codec:"data"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 23:25:39 +00:00
|
|
|
// RecordStatement represents a SQL RECORD statement.
|
2016-02-26 17:27:07 +00:00
|
|
|
type RecordStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
Type []Expr `codec:"type"`
|
|
|
|
When Expr `codec:"when"`
|
|
|
|
Data []Expr `codec:"data"`
|
|
|
|
Echo Token `codec:"echo"`
|
2016-02-26 17:27:07 +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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
2016-07-21 21:50:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// --------------------------------------------------
|
2016-09-20 23:25:39 +00:00
|
|
|
// Rules
|
2016-02-26 17:27:07 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-09-20 23:25:39 +00:00
|
|
|
// DefineRulesStatement represents an SQL DEFINE RULES statement.
|
|
|
|
type DefineRulesStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
When []string `codec:"-"`
|
|
|
|
Rule string `codec:"rule"`
|
|
|
|
Code string `codec:"code"`
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 23:25:39 +00:00
|
|
|
// RemoveRulesStatement represents an SQL REMOVE RULES statement.
|
|
|
|
type RemoveRulesStatement struct {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
2016-09-20 23:25:39 +00:00
|
|
|
When []string `codec:"-"`
|
2016-02-26 17:27:07 +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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
Name string `codec:"name"`
|
|
|
|
What []string `codec:"-"`
|
|
|
|
Type string `codec:"type"`
|
|
|
|
Enum []interface{} `codec:"enum"`
|
|
|
|
Code string `codec:"code"`
|
|
|
|
Min float64 `codec:"min"`
|
|
|
|
Max float64 `codec:"max"`
|
|
|
|
Match string `codec:"match"`
|
|
|
|
Default interface{} `codec:"default"`
|
|
|
|
Notnull bool `codec:"notnull"`
|
|
|
|
Readonly bool `codec:"readonly"`
|
|
|
|
Mandatory bool `codec:"mandatory"`
|
|
|
|
Validate bool `codec:"validate"`
|
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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
Name string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
Name string `codec:"name"`
|
|
|
|
What []string `codec:"-"`
|
|
|
|
Cols []string `codec:"cols"`
|
|
|
|
Uniq bool `codec:"unique"`
|
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 {
|
2016-09-14 21:23:02 +00:00
|
|
|
KV string `codec:"-"`
|
|
|
|
NS string `codec:"-"`
|
|
|
|
DB string `codec:"-"`
|
|
|
|
Name string `codec:"-"`
|
|
|
|
What []string `codec:"-"`
|
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-09-14 21:23:02 +00:00
|
|
|
// Field represents a SELECT AS clause.
|
|
|
|
type Field struct {
|
|
|
|
Expr Expr
|
|
|
|
Alias string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group represents a GROUP BY clause.
|
|
|
|
type Group struct {
|
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order represents a ORDER BY clause.
|
|
|
|
type Order struct {
|
|
|
|
Expr Expr
|
|
|
|
Dir Expr
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
// ClosedExpression represents a parenthesized expression.
|
|
|
|
type ClosedExpression struct {
|
|
|
|
Expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
// BinaryExpression represents a binary expression tree,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-09-06 13:30:59 +00:00
|
|
|
// Ident comment
|
|
|
|
type Ident struct {
|
|
|
|
ID string
|
2016-07-04 10:37:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 21:23:02 +00:00
|
|
|
func (this Ident) String() string {
|
|
|
|
return this.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIdent(ID string) *Ident {
|
|
|
|
return &Ident{ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Parts
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
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 (this Table) String() string {
|
|
|
|
return this.TB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable(TB string) *Table {
|
|
|
|
return &Table{TB}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Parts
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-14 21:23:02 +00:00
|
|
|
func (this Thing) String() string {
|
|
|
|
return fmt.Sprintf("@%s:%v", this.TB, this.ID)
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 21:23:02 +00:00
|
|
|
func NewThing(TB string, ID interface{}) *Thing {
|
|
|
|
if str, ok := ID.(string); ok {
|
|
|
|
if cnv, err := strconv.ParseFloat(str, 64); err == nil {
|
|
|
|
return &Thing{TB: TB, ID: cnv}
|
|
|
|
} else if cnv, err := time.Parse(RFCDate, str); err == nil {
|
|
|
|
return &Thing{TB: TB, ID: cnv.UTC()}
|
|
|
|
} else if cnv, err := time.Parse(RFCTime, str); err == nil {
|
|
|
|
return &Thing{TB: TB, ID: cnv.UTC()}
|
|
|
|
} else if cnv, err := time.Parse(RFCNorm, str); err == nil {
|
|
|
|
return &Thing{TB: TB, ID: cnv.UTC()}
|
|
|
|
} else if cnv, err := time.Parse(RFCText, str); err == nil {
|
|
|
|
return &Thing{TB: TB, ID: cnv.UTC()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Thing{TB: TB, ID: ID}
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|