|
| 1 | +package plan |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/src-d/go-mysql-server/sql" |
| 5 | + errors "gopkg.in/src-d/go-errors.v1" |
| 6 | +) |
| 7 | + |
| 8 | +var errDropViewChild = errors.NewKind("any child of DropView must be of type SingleDropView") |
| 9 | + |
| 10 | +type SingleDropView struct { |
| 11 | + database sql.Database |
| 12 | + viewName string |
| 13 | +} |
| 14 | + |
| 15 | +// NewSingleDropView creates a SingleDropView. |
| 16 | +func NewSingleDropView( |
| 17 | + database sql.Database, |
| 18 | + viewName string, |
| 19 | +) *SingleDropView { |
| 20 | + return &SingleDropView{database, viewName} |
| 21 | +} |
| 22 | + |
| 23 | +// Children implements the Node interface. It always returns nil. |
| 24 | +func (dv *SingleDropView) Children() []sql.Node { |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +// Resolved implements the Node interface. This node is resolved if and only if |
| 29 | +// its database is resolved. |
| 30 | +func (dv *SingleDropView) Resolved() bool { |
| 31 | + _, ok := dv.database.(sql.UnresolvedDatabase) |
| 32 | + return !ok |
| 33 | +} |
| 34 | + |
| 35 | +// RowIter implements the Node interface. It always returns an empty iterator. |
| 36 | +func (dv *SingleDropView) RowIter(ctx *sql.Context) (sql.RowIter, error) { |
| 37 | + return sql.RowsToRowIter(), nil |
| 38 | +} |
| 39 | + |
| 40 | +// Schema implements the Node interface. It always returns nil. |
| 41 | +func (dv *SingleDropView) Schema() sql.Schema { return nil } |
| 42 | + |
| 43 | +// String implements the fmt.Stringer interface, using sql.TreePrinter to |
| 44 | +// generate the string. |
| 45 | +func (dv *SingleDropView) String() string { |
| 46 | + pr := sql.NewTreePrinter() |
| 47 | + _ = pr.WriteNode("SingleDropView(%s.%s)", dv.database.Name(), dv.viewName) |
| 48 | + |
| 49 | + return pr.String() |
| 50 | +} |
| 51 | + |
| 52 | +// WithChildren implements the Node interface. It only succeeds if the length |
| 53 | +// of the specified children equals 0. |
| 54 | +func (dv *SingleDropView) WithChildren(children ...sql.Node) (sql.Node, error) { |
| 55 | + if len(children) != 0 { |
| 56 | + return nil, sql.ErrInvalidChildrenNumber.New(dv, len(children), 0) |
| 57 | + } |
| 58 | + |
| 59 | + return dv, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Database implements the Databaser interfacee. It returns the node's database. |
| 63 | +func (dv *SingleDropView) Database() sql.Database { |
| 64 | + return dv.database |
| 65 | +} |
| 66 | + |
| 67 | +// Database implements the Databaser interface, and it returns a copy of this |
| 68 | +// node with the specified database. |
| 69 | +func (dv *SingleDropView) WithDatabase(database sql.Database) (sql.Node, error) { |
| 70 | + newDrop := *dv |
| 71 | + newDrop.database = database |
| 72 | + return &newDrop, nil |
| 73 | +} |
| 74 | + |
| 75 | +// DropView is a node representing the removal of a list of views, defined by |
| 76 | +// the children member. The flag ifExists represents whether the user wants the |
| 77 | +// node to fail if any of the views in children does not exist. |
| 78 | +type DropView struct { |
| 79 | + children []sql.Node |
| 80 | + Catalog *sql.Catalog |
| 81 | + ifExists bool |
| 82 | +} |
| 83 | + |
| 84 | +// NewDropView creates a DropView node with the specified parameters, |
| 85 | +// setting its catalog to nil. |
| 86 | +func NewDropView(children []sql.Node, ifExists bool) *DropView { |
| 87 | + return &DropView{children, nil, ifExists} |
| 88 | +} |
| 89 | + |
| 90 | +// Children implements the Node interface. It returns the children of the |
| 91 | +// CreateView node; i.e., all the views that will be dropped. |
| 92 | +func (dvs *DropView) Children() []sql.Node { |
| 93 | + return dvs.children |
| 94 | +} |
| 95 | + |
| 96 | +// Resolved implements the Node interface. This node is resolved if and only if |
| 97 | +// all of its children are resolved. |
| 98 | +func (dvs *DropView) Resolved() bool { |
| 99 | + for _, child := range dvs.children { |
| 100 | + if !child.Resolved() { |
| 101 | + return false |
| 102 | + } |
| 103 | + } |
| 104 | + return true |
| 105 | +} |
| 106 | + |
| 107 | +// RowIter implements the Node interface. When executed, this function drops |
| 108 | +// all the views defined by the node's children. It errors if the flag ifExists |
| 109 | +// is set to false and there is some view that does not exist. |
| 110 | +func (dvs *DropView) RowIter(ctx *sql.Context) (sql.RowIter, error) { |
| 111 | + viewList := make([]sql.ViewKey, len(dvs.children)) |
| 112 | + for i, child := range dvs.children { |
| 113 | + drop, ok := child.(*SingleDropView) |
| 114 | + if !ok { |
| 115 | + return sql.RowsToRowIter(), errDropViewChild.New() |
| 116 | + } |
| 117 | + |
| 118 | + viewList[i] = sql.NewViewKey(drop.database.Name(), drop.viewName) |
| 119 | + } |
| 120 | + |
| 121 | + return sql.RowsToRowIter(), dvs.Catalog.ViewRegistry.DeleteList(viewList, !dvs.ifExists) |
| 122 | +} |
| 123 | + |
| 124 | +// Schema implements the Node interface. It always returns nil. |
| 125 | +func (dvs *DropView) Schema() sql.Schema { return nil } |
| 126 | + |
| 127 | +// String implements the fmt.Stringer interface, using sql.TreePrinter to |
| 128 | +// generate the string. |
| 129 | +func (dvs *DropView) String() string { |
| 130 | + childrenStrings := make([]string, len(dvs.children)) |
| 131 | + for i, child := range dvs.children { |
| 132 | + childrenStrings[i] = child.String() |
| 133 | + } |
| 134 | + |
| 135 | + pr := sql.NewTreePrinter() |
| 136 | + _ = pr.WriteNode("DropView") |
| 137 | + _ = pr.WriteChildren(childrenStrings...) |
| 138 | + |
| 139 | + return pr.String() |
| 140 | +} |
| 141 | + |
| 142 | +// WithChildren implements the Node interface. It always suceeds, returning a |
| 143 | +// copy of this node with the new array of nodes as children. |
| 144 | +func (dvs *DropView) WithChildren(children ...sql.Node) (sql.Node, error) { |
| 145 | + newDrop := dvs |
| 146 | + newDrop.children = children |
| 147 | + return newDrop, nil |
| 148 | +} |
0 commit comments