func (h *Having) Sum(op Operator, column string, value interface{}) *Having {
	h.addAggregate(op, Sum, column, asExpression(value))
	return h
}
Sum append sum(...)
package kdb
import (
	"bytes"
	"fmt"
	"github.com/sdming/kdb/ansi"
	"strings"
)
const (
	_defaultCapicity int    = 7
	_nilStr          string = "<nil>"
)
type RawSqler interface {
	ToSql() string
}
type Expression interface {
	Node() NodeType
}
func asExpression(v interface{}) Expression {
	if v == nil {
		return DbNull
	}
	e, ok := v.(Expression)
	if ok {
		return e
	}
	return &Value{Value: v}
}
type SortDir string
func (sd SortDir) String() string {
	return string(sd)
}
func (sd SortDir) ToSql() string {
	return string(sd)
}
const (
	Asc  SortDir = ansi.Asc
	Desc SortDir = ansi.Desc
)
type JoinType string
func (j JoinType) String() string {
	return string(j)
}
func (j JoinType) ToSql() string {
	return string(j)
}
const (
	CrossJoin JoinType = ansi.CrossJoin
	InnerJoin JoinType = ansi.InnerJoin
	LeftJoin  JoinType = ansi.LeftJoin
	RightJoin JoinType = ansi.RightJoin
)
type Func string
func (f Func) String() string {
	return string(f)
}
func (f Func) Node() NodeType {
	return NodeFunc
}
const (
	Count       Func = ansi.Count
	Sum         Func = ansi.Sum
	Avg         Func = ansi.Avg
	Min         Func = ansi.Min
	Max         Func = ansi.Max
	CurrentTime Func = "currenttime"
)
type Operator string
func (op Operator) String() string {
	return string(op)
}
func (op Operator) ToSql() string {
	return string(op)
}
func (op Operator) Node() NodeType {
	return NodeOperator
}
const (
	IsNull           Operator = ansi.IsNull
	IsNotNull        Operator = ansi.IsNotNull
	LessThan         Operator = ansi.LessThan
	LessOrEquals     Operator = ansi.LessOrEquals
	GreaterThan      Operator = ansi.GreaterThan
	GreaterOrEquals  Operator = ansi.GreaterOrEquals
	Equals           Operator = ansi.Equals
	NotEquals        Operator = ansi.NotEquals
	Like             Operator = ansi.Like
	NotLike          Operator = ansi.NotLike
	In               Operator = ansi.In
	NotIn            Operator = ansi.NotIn
	Exists           Operator = ansi.Exists
	NotExists        Operator = ansi.NotExists
	All              Operator = ansi.All
	Some             Operator = ansi.Some
	Any              Operator = ansi.Any
	And              Operator = ansi.And
	Or               Operator = ansi.Or
	OpenParentheses  Operator = ansi.OpenParentheses
	CloseParentheses Operator = ansi.CloseParentheses
)
type NodeType int
const (
	NodeZero      NodeType = 0
	NodeText      NodeType = 1
	NodeProcedure NodeType = 2
	NodeInsert    NodeType = 3
	NodeQuery     NodeType = 4
	NodeUpdate    NodeType = 5
	NodeDelete    NodeType = 6
	NodeNull  NodeType = 11
	NodeValue NodeType = 12
	NodeSql   NodeType = 13
	NodeTable     NodeType = 31
	NodeColumn    NodeType = 32
	NodeAlias     NodeType = 33
	NodeCondition NodeType = 34
	NodeSet       NodeType = 35
	NodeAggregate NodeType = 36
	NodeSelect  NodeType = 41
	NodeFrom    NodeType = 42
	NodeJoin    NodeType = 43
	NodeWhere   NodeType = 44
	NodeGroupBy NodeType = 45
	NodeHaving  NodeType = 46
	NodeOrderBy NodeType = 47
	NodeOutput  NodeType = 48
	NodeOperator  = 61
	NodeFunc      = 62
	NodeParameter = 63
)
func (n NodeType) String() string {
	switch n {
	case NodeZero:
		return "Zero"
	case NodeText:
		return "Text"
	case NodeProcedure:
		return "Procedure"
	case NodeInsert:
		return "Insert"
	case NodeQuery:
		return "Query"
	case NodeUpdate:
		return "Update"
	case NodeDelete:
		return "Delete"
	case NodeNull:
		return "Null"
	case NodeValue:
		return "Value"
	case NodeSql:
		return "Sql"
	case NodeTable:
		return "Table"
	case NodeColumn:
		return "Column"
	case NodeAlias:
		return "Alias"
	case NodeCondition:
		return "Condition"
	case NodeSet:
		return "Set"
	case NodeAggregate:
		return "Aggregate"
	case NodeSelect:
		return "Select"
	case NodeFrom:
		return "From"
	case NodeJoin:
		return "Join"
	case NodeWhere:
		return "Where"
	case NodeGroupBy:
		return "GroupBy"
	case NodeHaving:
		return "Having"
	case NodeOrderBy:
		return "OrderBy"
	case NodeOutput:
		return "Output "
	case NodeOperator:
		return "Operator"
	case NodeFunc:
		return "Func"
	}
	return "Unknow"
}
type Null string
func (n Null) String() string {
	return string(n)
}
func (n Null) ToSql() string {
	return string(n)
}
func (n Null) Node() NodeType {
	return NodeNull
}
const DbNull Null = ansi.Null
type Sql string
func (s Sql) String() string {
	return string(s)
}
func (s Sql) ToSql() string {
	return string(s)
}
func (s Sql) Node() NodeType {
	return NodeSql
}
type Column string
func (c Column) String() string {
	return string(c)
}
func (c Column) Split() (table string, column string) {
	s := string(c)
	if s == "" {
		return "", ""
	}
	i := strings.Index(s, ".")
	if i < 0 {
		return "", s
	}
	return s[0:i], s[i+1:]
}
func (c Column) Node() NodeType {
	return NodeColumn
}
type Value struct {
	Value interface{}
}
func (v *Value) String() string {
	if v == nil {
		return _nilStr
	}
	return fmt.Sprint(v.Value)
}
func (v *Value) Node() NodeType {
	return NodeValue
}
type Set struct {
	Column Column
	Value  Expression
}
func (a *Set) String() string {
	if a == nil {
		return _nilStr
	}
	return fmt.Sprintf("%v = %v", a.Column, a.Value)
}
func (a *Set) Node() NodeType {
	return NodeSet
}
func newSet(column string, value Expression) *Set {
	return &Set{
		Column: Column(column),
		Value:  value,
	}
}
type Condition struct {
	Right Expression
	Left  Expression
	Op    Operator
}
func (c *Condition) String() string {
	if c == nil {
		return _nilStr
	}
	if c.Right == nil && c.Left == nil {
		return fmt.Sprint(c.Op)
	} else if c.Left == nil {
		return fmt.Sprint(c.Op, "(", c.Right, ")")
	} else if c.Right == nil {
		return fmt.Sprint(c.Left, " ", c.Op)
	}
	return fmt.Sprintf("%v %v %v", c.Left, c.Op, c.Right)
}
func (c *Condition) Node() NodeType {
	return NodeCondition
}
type Conditions struct {
	Conditions        []Expression
	needLogicOperator bool
}
func (c *Conditions) isEmpty() bool {
	if c == nil || c.Conditions == nil || len(c.Conditions) == 0 {
		return true
	}
	return false
}
func (c *Conditions) String() string {
	if c == nil {
		return _nilStr
	}
	buf := bytes.Buffer{}
	deep := 0
	for i := 0; i < len(c.Conditions); i++ {
		item := c.Conditions[i]
		if i > 0 {
			buf.WriteString("\n")
		}
		if item == CloseParentheses {
			deep--
		}
		if deep > 0 {
			buf.WriteString(strings.Repeat("\t", deep))
		}
		buf.WriteString(fmt.Sprint(item))
		if item == OpenParentheses {
			deep++
		}
	}
	return buf.String()
}
func (c *Conditions) checkLogicOperator() {
	if c.needLogicOperator {
		c.Conditions = append(c.Conditions, And)
	}
}
func (c *Conditions) set(exp Expression) {
	c.checkLogicOperator()
	if c.Conditions == nil {
		c.Conditions = make([]Expression, 0, _defaultCapicity)
	}
	c.Conditions = append(c.Conditions, exp)
	c.needLogicOperator = true
}
func (c *Conditions) Condition(op Operator, left, right Expression) *Conditions {
	c.set(&Condition{
		Op:    op,
		Right: right,
		Left:  left,
	})
	return c
}
func (c *Conditions) And() *Conditions {
	if c.needLogicOperator {
		c.Conditions = append(c.Conditions, And)
		c.needLogicOperator = false
	}
	return c
}
func (c *Conditions) Or() *Conditions {
	if c.needLogicOperator {
		c.Conditions = append(c.Conditions, Or)
		c.needLogicOperator = false
	}
	return c
}
func (c *Conditions) OpenParentheses() *Conditions {
	c.checkLogicOperator()
	c.Conditions = append(c.Conditions, OpenParentheses)
	c.needLogicOperator = false
	return c
}
func (c *Conditions) CloseParentheses() *Conditions {
	c.Conditions = append(c.Conditions, CloseParentheses)
	c.needLogicOperator = true
	return c
}
func (c *Conditions) Sql(sqlStr string) *Conditions {
	c.set(Sql(sqlStr))
	return c
}
func (c *Conditions) Exists(exp Expression) *Conditions {
	return c.Condition(Exists, nil, exp)
}
func (c *Conditions) NotExists(exp Expression) *Conditions {
	return c.Condition(NotExists, nil, exp)
}
func (c *Conditions) Compare(op Operator, column string, value interface{}) *Conditions {
	return c.Condition(op, Column(column), asExpression(value))
}
func (c *Conditions) Like(column string, value string) *Conditions {
	return c.Condition(Like, Column(column), &Value{Value: value})
}
func (c *Conditions) NotLike(column string, value string) *Conditions {
	return c.Condition(NotLike, Column(column), &Value{Value: value})
}
func (c *Conditions) LessOrEquals(column string, value interface{}) *Conditions {
	return c.Condition(LessOrEquals, Column(column), asExpression(value))
}
func (c *Conditions) LessThan(column string, value interface{}) *Conditions {
	return c.Condition(LessThan, Column(column), asExpression(value))
}
func (c *Conditions) GreaterOrEquals(column string, value interface{}) *Conditions {
	return c.Condition(GreaterOrEquals, Column(column), asExpression(value))
}
func (c *Conditions) GreaterThan(column string, value interface{}) *Conditions {
	return c.Condition(GreaterThan, Column(column), asExpression(value))
}
func (c *Conditions) Equals(column string, value interface{}) *Conditions {
	return c.Condition(Equals, Column(column), asExpression(value))
}
func (c *Conditions) NotEquals(column string, value interface{}) *Conditions {
	return c.Condition(NotEquals, Column(column), asExpression(value))
}
func (c *Conditions) IsNull(column string) *Conditions {
	return c.Condition(IsNull, Column(column), nil)
}
func (c *Conditions) IsNotNull(column string) *Conditions {
	return c.Condition(IsNotNull, Column(column), nil)
}
func (c *Conditions) In(column string, value interface{}) *Conditions {
	return c.Condition(In, Column(column), asExpression(value))
}
func (c *Conditions) NotIn(column string, value interface{}) *Conditions {
	return c.Condition(NotIn, Column(column), asExpression(value))
}
func newConditions() *Conditions {
	return &Conditions{
		Conditions: make([]Expression, 0, _defaultCapicity),
	}
}
type Aggregate struct {
	Name Func
	Exp  Expression
}
func (a *Aggregate) String() string {
	if a == nil {
		return _nilStr
	}
	return fmt.Sprintf("%v (%v)", a.Name, a.Exp)
}
func (a *Aggregate) Node() NodeType {
	return NodeAggregate
}
func NewAggregate(name Func, exp Expression) *Aggregate {
	return &Aggregate{
		Name: name,
		Exp:  exp,
	}
}
type Where struct {
	*Conditions
}
func (w *Where) String() string {
	if w == nil {
		return _nilStr
	}
	return fmt.Sprint(ansi.Where, "\n", w.Conditions)
}
func (w *Where) Node() NodeType {
	return NodeWhere
}
func NewWhere() *Where {
	return &Where{newConditions()}
}
type Having struct {
	*Conditions
}
func (h *Having) String() string {
	if h == nil {
		return _nilStr
	}
	return fmt.Sprint(ansi.Having, "\n", h.Conditions)
}
func (h *Having) Node() NodeType {
	return NodeHaving
}
func (h *Having) addAggregate(op Operator, name Func, column string, value Expression) {
	h.Condition(op, NewAggregate(name, Column(column)), value)
}
func (h *Having) Avg(op Operator, column string, value interface{}) *Having {
	h.addAggregate(op, Avg, column, asExpression(value))
	return h
}
func (h *Having) Count(op Operator, column string, value interface{}) *Having {
	h.addAggregate(op, Count, column, asExpression(value))
	return h
}