Add sql.MultExpression type

This commit is contained in:
Tobie Morgan Hitchcock 2018-03-18 21:01:29 +00:00
parent f77daca0da
commit 0dae9d2366
4 changed files with 102 additions and 13 deletions

View file

@ -521,6 +521,11 @@ type SubExpression struct {
Expr Expr Expr Expr
} }
// MultExpression represents multiple queries.
type MultExpression struct {
Expr []Expr
}
// IfelExpression represents an if else clause. // IfelExpression represents an if else clause.
type IfelExpression struct { type IfelExpression struct {
Cond Exprs Cond Exprs

View file

@ -262,6 +262,28 @@ func (this *SubExpression) UnmarshalCORK(r *cork.Reader) (err error) {
return return
} }
// --------------------------------------------------
// MultExpression
// --------------------------------------------------
func init() {
cork.Register(&MultExpression{})
}
func (this *MultExpression) ExtendCORK() byte {
return 0x22
}
func (this *MultExpression) MarshalCORK(w *cork.Writer) (err error) {
w.EncodeAny(this.Expr)
return
}
func (this *MultExpression) UnmarshalCORK(r *cork.Reader) (err error) {
r.DecodeAny(&this.Expr)
return
}
// -------------------------------------------------- // --------------------------------------------------
// IfelExpression // IfelExpression
// -------------------------------------------------- // --------------------------------------------------
@ -271,7 +293,7 @@ func init() {
} }
func (this *IfelExpression) ExtendCORK() byte { func (this *IfelExpression) ExtendCORK() byte {
return 0x22 return 0x23
} }
func (this *IfelExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *IfelExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -297,7 +319,7 @@ func init() {
} }
func (this *FuncExpression) ExtendCORK() byte { func (this *FuncExpression) ExtendCORK() byte {
return 0x23 return 0x24
} }
func (this *FuncExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *FuncExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -323,7 +345,7 @@ func init() {
} }
func (this *ItemExpression) ExtendCORK() byte { func (this *ItemExpression) ExtendCORK() byte {
return 0x24 return 0x25
} }
func (this *ItemExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *ItemExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -349,7 +371,7 @@ func init() {
} }
func (this *BinaryExpression) ExtendCORK() byte { func (this *BinaryExpression) ExtendCORK() byte {
return 0x25 return 0x26
} }
func (this *BinaryExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *BinaryExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -375,7 +397,7 @@ func init() {
} }
func (this *PathExpression) ExtendCORK() byte { func (this *PathExpression) ExtendCORK() byte {
return 0x26 return 0x27
} }
func (this *PathExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *PathExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -397,7 +419,7 @@ func init() {
} }
func (this *PartExpression) ExtendCORK() byte { func (this *PartExpression) ExtendCORK() byte {
return 0x27 return 0x28
} }
func (this *PartExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *PartExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -419,7 +441,7 @@ func init() {
} }
func (this *JoinExpression) ExtendCORK() byte { func (this *JoinExpression) ExtendCORK() byte {
return 0x28 return 0x29
} }
func (this *JoinExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *JoinExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -441,7 +463,7 @@ func init() {
} }
func (this *SubpExpression) ExtendCORK() byte { func (this *SubpExpression) ExtendCORK() byte {
return 0x29 return 0x30
} }
func (this *SubpExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *SubpExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -467,7 +489,7 @@ func init() {
} }
func (this *PermExpression) ExtendCORK() byte { func (this *PermExpression) ExtendCORK() byte {
return 0x30 return 0x31
} }
func (this *PermExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *PermExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -495,7 +517,7 @@ func init() {
} }
func (this *DataExpression) ExtendCORK() byte { func (this *DataExpression) ExtendCORK() byte {
return 0x31 return 0x32
} }
func (this *DataExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *DataExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -517,7 +539,7 @@ func init() {
} }
func (this *DiffExpression) ExtendCORK() byte { func (this *DiffExpression) ExtendCORK() byte {
return 0x32 return 0x33
} }
func (this *DiffExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *DiffExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -539,7 +561,7 @@ func init() {
} }
func (this *MergeExpression) ExtendCORK() byte { func (this *MergeExpression) ExtendCORK() byte {
return 0x33 return 0x34
} }
func (this *MergeExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *MergeExpression) MarshalCORK(w *cork.Writer) (err error) {
@ -561,7 +583,7 @@ func init() {
} }
func (this *ContentExpression) ExtendCORK() byte { func (this *ContentExpression) ExtendCORK() byte {
return 0x34 return 0x35
} }
func (this *ContentExpression) MarshalCORK(w *cork.Writer) (err error) { func (this *ContentExpression) MarshalCORK(w *cork.Writer) (err error) {

View file

@ -593,6 +593,58 @@ func (p *parser) parseSubq() (exp *SubExpression, err error) {
} }
func (p *parser) parseMult() (exp *MultExpression, err error) {
exp = &MultExpression{}
if _, _, err = p.shouldBe(LPAREN); err != nil {
return nil, err
}
for {
var stm Expr
tok, _, exi := p.mightBe(CREATE, UPDATE, DELETE, RELATE, INSERT, UPSERT)
if !exi {
break
}
switch tok {
case CREATE:
p.buf.rw = true
stm, err = p.parseCreateStatement()
case UPDATE:
p.buf.rw = true
stm, err = p.parseUpdateStatement()
case DELETE:
p.buf.rw = true
stm, err = p.parseDeleteStatement()
case RELATE:
p.buf.rw = true
stm, err = p.parseRelateStatement()
case INSERT:
p.buf.rw = true
stm, err = p.parseInsertStatement()
case UPSERT:
p.buf.rw = true
stm, err = p.parseUpsertStatement()
}
exp.Expr = append(exp.Expr, stm)
_, _, _ = p.mightBe(SEMICOLON)
}
if _, _, err = p.shouldBe(RPAREN); err != nil {
return nil, err
}
return
}
func (p *parser) parseIfel() (exp *IfelExpression, err error) { func (p *parser) parseIfel() (exp *IfelExpression, err error) {
exp = &IfelExpression{} exp = &IfelExpression{}

View file

@ -736,6 +736,16 @@ func (this SubExpression) String() string {
) )
} }
func (this MultExpression) String() string {
m := make([]string, len(this.Expr))
for k := range this.Expr {
m[k] = print("%v;", this.Expr[k])
}
return print("(%v)",
strings.Join(m, " "),
)
}
func (this IfelExpression) String() string { func (this IfelExpression) String() string {
m := make([]string, len(this.Cond)) m := make([]string, len(this.Cond))
for k := range this.Cond { for k := range this.Cond {