func (p *DropCheck) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
	}
	return NewAlterDropCheck(children[0], p.Name), nil
}
WithChildren implements the Node interface.
package plan
import (
	"fmt"
	"io"
	"gopkg.in/src-d/go-errors.v1"
	"github.com/dolthub/go-mysql-server/sql"
	"github.com/dolthub/go-mysql-server/sql/expression"
)
var (
	ErrNoCheckConstraintSupport = errors.NewKind("the table does not support check constraint operations: %s")
	ErrCheckFailed = errors.NewKind("check constraint %s is violated.")
)
type CreateCheck struct {
	UnaryNode
	Check *sql.CheckConstraint
}
type DropCheck struct {
	UnaryNode
	Name string
}
func NewAlterAddCheck(table sql.Node, check *sql.CheckConstraint) *CreateCheck {
	return &CreateCheck{
		UnaryNode: UnaryNode{table},
		Check:     check,
	}
}
func NewAlterDropCheck(table sql.Node, name string) *DropCheck {
	return &DropCheck{
		UnaryNode: UnaryNode{Child: table},
		Name:      name,
	}
}
func getCheckAlterable(node sql.Node) (sql.CheckAlterableTable, error) {
	switch node := node.(type) {
	case sql.CheckAlterableTable:
		return node, nil
	case *ResolvedTable:
		return getCheckAlterableTable(node.Table)
	default:
		return nil, ErrNoCheckConstraintSupport.New(node.String())
	}
}
func getCheckAlterableTable(t sql.Table) (sql.CheckAlterableTable, error) {
	switch t := t.(type) {
	case sql.CheckAlterableTable:
		return t, nil
	case sql.TableWrapper:
		return getCheckAlterableTable(t.Underlying())
	case *ResolvedTable:
		return getCheckAlterableTable(t.Table)
	default:
		return nil, ErrNoCheckConstraintSupport.New(t.Name())
	}
}
func (c *CreateCheck) Expressions() []sql.Expression {
	return []sql.Expression{c.Check.Expr}
}
func (c *CreateCheck) Resolved() bool {
	return c.Child.Resolved() && c.Check.Expr.Resolved()
}
func (c *CreateCheck) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
	if len(exprs) != 1 {
		return nil, fmt.Errorf("expected one expression, got: %d", len(exprs))
	}
	nc := *c
	nc.Check.Expr = exprs[0]
	return &nc, nil
}
func (c *CreateCheck) Execute(ctx *sql.Context) error {
	chAlterable, err := getCheckAlterable(c.UnaryNode.Child)
	if err != nil {
		return err
	}
	var res interface{}
	rowIter, err := c.UnaryNode.Child.RowIter(ctx, nil)
	if err != nil {
		return err
	}
	for {
		row, err := rowIter.Next()
		if row == nil || err != io.EOF {
			break
		}
		res, err = sql.EvaluateCondition(ctx, c.Check.Expr, row)
		if err != nil {
			return err
		}
		if sql.IsFalse(res) {
			return ErrCheckFailed.New(c.Check.Name)
		}
	}
	check, err := NewCheckDefinition(ctx, c.Check)
	if err != nil {
		return err
	}
	return chAlterable.CreateCheck(ctx, check)
}
func (c *CreateCheck) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1)
	}
	return NewAlterAddCheck(children[0], c.Check), nil
}
func (c *CreateCheck) Schema() sql.Schema { return nil }
func (c *CreateCheck) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	err := c.Execute(ctx)
	if err != nil {
		return nil, err
	}
	return sql.RowsToRowIter(), nil
}
func (c CreateCheck) String() string {
	pr := sql.NewTreePrinter()
	_ = pr.WriteNode("AddCheck(%s)", c.Check.Name)
	_ = pr.WriteChildren(
		fmt.Sprintf("Table(%s)", c.UnaryNode.Child.String()),
		fmt.Sprintf("Expr(%s)", c.Check.Expr.String()),
	)
	return pr.String()
}
func (p *DropCheck) Execute(ctx *sql.Context) error {
	chAlterable, err := getCheckAlterable(p.UnaryNode.Child)
	if err != nil {
		return err
	}
	return chAlterable.DropCheck(ctx, p.Name)
}
func (p *DropCheck) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	err := p.Execute(ctx)
	if err != nil {
		return nil, err
	}
	return sql.RowsToRowIter(), nil
}